This commit is contained in:
louiscklaw
2025-01-31 19:15:17 +08:00
parent 09adae8c8e
commit 6c60a73f30
1546 changed files with 286918 additions and 0 deletions

View File

@@ -0,0 +1,104 @@
import java.util.Scanner;
public class BingoGameBoard {
private Scanner input;
private List playerList;
private List records;
public BingoGameBoard(){
input = new Scanner(System.in);
playerList = new List();
records = new List();
}
public void regPlayer(Player player){
playerList.append(player);
player.setBingoGameCard(BingoGameCard.generateGameCard(playerList.getSize()));
}
public void printGameBoard(){
for(int i = 0; i < playerList.getSize(); i++){
Player player = (Player) playerList.getItemAt(i);
System.out.printf("Player%s's Card:\n", player.getName());
player.getBingoGameCard().print();
}
System.out.println();
}
public boolean hasWin(){
for(int i = 0; i < playerList.getSize(); i++){
Player player = (Player) playerList.getItemAt(i);
if(player.getBingoGameCard().isBingo())
return true;
}
return false;
}
public int getUserInput(){
int num = -1;
do{
try{
System.out.print("Game Host call (0 to exit): ");
num = Integer.parseInt(this.input.nextLine());
if(num < 0 || num > 25)
throw new NumberFormatException();
if(duplicateHistory(num))
throw new NumberRepeatException(num);
}catch(NumberFormatException e){
System.out.println("The number must be between 1 to 25, please call again!");
}catch(NumberRepeatException e){
System.out.println(e.getMessage());
num = -1;
}
}while(!(num < 26 && num > -1));
return num;
}
public boolean duplicateHistory(int num){
for(int i = 0; i < records.getSize(); i++){
if((int)records.getItemAt(i) == num)
return true;
}
return false;
}
public void place(int number){
for(int i = 0; i < playerList.getSize(); i++){
Player player = (Player) playerList.getItemAt(i);
player.getBingoGameCard().replace(number);
}
}
public void start(){
while(!hasWin()){
printGameBoard();
int num = getUserInput();
if(num == 0)
System.exit(1);
place(num);
records.append(num);
}
printGameBoard();
printBingoList();
}
private void printBingoList(){
for(int i = 0; i < playerList.getSize(); i++){
Player player = (Player) playerList.getItemAt(i);
if(player.getBingoGameCard().isBingo())
System.out.printf("Player%s Bingo!\n", player.getName());
}
}
}
class NumberRepeatException extends Exception{
NumberRepeatException(int num){
super("The number " + num +" is repeated, please call again!");
}
}

View File

@@ -0,0 +1,110 @@
public class BingoGameCard {
int[][] card;
private final static int[][][] CARD = {
{
{24,2,8,1,25},
{12,16,7,17,15},
{5,6,20,19,13},
{14,23,22,4,3},
{10,18,11,21,9}
},
{
{24,21,17,15,6},
{10,3,8,18,20},
{14,7,16,12,5},
{25,23,13,19,11},
{22,4,9,1,2}
}
};
public BingoGameCard(int[][] card){
this.card = card;
}
public void print(){
for(int i = 0; i < card.length; i++){
for(int j = 0; j < card[i].length; j++){
if(card[i][j] == -1)
System.out.print(" XX ");
else
System.out.printf(" %2d ", card[i][j]);
}
System.out.println();
}
}
public static BingoGameCard generateGameCard(int index){
if(index % 2 == 0)
return new BingoGameCard(CARD[1]);
return new BingoGameCard(CARD[0]);
}
public boolean isBingo(){
for(int i = 0; i < card.length; i++){
int count = 0;
for( int j = 0; j < card[i].length; j++){
if(j>0 && count == 0)
break;
if(card[i][j] == -1)
count++;
}
if(count == card[i].length)
return true;
}
for(int i = 0; i < card[0].length; i++){
int count = 0;
for(int j = 0; j < card.length; j++){
if(j>0 && count == 0)
break;
if(card[j][i] == -1)
count++;
}
if(count == card.length)
return true;
}
int count = 0;
for(int i = 0; i < card.length; i++){
if(i>0 && count == 0)
break;
if(card[i][card[i].length-i-1] == -1)
count++;
}
if (count == card.length)
return true;
count = 0;
for(int i = 0; i < card.length; i++){
if(i>0 && count == 0)
break;
if(card[i][i] == -1)
count++;
}
if(count == card.length)
return true;
return false;
}
public void replace(int number){
for(int i = 0; i < card.length; i++){
for(int j = 0; j < card[i].length; j++){
if(card[i][j] == number){
card[i][j] = -1;
break;
}
}
}
}
}

View File

