Java实现有规则的骰子游戏
/**
*
* @author Afiq's
* @author Niu Zhaohang
*/
public class DiceGame {
/* This program simulates rolling a pair of dice.
The number that comes up on each dice is output,
followed by the total of the two player's dices.
*/
public static void main(String[] args) {
int dice11, dice12; // The number on the first player's dice.
int dice21, dice22; // The number on the second player's dice.
int sum1=0 ; // The total roll (sum of the two dice).
int sum2=0 ; // The total roll (sum of the two dice).
do{
dice11 = (int)(Math.random()*6) + 1;
dice12 = (int)(Math.random()*6) + 1;
System.out.println("The player1's first dice comes up " + dice11);
System.out.println("The player1's second dice comes up " + dice12);
sum1 += dice11+dice12;
if(dice11 == 1 && dice12 == 1) {
sum1 += (sum1 * 2) - 2;
System.out.println("Player1 total point is " +sum1);
}else if (dice11%2 !=0 && dice12%2 !=0 && dice11 != 1 && dice12 !=1) {
sum1 += -5;
System.out.println("Player1 total point is " +sum1);
}else if (dice11 == 6 && dice12 ==6) {
sum1 = 0;
System.out.println("Double 6, need to roll again.");
}else{
System.out.println("Player1 total point is " +sum1);
}
}while(dice11 == 6 && dice12 ==6);
do{ dice21 = (int)(Math.random()*6) + 1;
dice22 = (int)(Math.random()*6) + 1;
System.out.println("The player2's first dice comes up " + dice21);
System.out.println("The player2's second dice comes up " + dice22);
sum2 += dice21+dice22;
if(dice21 == 1 && dice22 == 1) {
sum2 += (sum2 * 2) - 2;
System.out.println("Player2 total point is " +sum2);
}else if (dice21%2 !=0 && dice22%2 !=0 && dice21 != 1 && dice22 !=1) {
sum2 += -5;
System.out.println("Player2 total point is " +sum2);
}else if (dice21 == 6 && dice22 ==6) {
sum2 = 0;
System.out.println("Double 6, need to roll again.");
}else{
System.out.println("Player2 total point is " +sum2);
}
}while(dice21 == 6 && dice22 ==6);
if(sum1 > sum2) {
System.out.println("Player1 won the dice game with " + sum1);
}else if(sum2 > sum1) {
System.out.println("Player2 won the dice game with " + sum2);
}else{
System.out.println("It's DRAW!!!!");
}
} // end main()
} // end class
*
* @author Afiq's
* @author Niu Zhaohang
*/
public class DiceGame {
/* This program simulates rolling a pair of dice.
The number that comes up on each dice is output,
followed by the total of the two player's dices.
*/
public static void main(String[] args) {
int dice11, dice12; // The number on the first player's dice.
int dice21, dice22; // The number on the second player's dice.
int sum1=0 ; // The total roll (sum of the two dice).
int sum2=0 ; // The total roll (sum of the two dice).
do{
dice11 = (int)(Math.random()*6) + 1;
dice12 = (int)(Math.random()*6) + 1;
System.out.println("The player1's first dice comes up " + dice11);
System.out.println("The player1's second dice comes up " + dice12);
sum1 += dice11+dice12;
if(dice11 == 1 && dice12 == 1) {
sum1 += (sum1 * 2) - 2;
System.out.println("Player1 total point is " +sum1);
}else if (dice11%2 !=0 && dice12%2 !=0 && dice11 != 1 && dice12 !=1) {
sum1 += -5;
System.out.println("Player1 total point is " +sum1);
}else if (dice11 == 6 && dice12 ==6) {
sum1 = 0;
System.out.println("Double 6, need to roll again.");
}else{
System.out.println("Player1 total point is " +sum1);
}
}while(dice11 == 6 && dice12 ==6);
do{ dice21 = (int)(Math.random()*6) + 1;
dice22 = (int)(Math.random()*6) + 1;
System.out.println("The player2's first dice comes up " + dice21);
System.out.println("The player2's second dice comes up " + dice22);
sum2 += dice21+dice22;
if(dice21 == 1 && dice22 == 1) {
sum2 += (sum2 * 2) - 2;
System.out.println("Player2 total point is " +sum2);
}else if (dice21%2 !=0 && dice22%2 !=0 && dice21 != 1 && dice22 !=1) {
sum2 += -5;
System.out.println("Player2 total point is " +sum2);
}else if (dice21 == 6 && dice22 ==6) {
sum2 = 0;
System.out.println("Double 6, need to roll again.");
}else{
System.out.println("Player2 total point is " +sum2);
}
}while(dice21 == 6 && dice22 ==6);
if(sum1 > sum2) {
System.out.println("Player1 won the dice game with " + sum1);
}else if(sum2 > sum1) {
System.out.println("Player2 won the dice game with " + sum2);
}else{
System.out.println("It's DRAW!!!!");
}
} // end main()
} // end class