-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
77 lines (65 loc) · 2.01 KB
/
Board.java
File metadata and controls
77 lines (65 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package Sokoban;
import java.util.ArrayList;
public class Board {
private ArrayList<ArrayList<String>> board;
private int size;
private String printBoard = "";
public Board(int size){
this.size = size;
ArrayList<ArrayList<String>> doubleArray = new ArrayList<ArrayList<String>>();
this.board = doubleArray;
board.add(new ArrayList<String>());
for (int i = 0; i<size;i++){
board.get(0).add(Square.getWall());
}
for (int i = 1; i<size-1;i++){
board.add(new ArrayList<String>());
board.get(i).add(Square.getWall());
for (int j = 0; j<size-2;j++){
board.get(i).add(Square.getEmptySquare());
}
board.get(i).add(Square.getWall());
}
board.add(new ArrayList<String>());
for (int i = 0; i<size;i++){
board.get(size-1).add(Square.getWall());
}
setBoard();
}
public ArrayList<ArrayList<String>> getBoard(){
return board;
}
public int ledigeRuter(){
int lengde = size-4;
return (int) Math.pow(lengde, 2);
}
public void setBoard(){
int numberOfPossibleSquares = (int) (Math.round(ledigeRuter()/2));
while (numberOfPossibleSquares!=0){
int arrayArray = (int) Math.round(Math.random()*(size-4)+2);
if (arrayArray>2 && arrayArray<size-2){
int arrayIndex = (int) Math.round(Math.random()*(size-4)+2);
if (arrayIndex>2 && arrayIndex<size-2){
if ((Square.getEmptySquare() == board.get(arrayArray).get(arrayIndex))
&& ((Square.getEmptySquare() == board.get(arrayArray-1).get(arrayIndex))
|| (Square.getEmptySquare() == board.get(arrayArray+1).get(arrayIndex))
|| (Square.getEmptySquare() == board.get(arrayArray).get(arrayIndex-1))
|| (Square.getEmptySquare() == board.get(arrayArray).get(arrayIndex+1)))){
board.get(arrayArray).set(arrayIndex, Square.getBox());
numberOfPossibleSquares--;
}
}
}
}
}
public String toString(){
for (ArrayList<String> a:board){
for (int i = 0; i<size;i++){
String s = (String) a.get(i);
printBoard += s;
}
printBoard += "\r\n";
}
return printBoard;
}
}