【发布时间】:2013-11-17 07:37:57
【问题描述】:
我是大学新生,正在学习 Java。我们有一个项目要做作为家庭作业。我们需要创建一个骰子游戏。我将首先分享游戏规则,然后发布我的代码和我遇到的问题。 玩家和 CPU 掷骰子,骰子较大的人开始游戏。你开始掷骰子,除非你说停止。 (HOLD)当你拿着它时,你将你的临时安全分数添加到你的保险箱中,然后轮到你了。如果你掷出 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
}}
【问题讨论】:
-
是时候学习如何使用调试器了 =)
-
也许也有一些课程..
-
您需要计算机 AI 方面的帮助是什么意思?这个游戏有策略,还是只是一个简单的骰子游戏?
-
我会把这篇文章留给任何感兴趣的人 - java-articles.info/articles/?p=460