I made a Rock, Paper, Scissors game and would like the code to be reviewed.
Main
class:
public class Main {
public static void main(String args[]){
Game game = new Game();
game.gameLoop();
}
}
Game
class:
import java.util.Scanner;
public class Game {
String yesOrNo;
Input inputClass = new Input();
Computer comp = new Computer();
Checking checker = new Checking();
Score score = new Score();
Scanner input = new Scanner(System.in);
public void gameLoop() {
while (true) {
inputClass.setUserInput();
comp.setComputerInput();
comp.displayComputerInout();
if (checker.check(inputClass.getUserInput(),
comp.getComputerInput()) == 0) {
System.out.println("Tie!");
score.scoreBoard(0);
} else if (checker.check(inputClass.getUserInput(),
comp.getComputerInput()) == 1) {
System.out.println("Win!");
score.scoreBoard(1);
} else if (checker.check(inputClass.getUserInput(),
comp.getComputerInput()) == 2) {
System.out.println("Lose");
score.scoreBoard(2);
} else if (checker.check(inputClass.getUserInput(),
comp.getComputerInput()) == -1) {
break;
}
System.out.println("Go to score menu? y/n");
yesOrNo = input.nextLine();
if(yesOrNo.equals("y")){
score.ScoreMenu();
}else if(yesOrNo.equals("n")){
}else{
System.out.println("Invalid command! Continuing without going to score menu!");
}
System.out.println("Want to play again? y/n");
yesOrNo = input.next();
if(yesOrNo.equals("y")){
}else if(yesOrNo.equals("n")){
break;
}else{
System.out.println("Invalid command! Continuing!");
}
}
}
}
Input
Class:
import java.util.Scanner;
public class Input {
private Scanner input = new Scanner(System.in);
private int uInput;
public void setUserInput(){
System.out.println("Rock: 1, Paper: 2, Scissor: 3");
uInput = input.nextInt();
if(uInput<1 || uInput>3){
System.out.println("Invalid value!\nYou get Rock");
uInput=1;
}
}
public int getUserInput(){
return uInput;
}
}
Computer
Class:
public class Computer {
private int computerGesture;
public void setComputerInput(){
computerGesture = (int) (Math.random()*4);
if(computerGesture == 0){
computerGesture = 1;
}
}
public int getComputerInput(){
return computerGesture;
}
public void displayComputerInout(){
if(computerGesture == 1)
{
System.out.println("Computer chooses rock");
}else if(computerGesture == 2){
System.out.println("Computer chooses paper");
}else if(computerGesture == 3){
System.out.println("Computer chooses scissor");
}
}
}
Checking
Class:
public class Checking {
private final int TIE = 0;
private final int WIN = 1;
private final int LOSE = 2;
private final int ERROR = -1;
public int check(int pGesture, int cGesture) {
if (pGesture == cGesture) {
return TIE;
} else if (pGesture == 1 && cGesture == 2) {
return LOSE;
} else if (pGesture == 2 && cGesture == 1) {
return WIN;
} else if (pGesture == 1 && cGesture == 3) {
return WIN;
} else if (pGesture == 3 && cGesture == 1) {
return LOSE;
} else if (pGesture == 2 && cGesture == 3) {
return LOSE;
} else if (pGesture == 3 && cGesture == 2) {
return WIN;
} else {
return ERROR;
}
}
}
Score
Class:
import java.util.Scanner;
public class Score {
Scanner input = new Scanner(System.in);
private int wins = 0;
private int loses = 0;
private int ties = 0;
private String choise;
public void scoreBoard(int winOrLoseOrTie){
if(winOrLoseOrTie == 1){
wins++;
}else if(winOrLoseOrTie == 2){
loses++;
}else if(winOrLoseOrTie == 0){
ties++;
}
}
public void displayScore(){
System.out.println("Wins Loses Ties\n"+wins+" "+loses+" "+ties);
}
public void resetScoreBoard(){
wins = 0;
loses = 0;
ties = 0;
}
public void ScoreMenu(){
System.out.println("Display Score: display");
System.out.println("Reset Score: reset");
System.out.println("Exit score menu: exit");
choise = input.next();
if(choise.equals("display")){
displayScore();
}else if(choise.equals("reset")){
resetScoreBoard();
}else if(choise.equals("exit")){
}else{
System.out.println("Invalid command!");
ScoreMenu();
}
}
}
Random
comes with the methodnextInt()
so you could doRandom.nextInt(4)
instead of multiplying a floating point. Please someone correct me if I'm wrong, but I believe the numbers should be distributed more randomly than by multiplying a random floating point number. \$\endgroup\$