Skip to content

Instantly share code, notes, and snippets.

@davecdempsey
Created June 15, 2019 18:06
Show Gist options
  • Save davecdempsey/ebc8c0174031c4e2c11d21a51ae3737f to your computer and use it in GitHub Desktop.
Save davecdempsey/ebc8c0174031c4e2c11d21a51ae3737f to your computer and use it in GitHub Desktop.
import java.awt.List;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
class Main {
public static ArrayList<Integer> bPotentialColumn = new ArrayList<Integer>();
public static ArrayList<Integer> iPotentialColumn = new ArrayList<Integer>();
public static ArrayList<Integer> nPotentialColumn = new ArrayList<Integer>();
public static ArrayList<Integer> gPotentialColumn = new ArrayList<Integer>();
public static ArrayList<Integer> oPotentialColumn = new ArrayList<Integer>();
public static ArrayList<Integer> bColumn = new ArrayList<Integer>();
public static ArrayList<Integer> iColumn = new ArrayList<Integer>();
public static ArrayList<Integer> nColumn = new ArrayList<Integer>();
public static ArrayList<Integer> gColumn = new ArrayList<Integer>();
public static ArrayList<Integer> oColumn = new ArrayList<Integer>();
public static void main(String[] args) {
Scanner reader = new Scanner(System.in); // Reading from System.in
PopulatePotentialBingoNumbers();
boolean inGame = true;
boolean hasStartedNewGame = false;
while (inGame) {
if (hasStartedNewGame == false) {
if (StartNewGame(reader)) {
StartNewGame();
System.out.println("Here is your card!");
System.out.println(CardDescriptionWithHeader());
hasStartedNewGame = true;
} else {
inGame = false;
}
}
if (AskThenCallNewNumber(reader)) {
System.out.println("WINNER");
hasStartedNewGame = false;
}
}
}
private static boolean AskThenCallNewNumber(Scanner reader)
{
System.out.println("Enter the called number: ");
int response = reader.nextInt(); // Scans the next token of the input as an int.
CallNumber(response);
System.out.println(CardDescriptionWithHeader());
return CheckForWin();
}
private static boolean StartNewGame(Scanner reader)
{
boolean hasNoResponse = false;
while (hasNoResponse == false) {
System.out.println("Start New Game? (Y: YES, N: NO) ");
String response = reader.next(); // Scans the next token of the input as an string.
if (response.equals("Y")) {
return true;
}
if (response.equals("N")) {
return false;
}
System.out.println("Please enter either Y for YES or N for NO");
}
return false;
}
private static void StartNewGame()
{
PopulateColumnWithRandomNumbers();
}
private static void CallNumber(Integer calledNumber)
{
if (bColumn.contains(calledNumber)) {
int index = bColumn.indexOf(calledNumber);
bColumn.add(index, 0);
}
if (iColumn.contains(calledNumber)) {
int index = iColumn.indexOf(calledNumber);
iColumn.add(index, 0);
}
if (nColumn.contains(calledNumber)) {
int index = nColumn.indexOf(calledNumber);
nColumn.add(index, 0);
}
if (gColumn.contains(calledNumber)) {
int index = gColumn.indexOf(calledNumber);
gColumn.add(index, 0);
}
if (oColumn.contains(calledNumber)) {
int index = oColumn.indexOf(calledNumber);
oColumn.add(index, 0);
}
}
private static boolean CheckForWin()
{
if (CheckForWinInColumn(bColumn)) {
return true;
}
if (CheckForWinInColumn(iColumn)) {
return true;
}
if (CheckForWinInColumn(nColumn)) {
return true;
}
if (CheckForWinInColumn(gColumn)) {
return true;
}
if (CheckForWinInColumn(oColumn)) {
return true;
}
for (int i = 0; i < 5; i += 1) {
if (CheckForWinInRow(i)) {
return true;
}
}
if (bColumn.get(0) == 0 &&
iColumn.get(1) == 0 &&
nColumn.get(2) == 0 &&
gColumn.get(3) == 0 &&
oColumn.get(4) == 0) {
return true;
}
if (bColumn.get(4) == 0 &&
iColumn.get(3) == 0 &&
nColumn.get(2) == 0 &&
gColumn.get(1) == 0 &&
oColumn.get(0) == 0) {
return true;
}
return false;
}
private static boolean CheckForWinInRow(int column)
{
if (bColumn.indexOf(column) != 0) {
return false;
}
if (iColumn.indexOf(column) != 0) {
return false;
}
if (nColumn.indexOf(column) != 0) {
return false;
}
if (gColumn.indexOf(column) != 0) {
return false;
}
if (oColumn.indexOf(column) != 0) {
return false;
}
return true;
}
private static boolean CheckForWinInColumn(ArrayList<Integer> column)
{
for (int i = 0; i < column.size(); i += 1) {
if (column.indexOf(i) != 0) {
return false;
}
}
return true;
}
private static void PopulatePotentialBingoNumbers()
{
bPotentialColumn.clear();
bPotentialColumn.addAll(PotentialBingoNumbersForColumn(1));
iPotentialColumn.clear();
iPotentialColumn.addAll(PotentialBingoNumbersForColumn(16));
nPotentialColumn.clear();
nPotentialColumn.addAll(PotentialBingoNumbersForColumn(31));
gPotentialColumn.clear();
gPotentialColumn.addAll(PotentialBingoNumbersForColumn(46));
oPotentialColumn.clear();
oPotentialColumn.addAll(PotentialBingoNumbersForColumn(61));
}
private static ArrayList<Integer> PotentialBingoNumbersForColumn(int starting)
{
ArrayList<Integer> column = new ArrayList<>();
for (int i = 0; i < 15; i += 1) {
column.add(starting + i);
}
return column;
}
public static void PopulateColumnWithRandomNumbers()
{
bColumn.clear();
bColumn.addAll(RandomNumbersFrom(bPotentialColumn, 5));
iColumn.clear();
iColumn.addAll(RandomNumbersFrom(iPotentialColumn, 5));
nColumn.clear();
nColumn.addAll(RandomNumbersFrom(nPotentialColumn, 5));
gColumn.clear();
gColumn.addAll(RandomNumbersFrom(gPotentialColumn, 5));
oColumn.clear();
oColumn.addAll(RandomNumbersFrom(oPotentialColumn, 5));
}
private static ArrayList<Integer> RandomNumbersFrom(ArrayList<Integer> potential, Integer count)
{
ArrayList<Integer> column = new ArrayList<>();
column.addAll(potential);
while(column.size() > count) {
column.remove(new Random().nextInt(column.size()));
}
return column;
}
private static String CardDescriptionWithHeader()
{
String card = "B I N G O" + "\n";
for (int i = 0; i < 5; i += 1) {
card += RowForIndex(i) + "\n";
}
return card;
}
private static String RowForIndex(int index)
{
return bColumn.toArray()[index] + " " +
iColumn.toArray()[index] + " " +
nColumn.toArray()[index] + " " +
gColumn.toArray()[index] + " " +
oColumn.toArray()[index];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment