I need help! I can print the bingo board but cant play the game. I need to be ab
ID: 3540858 • Letter: I
Question
I need help! I can print the bingo board but cant play the game. I need to be able to play the game.....please help!
import java.util.*;
public class Bingo{
static ArrayList<Integer> B = new ArrayList<Integer>();
static ArrayList<Integer> I = new ArrayList<Integer>();
static ArrayList<Integer> N = new ArrayList<Integer>();
static ArrayList<Integer> G = new ArrayList<Integer>();
static ArrayList<Integer> O = new ArrayList<Integer>();
public static int randomInt(int low, int high) {
return (int)(Math.random() * (high - low + 1)) + low;
}
public static void setArrayList(ArrayList<Integer> al, int sz, int r1, int r2) {
int n;
for (int a=0; a<sz; a++) {
while (true) {
n = randomInt(r1, r2);
if (!al.contains(n)) {
al.add(n);
break;
}
}
}
}
public static void main(String[] args) {
setArrayList(B, 5, 1, 15);
setArrayList(I, 5, 16, 30);
setArrayList(N, 4, 31, 45);
setArrayList(G, 5, 46, 60);
setArrayList(O, 5, 61, 75);
System.out.println(" B I N G O");
for (int a=0; a<5; a++) {
if (a<2)
System.out.printf("%2d %2d %2d %2d %2d ",
B.get(a), I.get(a), N.get(a), G.get(a), O.get(a));
else if (a==2)
System.out.printf("%2d %2d %s %2d %2d ",
B.get(a), I.get(a), " ", G.get(a), O.get(a));
else
System.out.printf("%2d %2d %2d %2d %2d ",
B.get(a), I.get(a), N.get(a-1), G.get(a), O.get(a));
}
}
}
Explanation / Answer
/*
* LightzOut
* 1/14/10
*
* Random Generator - For Bingo
*/
// Only importing 2 classes out of the API: The Random class and the Scanner class.
import java.util.Random;
import java.util.Scanner;
public class RandomGenMain
{
private static Scanner in = new Scanner(System.in);
public static void main(String[] args)
{
// Don't include a seed because you want it to generate different
// numbers every time you run the program.
Random randGen = new Random();
// Centering the title (for my program atleast).
System.out.println(" -BINGO-");
System.out.println();
String enter;
int rNum = randGen.nextInt(24);
// Declaring an array to store the words in the bingo game.
// Could have used a loop to initialize each spot in the array that way
// (using user input for each word)
// but i had to do it like this for my class because
// these were the words my group was using in the bingo game.
// And it wouldnt make sense to have to fill in a user input for the all of the words.
// So, delete this array and ill give you code to put here instead using a loop
// so that you can create your own words/numbers for each index.
String[] words = new String[24];
words[0] = "Protest";
words[1] = "Picket Sign";
words[2] = "Eviction";
words[3] = "Farmers";
words[4] = "Mobs";
words[5] = "Foreclosure";
words[6] = "Starvation";
words[7] = "Unemployment";
words[8] = "Dreamseller";
words[9] = "Poor";
words[10] = "Roosevelt";
words[11] = "Bonus Army";
words[12] = "Discontent";
words[13] = "Beatings";
words[14] = "Looting";
words[15] = "Depressed";
words[16] = "Ragged Individualism";
words[17] = "Communists";
words[18] = "Arkansas";
words[19] = "Poverty";
words[20] = "Government";
words[21] = "Oklahoma";
words[22] = "Hunger March";
words[23] = "Protest";
// Deffinately should have used an array - loop here, but i was too lazy
// to change it after i noticed i should have used an array. If you are
// familiar with arrays then i suggest you change it to that in your
// own program (unless your as lazy as i am). Copy/Paste ftw.
boolean tf1 = false;
boolean tf2 = false;
boolean tf3 = false;
boolean tf4 = false;
boolean tf5 = false;
boolean tf6 = false;
boolean tf7 = false;
boolean tf8 = false;
boolean tf9 = false;
boolean tf10 = false;
boolean tf11 = false;
boolean tf12 = false;
boolean tf13 = false;
boolean tf14 = false;
boolean tf15 = false;
boolean tf16 = false;
boolean tf17 = false;
boolean tf18 = false;
boolean tf19 = false;
boolean tf20 = false;
boolean tf21 = false;
boolean tf22 = false;
boolean tf23 = false;
boolean tf24 = false;
int total = 24;
// Just creating a simple for loop that ends once i is greater than 24
// And you don't add to i for a reason - you subtract from the variable
// total (which = 24 | as declared above).
for(int i = 0; i < total;)
{
// This is the random # generator, it will generate numbers
// 0 - 23
rNum = randGen.nextInt(24);
// Ehh just a series of if statements which first check if tf# is = to
// false (this is so that you don't have a bunch of repeated words).
if(tf1 == false)
{
// If the random number that is generated (rNum) is = to the # on
// the right (in this case 0), then it does the code corresponding
// below it. (If not it just skips it). Then it just prints out
// the dashes (for looks) and then the word/number.
// It also makes tf# = true so that it wont repeat that word again.
// And also subtracts 1 from total.
if(rNum == 0)
{
System.out.print("--------------------------------------------------------------------------------");
enter = in.nextLine();
System.out.println(" The word is: " + words[0]);
System.out.println();
tf1 = true;
--total;
}
}
if(tf2 == false)
{
if(rNum == 1)
{
System.out.print("--------------------------------------------------------------------------------");
enter = in.nextLine();
System.out.println(" The word is: " + words[1]);
System.out.println();
tf2 = true;
--total;
}
}
if(tf3 == false)
{
if(rNum == 2)
{
System.out.print("--------------------------------------------------------------------------------");
enter = in.nextLine();
System.out.println(" The word is: " + words[2]);
System.out.println();
tf3 = true;
--total;
}
}
if(tf4 == false)
{
if(rNum == 3)
{
System.out.print("--------------------------------------------------------------------------------");
enter = in.nextLine();
System.out.println(" The word is: " + words[3]);
System.out.println();
tf4 = true;
--total;
}
}
if(tf5 == false)
{
if(rNum == 4)
{
System.out.print("--------------------------------------------------------------------------------");
enter = in.nextLine();
System.out.println(" The word is: " + words[4]);
System.out.println();
tf5 = true;
--total;
}
}
if(tf6 == false)
{
if(rNum == 5)
{
System.out.print("--------------------------------------------------------------------------------");
enter = in.nextLine();
System.out.println(" The word is: " + words[5]);
System.out.println();
tf6 = true;
--total;
}
}
if(tf7 == false)
{
if(rNum == 6)
{
System.out.print("--------------------------------------------------------------------------------");
enter = in.nextLine();
System.out.println(" The word is: " + words[6]);
System.out.println();
tf7 = true;
--total;
}
}
if(tf8 == false)
{
if(rNum == 7)
{
System.out.print("--------------------------------------------------------------------------------");
enter = in.nextLine();
System.out.println(" The word is: " + words[7]);
System.out.println();
tf8 = true;
--total;
}
}
if(tf9 == false)
{
if(rNum == 8)
{
System.out.print("--------------------------------------------------------------------------------");
enter = in.nextLine();
System.out.println(" The word is: " + words[8]);
System.out.println();
tf9 = true;
--total;
}
}
if(tf10 == false)
{
if(rNum == 9)
{
System.out.print("--------------------------------------------------------------------------------");
enter = in.nextLine();
System.out.println(" The word is: " + words[9]);
System.out.println();
tf10 = true;
--total;
}
}
if(tf11 == false)
{
if(rNum == 10)
{
System.out.print("--------------------------------------------------------------------------------");
enter = in.nextLine();
System.out.println(" The word is: " + words[10]);
System.out.println();
tf11 = true;
--total;
}
}
if(tf12 == false)
{
if(rNum == 11)
{
System.out.print("--------------------------------------------------------------------------------");
enter = in.nextLine();
System.out.println(" The word is: " + words[11]);
System.out.println();
tf12 = true;
--total;
}
}
if(tf13 == false)
{
if(rNum == 12)
{
System.out.print("--------------------------------------------------------------------------------");
enter = in.nextLine();
System.out.println(" The word is: " + words[12]);
System.out.println();
tf13 = true;
--total;
}
}
if(tf14 == false)
{
if(rNum == 13)
{
System.out.print("--------------------------------------------------------------------------------");
enter = in.nextLine();
System.out.println(" The word is: " + words[13]);
System.out.println();
tf14 = true;
--total;
}
}
if(tf15 == false)
{
if(rNum == 14)
{
System.out.print("--------------------------------------------------------------------------------");
enter = in.nextLine();
System.out.println(" The word is: " + words[14]);
System.out.println();
tf15 = true;
--total;
}
}
if(tf16 == false)
{
if(rNum == 15)
{
System.out.print("--------------------------------------------------------------------------------");
enter = in.nextLine();
System.out.println(" The word is: " + words[15]);
System.out.println();
tf16 = true;
--total;
}
}
if(tf17 == false)
{
if(rNum == 16)
{
System.out.print("--------------------------------------------------------------------------------");
enter = in.nextLine();
System.out.println(" The word is: " + words[16]);
System.out.println();
tf17 = true;
--total;
}
}
if(tf18 == false)
{
if(rNum == 17)
{
System.out.print("--------------------------------------------------------------------------------");
enter = in.nextLine();
System.out.println(" The word is: " + words[17]);
System.out.println();
tf18 = true;
--total;
}
}
if(tf19 == false)
{
if(rNum == 18)
{
System.out.print("--------------------------------------------------------------------------------");
enter = in.nextLine();
System.out.println(" The word is: " + words[18]);
System.out.println();
tf19 = true;
--total;
}
}
if(tf20 == false)
{
if(rNum == 19)
{
System.out.print("--------------------------------------------------------------------------------");
enter = in.nextLine();
System.out.println(" The word is: " + words[19]);
System.out.println();
tf20 = true;
--total;
}
}
if(tf21 == false)
{
if(rNum == 20)
{
System.out.print("--------------------------------------------------------------------------------");
enter = in.nextLine();
System.out.println(" The word is: " + words[20]);
System.out.println();
tf21 = true;
--total;
}
}
if(tf22 == false)
{
if(rNum == 21)
{
System.out.print("--------------------------------------------------------------------------------");
enter = in.nextLine();
System.out.println(" The word is: " + words[21]);
System.out.println();
tf22 = true;
--total;
}
}
if(tf23 == false)
{
if(rNum == 22)
{
System.out.print("--------------------------------------------------------------------------------");
enter = in.nextLine();
System.out.println(" The word is: " + words[22]);
System.out.println();
tf23 = true;
--total;
}
}
if(tf24 == false)
{
if(rNum == 23)
{
System.out.print("--------------------------------------------------------------------------------");
enter = in.nextLine();
System.out.println(" The word is: " + words[23]);
System.out.println();
tf24 = true;
--total;
}
}
}
System.out.println("--------------------------------------------------------------------------------");
System.out.println("End of Program. ");
System.out.println();
}
}