Design a bingo game.
Questions I asked:
Will the player board change?
No, The players board will always be a 5x5.
How many player can play?
You can assume there will be less than 10 players at one time.
How would you like me to store the bingo slots on player board String or object or something?
Does not matter just make the sure you have 75 bingo cards to choose from and each have equal probabilty to be chosen.
In the last five minute he ask How would you make this production ready?.. I kinda fumble and said make sure we make private methods and add unit test.. but I was blurred it out. Not sure what I was suppose to say here.
On Site
Location: Pittsburg
Experience L4
This is how I did. Please tell me if I did anything wrong or can improve.
public static void main(String[] args) {
String[] players = {"Betty white", "My Grandma", "Zombie", "Drunk guy", "CIA agent", "Harry potter"};
BingoPlayers game = new BingoPlayers(players);
game.simulateGame();
}
static class BingoDrawer{
LinkedList<Integer> drawer;
BingoDrawer(){
drawer = new LinkedList();
for(int i=1; i<=75; i++)
drawer.addLast(i);
}
int drawCard() {
int cardIndex = (int)(Math.random()*(drawer.size()-1));
int card = drawer.get(cardIndex);
drawer.remove(cardIndex);
return card;
}
boolean canDraw(){
return drawer.size() > 0;
}
}
static class BingoBoard {
BingoBoard() {
board = new int[5][5];
cardLeftOpen = new HashSet();
setUpBoard();
}
boolean playCard(int card) {
if(!cardLeftOpen.contains(card)) {
// card does not exist on you board
return false;
}
for(int i = 0; i < board.length; i++) {
for(int j = 0; j < board[0].length; j++) {
if(board[i][j] == card){
cardLeftOpen.remove(card);
board[i][j] = 0;
return checkIfWon(i,j);
}
}
}
return false;
}
void printBoard() {
for(int i = 0; i < board.length; i++) {
for(int j = 0; j < board[0].length; j++) {
System.out.print(board[i][j]+", ");
}
System.out.print("\n");
}
System.out.println("");
}
// private fuctions:
private int[][] board;
private Set<Integer> cardLeftOpen;
private void setUpBoard() {
Set<Integer> takenCards = new HashSet();
for(int i = 0; i < board.length; i++) {
for(int j=0; j < board[0].length; j++) {
while( 0 == board[i][j]) {
int randCard = (int)(Math.random()*75+1);
if(!takenCards.contains(randCard)) {
board[i][j] = randCard;
takenCards.add(randCard);
}
}
}
}
cardLeftOpen.addAll(takenCards);
}
private boolean checkIfWon(int i, int j) {
int count = 0;
int index = 0;
// check horizontal row
while(index < board[i].length) {
if(board[i][index++] == 0) count++;
}
if(count == board.length) return true;
count = 0;
index = 0;
//check vertical
while(index < board.length) {
if(board[index++][j] == 0) count++;
}
if(count == board.length) return true;
// check diagonals top left to bottom right
int x = 0;
int y = 0;
count = 0;
while(x < board.length && y < board[0].length) {
if(board[x++][y++] == 0) count++;
}
if(count == board.length) return true;
// check diagonals bottom left to top right
x = board.length-1;
y = 0;
count = 0;
while(x >=0 && y > board[0].length) {
if(board[x--][y++] == 0) count++;
}
return count == board.length;
}
}
static class BingoPlayers {
private BingoDrawer drawer = new BingoDrawer();
private Map<String, BingoBoard> bingoBoards;
BingoPlayers(String[] players) {
bingoBoards = new HashMap();
for(String player : players)
bingoBoards.put(player, new BingoBoard());
}
void simulateGame() {
while(true) {
if(!drawer.canDraw()) {
System.out.println("No one won the game");
return;
}
int cardCalled = drawer.drawCard();
for(Map.Entry<String, BingoBoard> entry : bingoBoards.entrySet()) {
// each plays their card and see if the won
if(entry.getValue().playCard(cardCalled)) {
System.out.println(entry.getKey() + " won the game.");
entry.getValue().printBoard();
return;
}
}
}
}
}