提示:“写一个程序对着电脑玩猪游戏。在每个回合,当前玩家将掷出一对骰子并累积点数。目标是在你的对手之前达到100分或更多。(为了测试的目的,使用30点而不是100点)如果在任何回合中,玩家掷出一个1,则该回合积累的所有点数都将被没收,骰子的控制权将转移到另一个玩家身上。如果玩家在一个回合中滚动两个1,则该玩家失去迄今为止积累的所有点数,控制权转移到另一个玩家身上。玩家可以在每次掷骰后自愿交出骰子的控制权。因此,玩家必须决定再掷一次(当猪),冒着丢分的风险,或者放弃对骰子的控制,可能会让另一个玩家赢。计算机将掷硬币来选择第一个玩家“
我的问题:我得到的程序输出,要么电脑或播放器是第一个基于硬币翻转。然而,我将如何实际提示程序运行一个选择的人首先开始的方法,然后我将如何在每个回合结束时在计算机和播放器之间切换?顺便说一句,我知道这段代码是不完整的,但我希望我的问题是有意义的。
import java.util.*;
public class NavaiPigGame
{
public static final int POINT = 30;
public static final int FORFEIT_POINTS = 20;
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
Random rand = new Random();
play(rand,input);
}
// desription of game
public static void description()
{
System.out.println("***********************************************************************************");
System.out.println("Write a program to play the pig game against the computer. At each turn, the current player will");
System.out.println("roll a pair of dice and accumulates points. The goal is to reach to 100 or more points before your");
System.out.println("opponent does. (For the testing purposes use 30 instead of 100 points) If, on any turn, the player");
System.out.println("rolls a 1, all the points accumulated for that round are forfeited and the control of the dice");
System.out.println("moves to the other player. If the player rolls two 1s in one turn, the player loses all the points");
System.out.println("accumulated thus far are forfeited and the control moves to the other player. The player may");
System.out.println("voluntarily turn over the control of the dice after each roll. Therefore player must decide to roll");
System.out.println("again (be a pig) and risk losing points, or relinquish control of the dice, possibly allowing the");
System.out.println("other player to win. Computer is going to flip a coin to choose the first player");
System.out.println("***********************************************************************************");
System.out.println("lets start the fun");
}
//flips a coin and decides who starts the game
public static String flipCoin(Random rand)
{
int coin = rand.nextInt(2);
String comp = "";
switch (coin)
{
case 0: comp = "heads";
break;
case 1: comp = "tails";
break;
}
return comp;
}
public static int rollDice(Random rand)
{
int dice1 = rand.nextInt(6)+1;
int dice2 = rand.nextInt(6)+1;
System.out.println("Dice 1: " +dice1);
System.out.println("Dice 2: " +dice2);
return dice1+dice2;
}
// select a random name of the computer via arrays
public static String nameComputer(Random rand)
{
int name = rand.nextInt(10);
String compName = "";
switch (name)
{
case 0: compName = "Lisa";
break;
case 1: compName = "Kathy";
break;
case 2: compName = "Hali";
break;
case 3: compName = "Jack";
break;
case 4: compName = "Alex";
break;
case 5: compName = "Max";
break;
case 6: compName = "Jill";
break;
case 7: compName = "James";
break;
case 8: compName = "Martha";
break;
case 9: compName = "Lauren";
break;
}
return compName;
}
public static void play(Random rand, Scanner input)
{
int playerScores = 0;
int playerTotal = 0;
int computerScores = 0;
int computerTotal = 0;
boolean gameOver = false
boolean turnOver = false
description();
String compName = nameComputer(rand);
System.out.println("Hi my name is " + compName);
System.out.print("What is your name? ");
String name = input.nextLine();
System.out.println("Hi " + name + ", I am flipping the coin to determine who goes first");
System.out.print("press any key to start the game. ");
input.nextLine();
String flip = flipCoin(rand);
int turn;
if (flip.equals("heads"))
{
turn = 1;
System.out.println("You are going to start the game");
}
else
{
turn = 0;
System.out.println(compName + " is going to start the game");
}
}
}
创建:
playturn(int turn)
函数(1用于玩家,0用于计算机)第一次在if()else
状态中使用该函数,如下所示:
if (flip.equals("heads"))
{
turn = 1;
System.out.println("You are going to start the game");
playTurn(turn);
}
else
{
turn = 0;
System.out.println(compName + " is going to start the game");
playTurn(turn);
}
do {
if(turn == 1){
turn = 0;
playTurn(turn);
}else{
turn == 1;
playTurn(turn);
}
while ( !isWinner(1)|| !isWinner(0) );
这不是很好的设计,但它应该希望给你一个关于下一步该做什么的提示。
我是一个C++初学者,我需要创建一个骰子游戏模拟掷两个骰子。我对头文件的使用感到很困惑。但首先,为什么我需要返回骰子的票面号码?其次,int roll函数做什么?来重置价值观和面孔?如果是,默认值是多少?而最后一个函数骰子(int n),我是否使用这个函数来控制骰子值的最大总和?函数必须有一个具有以下函数的类头文件:
我试图为一个游戏的掷骰子程序,其中用户输入一个下注金额和2个六面骰子滚动,如果7或11是滚动,他们赢了。如果掷2,3或12就输了,如果掷其他任何一个数字,它就显示该数字为一个点。它会继续掷骰子,直到掷7或该点为止,如果掷7就输了,否则就赢了。出于某种原因,在掷骰子时,他会掷骰子,然后再掷一次,看看他们是否赢了或输了。我不知道如何解决这个问题,任何帮助都不会附带
本文向大家介绍Java编写掷骰子游戏,包括了Java编写掷骰子游戏的使用技巧和注意事项,需要的朋友参考一下 废话不多说了,直接奔主题。 **多线程&&观察者模式 题目要求:《掷骰子》窗体小游戏,在该游戏中,玩家初始拥有1000的金钱,每次输入押大还是押小,以及下注金额,随机3个骰子的点数,如果3个骰子的总点数小于等于9,则开小,否则开大,然后判断玩家是否押对,如果未押对则扣除下注金额,如果押对则奖
代码的目的是让两个玩家掷一对骰子。第一个掷出20分的玩家赢得比赛。我很难弄清楚如何正确地跟踪滚动的总和;它只给我当前回合的总和,然后当每个玩家滚动10次时游戏就结束了。 我如何正确地计算每个玩家游戏的总和,然后当其中一个玩家的总和等于20时停止循环?
程序在最后未能计算出正确的和。我做错了什么?如有任何帮助,不胜感激。谢了。
有人能在这里给我指个正确的方向吗?我的游戏工作完美,但我想添加一些实际的互动/目标。谢谢