Rock Paper Scissors game using OOP
up vote
0
down vote
favorite
Im complete beginner to Java and we have just started learning objects and classes in school. Im trying to create a very simple text based Rock Paper Scissors game. I do realise that my code is a mess, so Im asking for some suggestions on how could I improve it and if Im even in the right direction with oop.
Main class:
public class Main {
public static void main(String args) {
new Game();
}
}
Player class
public abstract class Player {
private String name;
private String choice;
public Player() {}
public Player(String name) {this.name = name;}
public void setName(String newName) {
name = newName;
}
public String getName() {
return name;
}
public String getChoice() {
return choice;
}
public void setChoice(String newChoice) {
choice = newChoice;
}
public abstract void selectChoice();
}
User class
import java.util.Scanner;
public class User extends Player {
private Scanner input;
public User() {
input = new Scanner(System.in);
}
public void selectChoice() {
System.out.println("Enter your choice: R - Rock, P - Paper, S - Scissors");
setChoice(input.nextLine().toUpperCase());
}
}
Computer class
import java.util.Random;
public class Computer extends Player {
private Random rand;
private final int MAX_NUMBER = 3;
public Computer() {
setName("Computer");
rand = new Random();
}
public void selectChoice() {
int randomNumber = rand.nextInt(MAX_NUMBER);
switch(randomNumber) {
case 0:
setChoice("ROCK");
break;
case 1:
setChoice("PAPER");
break;
case 2:
setChoice("SCISSORS");
break;
}
}
}
Game class
import java.util.Scanner;
public class Game {
private User p;
private Computer com;
private int playerWins;
private int playerLoses;
private int ties;
private boolean isRunning = false;
private Scanner scan;
public Game() {
p = new User();
com = new Computer();
scan = new Scanner(System.in);
start();
}
private void start() {
isRunning = true;
System.out.println("Please, enter your name:");
p.setName(scan.nextLine());
while(isRunning) {
displayScore();
p.selectChoice();
com.selectChoice();
displayChoices();
displayWinner(decideWinner());
updateScore(decideWinner());
playAgain();
}
}
private void displayScore() {
System.out.println(p.getName());
System.out.println("----------");
System.out.println("Wins: " + playerWins);
System.out.println("Loses: " + playerLoses);
System.out.println("Ties: " + ties);
System.out.println("----------");
}
private int decideWinner() {
// 0 - User wins
// 1 - Computer wins
// 2 - tie
if(p.getChoice().equals("ROCK") && com.getChoice().equals("SCISSORS"))
return 0;
else if(p.getChoice().equals("PAPER") && com.getChoice().equals("ROCK"))
return 0;
else if(p.getChoice().equals("SCISSORS") && com.getChoice().equals("PAPER"))
return 0;
else if(com.getChoice().equals("ROCK") && p.getChoice().equals("SCISSORS"))
return 1;
else if(com.getChoice().equals("PAPER") && p.getChoice().equals("ROCK"))
return 1;
else if(com.getChoice().equals("SCISSORS") && p.getChoice().equals("PAPER"))
return 1;
else
return 2;
}
private void displayChoices() {
System.out.println("User has selected: " + p.getChoice());
System.out.println("Computer has selected: " + com.getChoice());
}
private void displayWinner(int winner) {
switch(winner) {
case 0:
System.out.println("User has won!");
break;
case 1:
System.out.println("Computer has won!");
break;
case 2:
System.out.println("It is a tie!");
}
}
private void playAgain() {
String choice;
System.out.println("Do you want to play again? Enter Yes to play again.");
choice = scan.nextLine();
if(!(choice.toUpperCase().equals("YES") ))
isRunning = false;
}
private void updateScore(int winner) {
if(winner == 0)
playerWins++;
else if(winner == 1)
playerLoses++;
else if(winner == 2)
ties++;
}
}
java beginner
New contributor
Žiga Černič is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
up vote
0
down vote
favorite
Im complete beginner to Java and we have just started learning objects and classes in school. Im trying to create a very simple text based Rock Paper Scissors game. I do realise that my code is a mess, so Im asking for some suggestions on how could I improve it and if Im even in the right direction with oop.
Main class:
public class Main {
public static void main(String args) {
new Game();
}
}
Player class
public abstract class Player {
private String name;
private String choice;
public Player() {}
public Player(String name) {this.name = name;}
public void setName(String newName) {
name = newName;
}
public String getName() {
return name;
}
public String getChoice() {
return choice;
}
public void setChoice(String newChoice) {
choice = newChoice;
}
public abstract void selectChoice();
}
User class
import java.util.Scanner;
public class User extends Player {
private Scanner input;
public User() {
input = new Scanner(System.in);
}
public void selectChoice() {
System.out.println("Enter your choice: R - Rock, P - Paper, S - Scissors");
setChoice(input.nextLine().toUpperCase());
}
}
Computer class
import java.util.Random;
public class Computer extends Player {
private Random rand;
private final int MAX_NUMBER = 3;
public Computer() {
setName("Computer");
rand = new Random();
}
public void selectChoice() {
int randomNumber = rand.nextInt(MAX_NUMBER);
switch(randomNumber) {
case 0:
setChoice("ROCK");
break;
case 1:
setChoice("PAPER");
break;
case 2:
setChoice("SCISSORS");
break;
}
}
}
Game class
import java.util.Scanner;
public class Game {
private User p;
private Computer com;
private int playerWins;
private int playerLoses;
private int ties;
private boolean isRunning = false;
private Scanner scan;
public Game() {
p = new User();
com = new Computer();
scan = new Scanner(System.in);
start();
}
private void start() {
isRunning = true;
System.out.println("Please, enter your name:");
p.setName(scan.nextLine());
while(isRunning) {
displayScore();
p.selectChoice();
com.selectChoice();
displayChoices();
displayWinner(decideWinner());
updateScore(decideWinner());
playAgain();
}
}
private void displayScore() {
System.out.println(p.getName());
System.out.println("----------");
System.out.println("Wins: " + playerWins);
System.out.println("Loses: " + playerLoses);
System.out.println("Ties: " + ties);
System.out.println("----------");
}
private int decideWinner() {
// 0 - User wins
// 1 - Computer wins
// 2 - tie
if(p.getChoice().equals("ROCK") && com.getChoice().equals("SCISSORS"))
return 0;
else if(p.getChoice().equals("PAPER") && com.getChoice().equals("ROCK"))
return 0;
else if(p.getChoice().equals("SCISSORS") && com.getChoice().equals("PAPER"))
return 0;
else if(com.getChoice().equals("ROCK") && p.getChoice().equals("SCISSORS"))
return 1;
else if(com.getChoice().equals("PAPER") && p.getChoice().equals("ROCK"))
return 1;
else if(com.getChoice().equals("SCISSORS") && p.getChoice().equals("PAPER"))
return 1;
else
return 2;
}
private void displayChoices() {
System.out.println("User has selected: " + p.getChoice());
System.out.println("Computer has selected: " + com.getChoice());
}
private void displayWinner(int winner) {
switch(winner) {
case 0:
System.out.println("User has won!");
break;
case 1:
System.out.println("Computer has won!");
break;
case 2:
System.out.println("It is a tie!");
}
}
private void playAgain() {
String choice;
System.out.println("Do you want to play again? Enter Yes to play again.");
choice = scan.nextLine();
if(!(choice.toUpperCase().equals("YES") ))
isRunning = false;
}
private void updateScore(int winner) {
if(winner == 0)
playerWins++;
else if(winner == 1)
playerLoses++;
else if(winner == 2)
ties++;
}
}
java beginner
New contributor
Žiga Černič is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Im complete beginner to Java and we have just started learning objects and classes in school. Im trying to create a very simple text based Rock Paper Scissors game. I do realise that my code is a mess, so Im asking for some suggestions on how could I improve it and if Im even in the right direction with oop.
Main class:
public class Main {
public static void main(String args) {
new Game();
}
}
Player class
public abstract class Player {
private String name;
private String choice;
public Player() {}
public Player(String name) {this.name = name;}
public void setName(String newName) {
name = newName;
}
public String getName() {
return name;
}
public String getChoice() {
return choice;
}
public void setChoice(String newChoice) {
choice = newChoice;
}
public abstract void selectChoice();
}
User class
import java.util.Scanner;
public class User extends Player {
private Scanner input;
public User() {
input = new Scanner(System.in);
}
public void selectChoice() {
System.out.println("Enter your choice: R - Rock, P - Paper, S - Scissors");
setChoice(input.nextLine().toUpperCase());
}
}
Computer class
import java.util.Random;
public class Computer extends Player {
private Random rand;
private final int MAX_NUMBER = 3;
public Computer() {
setName("Computer");
rand = new Random();
}
public void selectChoice() {
int randomNumber = rand.nextInt(MAX_NUMBER);
switch(randomNumber) {
case 0:
setChoice("ROCK");
break;
case 1:
setChoice("PAPER");
break;
case 2:
setChoice("SCISSORS");
break;
}
}
}
Game class
import java.util.Scanner;
public class Game {
private User p;
private Computer com;
private int playerWins;
private int playerLoses;
private int ties;
private boolean isRunning = false;
private Scanner scan;
public Game() {
p = new User();
com = new Computer();
scan = new Scanner(System.in);
start();
}
private void start() {
isRunning = true;
System.out.println("Please, enter your name:");
p.setName(scan.nextLine());
while(isRunning) {
displayScore();
p.selectChoice();
com.selectChoice();
displayChoices();
displayWinner(decideWinner());
updateScore(decideWinner());
playAgain();
}
}
private void displayScore() {
System.out.println(p.getName());
System.out.println("----------");
System.out.println("Wins: " + playerWins);
System.out.println("Loses: " + playerLoses);
System.out.println("Ties: " + ties);
System.out.println("----------");
}
private int decideWinner() {
// 0 - User wins
// 1 - Computer wins
// 2 - tie
if(p.getChoice().equals("ROCK") && com.getChoice().equals("SCISSORS"))
return 0;
else if(p.getChoice().equals("PAPER") && com.getChoice().equals("ROCK"))
return 0;
else if(p.getChoice().equals("SCISSORS") && com.getChoice().equals("PAPER"))
return 0;
else if(com.getChoice().equals("ROCK") && p.getChoice().equals("SCISSORS"))
return 1;
else if(com.getChoice().equals("PAPER") && p.getChoice().equals("ROCK"))
return 1;
else if(com.getChoice().equals("SCISSORS") && p.getChoice().equals("PAPER"))
return 1;
else
return 2;
}
private void displayChoices() {
System.out.println("User has selected: " + p.getChoice());
System.out.println("Computer has selected: " + com.getChoice());
}
private void displayWinner(int winner) {
switch(winner) {
case 0:
System.out.println("User has won!");
break;
case 1:
System.out.println("Computer has won!");
break;
case 2:
System.out.println("It is a tie!");
}
}
private void playAgain() {
String choice;
System.out.println("Do you want to play again? Enter Yes to play again.");
choice = scan.nextLine();
if(!(choice.toUpperCase().equals("YES") ))
isRunning = false;
}
private void updateScore(int winner) {
if(winner == 0)
playerWins++;
else if(winner == 1)
playerLoses++;
else if(winner == 2)
ties++;
}
}
java beginner
New contributor
Žiga Černič is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Im complete beginner to Java and we have just started learning objects and classes in school. Im trying to create a very simple text based Rock Paper Scissors game. I do realise that my code is a mess, so Im asking for some suggestions on how could I improve it and if Im even in the right direction with oop.
Main class:
public class Main {
public static void main(String args) {
new Game();
}
}
Player class
public abstract class Player {
private String name;
private String choice;
public Player() {}
public Player(String name) {this.name = name;}
public void setName(String newName) {
name = newName;
}
public String getName() {
return name;
}
public String getChoice() {
return choice;
}
public void setChoice(String newChoice) {
choice = newChoice;
}
public abstract void selectChoice();
}
User class
import java.util.Scanner;
public class User extends Player {
private Scanner input;
public User() {
input = new Scanner(System.in);
}
public void selectChoice() {
System.out.println("Enter your choice: R - Rock, P - Paper, S - Scissors");
setChoice(input.nextLine().toUpperCase());
}
}
Computer class
import java.util.Random;
public class Computer extends Player {
private Random rand;
private final int MAX_NUMBER = 3;
public Computer() {
setName("Computer");
rand = new Random();
}
public void selectChoice() {
int randomNumber = rand.nextInt(MAX_NUMBER);
switch(randomNumber) {
case 0:
setChoice("ROCK");
break;
case 1:
setChoice("PAPER");
break;
case 2:
setChoice("SCISSORS");
break;
}
}
}
Game class
import java.util.Scanner;
public class Game {
private User p;
private Computer com;
private int playerWins;
private int playerLoses;
private int ties;
private boolean isRunning = false;
private Scanner scan;
public Game() {
p = new User();
com = new Computer();
scan = new Scanner(System.in);
start();
}
private void start() {
isRunning = true;
System.out.println("Please, enter your name:");
p.setName(scan.nextLine());
while(isRunning) {
displayScore();
p.selectChoice();
com.selectChoice();
displayChoices();
displayWinner(decideWinner());
updateScore(decideWinner());
playAgain();
}
}
private void displayScore() {
System.out.println(p.getName());
System.out.println("----------");
System.out.println("Wins: " + playerWins);
System.out.println("Loses: " + playerLoses);
System.out.println("Ties: " + ties);
System.out.println("----------");
}
private int decideWinner() {
// 0 - User wins
// 1 - Computer wins
// 2 - tie
if(p.getChoice().equals("ROCK") && com.getChoice().equals("SCISSORS"))
return 0;
else if(p.getChoice().equals("PAPER") && com.getChoice().equals("ROCK"))
return 0;
else if(p.getChoice().equals("SCISSORS") && com.getChoice().equals("PAPER"))
return 0;
else if(com.getChoice().equals("ROCK") && p.getChoice().equals("SCISSORS"))
return 1;
else if(com.getChoice().equals("PAPER") && p.getChoice().equals("ROCK"))
return 1;
else if(com.getChoice().equals("SCISSORS") && p.getChoice().equals("PAPER"))
return 1;
else
return 2;
}
private void displayChoices() {
System.out.println("User has selected: " + p.getChoice());
System.out.println("Computer has selected: " + com.getChoice());
}
private void displayWinner(int winner) {
switch(winner) {
case 0:
System.out.println("User has won!");
break;
case 1:
System.out.println("Computer has won!");
break;
case 2:
System.out.println("It is a tie!");
}
}
private void playAgain() {
String choice;
System.out.println("Do you want to play again? Enter Yes to play again.");
choice = scan.nextLine();
if(!(choice.toUpperCase().equals("YES") ))
isRunning = false;
}
private void updateScore(int winner) {
if(winner == 0)
playerWins++;
else if(winner == 1)
playerLoses++;
else if(winner == 2)
ties++;
}
}
java beginner
java beginner
New contributor
Žiga Černič is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Žiga Černič is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Žiga Černič is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 19 mins ago
Žiga Černič
1
1
New contributor
Žiga Černič is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Žiga Černič is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Žiga Černič is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Žiga Černič is a new contributor. Be nice, and check out our Code of Conduct.
Žiga Černič is a new contributor. Be nice, and check out our Code of Conduct.
Žiga Černič is a new contributor. Be nice, and check out our Code of Conduct.
Žiga Černič is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209331%2frock-paper-scissors-game-using-oop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown