我是大学一年级学生,正在学习Java。我们有一个项目要做作为家庭作业。我们需要创建一个骰子游戏。我将首先分享游戏规则,然后发布我的代码和我遇到的问题。 玩家和 CPU 掷骰子,骰子较大的一方开始游戏。除非你说停止,否则你就开始掷骰子。 (持有)当你持有它时,你将你的临时安全分数添加到你的安全中并失去你的回合。如果你 throw 1,你将失去你的回合并且一无所获。同样适用于CPU。第一个达到 100 分的人获胜。 现在用我的代码:当任何人达到 100 时,游戏不会停止。而且我也不认为我的 CPU A.I 不好,我可能也需要这方面的帮助。此外,当第一个骰子相等时(骰子决定谁将开始游戏),游戏就不会开始。
import java.util.Scanner;
public class Test {
public static void main (String args[]) {
int player_safe = 0;
int cpu_safe = 0;
int player_temp = 0;
int cpu_temp = 0;
boolean cpu_turn = false;
boolean player_turn = false;
Scanner name = new Scanner(System.in) ;
System.out.println ( "Enter your name : " ) ;
String player = name.nextLine();
System.out.println ( "Dice Game" ) ;
System.out.println ( "RULES" ) ;
System.out.println ( "" ) ;
System.out.println ( "If a 1 is rolled player's turn ends and earns no points." ) ;
System.out.println ( "If player chooses to hold, player will gain all the points in that turn and loose turn." ) ;
System.out.println ( "" ) ;
System.out.println ( "To determine who will start the game " +player+ " and CPU will roll a dice. The one who rolls higher will start the game." ) ;
System.out.println ( "" ) ;
System.out.println ( "If you are ready to roll a dice press 1." ) ;
// ROLL A DICE TO DETERMINE WHO WILL START THE GAME
int dice;
int dice_player = 0;
int dice_cpu = 0;
Scanner begin = new Scanner(System.in);
int player_roll = begin.nextInt();
if (player_roll == 1 ) {
for (int i = 0; i < 1; i++ ) {
dice_player = (int) (Math.random()*6+1) ;
dice_cpu = (int) (Math.random()*6+1) ;
System.out.println ( "You rolled " +dice_player ) ;
System.out.println ( "CPU rolled " +dice_cpu );
if ( dice_player > dice_cpu) {
System.out.println ("Player starts the game.");
cpu_turn = false;
player_turn = true; }
else if ( dice_player < dice_cpu) {
System.out.println ("CPU starts the game.");
player_turn = false;
cpu_turn=true; };
if ( dice_player == dice_cpu ) {
System.out.println ("It is a tie! Re-rolling...");
dice_player = (int) (Math.random()*6+1) ;
dice_cpu = (int) (Math.random()*6+1) ;
System.out.println ( "You rolled " +dice_player ) ;
System.out.println ( "CPU rolled " +dice_cpu ); }
}
//MAIN WHILE
while ( player_safe <= 100 || cpu_safe <=100 ) {
// PLAYER TURN WHILE
while ( player_turn == true && cpu_turn == false ) {
Scanner input = new Scanner(System.in);
System.out.println ("Roll or hold? (1/0) ") ;
int choice = input.nextInt();
if ( choice == 1 ) {
dice = (int) (Math.random()*6+1) ;
if (dice == 1 ) {
player_temp = 0;
player_turn = false;
cpu_turn= true;
System.out.println ("You rolled 1 you earned nothing.") ; }
else {
System.out.println ("You rolled " +dice ) ;
player_temp += dice;
System.out.println ("Your temporary safe: " +player_temp ); }
}
else if ( choice == 0 ) {
player_safe += player_temp ;
player_turn = false;
cpu_turn = true;
player_temp = 0;
System.out.println ("You have " +player_safe+ " points in your safe." ); } }
// CPU TURN WHILE
while ( player_turn == false && cpu_turn == true ) {
dice = (int) (Math.random()*6+1) ;
if (dice == 1 ) {
cpu_temp = 0;
cpu_turn = false;
player_turn = true;
System.out.println ("CPU rolled 1 and earned nothing."); }
else {
cpu_temp +=dice;
if ( cpu_safe < 20 && cpu_temp >= 15 ) {
cpu_safe += cpu_temp;
cpu_turn = false;
player_turn = true;
cpu_temp = 0; }
if ( cpu_safe <= 40 && cpu_temp >= 12 && cpu_safe - 10 <= player_safe ) {
cpu_safe += cpu_temp;
cpu_turn = false;
player_turn = true;
cpu_temp = 0; }
if ( cpu_safe <= 60 && cpu_temp >= 10 ) {
cpu_safe += cpu_temp;
cpu_turn = false;
player_turn = true;
cpu_temp = 0; }
if ( cpu_safe <= 70 && cpu_temp >= 12 ) {
cpu_safe += cpu_temp;
cpu_turn = false;
player_turn = true;
cpu_temp = 0; }
if ( cpu_safe <= 80 && cpu_temp >= 6 ) {
cpu_safe += cpu_temp;
cpu_turn = false;
player_turn = true;
cpu_temp = 0; }
if ( cpu_safe <= 100 && ( cpu_safe > player_safe ) && cpu_temp >= 5 ) {
cpu_safe += cpu_temp;
cpu_turn= false;
player_turn = true;
cpu_temp = 0; }
if ( cpu_safe <= 100 && ( cpu_safe < player_safe ) && cpu_temp >= 12 ) {
cpu_safe += cpu_temp;
cpu_turn= false;
player_turn = true;
cpu_temp = 0; }
System.out.println ("rolled "+dice) ;
System.out.println ("safe "+cpu_safe) ; }
}
//while safe
//while turn
} // main
} //class
}}
最佳答案
What's wrong with my code?
这是一个开放式的问题。它值得一个冗长的答案。
每当您创建任何 Java 应用程序时,您都应该记住 model / view / controller architecture ,缩写MVC。几乎每个 Java 应用程序都需要模型、 View 和 Controller 。
这是您的游戏的数据模型。第一个模型类是 PlayerModel。
public class PlayerModel {
private static final int maxScore = 100;
private static final String computerName = "Computer";
private int numberOfRolls;
private int score;
private int tempScore;
private String name;
public PlayerModel(String name) {
this.name = name;
this.numberOfRolls = 0;
this.score = 0;
this.tempScore = 0;
}
public int getNumberOfRolls() {
return numberOfRolls;
}
public int getScore() {
return score;
}
public int getTempScore() {
return tempScore;
}
public String getName() {
return name;
}
public static int getMaxScore() {
return maxScore;
}
public static String getComputerName() {
return computerName;
}
public boolean addTempScore(int count) {
if (count == 1) {
this.tempScore = 0;
this.numberOfRolls = 0;
return false;
} else {
this.tempScore += count;
this.numberOfRolls++;
return true;
}
}
public void addScore() {
this.score += this.tempScore;
this.tempScore = 0;
this.numberOfRolls = 0;
}
public boolean isWinner() {
return (getScore() >= maxScore);
}
public boolean isComputer() {
return (getName().equals(computerName));
}
}
每个玩家都会有一个此类的实例。
下一个模型类是 DiceGameModel。
import java.util.ArrayList;
import java.util.List;
public class DiceGameModel {
private static final int maxPlayers = 4;
private int playerNumber;
private List<PlayerModel> players;
public DiceGameModel() {
this.players = new ArrayList<PlayerModel>();
this.playerNumber = -1;
}
public int getPlayerNumber() {
return playerNumber;
}
public int getNumberOfPlayers() {
return players.size();
}
public static int getMaxPlayers() {
return maxPlayers;
}
public void addPlayer(PlayerModel player) {
this.players.add(player);
}
public void setPlayerNumber(int playerNumber) {
this.playerNumber = playerNumber;
}
public PlayerModel getNextPlayer() {
this.playerNumber++;
this.playerNumber %= getNumberOfPlayers();
return getCurrentPlayer(playerNumber);
}
public PlayerModel getCurrentPlayer(int playerNumber) {
return players.get(playerNumber);
}
}
该类将有一个实例。此类包含玩游戏所需的所有信息。
View 是您使用 System.out 和 Scanner 与用户的交互。您应该将此代码隔离在一个或多个类中。
Controller 将模型与 View 联系起来。 Controller 将负责启动游戏、确定哪个玩家获胜、确定要执行哪些 System.out 方法和 Scanner 方法,以及游戏机制所需的任何其他内容。
我已经为您提供了一个模型。我相信您可以自己想出 View 和 Controller 。
关于java - 当任何人达到 100 时,骰子游戏不会停止。我的代码有什么问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20021194/