How to Create a Simple Rock-Paper-Scissors Game in Java
How to create a simple "Rock-Paper-Scissors" game in Java
Rock-paper-scissors is a classic game that everyone knows and loves. It’s a great way to have fun and test your critical thinking skills. So why not take the time to learn how to create a simple Rock-Paper-Scissors game in Java? This tutorial will show you how.
Step 1: Set Up Your Environment
To get started, you'll need a text editor or IDE capable of running Java. You can use any IDE or text editor you want, but I recommend using IntelliJ IDEA or Eclipse. Both are free, open-source, and easy to use.
Step 2: Create the Basic Java Classes
We'll be creating two classes for this game: one for the actual game logic and one to maintain the score. To start, create a new project in your IDE and add the following classes:
GameLogic.java
ScoreKeeper.java
Step 3: Write the Game Logic
The GameLogic class will contain all of the logic for the game. To start, we'll define three constants for the possible choices: ROCK, PAPER, and SCISSORS. We'll also define a method called play() that takes two parameters (player1Choice and player2Choice) and returns an integer indicating the winner: 1 for player1, -1 for player2, or 0 for a draw. This method should contain all of the logic of the game:
public class GameLogic {
// Constants representing the three possible choices
public static final int ROCK = 0;
public static final int PAPER = 1;
public static final int SCISSORS = 2;
// Method to determine the winner
public static int play(int player1Choice, int player2Choice) {
if (player1Choice == player2Choice) {
return 0; // draw
} else if ((player1Choice == ROCK && player2Choice == SCISSORS) ||
(player1Choice == PAPER && player2Choice == ROCK) ||
(player1Choice == SCISSORS && player2Choice == PAPER)) {
return 1; // player1 wins
} else {
return -1; // player2 wins
}
}
}
Step 4: Write the Score Keeper Class
The ScoreKeeper class will keep track of the score of the game. To start, we'll create two private variables to store the scores of each player:
public class ScoreKeeper {
// Private variables to store the player's scores
private int player1Score;
private int player2Score;
...
We'll create two public methods to update the scores when the results of a round are determined:
// Method to update the score when player 1 wins
public void player1Wins() {
player1Score++;
}
// Method to update the score when player 2 wins
public void player2Wins() {
player2Score++;
}
We'll also create two methods to return the current scores:
// Methods to return the current scores
public int getPlayer1Score() {
return player1Score;
}
public int getPlayer2Score() {
return player2Score;
}
Step 5: Put It All Together
Now that the basic classes are written, we can put them together in a main class to create a functioning game. For the sake of simplicity, we'll create a text-based version of the game, where the user will be prompted to enter their choice of ROCK, PAPER, or SCISSORS.
public class Main {
public static void main(String[] args) {
ScoreKeeper scoreKeeper = new ScoreKeeper();
while (true) {
// Prompt user to enter choice
System.out.println("Enter your choice (ROCK, PAPER, SCISSORS):");
// Read user input
Scanner scanner = new Scanner(System.in);
String player1Input = scanner.nextLine();
// Convert user input to numeric value
int player1Choice = 0;
if (player1Input.equalsIgnoreCase("rock")) {
player1Choice = GameLogic.ROCK;
} else if (player1Input.equalsIgnoreCase("paper")) {
player1Choice = GameLogic.PAPER;
} else if (player1Input.equalsIgnoreCase("scissors")) {
player1Choice = GameLogic.SCISSORS;
}
// Generate random number for computer choice
Random random = new Random();
int player2Choice = random.nextInt(3);
// Determine the winner
int result = GameLogic.play(player1Choice, player2Choice);
// Update the score
if (result == 1) {
scoreKeeper.player1Wins();
} else if (result == -1) {
scoreKeeper.player2Wins();
}
// Print the score
System.out.printf("Player 1: %d, Player 2: %d",
scoreKeeper.getPlayer1Score(),
scoreKeeper.getPlayer2Score());
}
}
}
And that's it! At this point, you have a working version of Rock-Paper-Scissors written in Java. You can expand on this game by adding more features, such as AI opponents, 3-player games, or other variations of the classic game. Have fun and happy coding!