gpt4 book ai didi

java - 我陷入困境,试图制作一款回合制游戏,但在结束回合和开始新回合时遇到困难

转载 作者:行者123 更新时间:2023-12-01 18:47:18 28 4
gpt4 key购买 nike

我可以结束第一个。我正在尝试在奇数轮玩家运行和偶数轮CPU运行时实现它。

public static void main(String[] args) {

Random random = new Random();

Scanner keyboard = new Scanner(System.in);

String mage = "mage";

System.out.println("mage, warrior, assassin, ranger?");

String input = keyboard.nextLine();

int playerAttackDice = random.nextInt(20) + 1;
int cpuAttackDice = random.nextInt(20) + 1;
int playerDefenseDice = random.nextInt(5) + 1;
int cpuDefenseDice = random.nextInt(5) + 1;

int turn = 1;


while(mage.equalsIgnoreCase(input)) {
Mage player = new Mage();
Warrior CPU = new Warrior();

System.out.println("You have selected: mage");

System.out.println("Your Attack Power is: " + player.getMageAttack());
System.out.println("Your Defense is: " + player.getMageDefense());
System.out.println("Your Health is: " + player.getMageHealth());

System.out.println("Your enemy is a: warrior");

System.out.println("Enemy Attack Power is: " + CPU.getMageAttack());
System.out.println("Enemy Defense is: " + CPU.getMageDefense());
System.out.println("Enemy Health is: " + CPU.getMageHealth());

System.out.println();

这就是转弯开始的地方。我试图对其进行设置,使其在 CPU 运行时播放器处于奇数状态。我是否以正确的方式处理这个问题?任何帮助将非常感激。

        while(player.mageHealth != 0) {
System.out.println("Your health is: " + player.mageHealth);

for(turn = 1; turn % 1 ==0; turn++) {
if(turn % 1==0) {
System.out.println("Your dice roll is:" + playerAttackDice);
int playerDamage = playerAttackDice + player.mageAttack;
int cpuDefense = cpuDefenseDice + CPU.mageDefense;
System.out.println(playerDamage);
System.out.println(cpuDefense);

System.out.println("CPU health is: " + (CPU.mageHealth + cpuDefense - playerDamage ));
return;
}

}
turn++;
for(turn = 2; turn % 2 ==0; turn++) {
while(turn % 2==0) {
System.out.println("CPU dice roll is:" + cpuAttackDice);
System.out.println("Your dice roll is:" + playerDefenseDice);
int cpuDamage = playerDefenseDice + player.mageAttack;
int playerDefense = cpuAttackDice + CPU.mageHealth;
System.out.println(cpuDamage);
System.out.println(playerDefense);
}
}



}

System.out.println("end loop");
break;

}

}

最佳答案

如果您希望玩家和 CPU 依次执行他们的 Action - 只需将他们的 Action 依次执行,没有内部循环和复杂的条件

while (!game.isOver()) {
player.makeMove();
cpu.makeMove();
}

或者有你的代码

while(player.mageHealth > 0) { // game over condition
//player move
System.out.println("Your health is: " + player.mageHealth);

System.out.println("Your dice roll is:" + playerAttackDice);
int playerDamage = playerAttackDice + player.mageAttack;
int cpuDefense = cpuDefenseDice + CPU.mageDefense;
System.out.println(playerDamage);
System.out.println(cpuDefense);

System.out.println("CPU health is: " + (CPU.mageHealth + cpuDefense - playerDamage ));

//cpu move
System.out.println("CPU dice roll is:" + cpuAttackDice);
System.out.println("Your dice roll is:" + playerDefenseDice);
int cpuDamage = playerDefenseDice + player.mageAttack;
int playerDefense = cpuAttackDice + CPU.mageHealth;
System.out.println(cpuDamage);
System.out.println(playerDefense);

player.mageHealth = player.mageHealth + playerDefence - cpuDamage;
}

关于java - 我陷入困境,试图制作一款回合制游戏,但在结束回合和开始新回合时遇到困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59803717/

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