I have a single file of a java Bingo program that I want to split into three separate classes (BingoMain, BingoGUI, BingoCard). Extending a program across several files is something I have never really done, so a little help is appreciated. I also want to add the ability to extend the current program to include functionality for up to 5 players. I know that all the code I need is pretty much in here already. Which lines/parts would go in which class and what would I edit in order to add the desired functionality? I don't expect any code, just some directions to get me on my way.
import java.awt.Container;
import java.awt.Font;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import javax.swing.*;
public class Bingo extends JFrame {
public static void main (String[] args) {
Integer[][] bingoCard = new Integer[5][5];
boolean[][] called = new boolean[][] { {false,false,false,false,false},
{false,false,false,false,false},
{false,false,true ,false,false},
{false,false,false,false,false},
{false,false,false,false,false}};
JFrame myBingoGUI;
/*
* Fill an array with 15 numbers (1-15) for the "B" column, put the numbers in random order, and
* put the first 5 of those numbers in the "B" column of the bingo card. Continue with I,N,G,and O columns.
*/
Integer[] row = new Integer[15];
for (int i=0; i<5; i++) {
fillRow(row, i*15+1);
randomize (row);
putRowInBingoCard(bingoCard,i,row);
}
/*
* Print the bingo card.
*/
myBingoGUI=new JFrame();
myBingoGUI.setSize(250, 250);
myBingoGUI.setLocation(400, 400);
myBingoGUI.setTitle("BINGO");
myBingoGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myBingoGUI.setVisible(true);
Container myContentPane = myBingoGUI.getContentPane();
JTextArea myTextArea = new JTextArea();
myTextArea.setFont(new Font("Monospaced", Font.PLAIN,14));
myContentPane.add(myTextArea);
myTextArea.append(toStringBingoCard(bingoCard,called));
System.out.println(toStringBingoCard(bingoCard,called));
/*
* Play the game:
*/
while (!winner(called)) {
String calledNumber = JOptionPane.showInputDialog(null,"Enter a BINGO call:");
int calledColumn=-1;
/*
* The first character of the input string is converted to a column number between 0 and 4
*/
if (Character.toUpperCase(calledNumber.charAt(0))=='B') calledColumn=0;
if (Character.toUpperCase(calledNumber.charAt(0))=='I') calledColumn=1;
if (Character.toUpperCase(calledNumber.charAt(0))=='N') calledColumn=2;
if (Character.toUpperCase(calledNumber.charAt(0))=='G') calledColumn=3;
if (Character.toUpperCase(calledNumber.charAt(0))=='O') calledColumn=4;
/*
* The remainder of the input string is converted to an integer
*/
int calledValue = Integer.parseInt(calledNumber.substring(1,calledNumber.length()));
/*
* The matrix of called numbers is update to show the number has been called.
*/
for (int i=0;i<5;i++)
if (bingoCard[i][calledColumn]==calledValue) called[i][calledColumn]=true;
/*
* Print the updated BINGO card.
*/
myTextArea.setText("");
myTextArea.append(toStringBingoCard(bingoCard,called));
System.out.println(toStringBingoCard(bingoCard,called));
} // while
myTextArea.append("\n\nBINGO!!");
} // main
//======================================================================================================
/*
* This method is just here for purposes of testing the program while it is being developed
*/
public static void printArray (Integer[] a) {
for (int i=0; i<a.length; i++)
System.out.print(" "+ a[i].intValue());
System.out.println();
}
/*
* Fill an array of Integers with 15 numbers with a given starting value.
*/
public static void fillRow(Integer[] theRow, int startValue){
for (int i = 0; i< 15; i++)
theRow[i]= new Integer(startValue++);
}
/*
* Take the array of sequential Integers and put them in random order.
*/
public static void randomize (Integer[] theArray) {
Collections.shuffle(Arrays.asList(theArray));
}
/*
* The BINGO card needs only five of the shuffled numbers, so just put in the first five.
*/
public static void putRowInBingoCard(Integer[][]card, int column, Integer[] row){
for (int i=0; i<5; i++)
card[i][column] = row[i];
}
/*
* Print the BINGO card.
*/
public static void printBingoCard(Integer[][] bcard,boolean[][] called) {
System.out.println(" B I N G O");
for (int i=0;i<5;i++) {
for (int j=0; j<5;j++) {
if (i==2 && j==2) System.out.print("<**>");
else if (called[i][j]) System.out.print("<"+bcard[i][j]+">");
else
System.out.print(" "+bcard[i][j]+" ");
if (bcard[i][j]<10) System.out.print(" ");
else System.out.print(" ");
} // for j
System.out.println();
} // for i
} //printBingoCard
public static String toStringBingoCard(Integer[][] bcard,boolean[][] called) {
String returnString="";
returnString+= (" B I N G O \n");
for (int i=0;i<5;i++) {
for (int j=0; j<5;j++) {
if (i==2 && j==2) returnString+= ("<**>");
else if (called[i][j]) returnString+= ("<"+bcard[i][j]+">");
else
returnString+= (" "+bcard[i][j]+" ");
if (bcard[i][j]<10) returnString+= (" ");
else returnString+= (" ");
} // for j
returnString+= "\n";
} // for i
return returnString;
} //toStringBingoCard
public static boolean winner(boolean[][] called){
boolean iAmAWinner = true;
// winner in a row?
for (int i=0;i<5;i++) {
for (int j=0;j<5;j++)
iAmAWinner &= called[i][j];
if(iAmAWinner) return true;
else
iAmAWinner= true;
} //for
// winner in a column?
for (int i=0;i<5;i++) {
for (int j=0;j<5;j++)
iAmAWinner &= called[j][i];
if(iAmAWinner) return true;
else
iAmAWinner= true;
} //for
// winner in a diagonal?
iAmAWinner=true;
for (int i=0;i<5;i++)
iAmAWinner &= called[i][i];
if(iAmAWinner) return true;
iAmAWinner=true;
for (int i=4;i>=0;i--)
iAmAWinner &= called[i][4-i];
if(iAmAWinner) return true;
return false;
}