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.
How to check for repeated numbers? If user already used a number then it should print out a message telling them that and then giving them back the input to type again. Here's my current code. public static void userIn(String[][] card1, String[][] card2) {
public static void userIn(String[][] card1, String[][] card2) {
String str = "";
System.out.print("Game host call (0 to exit): ");
choice = sc.nextInt();
// for user to exit the game
if (choice == 0) {
System.exit(0);
} else {
//check for out of bounds numbers
while (choice < 0 || choice > 25) {
System.out.println("The number must be between 1 to 25, please call again!");
System.out.print("Game host call (0 to exit): ");
choice = sc.nextInt();
}
//change user input to String
str = Integer.toString(choice);
//check if user input matches that on cards
for (i = 0;i <=4; i++) {
for (j = 0;j <=4; j++) {
if (str.equals(card1[i][j])) {
card1[i][j] = "XX";
}
if (str.equals(card2[i][j])) {
card2[i][j] = "XX";
}
}
}
//print player 1 card
System.out.println(player1);
for (i = 0; i < card1.length; i++) {
for (j = 0; j < card1.length; j++) {
System.out.printf(" %2s ", card1[i][j]);
}
System.out.print("\n");
}
// line break
System.out.print("\n");
//print player 2 card
System.out.println(player2);
for (i = 0; i < card2.length; i++) {
for (j = 0; j < card2.length; j++) {
System.out.printf(" %2s ", card2[i][j]);
}
System.out.print("\n");
}
// go to check if any player won.
bingoCheck(card1, card2);
}
}