-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXandOGame.java
More file actions
115 lines (90 loc) · 3.51 KB
/
XandOGame.java
File metadata and controls
115 lines (90 loc) · 3.51 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import java.util.ArrayList;
public class XandOGame {
int[][] gameState;
public XandOGame(){
gameState = new int[3][3];
}
public int[][] getGameState(){
return gameState;
}
public ArrayList<Integer> getAvailableCells(){
// create an array to store available cells
ArrayList<Integer> availableCells = new ArrayList<Integer>();
// initiate a double for loop to iterate through the gamestate array to find empty cells
for (int i = 0; i <= 2; i++){
for(int j = 0; j <= 2; j++){
//check wheather a cell is empty
if (gameState[i][j] == 0){
//check which row a cell belong to
if (i == 0){
//calculate the corresponding decimal position of the cells and add it to the array
availableCells.add(i+j+1);
}
else if (i == 1){
availableCells.add(i+j+3);
}
else if (i == 2){
availableCells.add(i+j+5);
}
}
}
}
return availableCells;
}
public void modifyGameState(int cellNumber, int symbolValue){
switch (cellNumber){
case 1:
gameState[0][0] = symbolValue;
break;
case 2:
gameState[0][1] = symbolValue;
break;
case 3:
gameState[0][2] = symbolValue;
break;
case 4:
gameState[1][0] = symbolValue;
break;
case 5:
gameState[1][1] = symbolValue;
break;
case 6:
gameState[1][2] = symbolValue;
break;
case 7:
gameState[2][0] = symbolValue;
break;
case 8:
gameState[2][1] = symbolValue;
break;
case 9:
gameState[2][2] = symbolValue;
break;
default:
System.out.println("invalid selection");
break;
}
}
public int checkGameStatus(){
if ((gameState[0][0]==gameState[1][0]) && (gameState[0][0]==gameState[2][0]) && (gameState[0][0] != 0)) return gameState[0][0];
else if ((gameState[0][1]==gameState[1][1]) && (gameState[0][1]==gameState[2][1]) && (gameState[0][1] != 0)) return gameState[0][0];
else if ((gameState[0][2]==gameState[1][2]) && (gameState[0][2]==gameState[2][2]) && (gameState[0][2] != 0)) return gameState[0][0];
else if ((gameState[0][0]==gameState[0][1]) && (gameState[0][0]==gameState[0][2]) && (gameState[0][0] != 0)) return gameState[0][0];
else if ((gameState[1][0]==gameState[1][1]) && (gameState[1][0]==gameState[1][2]) && (gameState[1][0] != 0)) return gameState[0][0];
else if ((gameState[2][0]==gameState[2][1]) && (gameState[2][0]==gameState[2][2]) && (gameState[2][0] != 0)) return gameState[0][0];
else if ((gameState[0][0]==gameState[1][1]) && (gameState[0][0]==gameState[2][2]) && (gameState[0][0] != 0)) return gameState[0][0];
else if ((gameState[2][0]==gameState[1][1]) && (gameState[2][0]==gameState[0][2]) && (gameState[2][0] != 0)) return gameState[0][0];
else {
return 0;
}
}
public void printGameBoard(){
System.out.println("----------------------------");
System.out.printf("%-3s%3c%-3s%-3s%3c%-3s%-3s%3c%-3s%-11s\n", "|",((char)gameState[0][0])," ", "|",((char)gameState[0][1])," ","|",((char)gameState[0][2])," ","|");
System.out.println("----------------------------");
System.out.printf("%-3s%3c%-3s%-3s%3c%-3s%-3s%3c%-3s%-11s\n", "|",((char)gameState[1][0])," ", "|",((char)gameState[1][1])," ","|",((char)gameState[1][2])," ","|");
System.out.println("----------------------------");
System.out.printf("%-3s%3c%-3s%-3s%3c%-3s%-3s%3c%-3s%-11s\n", "|",((char)gameState[2][0])," ", "|",((char)gameState[2][1])," ","|",((char)gameState[2][2])," ","|");
System.out.println("----------------------------");
}
}