I've completed this Tic Tac Toe simulator in java and so far it's working. It's just supposed to generate a pre-played game and announce the winner. I'm required to use a 2D array and this is the way I've found to get it done. But I ask more experienced developers if there is a way to do this that is a little less convoluted. Or for any pointers you may have.
PS: the display and winner label are in the rest of my code but that's not the main part I'm worried about. the int 1 stands for O and the int 2 stands for X.
public class gameBord {
// tic tac toe bord
public int[][] tBord = new int[3][3];
public int counter; // to count through the 9 spaces
public int x; // to hold the index of x
public int y; // to hold the index of y
public int xs; // to hold how many current x marks there are on the board
public int os; // to hold # of y marks on bord (these are to make sure there aren't
// more marks than the player has turns.
public int xAndO; // holds the temp variable to indicate which letter will
// be stored in the current index on the array
public Random r; // a random int between 1 and 2 will be chosen to define x and o
public String winMessage; // string to be displayed on lable to display winner
public void game(){
// 9 for the 9 squares in tic tac toe
counter = 9;
// x and y stand for the indexs in the array
x = 0;
y = 0;
os = 0;
xs = 0;
System.out.println("clicked");
// these while loops will loop through the rows this one being the last row
while (counter > 0){
// before the last row we go through the second row
while (counter > 3){
// before the second row we go through the first row (first 3 colmns)
while (counter > 6){
// label which row you're in
System.out.println("Row 1");
// get a random number between one and two to assign to this box
// these numbers will stand for the X or O on the bord
r = new Random();
xAndO = r.nextInt(2)+1;
// count how many xs and os are on the bord no one letter should
// go over 4
if (xAndO == 1){
os++;
}
else{
xs++;
}
// prints out a one or two above the indexs it'll be stored in
System.out.println("number to store " + xAndO);
// stores the number in the current index
tBord[x][y] = xAndO;
// displays the current index
System.out.println("Index x =" + x);
System.out.println("Index y =" + y);
// counter goes down which moves us to the next block of the array
counter--;
// there are only 3 columns (x Index) so this number has to stay between 0 and 2 (3 total)
// this will go from 0 to 2 and the next will go from 2 to 0
if (x < 2){
x++;
}
}
// enters the second row by changing the y index
y = 1;
// indicate what row it's on
System.out.println("Row 2");
// as long as one player hasnt placed too many xs or os
// if there are 4 xs or os already on the bord that player
// cannot make anymore moves so the rest will be the one
// that doesn't already have 4
if ( xs >= 4){
xAndO = 1;
}else if(os >= 4){
xAndO = 2;
}
else if (xs < 4 && os < 4){
// get a random number between one and two to assign to this box
// these numbers will stand for the X or O on the bord
// get a new random between 1 and 2 and set it
xAndO = r.nextInt(2)+1;
// continue adding the total xs and os
if (xAndO == 1){
os++;
}
else{
xs++;
}
}
// show which number will be saved above the index it'll be saved under
System.out.println("number to store " + xAndO);
// save the number at that index
tBord[x][y] = xAndO;
// display the index it was saved under
System.out.println("Index x =" + x);
System.out.println("Index y =" + y);
// continue counting down the number of spaces in the array
counter--;
// this one counts down the x index since the second column starts at x = 2
if (x > 0){
x--;
}
}
// last row the counter is now at 3 because there are 3 blocks left in the array
y = 2;
// show which row this index is on
System.out.println("Row 3");
// as long as one player hasnt placed too many xs or os
if ( xs >= 4){
xAndO = 1;
}else if(os >= 4){
xAndO = 2;
}
else if (xs < 4 && os < 4){
// get a random number between one and two to assign to this box
// these numbers will stand for the X or O on the bord
r = new Random();
xAndO = r.nextInt(2)+1;
if (xAndO == 1){
os++;
}
else{
xs++;
}
}
// indicate which number will be stored at this index
System.out.println("number to store " + xAndO);
// store the number
tBord[x][y] = xAndO;
// indicate which index the number will be stored under
System.out.println("Index x =" + x);
System.out.println("Index y =" + y);
// count down until all array spots have a value
counter--;
// count down the index of the columns
if (x < 2){
x++;
}
}
this.checkForWinner();
System.out.println("DONE BUILDING GAME");
}
OUTPUT:
clicked
Row 1
number to store 1
Index x =0
Index y =0
Row 1
number to store 1
Index x =1
Index y =0
Row 1
number to store 1
Index x =2
Index y =0
Row 2
number to store 1
Index x =2
Index y =1
Row 2
number to store 2
Index x =1
Index y =1
Row 2
number to store 2
Index x =0
Index y =1
Row 3
number to store 2
Index x =0
Index y =2
Row 3
number to store 2
Index x =1
Index y =2
Row 3
number to store 2
Index x =2
Index y =2
DONE BUILDING GAME