Reputation: 25
I am working on a tic-tac-toe game in my computer science class using processing. In my program, I am coding a 3 in a row checker and I am trying to code this using a 2D array code since my array was initialized as a 2D array when coding the X / O. As well as, my teacher gave us an example of doing this code but in a 1D array, and so I do not know how to convert his example regarding the FOR loops to 2D so that it can run onto my program. I have converted the array code to apply to my program, and it is running, but the code (FOR loops) regarding if someone won is not working. Can someone help me?
void onlyX() { //views only X pieces, runs checker for onlyX (attempted to
convert to 2D array form)
for (int i=0; i<3; i++) {
for (int j=0; j<3; j++) {
if (boardPiece[i][j] == "X") {
onlyXPiece[i][j] = "X";
}
}
}
}
/*void onlyX() { //1D Form
for (int i=0; i<numberOfBoardPieces; i++) {
if (boardPiece[i] == "X") {
onlyXPiece[i] = "X";
}
}
}*/
void onlyO() { //views only O pieces, runs checker for onlyO (attempted to
convert to 2D array form)
for (int i=0; i<3; i++) {
for (int j=0; j<3; j++) {
if (boardPiece[i][j] == "O") {
onlyOPiece[i][j] = "O";
}
}
}
}
/*void onlyO() { //1D form
for (int i=0; i<numberOfBoardPieces; i++) {
if (boardPiece[i] == "O") {
onlyOPiece[i] = "O";
}
}
}*/
Upvotes: 1
Views: 226
Reputation: 211176
To check if someone has won the game you have to check the columns, rows and diagonals. Write a checkBoard
function with one parameter, where you can specify the kind of piece ("X"
pr "O"
) which you want to check:
boolean checkBoard(String p) {
boolean won = false;
// check rows and columns
for (int i=0; !won && i<3; i++) {
boolean eqRow = true;
boolean eqCol = true;
for (int j=0; j<3; j++) {
eqRow = eqRow && boardPiece[i][j] == p;
eqCol = eqCol && boardPiece[j][i] == p;
}
won = eqRow || eqCol;
}
// check diagonals
if ( !won )
{
boolean d1 = true;
boolean d2 = true;
for (int i=0; !won && i<3; i++) {
d1 = d1 && boardPiece[i][i] == p;
d2 = d2 && boardPiece[2-i][i] == p;
}
won = d1 || d2;
}
return won;
}
Upvotes: 2
Reputation: 4561
Your 2D array FOR loop (usually called a double FOR LOOP) seems fine, though I can't quite understand why you would place the X's and O's in separate 2D arrays. There are multiple ways to check for a win condition, but this seems overly complex.
I adapted the double for loop at the top of your code to perform a win check that is easy to understand and usable for both X and O. Don't forget you also have to check for vertical and diagonal wins.
String[][] boardPiece = {{"X", "", "O"},
{"O", "O","O"},
{"X", "", "X"}};
void setup() {
println("X wins = "+ str(checkRows("X")));
println("O wins = "+ str(checkRows("O")));
}
boolean checkRows(String XorO) {
for (int i=0; i<3; i++) { //for every row
boolean win = true; //set win to true
for (int j=0; j<3; j++) { //for every column
if (boardPiece[i][j] != XorO) { //if the column does not equal the sign that you are checking
win = false; // meaning its the other, or it's empty, then set win to false
}
}
if (win) {
return true; //if win is true, then there are 3 X's or 3 O's in this row, so return true
}
}
return false; // if none of the rows contain 3 equal, return false
}
Upvotes: 2