public class Player {
private String playerName;
private int number;
public Player(String name){
this.playerName = name;
}
public int guessNumber() {
return Integer.parseInt(JOptionPane.showInputDialog(playerName
+"'s Turn\nGuess
+"'s Turn\nGuess the Number"));
JOptionPane.showMessageDialog(null, playerName+ "'s GUESS is " + number);
}
public String getPlayerName() {
return playerName;
}
}
public class GuessGame {
private int numberToGuess;
private Player p1;
private Player p2;
private Player p3;
//Would probably made amount of players dynamic as well.
public GuessGame(String player1, String player2, String player3){
numberToGuess = (int) (Math.random()*10);
p1 = new Player(player1);
p2 = new Player(player2);
p3 = new Player(player3);
}
//Would probably split this up in submethods too.
//The flagging is quite ugly too but I don't got time to edit it.
public void startGame(){
int flagWinner = 0;
while(0==flagWinner){
if (p1.guessNumber()==numberToGuess){
flagWinner = 1;
break;
}
JOptionPane.showMessageDialog(null, p1.getPlayerName()
+ "'s Guess is Wrong!");
if (p2.guessNumber()==numberToGuess){
flagWinner = 2;
break;
}
JOptionPane.showMessageDialog(null,p2.getPlayerName()
+ "'s Guess is Wrong!");
if (p3.guessNumber()==numberToGuess){
flagWinner = 3;
break;
}
JOptionPane.showMessageDialog(null,p3.getPlayerName()
+ "'s Guess is Wrong!");
}
if (1 == flagWinner){
JOptionPane.showMessageDialog(null,p1.getPlayerName()+ " Wins!");
} else if (2 == flagWinner){
JOptionPane.showMessageDialog(null,p2.getPlayerName()+ " Wins!");
} else JOptionPane.showMessageDialog(null,p3.getPlayerName()+ " Wins!");
}
public static void main(String[] e){
GuessGame gg = new GuessGame(
JOptionPane.showInputDialog("Enter Player 1 Name: "),
JOptionPane.showInputDialog("Enter Player 2 Name: "),
JOptionPane.showInputDialog("Enter Player 3 Name: ");
);
gg.startGame();
}
}
public class Player {
private String playerName;
private int number;
public Player(String name){
this.playerName = name;
}
public int guessNumber() {
return Integer.parseInt(JOptionPane.showInputDialog(playerName +"'s Turn\nGuess
the Number"));
JOptionPane.showMessageDialog(null, playerName+ "'s GUESS is " + number);
}
public String getPlayerName() {
return playerName;
}
}
public class GuessGame {
private int numberToGuess;
private Player p1;
private Player p2;
private Player p3;
//Would probably made amount of players dynamic as well.
public GuessGame(String player1, String player2, String player3){
numberToGuess = (int) (Math.random()*10);
p1 = new Player(player1);
p2 = new Player(player2);
p3 = new Player(player3);
}
//Would probably split this up in submethods too.
//The flagging is quite ugly too but I don't got time to edit it.
public void startGame(){
int flagWinner = 0;
while(0==flagWinner){
if (p1.guessNumber()==numberToGuess){
flagWinner = 1;
break;
}
JOptionPane.showMessageDialog(null, p1.getPlayerName() + "'s Guess is Wrong!");
if (p2.guessNumber()==numberToGuess){
flagWinner = 2;
break;
}
JOptionPane.showMessageDialog(null,p2.getPlayerName() + "'s Guess is Wrong!");
if (p3.guessNumber()==numberToGuess){
flagWinner = 3;
break;
}
JOptionPane.showMessageDialog(null,p3.getPlayerName() + "'s Guess is Wrong!");
}
if (1 == flagWinner){
JOptionPane.showMessageDialog(null,p1.getPlayerName()+ " Wins!");
} else if (2 == flagWinner){
JOptionPane.showMessageDialog(null,p2.getPlayerName()+ " Wins!");
} else JOptionPane.showMessageDialog(null,p3.getPlayerName()+ " Wins!");
}
public static void main(String[] e){
GuessGame gg = new GuessGame(JOptionPane.showInputDialog("Enter Player 1 Name: "),JOptionPane.showInputDialog("Enter Player 2 Name: "),JOptionPane.showInputDialog("Enter Player 3 Name: ");
gg.startGame();
}
}
public class Player {
private String playerName;
private int number;
public Player(String name){
this.playerName = name;
}
public int guessNumber() {
return Integer.parseInt(JOptionPane.showInputDialog(playerName
+"'s Turn\nGuess the Number"));
JOptionPane.showMessageDialog(null, playerName+ "'s GUESS is " + number);
}
public String getPlayerName() {
return playerName;
}
}
public class GuessGame {
private int numberToGuess;
private Player p1;
private Player p2;
private Player p3;
//Would probably made amount of players dynamic as well.
public GuessGame(String player1, String player2, String player3){
numberToGuess = (int) (Math.random()*10);
p1 = new Player(player1);
p2 = new Player(player2);
p3 = new Player(player3);
}
//Would probably split this up in submethods too.
//The flagging is quite ugly too but I don't got time to edit it.
public void startGame(){
int flagWinner = 0;
while(0==flagWinner){
if (p1.guessNumber()==numberToGuess){
flagWinner = 1;
break;
}
JOptionPane.showMessageDialog(null, p1.getPlayerName()
+ "'s Guess is Wrong!");
if (p2.guessNumber()==numberToGuess){
flagWinner = 2;
break;
}
JOptionPane.showMessageDialog(null,p2.getPlayerName()
+ "'s Guess is Wrong!");
if (p3.guessNumber()==numberToGuess){
flagWinner = 3;
break;
}
JOptionPane.showMessageDialog(null,p3.getPlayerName()
+ "'s Guess is Wrong!");
}
if (1 == flagWinner){
JOptionPane.showMessageDialog(null,p1.getPlayerName()+ " Wins!");
} else if (2 == flagWinner){
JOptionPane.showMessageDialog(null,p2.getPlayerName()+ " Wins!");
} else JOptionPane.showMessageDialog(null,p3.getPlayerName()+ " Wins!");
}
public static void main(String[] e){
GuessGame gg = new GuessGame(
JOptionPane.showInputDialog("Enter Player 1 Name: "),
JOptionPane.showInputDialog("Enter Player 2 Name: "),
JOptionPane.showInputDialog("Enter Player 3 Name: ")
);
gg.startGame();
}
}
The layout felt very C++ and very connected to "functions" instead of objects. Though you seem to got a nice idea of how it's work and do a lot better at objects than most people do.
I would do it like this (dirty version, should be improved, but it was a quick redesign):
public class Player {
private String playerName;
private int number;
public Player(String name){
this.playerName = name;
}
public int guessNumber() {
return Integer.parseInt(JOptionPane.showInputDialog(playerName +"'s Turn\nGuess
the Number"));
JOptionPane.showMessageDialog(null, playerName+ "'s GUESS is " + number);
}
public String getPlayerName() {
return playerName;
}
}
public class GuessGame {
private int numberToGuess;
private Player p1;
private Player p2;
private Player p3;
//Would probably made amount of players dynamic as well.
public GuessGame(String player1, String player2, String player3){
numberToGuess = (int) (Math.random()*10);
p1 = new Player(player1);
p2 = new Player(player2);
p3 = new Player(player3);
}
//Would probably split this up in submethods too.
//The flagging is quite ugly too but I don't got time to edit it.
public void startGame(){
int flagWinner = 0;
while(0==flagWinner){
if (p1.guessNumber()==numberToGuess){
flagWinner = 1;
break;
}
JOptionPane.showMessageDialog(null, p1.getPlayerName() + "'s Guess is Wrong!");
if (p2.guessNumber()==numberToGuess){
flagWinner = 2;
break;
}
JOptionPane.showMessageDialog(null,p2.getPlayerName() + "'s Guess is Wrong!");
if (p3.guessNumber()==numberToGuess){
flagWinner = 3;
break;
}
JOptionPane.showMessageDialog(null,p3.getPlayerName() + "'s Guess is Wrong!");
}
if (1 == flagWinner){
JOptionPane.showMessageDialog(null,p1.getPlayerName()+ " Wins!");
} else if (2 == flagWinner){
JOptionPane.showMessageDialog(null,p2.getPlayerName()+ " Wins!");
} else JOptionPane.showMessageDialog(null,p3.getPlayerName()+ " Wins!");
}
public static void main(String[] e){
GuessGame gg = new GuessGame(JOptionPane.showInputDialog("Enter Player 1 Name: "),JOptionPane.showInputDialog("Enter Player 2 Name: "),JOptionPane.showInputDialog("Enter Player 3 Name: ");
gg.startGame();
}
}
lang-java