我是大学新生,学习Java。我们有一个项目要做作业。我们需要创建一个骰子游戏。我将首先分享游戏规则,然后发布我的代码和问题。 播放器& CPU投掷骰子与较大的骰子开始游戏。除非你说停止,否则你开始掷骰子。 (握住)握住它时,将临时安全分数添加到保险箱并转弯。如果你扔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
}}
答案 0 :(得分:2)
要考虑的几件事情:
1)当玩家和CPU连续两次(1/36机会)时会发生什么?是否有更好的方法来重组初始滚动?
2)如果玩家的得分为101且CPU的得分为98,游戏会结束吗?查看while子句中的退出条件。
3)有没有办法使用函数和OO风格的设计来使这段代码更清晰,更具可读性?
答案 1 :(得分:2)
我的代码出了什么问题?
这样一个开放式的问题。它应该得到一个冗长的答案。
每当您创建任何Java应用程序时,都应该记住model / view / controller architecture,缩写为MVC。几乎每个Java应用程序都需要模型,视图和控制器。
这是您游戏的数据模型。第一个模型类是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);
}
}
这个类会有一个实例。该课程包含玩游戏所需的所有信息。
视图是您使用System.out和Scanner与用户的交互。您应该在一个或多个类中隔离此代码。
控制器将模型与视图联系起来。控制器将负责启动游戏,确定哪个玩家获胜,确定执行哪些System.out方法和Scanner方法,以及游戏机制所需的其他任何内容。
我为你提供了一个模型。我相信你可以自己提出观点和控制器。
答案 2 :(得分:0)
你的while循环需要修复; while ( player_safe <= 100 || cpu_safe <=100 )
表示其中一个分数低于100.您必须将其更改为&&
,以表示两个分数均低于100。
另外,我同意其他海报,您可能希望使用抽象来将代码分解为定义。我不确定你所涵盖的是什么,但使用函数可以使你的代码更清晰,更容易阅读。
此外,听起来你需要一个循环。如果骰子相等,你如何确定谁先走?
答案 3 :(得分:0)
一个钓鱼的人不会先扔他的杖,他必须先找到水。
从算法开始,而不是代码。