[英]Writing a bingo game in Java, having trouble
我是 Java 新手,我有一個項目需要創建一個賓果游戲,該游戲旨在在 CMD/終端中播放。 我在替換 2D 數組中的數字時遇到問題(如果玩家輸入的數字與 2 張牌中的任何一張相匹配,它將被替換為“XX”,然后使用標記為“XX”的數字繼續游戲)並檢查是否用戶已獲得一行中的所有數字,然后顯示“賓果游戲,玩家“姓名”獲勝!”(將只有 2 張玩家卡)。 我也無法弄清楚如何阻止用戶輸入已經划掉/重復的數字。
import java.util.Scanner;
public class demno {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int call, r = 0, c = 0; /* For the player to input bingo numbers.
To declare row and column globally for later use. */
// Initiate card 1 numbers
int[][] card1 = { {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} };
// Initiate card 2 numbers
int[][] card2 = { {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 } };
// Player 1's card
System.out.println("Player 1's card:");
for (r=0; r<card1.length; r++) {
for(c=0;c<card1.length; c++){
if (10 > card1[r][c] ){
System.out.print(" "+ card1[r][c] +" ");
}else {
System.out.print(card1[r][c] + " ");
}
}
System.out.println("");
}
// End Player 1's card
System.out.print('\n');
// Player 2's card
System.out.println("Player 2's card:");
for (r=0; r<card2.length; r++) {
for(c=0;c<card2.length; c++){
if (10 > card2[r][c]) {
System.out.print(" " + card2[r][c] + " ");
}else {
System.out.print(card2[r][c] + " ");
}
}
System.out.println("");
}
// End Player 2's card
// User input for bingo number
System.out.println();
System.out.print("Game Host call (0 to exit): ");
call = sc.nextInt();
sc.close();
// Checks user input for errors or if player want's to exit.
if (call >25) {
System.out.println("The number must be between 1 to 25, please call again! ");
System.out.print("Game Host call (0 to exit): ");
call = sc.nextInt();
sc.close();
}else if (call == 0) {
System.exit(0);
}
}
}
您可以用array[0] = "XX"
替換數組中的值。 所以在你的情況下它會是card2[r][c] = "XX"
。
為了確保每個數字都使用一次,一個簡單的解決方案是使用 ArrayList。 所以你可以像這樣使用它
List<Integer> usedNumbers = new ArrayList<Integer>();
// when number was used
usedNumbers.add(call);
//check if already used
boolean alreadyUsed = usedNumbers.contains(call);
提示:
for (r=0; r<card2.length; r++) {
for(c=0;c<card2.length; c++){
如果字段 c 長度與 r 長度相同,則有效。 如果 c 的長度與 r 不同,則會得到 OutOfBoundsException。 正確的方法是
for (r=0; r<card2.length; r++) {
for(c=0;c<card2[r].length; c++){
要實現游戲邏輯,您必須執行以下步驟:
在掃描數字后檢查玩家 a 是否匹配。
如果有匹配,給他一分(可能增加一個 int 變量?)。
然后用XX替換匹配。
玩家 b 也是如此。
如果點變量之一 == 5 打印獲勝者並停止應用程序
把事情分開。 更新卡片和檢查勝利是兩件不同的事情。 定義單獨的方法來執行它們。
private void updateCard(int[][] card, int ball) { ... }
private boolean isWinner(int[][] card) { ... }
因為您將卡片建模為int[][]
您顯然不能在其中放置“XX”,因為那是String
。 沒關系。 當球與卡片單元格匹配時,用不是合法值的標記替換值編號(例如,假設球的范圍從 1 到 25,您可以使用 -1 作為標記)。 創建一個方法,該方法將打印卡片,但將檢查單元格中的值,並在遇到標記時在輸出中將其替換為“XX”。
private void printCard(int[][] card) { ... }
賓果游戲的經典玩法是從籃子里抽出一系列球。 每個可能的數字都有一個球。 因此,您不能多次抽取相同的數字。 您可以通過創建一籃子數字來模擬這一點。 當用戶輸入一個數字時,將其從籃子中取出。 如果它不在籃子里,那么它就不是一個有效的數字,用戶必須輸入一個不同的數字。
public class Game {
private Set<Integer> basket = new HashSet<Integer>();
public static void main(String[] args) {
initialize(basket); // put all the balls are in the basket
...
}
然后您可以執行類似操作以確保尚未繪制球。
private int drawBall(Set<Integer> basket) {
Objects.requireNonNull(basket, "basket cannot be null");
if (basket.isEmpty()) {
throw new NoSuchElementException("There are no more balls in the basket");
}
int ball = getUserDraw();
while (basket.contains(ball)) {
System.out.println("Sorry, that ball has already been drawn");
ball = getUserDraw();
}
basket.remove(ball);
return ball;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.