I have a bingo game I am trying to code, I got hit with some logic problems and I am also looking for a way to tidy up the following code. (a Bingo game is won by crossing out the numbers in a straight line (diagonally straight line also counts.))
My current code for the 5x5 grid to check for straight "XX"s. Checks for all 5 straight rows and 2 diagonally straights.
public static void bingoCheck(String[][] card1, String[][] card2) {
//check for player 1 bingo.
if ((card1[0][0] == "XX") && (card1[0][1] == "XX") && (card1[0][2] == "XX") && (card1[0][3] == "XX") && (card1[0][4] == "XX")) {
System.out.print("Bingo! Player 1 wins!");
} else if ((card1[1][0] == "XX") && (card1[1][1] == "XX") && (card1[1][2] == "XX") && (card1[1][3] == "XX") && (card1[1][4] == "XX")){
System.out.print("Bingo! Player 1 wins!");
} else if ((card1[2][0] == "XX") && (card1[2][1] == "XX") && (card1[2][2] == "XX") && (card1[2][3] == "XX") && (card1[2][4] == "XX")){
System.out.print("Bingo! Player 1 wins!");
} else if ((card1[3][0] == "XX") && (card1[3][1] == "XX") && (card1[3][2] == "XX") && (card1[3][3] == "XX") && (card1[3][4] == "XX")){
System.out.print("Bingo! Player 1 wins!");
} else if ((card1[4][0] == "XX") && (card1[4][1] == "XX") && (card1[4][2] == "XX") && (card1[4][3] == "XX") && (card1[4][4] == "XX")){
System.out.print("Bingo! Player 1 wins!");
} else if ((card1[0][4] == "XX") && (card1[1][3] == "XX") && (card1[2][2] == "XX") && (card1[3][1] == "XX") && (card1[4][0] == "XX")){
System.out.print("Bingo! Player 1 wins!");
} else if ((card1[4][4] == "XX") && (card1[3][3] == "XX") && (card1[2][2] == "XX") && (card1[1][1] == "XX") && (card1[0][0] == "XX")){
System.out.print("Bingo! Player 1 wins!");
}
// player 2 check
else if ((card2[0][0] == "XX") && (card2[0][1] == "XX") && (card2[0][2] == "XX") && (card2[0][3] == "XX") && (card2[0][4] == "XX")) {
System.out.print("Bingo! Player 2 wins!");
} else if ((card2[1][0] == "XX") && (card2[1][1] == "XX") && (card2[1][2] == "XX") && (card2[1][3] == "XX") && (card2[1][4] == "XX")){
System.out.print("Bingo! Player 2 wins!");
} else if ((card2[2][0] == "XX") && (card2[2][1] == "XX") && (card2[2][2] == "XX") && (card2[2][3] == "XX") && (card2[2][4] == "XX")){
System.out.print("Bingo! Player 2 wins!");
} else if ((card2[3][0] == "XX") && (card2[3][1] == "XX") && (card2[3][2] == "XX") && (card2[3][3] == "XX") && (card2[3][4] == "XX")){
System.out.print("Bingo! Player 2 wins!");
} else if ((card2[4][0] == "XX") && (card2[4][1] == "XX") && (card2[4][2] == "XX") && (card2[4][3] == "XX") && (card2[4][4] == "XX")){
System.out.print("Bingo! Player 2 wins!");
} else if ((card2[0][4] == "XX") && (card2[1][3] == "XX") && (card2[2][2] == "XX") && (card2[3][1] == "XX") && (card2[4][0] == "XX")){
System.out.print("Bingo! Player 2 wins!");
} else if ((card2[4][4] == "XX") && (card2[3][3] == "XX") && (card2[2][2] == "XX") && (card2[1][1] == "XX") && (card2[0][0] == "XX")){
System.out.print("Bingo! Player 2 wins!");
} else {
//back to getting user input
userIn(card1, card2);
}
}
The logic problem. I want to be able to announce 2 winners at the same time if both grids get the straight line.
This is how the output is right now, both cards got straight lines at the same time but the code only announces Player 2 as the winner only.
Player 1's card:
24 XX 8 1 25
12 XX 7 17 15
5 XX 20 19 13
14 XX XX 4 3
10 XX 11 21 9
Player 2's card:
24 21 17 15 XX
10 3 8 XX 20
14 7 XX 12 5
25 XX 13 19 11
XX 4 9 1 XX
Bingo! Player 2 wins!
Expecting output :
Player 1's card:
24 XX 8 1 25
12 XX 7 17 15
5 XX 20 19 13
14 XX XX 4 3
10 XX 11 21 9
Player 2's card:
24 21 17 15 XX
10 3 8 XX 20
14 7 XX 12 5
25 XX 13 19 11
XX 4 9 1 XX
Bingo! Player 1 wins!
Bingo! Player 2 wins!
If there is any way to tidy up/easier to write code then that'd be great too.
CodePudding user response:
General rule in writing a code is that you should extract code that repeats into separate function and use it, instead of repeating its body many times.
In your case many things repeat.
Notice that you have exactly the same code for checking each player's board. That's the first thing that you should extract to a separate method and just call it with board of each user.
You are repeating logic for checking each row. You are also repeating code that's checks each column. You should extract two methods, i.e.
checkRow
andcheckColumn
and call it in the for loop for each row/column. Diagonal case can be handled as 3rd method.Checking all elements in one row/column should be done with a for loop, instead of manually checking each position individually.
Just to summarize: the core rule, that you should have applied here is: don't repeat yourself (DRY).
CodePudding user response:
This will do the job
public static void bingoCheck(String[][] card1, String[][] card2) {
if (checkWinner(card1)) {
System.out.println("Bingo! Player 1 wins!");
} else if (checkWinner(card2)) {
System.out.println("Bingo! Player 2 wins!");
} else {
userIn(card1, card2);
}
}
private static boolean checkWinner(String[][] cards) {
boolean isAllCrossed = true;
int row = 0, col = 0;
// check horizontals
for (row = 0; row < cards.length; row ) {
isAllCrossed = true;
// check if whole row have XX
for (col = 0; col < cards[0].length; col ) {
if (!card[row][col].equals("XX")) {
isAllCrossed = false;
break;
}
}
if (isAllCrossed) return true;
}
// check digonal from top left to bottom right
isAllCrossed = true;
row = col = 0;
while (row < cards.length) {
if (!card[row][col].equals("XX")) {
isAllCrossed = false;
break;
}
}
row ;
col ;
}
if (isAllCrossed) return true;
// check digonal from top right to bottom left
isAllCrossed = true;
row = 0;
col = cards[0].length - 1;
while (row < cards.length) {
if (!card[row][col].equals("XX")) {
isAllCrossed = false;
break;
}
row ;
col--;
}
return isAllCrossed;
}