@@ -0,0 +1,42 @@
public class List {
private Node head;
private Node tail;
private int size;
public List(){
head = tail = null;
size = 0;
}
public boolean isEmply(){
return head == null;
}
public void append(Object data){
if(head == null){
head = tail = new Node(data);
size++;
return;
}
tail.next = new Node(data);
tail = tail.next;
size++;
}
public Object getItemAt(int index){
Node tmp = head;
int tmpIndex = 0;
while(tmpIndex < index){
tmp = tmp.next;
tmpIndex++;
}
return tmp.data;
}
public int getSize(){
return size;
}
}

View File

@@ -0,0 +1,8 @@
public class Main {
public static void main(String[] args) {
BingoGameBoard game = new BingoGameBoard();
game.regPlayer(new Player("1"));
game.regPlayer(new Player("2"));
game.start();
}
}

View File

@@ -0,0 +1,14 @@
public class Node {
public Object data;
public Node next;
public Node(Object data){
this.data = data;
next = null;
}
public Node(Object data, Node next){
this.data = data;
this.next = next;
}
}

View File

@@ -0,0 +1,21 @@
public class Player {
private BingoGameCard card;
private String name;
public Player(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setBingoGameCard(BingoGameCard card){
this.card = card;
}
public BingoGameCard getBingoGameCard(){
return card;
}
}

View File

@@ -0,0 +1,168 @@
## BINGO game
### Test case
```
28
-2
16
10
10
22
6
20
18
2
23
```
### Expected Output
```
Player1's Card:
24 2 8 1 25
12 16 7 17 15
5 6 20 19 13
14 23 22 4 3
10 18 11 21 9
Player2's Card:
24 21 17 15 6
10 3 8 18 20
14 7 16 12 5
25 23 13 19 11
22 4 9 1 2
Game Host call (0 to exit): 28
-2
16
10
10
22
6
20
18
2
23
The number must be between 1 to 25, please call again!
Game Host call (0 to exit): The number must be between 1 to 25, please call again!
Game Host call (0 to exit):
Player1's Card:
24 2 8 1 25
12 XX 7 17 15
5 6 20 19 13
14 23 22 4 3
10 18 11 21 9
Player2's Card:
24 21 17 15 6
10 3 8 18 20
14 7 XX 12 5
25 23 13 19 11
22 4 9 1 2
Game Host call (0 to exit):
Player1's Card:
24 2 8 1 25
12 XX 7 17 15
5 6 20 19 13
14 23 22 4 3
XX 18 11 21 9
Player2's Card:
24 21 17 15 6
XX 3 8 18 20
14 7 XX 12 5
25 23 13 19 11
22 4 9 1 2
Game Host call (0 to exit): The number 10 is repeated, please call again!
Game Host call (0 to exit):
Player1's Card:
24 2 8 1 25
12 XX 7 17 15
5 6 20 19 13
14 23 XX 4 3
XX 18 11 21 9
Player2's Card:
24 21 17 15 6
XX 3 8 18 20
14 7 XX 12 5
25 23 13 19 11
XX 4 9 1 2
Game Host call (0 to exit):
Player1's Card:
24 2 8 1 25
12 XX 7 17 15
5 XX 20 19 13
14 23 XX 4 3
XX 18 11 21 9
Player2's Card:
24 21 17 15 XX
XX 3 8 18 20
14 7 XX 12 5
25 23 13 19 11
XX 4 9 1 2
Game Host call (0 to exit):
Player1's Card:
24 2 8 1 25
12 XX 7 17 15
5 XX XX 19 13
14 23 XX 4 3
XX 18 11 21 9
Player2's Card:
24 21 17 15 XX
XX 3 8 18 XX
14 7 XX 12 5
25 23 13 19 11
XX 4 9 1 2
Game Host call (0 to exit):
Player1's Card:
24 2 8 1 25
12 XX 7 17 15
5 XX XX 19 13
14 23 XX 4 3
XX XX 11 21 9
Player2's Card:
24 21 17 15 XX
XX 3 8 XX XX
14 7 XX 12 5
25 23 13 19 11
XX 4 9 1 2
Game Host call (0 to exit):
Player1's Card:
24 XX 8 1 25
12 XX 7 17 15
5 XX XX 19 13
14 23 XX 4 3
XX XX 11 21 9
Player2's Card:
24 21 17 15 XX
XX 3 8 XX XX
25 23 13 19 11
XX 4 9 1 XX
Game Host call (0 to exit):
Player1's Card:
24 XX 8 1 25
12 XX 7 17 15
5 XX XX 19 13
14 XX XX 4 3
XX XX 11 21 9
Player2's Card:
24 21 17 15 XX
XX 3 8 XX XX
14 7 XX 12 5
25 XX 13 19 11
XX 4 9 1 XX
Player1 Bingo!
Player2 Bingo!
```