プログラムはユーザーに何人のプレーヤーを持ちたいかを尋ね、プログラムの実行を促すと、プレーヤー 1 は 2 つの 6 面サイコロを 3 回 (3 回) 受け取り、次のプレーヤーについても同様です。プレイヤーは、どちらのロールを保持するか、両方を保持するかを選択できます。
ただし、問題は発生します。ロールはすべてのプレーヤーで常に同じです。これは、Math.random が私のプレーヤー クラス プログラムに影響を与えないようです。ここで別の問題が発生します。else { JOptionPane.showMessageDialog(Test,"Tie "+ data[0] + " And " + data[1]); 2 人のプレイヤーが選択された場合、プログラムは常にそのクラスの else ステートメントに移動します。2 人から 4 人のプレイヤーの場合、常に指定された else ステートメントに進む結果となるように、プレイヤー カウントごとに else ステートメントに進みます。
2 つのダイの周りで PairOFDice クラスの for ループを実行しようとしましたが、役に立たず、ダイスのロールは変更されませんでした。また、プログラムが 1 つのループを通過するたびに Die の値をリセットしようとしましたが、単に値が 0 のままになっていました。任意の入力をいただければ幸いです。
import javax.swing.*;//MAIN LOGIC PROGRAM
public class Logic {
public static void main (String [] args) {
PairOfDice p1 = new PairOfDice("Player 1");
PairOfDice p2 = new PairOfDice("Player 2");
Player PClass = new Player();
JFrame IntroPane = new JFrame ();
JFrame Test = new JFrame ();
int Crolls = 0;
int data[] = new int[4] ;
JOptionPane.showMessageDialog(Test,"Hello and welcome to the program! In the Following program, you will be playing a game of dice against another player\nEach of you will roll two six sided dice's three times, choosing to hold the first second or both die's\nThe highest total wins! Good luck!");
String c = JOptionPane.showInputDialog(Test,"To begin, how many players are playing?\n2 Players enter '2'" + "\n3 Players enter '3'" + "\n4 Players enter '4'");
int x = Integer.parseInt(c);
for (int i = 0; i < x; i++) {
for(int s = 0; s < 3; s++) {
p1.play();
p2.play();
JOptionPane.showMessageDialog(IntroPane,"Player " + (i+1));
JOptionPane.showMessageDialog(IntroPane,"Dice 1 rolled : " + p1.getDice1() + "\nDice 2 rolled : " + p1.getDice2());
Object[] options = {"Hold Dice 1",
"Hold Dice 2",
"Hold Both"};
int n = JOptionPane.showOptionDialog(Test,
"Which Roll would you like to keep?\nKeep Dice 1 or Dice 2\nOr keep both!\n\nYour Total so far is :" +data[i]
+ "",
"",
JOptionPane.YES_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[2]);
if(n == JOptionPane.YES_OPTION)
{
PClass.HoldFirstDie(p1.getDice1());
JOptionPane.showMessageDialog(Test,"You choose to hold :" +p1.getDice1() );
data[i] += PClass.getFirstDie();
Crolls++;
}
else if(n == JOptionPane.NO_OPTION) {
PClass.HoldSecondDie(p1.getDice2());
JOptionPane.showMessageDialog(Test,"You choose to hold :" +p1.getDice2() );
data[i] += PClass.getSecondDie();
Crolls++;
}
else if(n== JOptionPane.CANCEL_OPTION) {
PClass.HoldFirstDie(p1.getDice1());
PClass.HoldSecondDie(p1.getDice2());
JOptionPane.showMessageDialog(Test,"You choose to hold :" +p1.getDice1() + " and :" + p1.getDice2() );
data[i] += ( PClass.getFirstDie() + PClass.getSecondDie() ) ;
Crolls++;
}
}
}
if( x == 2) {
if(Crolls == 3 && data[0] > data[1]) {
JOptionPane.showMessageDialog(Test,"Player 1 wins");
}
else if (Crolls == 3 && data[1] > data[0]) {
JOptionPane.showMessageDialog(Test,"Player 2 wins");
}
else {
JOptionPane.showMessageDialog(Test,"Tie "+ data[0] + " And " + data[1]);
}
}
if(x == 3) {
if(Crolls == 3 && data[2] > data[0] && data[2] > data[1]) {
JOptionPane.showMessageDialog(Test,"Player 3 wins");
}
else if(Crolls == 3 && data[0] > data[1] && data[0] > data[2]) {
JOptionPane.showMessageDialog(Test,"Player 1 wins");
}
else if(Crolls == 3 && data[1] > data[0] && data[1] > data[2]) {
JOptionPane.showMessageDialog(Test,"Player 2 wins");
}
else {
JOptionPane.showMessageDialog(Test,"Tie");
}
}
if(x ==4) {
if(Crolls == 3 && data[0] > data[1] && data[0] > data[2] && data[0] > data[3]) {
JOptionPane.showMessageDialog(Test,"Player 1 wins");
}
else if(Crolls == 3 && data[1] > data[0] && data[1] > data[2] && data[1] > data[3]) {
JOptionPane.showMessageDialog(Test,"Player 2 wins");
}
else if(Crolls == 3 && data[2] > data[0] && data[2] > data[1] && data[2] > data[3]) {
JOptionPane.showMessageDialog(Test,"Player 3 wins");
}
else if(Crolls == 3 && data[3] > data[0] && data[3] > data[1] && data[3] > data[2]) {
JOptionPane.showMessageDialog(Test,"Player 4 wins");
}
else {
JOptionPane.showMessageDialog(Test,"Tie");
}
}
}}
PairOFDice クラス
public class PairOfDice {
private int Dice1 = 0, Dice2 = 0;
public String name;
PairOfDice(String name){
this.name = name;
}
public void PairOfDice2() {
play();
}
public void play () {
//for(int i = 0; i < 3; i++) {
Dice1 = (int)(Math.random () * 6) + 1;
Dice2 = (int)(Math.random () * 6) + 1;
}
//}
public int getDice1() {
return Dice1;
}
public int getDice2() {
return Dice2;
}
public int getTotal () {
return Dice1 + Dice2;
}
}
プレイヤークラス
public class Player {
private int holdDice1 = 0;
private int holdDice2 = 0;
public void HoldFirstDie (int FirstDie) {
holdDice1 = FirstDie;
}
public void HoldSecondDie(int SecondDie) {
holdDice2 = SecondDie;
}
public int getFirstDie() {
return holdDice1;
}
public int getSecondDie() {
return holdDice2;
}
}