-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
204 lines (168 loc) · 5.16 KB
/
Board.java
File metadata and controls
204 lines (168 loc) · 5.16 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package com.nathan;
import java.io.Serializable;
import java.util.Arrays;
import android.content.Context;
import android.util.Log;
public class Board implements Serializable {
//-------------------------------------------------CONSTANTS
// constants to hold the empty, x and o values
private static final long serialVersionUID = 1L;
public static final int EMPTY = 0;
public static final int EX = 1;
public static final int OH = 2;
//-------------------------------------------------PROPERTIES
// ints to hold the turn count and the touch count
private int turn, touchCount;
//two dimensional array to hold the board value
private int[][] board;
//// boolen to check if the game is over, or which player won or a tie
private boolean isGameOver;
private boolean isP1Win;
private boolean isP2Win;
private boolean isTie;
// strings to hold the concatonated board values for rows, columns and diagonals
private String strRow,strCol,strDiag1,strDiag2;
// string to hold the message on game over
private String winMessage;
// array to check the board values
private String[] aryCol,aryRow;
// global context to hold the context passed in
private static Context context;
//-------------------------------------------------CONSTRUCTOR
public Board(Context con){
// init the
board = new int[3][3];
// reset the board
resetBoard(0);
// init arrays and assign the context to the global
aryRow = new String[3];
aryCol = new String[3];
context = con;
}
//-------------------------------------------------GETS/SETS
public int getTurn() {
return turn;
}
public void setTurn(int turn) {
this.turn = turn;
}
public int[][] getBoard() {
return board;
}
public void setBoard(int posR,int posY, int val) {
board[posR][posY] = val;
}
public boolean isGameOver() {
return isGameOver;
}
public int getTouchCount(){
return touchCount;
}
public boolean isP1Win(){
return isP1Win;
}
public boolean isP2Win(){
return isP2Win;
}
public boolean isTie(){
return isTie;
}
//-------------------------------------------------PUBLIC METHODS
// method to reset the game
public void resetBoard(int myGameType){
// reset the properties
isGameOver = false;
isP1Win = false;
isP2Win = false;
touchCount = 0;
turn= 0;
winMessage = "";
// loop through board and set each position to emprty
for(int r = 0; r <=2; r++){
for(int c = 0; c <=2; c++){
board[r][c] = EMPTY;
}
}
}
// main method of the board class
public String ticTacToe(){
// increment the touch count
touchCount++;
//game has to toggle the turn by itself
if(turn == 1){
turn = 0;
}else{
turn = 1;
}
// use check win method to check if either play won or a tie
checkWin();
// return the win message set in the check Win method
return winMessage;
}
//-------------------------------------------------PRIVATE METHODS
private boolean checkWin(){
// set the game over booleans to false
isGameOver = false;
isP1Win = false;
isP2Win = false;
isTie = false;
// set the counter to 1 and empty all the variables and arrays
int counter = 1;
strRow = "";
strCol = "";
strDiag1 = "";
strDiag2 = "";
Arrays.fill(aryRow,"");
Arrays.fill(aryCol,"");
// only way to check the right diagonal is by manualy checking each position
strDiag2 += board[0][2];
strDiag2 += board[1][1];
strDiag2 += board[2][0];
// nested loop to run through board
for(int r = 0; r <=2; r++){
//clear strings after each row and column
strRow="";
strCol="";
for(int c = 0; c <=2; c++){
// concat the value of each position of each row and colum
strRow += board[r][c];
strCol += board[c][r];
// checks the left side diagonal and concats into the string
if(r == c){
strDiag1 += board[r][c];
}
// at the end of each rowand column
if(counter % 3 == 0){
aryRow[r] = strRow;
aryCol[c] = strCol;
// check the strings if they are all ones or twos
// if they are set the win message and booleans to either player and break the loop
// PLAYER ONE
if((aryRow[r].equals("111")) || (aryCol[c].equals("111"))
|| (strDiag1.equals("111")) || strDiag2.equals("111")){
winMessage = context.getResources().getString(R.string.xWins);
isP1Win = true;
isGameOver = true;
break;
// PLAYER TWO / AI
}else if((aryRow[r].equals("222")) || (aryCol[c].equals("222"))
|| (strDiag1.equals("222")) || strDiag2.equals("222")){
winMessage = context.getResources().getString(R.string.oWins);
isP2Win = true;
isGameOver = true;
break;
}
}
// if the touch count is up to 9 and the game is still not over then it is a tie
if((touchCount == 9) && (!isGameOver)){
winMessage = context.getResources().getString(R.string.gameTie);
isTie = true;
isGameOver = true;
break;
}
counter++;
}
}
return isGameOver;
}
}