的目標是要建立兩個玩家通過擲骰子擲控制檯遊戲文本菜單。我有3個課程,遊戲,玩家和骰子。玩家類只是有串球員姓名字段和INT球員得分場骰子類有RNG代碼,但在比賽的大部分是在遊戲類。添加到對象場
開始遊戲時,我命名兩名玩家(從用戶處獲得的文字)並創建兩個玩家對象。我可以稱之爲「擲骰子」方法,它可以從一輪遊戲中返回得分,但是如何將該得分添加到已經在玩家對象中定義的區域?
有應該是每位玩家一個對象,但我似乎不能夠指每個單獨的對象。用不同的方法創建這些對象,但與嘗試增加玩家分數的方法相同的類中創建對象有問題嗎?
import java.util.Scanner;
public class Game
{
// instance variables - replace the example below with your own
private int x;
/**
* Constructor for objects of class Game
*/
public Game()
{
// initialise instance variables
x = 0;
}
public void menu()
{
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to My Dice-and-Roll Game!");
System.out.println("=================================");
System.out.println("(1) Start a New Game");
System.out.println("(2) Play One Round");
System.out.println("(3) Who is leading now?");
System.out.println("(4) Display Game Help");
System.out.println("(5) Exit Game");
System.out.println("(6) Choose an option:");
int selection = scanner.nextInt();
switch (selection) {
case 1:
// Perform "original number" case.
nameplayers();
playround();
break;
case 2:
// Perform "encrypt number" case.
System.out.println("(2) ");
break;
case 3:
// Perform "decrypt number" case.
System.out.println("(3) ");
break;
case 4:
// Perform "quit" case.
System.out.println("(4) ");
break;
default:
// The user input an unexpected choice.
}
}
public void nameplayers()
{
String namep1;
String namep2;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the name of player 1: ");
namep1 = scanner.next();
Player P1 = new Player();
P1.setName(namep1);
System.out.print("Enter the name of player 2: ");
namep2 = scanner.next();
Player P2 = new Player();
P2.setName(namep2);
}
public void rolldice()
{
int dice1;
int dice2;
int rollTotal;
// put your code here
dice1 = (int)(Math.random()*6) + 1;
dice2 = (int)(Math.random()*6) + 1;
if(dice1 == dice2){
rollTotal = ((dice1 + dice2) *2);
}
else {
rollTotal = (dice1 + dice2);
}
System.out.println(" has rolled a " + dice1 + " and " + dice2 + " which totals " + rollTotal + " points");
P1.addScore(rollTotal);
}
public void playround()
{
String namep1;
String namep2;
Scanner scanner = new Scanner(System.in);
System.out.println("The dice are thrown!");
try{
Thread.sleep(2000);//2000ms = 2s
}catch(InterruptedException ex){
}
System.out.println("..");
try{
Thread.sleep(2000);//2000ms = 2s
}catch(InterruptedException ex){
}
System.out.println(" ..");
try{
Thread.sleep(2000);//2000ms = 2s
}catch(InterruptedException ex){
}
System.out.println(" :");
try{
Thread.sleep(2000);//2000ms = 2s
}catch(InterruptedException ex){
}
System.out.println(" ..");
try{
Thread.sleep(2000);//2000ms = 2s
}catch(InterruptedException ex){
}
rolldice();
}
}
import java.util.Scanner;
public class Player
{
// the name of the player
private String playerName;
// the current score of the player
private int playerScore;
/**
* Constructor for objects of class Player
*/
public Player()
{
String playerName = "";
int playerScore = 0;
}
public void setName(String newName)
{
// put your code here
playerName = newName;
System.out.println("Player name set as " +
newName);
}
public void addScore(int addScore)
{
// put your code here
playerScore = playerScore + addScore;
}
}