Create a Simple “Rock-Paper-Scissors” Game in Java – 在Java中创建一个简单的 “石头-剪子布 “游戏

最后修改: 2022年 3月 1日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this short tutorial, we’ll see how to create a simple “Rock-Paper-Scissors” game in Java.

在这个简短的教程中,我们将看到如何在Java中创建一个简单的 “石头-剪子布 “游戏。

2. Create Our “Rock-Paper-Scissors” Game

2.创建我们的 “石头-剪子布 “游戏

Our game will allow players to enter “rock”, “paper”, or “scissors” as the value of each move.

我们的游戏将允许玩家输入 “石头”、”纸 “或 “剪刀 “作为每一步棋的数值。

First, let’s create an enum for the moves:

首先,让我们为这些动作创建一个枚举。

enum Move {
    ROCK("rock"),
    PAPER("paper"),
    SCISSORS("scissors");

    private String value;
    
    //...
}

Then, let’s create a method that generates random integers and returns the computer’s move:

然后,让我们创建一个方法,生成随机的整数并返回计算机的动作。

private static String getComputerMove() {
    Random random = new Random();
    int randomNumber = random.nextInt(3);
    String computerMove = Move.values()[randomNumber].getValue();
    System.out.println("Computer move: " + computerMove);
    return computerMove;
}

And a method that checks if the player wins:

还有一个检查玩家是否获胜的方法。

private static boolean isPlayerWin(String playerMove, String computerMove) {
    return playerMove.equals(Move.ROCK.value) && computerMove.equals(Move.SCISSORS.value)
            || (playerMove.equals(Move.SCISSORS.value) && computerMove.equals(Move.PAPER.value))
            || (playerMove.equals(Move.PAPER.value) && computerMove.equals(Move.ROCK.value));
}

Finally, we’ll use them to form a complete program:

最后,我们将用它们来组成一个完整的程序。

Scanner scanner = new Scanner(System.in);
int wins = 0;
int losses = 0;

System.out.println("Welcome to Rock-Paper-Scissors! Please enter \"rock\", \"paper\", \"scissors\", or \"quit\" to exit.");

while (true) {
    System.out.println("-------------------------");
    System.out.print("Enter your move: ");
    String playerMove = scanner.nextLine();

    if (playerMove.equals("quit")) {
        System.out.println("You won " + wins + " times and lost " + losses + " times.");
        System.out.println("Thanks for playing! See you again.");
        break;
    }

    if (Arrays.stream(Move.values()).noneMatch(x -> x.getValue().equals(playerMove))) {
        System.out.println("Your move isn't valid!");
        continue;
    }

    String computerMove = getComputerMove();

    if (playerMove.equals(computerMove)) {
        System.out.println("It's a tie!");
    } else if (isPlayerWin(playerMove, computerMove)) {
        System.out.println("You won!");
        wins++;
    } else {
        System.out.println("You lost!");
        losses++;
    }
}

As can be seen above, we use Java Scanner to read the user input value.

从上面可以看出,我们使用Java Scanner来读取用户的输入值。

Let’s play a bit and see the output:

让我们玩一下,看看输出结果。

Welcome to Rock-Paper-Scissors! Please enter "rock", "paper", "scissors", or "quit" to exit.
-------------------------
Enter your move: rock
Computer move: scissors
You won!
-------------------------
Enter your move: paper
Computer move: paper
It's a tie!
-------------------------
Enter your move: quit
You won 1 times and lost 0 times.
Thanks for playing! See you again.

3. Conclusion

3.总结

In this quick tutorial, we’ve learned how to create a simple “Rock-Paper-Scissors” game in Java.

在这个快速教程中,我们已经学会了如何在Java中创建一个简单的 “石头剪刀布 “游戏。

As always, the example code from this article can be found over on GitHub.

一如既往,本文中的示例代码可以在GitHub上找到over