Use Map instead of 2d array

This commit is contained in:
Thomas Kwashnak
2022-01-05 12:20:07 -05:00
committed by GitHub
parent fb8a87bade
commit b250689f37

View File

@@ -1,25 +1,27 @@
import java.util.Map;
import java.util.HashMap;
public class Board { public class Board {
private int[][] board; private final Map<Integer,Integer> board;
public Board() { public Board() {
board = new int[7][7]; board = new HashMap<>();
//Set all of the corners to -1, and place pegs in proper spaces int[] locations = new int[] {13,14,15,
for(int i = 0; i < 7; i++) { 22,23,24,
for(int j = 0; j < 7; j++) { 29,30,31,32,33,34,35,
if((i < 3 || i > 5) && (j < 3 || j > 5)) { 38,39,40,42,43,44,
//If both i and j are either less than 3 or greater than 5, then the index is a corner 47,48,49,50,51,52,53,
board[i][j] = -1; 58,59,60,
} else if(i == 4 && j == 4) { 67,68,69};
//Do not place a peg in the center
board[i][j] = 0; for(int i : locations) {
} else { //put board(i) in
//Place a peg everywhere else
board[i][j] = 1;
}
}
} }
//set the center position as 0
} }
public String toString() { public String toString() {