patternjavaMinor
Rocks, Paper Scissors game
Viewed 0 times
scissorspaperrocksgame
Problem
I wrote a Rock Paper Scissors game and was hoping to get some feedback on issues like:
Main
```
import java.util.Scanner;
public class RPS {
String comparableChoice;
String userComparableChoice;
public String startGame() {
Scanner in = new Scanner(System.in);
System.out.println("Choose, (1)Rock, (2)Paper or (3)Scissors");
System.out.println("enter number: 1,2 or 3");
int userChoice = in.nextInt();
if (userChoice == 1){
- Am I utilizing object oriented techniques correctly? What can I do better?
- Am I using
main()correctly? My understanding is that it should be a main controller for the rest of the program. Should I be utilizing a counter in a different method, instead of in main?
- Am I using my instance variables correctly?
- I realize that the computer choice is skewed. How should I do it differently?
- The way I check for wins seems a little convoluted. Should I be using a different system to check?
- What did I do well, so that I can remember to use those techniques? What did I do poorly?
Main
import java.util.*;
public class driver {
public static void main(String[] args) {
int userWins = 0, computerWins = 0;
System.out.println("Welcome to Rocks, Paper, Scissors. How many games would you like to play?");
Scanner in = new Scanner(System.in);
int numberOfGames = in.nextInt();
int userCounter = 0;
int computerCounter = 0;
for (int i = 0; i < numberOfGames; i++) {
RPS Game = new RPS();
String user = Game.startGame();
String computer = Game.computerChoice();
boolean wins = Game.evaluateGame(user, computer);
if (wins == true){
userWins++;
}
else {
computerWins++;
}
System.out.println("Number of wins:");
System.out.println("Computer: " + computerWins);
System.out.println("User: " + userWins);
System.out.println();
}
}
}RPS```
import java.util.Scanner;
public class RPS {
String comparableChoice;
String userComparableChoice;
public String startGame() {
Scanner in = new Scanner(System.in);
System.out.println("Choose, (1)Rock, (2)Paper or (3)Scissors");
System.out.println("enter number: 1,2 or 3");
int userChoice = in.nextInt();
if (userChoice == 1){
Solution
I would only call the method
Inside the
You can make the other methods of the
startGame() inside the main function and handle from there the entire logic of the game. The main method shouldn't be aware of the exact procedure of a game. It just wants to start a game and wants to know who won. As an output from that method you could return true if the user wins and false if the computer wins. If you want to handle a tie separately you can return an int value instead for indicating the outcome (-1 computer wins, 0 draw, 1 user wins).Inside the
startGame() method you can call the game logic functions/your algorithm. In pseudo code it would look sth like this:public int startGame()
{
userChoice();
computerChoice();
return evaluateWinner(); // Evaluate winner and returns -1, 0, 1 depending on the user and computer choice (i.e. who the winner is or if its a tie)
}You can make the other methods of the
RPS class private to have a clean interface for this class, allowing other classes only to start a game and prevent them interfering the game algorithm itself.Code Snippets
public int startGame()
{
userChoice();
computerChoice();
return evaluateWinner(); // Evaluate winner and returns -1, 0, 1 depending on the user and computer choice (i.e. who the winner is or if its a tie)
}Context
StackExchange Code Review Q#79515, answer score: 8
Revisions (0)
No revisions yet.