我试图正确地结束这个游戏,但是做而循环或我正在做的事情导致游戏继续进行。如果有人进一步建议我在得分达到99分后结束这场比赛,我将不胜感激。我已经粘贴了两个类源。我也是学生,所以请原谅我。
类
package programming2;
import java.util.Random;
import java.util.Scanner;
public class Matador {
public static void main(String[] args) {
/*The game is played between the player and the computer.
The player's score and the computer's score each begin at 0.
The object of the game is to be the first reach 99 points.
The player and the computer take turns. On each turn: */
Scanner scnr = new Scanner(System.in);
Die matador = new Die();
int playerScore = 0;
int computerScore = 0;
int playerPick = 0;
int cpu;
int turn;
Random randGen = new Random();
cpu = randGen.nextInt(6) + 2;
int cpuPick = 0;
int quit = 0;
System.out.println("Welcome to el Matador!");
System.out.println("The winner of this game is the first to score 99 points");
do {System.out.println("Player 1 please pick your number"); // start of master loop
playerPick = scnr.nextInt();
System.out.println("Player 1 you selected " + playerPick);
System.out.println("Player 2 please pick your number");
cpuPick = cpu;
System.out.println("Player 2 selects " + cpuPick);
//
do { // start of game loop - master do loop player 1
if (computerScore > 98) {
quit = 1;
break;
}
System.out.println("Player 1 rolls dice");
matador.rollDieNow();
System.out.println("Values for 1st dice is: " + matador.getValueDie1() + " and " + matador.getValueDie2());
System.out.println("Combined values are " + matador.getValue());
turn = 0; // start of loop marker for player 1
if (matador.getValueDie1() == 1 && matador.getValueDie2() == 1) {
playerScore = playerScore + -25;
break;
}
if (matador.getValueDie1() == 1 || matador.getValueDie2() == 1) {
playerScore = playerScore + 0;
System.out.println("Changing Turns!");
break;
}
if (matador.getValueDie1() != 1 || matador.getValueDie2() != 1) { // sum of dicevalue added to score
playerScore = playerScore + matador.getValue();
}
if (playerPick == matador.getValueDie1() || playerPick == matador.getValueDie2()) { // if picked number appears on at least 1
turn = turn + 1;
}
if (playerPick == matador.getValueDie1() && playerPick == matador.getValueDie2()) { // if picked number appears on both dice
System.out.println("You are the winner player 1 you matched your numbers!!");
System.out.print("Player 1 final score is: " + playerScore + " |-| player 2 final score is " + computerScore);
quit = 1;
break;
}
System.out.println("Player 1 Score: " + playerScore);
}
while (turn < turn + 1 || quit != 1);
// turn changes and also end of loop marker
System.out.println("Score update! Player 1 score is: " + playerScore + " |-| player 2 score is " + computerScore);
turn = 0; // turn counter resets
do { // start of computer player's turn
if (playerScore > 98) {
quit = 1;
break;
}
System.out.println("Player 2 rolls dice");
matador.rollDieNow();
System.out.println("Values for 1st dice is: " + matador.getValueDie1() + " and " + matador.getValueDie2());
System.out.println("Combined values are " + matador.getValue());
if (matador.getValueDie1() == 1 && matador.getValueDie2() == 1) { // loop marker for cpu starts
computerScore = computerScore + -25;
break;
}
if (matador.getValueDie1() == 1 && matador.getValueDie2() == 1) { // loses turns if 1 appears on both sides
computerScore = computerScore + 0;
System.out.println("Changing Turns!");
break;
}
if (matador.getValueDie1() != 1 || matador.getValueDie2() != 1) { // computer scores value of dice
computerScore = computerScore + matador.getValue();
if (computerScore > 98) {
break;
}
}
if (cpuPick == matador.getValueDie1() || cpuPick == matador.getValueDie2()) {
turn = turn + 1;
}
if (cpuPick == matador.getValueDie1() && cpuPick == matador.getValueDie2()) {
System.out.println("You are the winner player 2 you matched your numbers!!");
System.out.println("Player 1 final score is: " + playerScore + " |-| player 2 final score is " + computerScore);
quit = 1;
break;
}
System.out.println("Player 2 Score: " + computerScore);
}
while (playerScore < 98 || computerScore <98 || quit != 1);
}
while (playerScore < 98 || computerScore < 98 || quit != 1); // end of master loop
System.out.println("Player 1 final score is: " + playerScore + " |-| player 2 final score is " + computerScore);
}
}
如果需要,其他类,但它与分数无关,因为它主要用于确定一些值:
package programming2;
import java.util.Random;
import java.util.Scanner;
public class Die {
private int die1; //instance variable
private int die2;
private int rolledDice;
public Die() { //constructor assigns random value
die1 = (int) (Math.random()*6)+1;
die2 = (int) (Math.random()*6)+1;
}
public void rollDieNow () { //rolls dice
Random randGen = new Random();
die1 = randGen.nextInt(6) + 1;
die2 = randGen.nextInt(6) + 1;
}
public int getValue() { // gets dice value
rolledDice = die1 + die2;
return rolledDice;
}
public int getValueDie1() { // gets die1 value
return die1;
}
public int getValueDie2() { // gets die2 value
return die2;
}
}
骰子课
package finalproject2;
import java.util.Random;
public class Die {
Random randGen = new Random();
private int value; // instance variable -> stores the roll
// each Die object gets its own copy of the instance variable
public Die() { // constructor rolls dice (calls rollDieNow() method)
rollDieNow();
}
public void rollDieNow() { // rolls dice
value = randGen.nextInt(6) + 1;
}
public int getValue() { // gets dice value
return value;
}
}
斗牛士班
package finalproject2;
import java.util.Random;
import java.util.Scanner;
public class Matador {
public static void main(String[] args) {
/*The game is played between the player and the computer.
The player's score and the computer's score each begin at 0.
The object of the game is to be the first reach 99 points.
The player and the computer take turns. On each turn: */
Scanner scnr = new Scanner(System.in);
Die d1 = new Die();
Die d2 = new Die();
int playerScore = 0;
int computerScore = 0;
int playerPick = 0;
int cpu;
int turn;
int sum ;
Random randGen = new Random();
cpu = randGen.nextInt(6) + 2;
int cpuPick = 0;
int quit = 0;
System.out.println("Welcome to el Matador!");
System.out.println("The winner of this game is the first to score 99 points");
// use System.exit(0) to stop program execution
do {
if (playerScore >= 99 || computerScore >= 99) {
System.out.println("Game Over!");
System.exit(0);
}
System.out.println("Player 1 please pick your number"); // start of master loop
playerPick = scnr.nextInt();
System.out.println("Player 1 you selected " + playerPick);
System.out.println("Player 2 please pick your number");
cpuPick = cpu;
System.out.println("Player 2 selects " + cpuPick);
//
do { // start of game loop - master do loop player 1
System.out.println("Player 1 rolls dice");
d1.rollDieNow();
d2.rollDieNow();
sum = d1.getValue() + d2.getValue() ;
System.out.println("Values for both dice are: " + d1.getValue() + " and " + d2.getValue());
System.out.println("Combined values are " + sum);
turn = 0; // start of loop marker for player 1
if (d1.getValue() == 1 && d2.getValue() == 1) {
playerScore = playerScore + -25;
System.out.println("Ouch! Lost 25 points!");
break; // lost of turn, loop ends
}
if (d1.getValue() == 1 || d2.getValue() == 1) {
playerScore = playerScore + 0;
System.out.println("Changing Turns!");
break; // lost of turn loop ends
}
if (d1.getValue() != 1 || d2.getValue() != 1) { // sum of dice value added to score
playerScore = playerScore + (d1.getValue() + d2.getValue());
System.out.println("Gained points! " + (d1.getValue() + d2.getValue()));
}
if (playerPick == d1.getValue() || playerPick == d2.getValue()) { // if picked number appears on at least 1
turn = turn + 1;
}
if (playerPick == d1.getValue() && playerPick == d2.getValue()) { // if picked number appears on both dice
System.out.println("You are the winner player 1 you matched your numbers!!");
System.out.print("Player 1 final score is: " + playerScore + " |-| player 2 final score is " + computerScore);
System.exit(0); // game ends completely
}
System.out.println("Player 1 Score: " + playerScore);
}
while (turn < 1);
// turn changes and also end of loop marker
System.out.println("Score update! Player 1 score is: " + playerScore + " |-| player 2 score is " + computerScore);
turn = 0; // turn counter resets
do { // start of computer player's turn
if (playerScore >= 99 || computerScore >= 99) {
System.out.println("Game Over!");
System.exit(0);
}
System.out.println("Player 2 rolls dice");
d1.rollDieNow();
d2.rollDieNow();
System.out.println("Values for 1st dice is: " + d1.getValue() + " and " + d2.getValue());
System.out.println("Combined values are " + (d1.getValue() + d2.getValue()));
if (d1.getValue() == 1 && d2.getValue() == 1) { // loses points and turn
computerScore = computerScore + -25;
System.out.println("Ouch! Lost 25 points!");
break;
}
if (d1.getValue() == 1 || d2.getValue() == 1) { // loses turns if 1 appears on both sides
computerScore = computerScore + 0;
System.out.println("Changing Turns!");
break;
}
if (d1.getValue() != 1 || d2.getValue() != 1) { // computer scores value of dice
computerScore = computerScore + (d1.getValue() + d2.getValue());
System.out.println("Gained points! " + (d1.getValue() + d2.getValue()));
}
if (cpuPick == d1.getValue() || cpuPick == d2.getValue()) {
turn = turn + 1;
}
if (cpuPick == d1.getValue() && cpuPick == d2.getValue()) {
System.out.println("You are the winner player 2 you matched your numbers!!");
System.out.println("Player 1 final score is: " + playerScore + " |-| player 2 final score is " + computerScore);
System.exit(0);
}
System.out.println("Player 2 Score: " + computerScore);
}
while (turn < 1); // for some reason on my previous revision this was set to
// a different condition than player 1, now the game ends appropiate
}
while (playerScore <= 99 || computerScore <= 99); // end of master loop
System.out.println("Player 1 final score is: " + playerScore + " |-| player 2 final score is " + computerScore);
}
}
有人能在这里给我指个正确的方向吗?我的游戏工作完美,但我想添加一些实际的互动/目标。谢谢
我是一个C++初学者,我需要创建一个骰子游戏模拟掷两个骰子。我对头文件的使用感到很困惑。但首先,为什么我需要返回骰子的票面号码?其次,int roll函数做什么?来重置价值观和面孔?如果是,默认值是多少?而最后一个函数骰子(int n),我是否使用这个函数来控制骰子值的最大总和?函数必须有一个具有以下函数的类头文件:
我试图创建一个程序,掷2个骰子,给出和的数量,然后将它们相加。我得到了骰子的随机数,但是当它们相加(总和)时,总数是不正确的。 我尝试过更改,,我已经将总和更改为,但总和总是出错。如果有人可以查看代码,看看我是否将其他所有内容放在正确的位置。提前感谢。 这应该是掷骰子1,给我骰子1的面值,然后给我骰子2的面值,然后给我两个骰子的总数。
1-4:玩家从1-6随机向前移动 5:玩家从4-11向前移动一个随机量(随机8+4) 6:玩家移动到另一个玩家所在的位置(见下文) 我是新的代码编写(这是我的第四个程序编写),并没有广泛的知识知道什么是错误的。代码不需要熟练地完成,我只想让它正常工作。我们非常感谢您的帮助。 }
null
我试图为一个游戏的掷骰子程序,其中用户输入一个下注金额和2个六面骰子滚动,如果7或11是滚动,他们赢了。如果掷2,3或12就输了,如果掷其他任何一个数字,它就显示该数字为一个点。它会继续掷骰子,直到掷7或该点为止,如果掷7就输了,否则就赢了。出于某种原因,在掷骰子时,他会掷骰子,然后再掷一次,看看他们是否赢了或输了。我不知道如何解决这个问题,任何帮助都不会附带