Bingo game Cover() method not working! Help!
I have written all the code for my Bingo game to work, but my cover() method in BingoCard is not working. I have attached the BingoCard.java code below and below that is the BingoGame.java code to make the whole thing work.
The cover() method is supposed to replace the BingoSquare with a null, which will then be changed to "****" in the toString() method. I think I have all the code correct, but the cover() method is not covering any of the squares of myBingoCard with null. Help?
Code:
public class BingoCard
{
private BingoSquare[][] myCard;
//hard coding numbers into each row and column
public BingoCard()
{
myCard = new BingoSquare[5][5];
myCard[0][0] = new BingoSquare(3);
myCard[0][1] = new BingoSquare(18);
myCard[0][2] = new BingoSquare(33);
myCard[0][3] = new BingoSquare(48);
myCard[0][4] = new BingoSquare(63);
myCard[1][0] = new BingoSquare(6);
myCard[1][1] = new BingoSquare(21);
myCard[1][2] = new BingoSquare(36);
myCard[1][3] = new BingoSquare(51);
myCard[1][4] = new BingoSquare(66);
myCard[2][0] = new BingoSquare(9);
myCard[2][1] = new BingoSquare(24);
//myCard[2][2] = null; moved down to toString()
myCard[2][3] = new BingoSquare(54);
myCard[2][4] = new BingoSquare(69);
myCard[3][0] = new BingoSquare(12);
myCard[3][1] = new BingoSquare(27);
myCard[3][2] = new BingoSquare(42);
myCard[3][3] = new BingoSquare(57);
myCard[3][4] = new BingoSquare(72);
myCard[4][0] = new BingoSquare(15);
myCard[4][1] = new BingoSquare(30);
myCard[4][2] = new BingoSquare(45);
myCard[4][3] = new BingoSquare(60);
myCard[4][4] = new BingoSquare(75);
}
public String toString()
{
myCard[2][2] = null;
String a = "";
for(int j = 0; j < 5; j++)
{
for(int i = 0; i < 5; i++)
{
if(myCard[j][i] != null){
a += myCard[j][i] + " ";
}
else a += "**** ";
}
a += "\n";
}
return a;
}
public boolean cover(BingoBall ball){
for(int row = 0; row < 5; row++){
for(int col = 0; col < myCard[row].length; col++)
{
if(myCard[row][col] != null && myCard[row][col].equals(ball.toString()))
{
myCard[row][col] = null;
return true;
}
}
}
return false;
}
public boolean hasBingo(){
//four for loop for rows, columns, and two diagonals
for(int row = 0; row < myCard.length; row++){
for(int col = 0; col < myCard[row].length; col++){
if(myCard[row][col] == null){
return true;
}
}
}
return false;
}
}
************************************************** *********************
Code:
BingoGame.java code:
//********************************************************************
//
//
//
// Driver to test cover method in BingoCard class
//********************************************************************
public class BingoGame
{
/******************************************************************
Creates myBingoCard as a 5 x 5 array of BingoBalls
Creates 5 specific BingoBalls and covers myBingocard
Prints "Bingo" on a true return from cover method
Prints final state of myBingoCard
*******************************************************************/
public static void main (String[] args)
{
// create my Bingo Card
BingoCard myBingoCard = new BingoCard();
// create some Bingo Balls, cover on myBingoCard, and check for Bingo
// change BingoBall values to test numbers on and not on your card
// test at least one Bingo on a row, a column, or a diagonal
if (myBingoCard.cover(new BingoBall(1)) && myBingoCard.hasBingo()) // not on myBingoCard
System.out.println("Bingo on 1st");
if (myBingoCard.cover(new BingoBall(3)) && myBingoCard.hasBingo()) // next four are a diagonal
System.out.println("Bingo on 2nd");
if (myBingoCard.cover(new BingoBall(21)) && myBingoCard.hasBingo())
System.out.println("Bingo on 3rd");
if (myBingoCard.cover(new BingoBall(57)) && myBingoCard.hasBingo())
System.out.println("Bingo on 4th");
if (myBingoCard.cover(new BingoBall(75)) && myBingoCard.hasBingo())
System.out.println("Bingo on 5th");
// print the final state of myBingoCard
System.out.println(myBingoCard);
}
}
Re: Bingo game Cover() method not working! Help!
Have you tried debugging the code to see why the cover() method is not doing what you want it to do?
One debug technique is to add println statements that print out the values of the variables that are used to control the program. The print outs will show you what the computer sees so you will know what the problem is.
The posted code does not compile without errors. For example missing class definitions
Re: Bingo game Cover() method not working! Help!
The posted code does not compile because it needs the BingoBall.java. I am adding that below:
Code:
public class BingoBall extends BingoNumber
{
public BingoBall(int num){
super(num);
}
}
And also needs BingoNumber.java which is below:
Code:
public class BingoNumber
{
protected char letter;
protected int number;
/******************************************************************
Sets up this Bingo number with the specified number and the
appropriate letter.
******************************************************************/
public BingoNumber (int num)
{
number = num;
if (num <= 15)
letter = 'B';
else
if (num <= 30)
letter = 'I';
else
if (num <= 45)
letter = 'N';
else
if (num <= 60)
letter = 'G';
else
letter = 'O';
}
/******************************************************************
Returns a boolean true if this number equals that number.
Returns false otherwise.
******************************************************************/
public boolean equals (BingoNumber that)
{
return that != null && this.number == that.number;
}
/******************************************************************
Returns a string representation of this Bingo number.
******************************************************************/
public String toString ()
{
return (letter + " " + ((number > 9) ? number : "0" + number));
}
}
I debugged the code with println statements. The program compiles just fine, but does not compare the two strings together to cover the squares.
This is the code I have:
Code:
if(myBingoCard[row][col] != null && myBingoCard.equals(bingoBall.toString()))
{
myBingoCard[row][col] = null;
return true;
How do I compare these two strings together, the myCard[row][col] and the bingoBall?
Re: Bingo game Cover() method not working! Help!
Quote:
does not compare the two strings together to cover the squares.
What keeps the code from doing the compare?
Quote:
How do I compare these two strings together, the myCard[row][col] and the bingoBall?
Code:
myCard[row][col].equals(bingoBall)
The above code assumes myCard[row][col] and bingoBall are both String objects
What does the println statements print out for the values of those two Strings before they are compared in the if statement?
THE CODE DOES NOT COMPILE BECAUSE OF MISSING CLASSES!!!
Re: Bingo game Cover() method not working! Help!
I figured out my problem. Stepping through it with println statements helped a ton! Thank you very much!
Re: Bingo game Cover() method not working! Help!