1

Ok, the title might be deceiving. All i want to do is take my Bingo program and when the second bingo card is printed, i want to replace all the "0"'s with "X"'s. I was thinking i would have to go and change the array to an string, but i'm not surer where to start.

Here is the Bingo program:

import java.util.*;
import java.io.*;
import java.util.Arrays;

 public class Bingo
 {
public static final int ROWS = 5;
public static final int COLS = 5;
public static final int VERTICAL = 1;
public static final int DIAGONAL = 2;
public static final int HORIZONTAL = 3;
public static int winFound;
public static int currPick = 0;
public static int randomPick = 0;
public static int WinFound;

public static void main(String[] args)
{   
int Totcards;
int[][] card = new int[ROWS][COLS];
int[] picks = new int[25];

fillCard (card);
printCard(card);
playGame(card);
printCard(card);
finalCard(card);

   }

private static void fillCard (int[][] card)
{
//  FileReader fileIn = new FileReader("Bingo.in");
//  Bufferreader in = new Bufferreader(fileIn);

    try {
    Scanner scan = new Scanner(new File("bingo.in"));
      for (int i=0; i<card.length; i++){
               for (int j=0; j<card[0].length; j++){
                card[i][j] = scan.nextInt();
    }
    }
    } catch(FileNotFoundException fnfe) {
      System.out.println(fnfe.getMessage());
    }

}

private static void printCard (int[][] card)
{
    System.out.println("\n\tYOUR BINGO CARD : ");
    System.out.println("\n\tB    I    N    G    O");
    System.out.println("\t----------------------");

    for (int i=0; i<card.length; i++){
          for (int j=0; j<card[0].length; j++){
              System.out.print("\t" + card[i][j]);
              }
    System.out.print("\n");
    }
}

private static void playGame (int[][] card)
{

    int numPicks = 0;       

    System.out.println("\n\tBINGO NUMBERS PICKED AT RANDOM FROM BIN: ");    
    while (true)
            {
               markCard (card);   // Generate a random num & zero-it out
       winFound = checkForWin(card);  //  Look for zero sums
       numPicks++;

               if (winFound != 0)
               {
        if (winFound == 1)
        {
            System.out.print("\n\n\tYOU WIN WITH A VERTICAL WIN AFTER " + numPicks + " PICKS\n");
        }
        else if (winFound == 2){
            System.out.print("\n\n\tYOU WIN WITH A DIAGONAL WIN AFTER " + numPicks + " PICKS\n");
        }
        else if (winFound == 3){
            System.out.print("\n\n\tYOU WIN WITH A HORIZONTAL WIN AFTER " + numPicks + " PICKS\n");
        }

                 announceWin (numPicks);
                 return;
               }
            }


   }        
private static void markCard (int[][] card)
{
  int randomPick = (int) (Math.random() * 74) + 1;


  for (int j = 0;  j < ROWS;  j++){
        for (int k = 0;  k < COLS;  k++){
            if (card[j][k]==randomPick)
                    card[j][k] = 0;}    
    }
    System.out.print("\t " + randomPick + " ");
System.out.print("");
}

private static int checkForWin(int[][] card)
{
  int sum=0;

   for (int i = 0;  i < ROWS;  i++)
       {
        sum = 0;
        for (int j = 0;  j < COLS;  j++)
        sum += card[i][j];

        if (sum == 0)
            return HORIZONTAL;
       }

       for (int j = 0;  j < COLS;  j++)
       {
         sum = 0;
         for (int i = 0;  i < ROWS;  i++)
            sum += card[i][j];

        if (sum == 0)
            return VERTICAL;
       }

       sum = 0;
       for (int i = 0;  i < ROWS;  i++)
            sum += card[i][ROWS-i-1];
       if (sum == 0)
            return DIAGONAL;

       sum = 0;
       for (int i = 0;  i < ROWS;  i++)
          sum += card[i][i];

       if (sum == 0)
            return DIAGONAL;

       return WinFound;
    } 

private static void makeCard(int[][] card, int[] picks)
{
            int count = 100;
            int currPick = 0;
            for (int i=0; i<count; i++){
             currPick = (int)(Math.random() * 74) + 1;
             System.out.print(" " + currPick + "\n");
    picks[i] = currPick;
            }
}

private static void announceWin(int numPicks)
{

}

private static boolean duplicate (int currPick, int[] picks, int numPicks)
{
    for (int i = 0;  i < numPicks;  i++){
                    if (picks[i] == currPick){
                            return true;}
        }
            return false;

}

private static void finalCard (int[][] card)
{ 
    Arrays.sort(card);
    final String stringRep = Arrays.toString(card);
    final String[] out =
        stringRep.substring(1, stringRep.length() - 1).split("\\s*,\\s*");

    System.out.println(Arrays.toString(out));
}
   }
1
  • 4
    Don't post your whole code, just post the relevant parts. Commented Sep 13, 2012 at 21:10

2 Answers 2

1

Try this:

System.out.print("\t" + (card[i][j] == 0 ? "X" : card[i][j]))
Sign up to request clarification or add additional context in comments.

2 Comments

that works but it just prints the string in a line. I need it to look like the regular printCard method.
See the edit above - this should be like your original printCard in terms of spacing.
1

Why don't you just use a String[][] for the fields from the beginning? You can still compare String values with ints (with Integer.valueOf for instance) and this way you don't have to switch types runtime...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.