Results 1 to 6 of 6
  1. #1
    08cmoss is offline Member
    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0

    Exclamation 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?

    Java 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;
      }
    }
    ************************************************** *********************
    Java 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);
      }
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,610
    Rep Power
    23

    Default 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
    Last edited by Norm; 09-27-2012 at 05:00 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    08cmoss is offline Member
    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0

    Default 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:
    Java Code:
    public class BingoBall extends BingoNumber
    {
      public BingoBall(int num){
       super(num);
       }
    }
    And also needs BingoNumber.java which is below:
    Java 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:
    Java 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?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,610
    Rep Power
    23

    Default Re: Bingo game Cover() method not working! Help!

    does not compare the two strings together to cover the squares.
    What keeps the code from doing the compare?

    How do I compare these two strings together, the myCard[row][col] and the bingoBall?
    Java 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!!!
    Last edited by Norm; 09-27-2012 at 06:25 PM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    08cmoss is offline Member
    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0

    Default 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!

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,610
    Rep Power
    23

    Default Re: Bingo game Cover() method not working! Help!

    Glad you worked it out.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Bingo cover() method not working. Help!
    By 08cmoss in forum New To Java
    Replies: 1
    Last Post: 09-27-2012, 02:30 AM
  2. working on a pacman game need help
    By paris72 in forum Java Gaming
    Replies: 2
    Last Post: 06-04-2012, 10:04 AM
  3. Multiplayer Bingo game
    By js4learn in forum Java Gaming
    Replies: 1
    Last Post: 07-08-2011, 03:43 AM
  4. Game using swings not working
    By humanbeing in forum Java Gaming
    Replies: 7
    Last Post: 10-06-2010, 03:04 PM
  5. Help please, bingo game.
    By XMarkoX in forum New To Java
    Replies: 8
    Last Post: 11-23-2009, 08:45 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •