gpt4 book ai didi

java - 重做二十一点游戏

转载 作者:行者123 更新时间:2023-12-02 05:30:49 26 4
gpt4 key购买 nike

我正在尝试创建一个二十一点游戏,玩家从两张牌开始,然后询问他/她是否想要另一张牌(用户输入:是或否),如果是,则另一张牌是添加到总数中。如果不是,游戏就终止。

这是我试图获取的示例输出:

这就是我到目前为止所拥有的(就放置而言可能是错误的):

import java.util.Scanner;

public class BlackJackGame {
public static void main(String[] args) {

int randomnum1 = (int) (1 + Math.random() * 10);
int randomnum2 = (int) (1 + Math.random() * 10);
int randomnum3 = (int) (1 + Math.random() * 10);
int total;
char anotherCard = 'y';

Scanner input = new Scanner(System.in);

System.out.println("First cards: " + randomnum1 + ", " + randomnum2);

total = randomnum1 + randomnum2;

System.out.println("Total: " + total);

while (anotherCard != 'n')
{
System.out.print("Card: " + randomnum3);

System.out.print("Do you want another card? (y/n): ");
anotherCard = input.next().charAt(0);
}
}
}

我们将非常感谢提示和修改源代码。

最佳答案

就纸牌游戏而言,一副牌中有 52 张牌,我假设只有一副牌。

如果你希望这是一场公平的游戏,那么你必须牢记这一点。

但如果您只是想让输出看起来正确,则只需避免获得超过 4 个 A、2、3 和 4。

实现此目的的一种方法是创建一个大小为 52 的 int 数组,其中每张卡有 4 个。我想 Ace 是 1,10,J,Q,K 是 10,所以会有 16 个 10。

获取 0 到 51 之间的随机数以获取要使用的数组的索引。使用该索引后,将该数组的值设置为 -1,并在使用该索引之前始终检查 -1,如果为 -1,则获取另一个随机值。

int [] deck = size 52 array with 4 of each card.
int random = get random number between 0 and 51.

while(deck[random] == -1){
random = get random number between 0 and 51.
}
int card1 = deck[random]
deck[random] = -1;

类似的事情......我只是很快就做到了,希望你能明白。

关于java - 重做二十一点游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25554213/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com