3 回答

TA貢獻(xiàn)1850條經(jīng)驗 獲得超11個贊
這種任務(wù)不是很容易的水平,這里有很多事情要做,也許你必須從一些更基本的東西開始。但是,如果您確定要通過,這里有一些建議和代碼:您必須score向 Card 類添加字段(類的單個名稱優(yōu)于復(fù)數(shù),變量的首字母小寫,這是一些代碼約定) . 不容易,因為 A 可以具有多值。使用 LinkedList 而不是 ArrayList 作為 Deck.cards。發(fā)牌者每次輪詢一張牌,這與真實游戲更相似。
把this.cards.add(cards);'deck [Suits.length * i + j] = Ranks [i] + " of " + Suits [j];' 你根本不需要這個字符串?dāng)?shù)組。最重要的部分是您可以將骨架放在您指向的地方,最需要幫助的地方:
private static void playGame(Scanner scan) {
Deck deck = new Deck();
String playGame;
Integer yourScore = 0;
Random random = new Random();
Boolean playerEnough = false;
Boolean dealerEnough = false;
Integer dealerScore = 0;
deck.shuffle();
while ((yourScore <= 21 && dealerScore <= 21) && (!dealerEnough || !playerEnough)) {
if (yourScore == 0) {
yourScore += getCardScore(deck, "Player");
yourScore += getCardScore(deck, "Player");
}
if (dealerScore == 0) {
dealerScore += getCardScore(deck, "Dealer");
dealerScore += getCardScore(deck, "Dealer");
}
if (!playerEnough) {
System.out.println("Want a card?");
playGame = scan.nextLine();
if (playGame.equals("yes")) {
yourScore += getCardScore(deck, "Player");
} else {
System.out.println("PlayerDone");
playerEnough = true;
}
}
if (!dealerEnough) {
if (random.nextBoolean()) {
dealerScore += getCardScore(deck, "Dealer");
} else {
System.out.println("DealerDone");
dealerEnough = true;
}
}
System.out.println(yourScore + " [p] : [d] " + dealerScore);
}
// decide who is a winner
}
private static Integer getCardScore(Deck deck, String who) {
System.out.print(who + " given: ");
Cards cards = deck.cards.pollFirst();
System.out.println(cards);
return cards.getScore();
}
這可以幫助你更進(jìn)一步,或者沒有,那么我建議你解決一些較小的練習(xí)。

TA貢獻(xiàn)1853條經(jīng)驗 獲得超6個贊
一種有點過時但仍然非常有效的軟件設(shè)計方法稱為瀑布過程:從最頂層開始,逐步深入到細(xì)節(jié),而不是相反。比如先從游戲的整體流程說起:
public static class Game {
public static void main(String args[]) {
while (/* player wishes to continue */) {
/* play a hand */
}
System.out.println('Thank you for playing.');
}
}
現(xiàn)在,考慮需要為上面的評論做些什么。你希望如何與玩家溝通以獲得他的意圖?鍵盤?圖形用戶界面?這將決定您如何充實該部分。然后決定“玩一手牌”部分的總體策略,你會得到一個新的水平:
public class Game {
. . .
public void playOneHand(Player p) {
/* create new shuffled deck */
/* deal initial cards */
while (/* player has choices */) {
/* get player choices */
}
/* play dealer hand */
/* display result */
}
}
public class Player {
/* Maybe actions should be numbers from a menu? An enum class? */
public String getPlayerAction(String prompt) {
Scanner sc = new Scanner(System.in);
System.out.println(prompt);
return sc.nextLine();
}
}
那是第 2 級?,F(xiàn)在開始填寫每條評論,這將帶您進(jìn)入第 3 級,依此類推。最終你會深入了解卡片和數(shù)字的細(xì)節(jié),但直到你準(zhǔn)備好將它們放在某個地方。

TA貢獻(xiàn)1805條經(jīng)驗 獲得超10個贊
現(xiàn)在我對 Java 比較陌生,所以我假設(shè)會有更多知識的人出現(xiàn)并能夠為您提供更多幫助,但這就是我現(xiàn)在所擁有的:
1)我建議你刪除,public Cards() {} constructor因為你可能會不小心創(chuàng)建一個無效的卡(沒有設(shè)置任何屬性)。
2)如評論所述,我會做一個 while 循環(huán)來檢查是否按下了一個鍵,然后執(zhí)行相關(guān)操作。
while (gameIsRunning) {
if (key1 is pressed) {
...
}
...
if (key10 is pressed) {
...
System.out.println("You won! Game over.");
gameIsRunning = false;
}
}
我會在一個類或方法的某個地方定義一輪游戲的過程,或者為它編寫所有的輔助方法,然后將它們放入這個循環(huán)結(jié)構(gòu)中?;蛘?,您可以用文本提示用戶并等待文本輸入以進(jìn)行下一步操作。
希望我能幫上忙!
添加回答
舉報