- Python 版本
python
import random
def get_computer_choice(): joy-your-future.com
choices = [“石头”, “剪刀”, “布”]
return random.choice(choices)
def determine_winner(player, computer):
if player == computer:
return “平局!”
elif (player == “石头” and computer == “剪刀”) or \
(player == “剪刀” and computer == “布”) or \
(player == “布” and computer == “石头”):
return “你赢了!”
else:
return “你输了!”
def play_game():
choices = [“石头”, “剪刀”, “布”]
player_choice = input(“请输入你的选择(石头/剪刀/布):”).strip()
while player_choice not in choices:
print("无效输入,请重新输入。")
player_choice = input("请输入你的选择(石头/剪刀/布):").strip()
computer_choice = get_computer_choice()
print(f"计算机的选择是:{computer_choice}")
print(determine_winner(player_choice, computer_choice))
if name == “main”:
play_game()
2. JavaScript (HTML + JavaScript) 版本
html
石头剪刀布游戏
石头 剪刀 布<script>
function getComputerChoice() {
const choices = ["石头", "剪刀", "布"];
return choices[Math.floor(Math.random() * choices.length)];
}
function determineWinner(player, computer) {
if (player === computer) {
return "平局!";
} else if ((player === "石头" && computer === "剪刀") ||
(player === "剪刀" && computer === "布") ||
(player === "布" && computer === "石头")) {
return "你赢了!";
} else {
return "你输了!";
}
}
function playGame(playerChoice) {
const computerChoice = getComputerChoice();
document.getElementById('result').innerText = `计算机的选择是:${computerChoice}<br>${determineWinner(playerChoice, computerChoice)}`;
}
</script>
3. Java 版本 (控制台应用程序) java import java.util.Random; import java.util.Scanner;
public class RockPaperScissors {
public static String getComputerChoice() {
String[] choices = {"石头", "剪刀", "布"};
Random rand = new Random();
return choices[rand.nextInt(choices.length)];
}
public static String determineWinner(String player, String computer) {
if (player.equals(computer)) {
return "平局!";
} else if ((player.equals("石头") && computer.equals("剪刀")) ||
(player.equals("剪刀") && computer.equals("布")) ||
(player.equals("布") && computer.equals("石头"))) {
return "你赢了!";
} else {
return "你输了!";
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的选择(石头/剪刀/布):");
String playerChoice = scanner.nextLine().trim();
while (!playerChoice.equals("石头") && !playerChoice.equals("剪刀") && !playerChoice.equals("布")) {
System.out.println("无效输入,请重新输入。");
当然,这次我将为你提供几种不同编程语言中实现的一个简单小游戏代码示例,我们将创建一个“石头剪刀布”游戏。在这个游戏中,玩家将与计算机对战,通过输入“石头”、“剪刀”或“布”来决出胜负。
- Python 版本
python
import random
def get_computer_choice():
choices = [“石头”, “剪刀”, “布”]
return random.choice(choices)
def determine_winner(player, computer):
if player == computer:
return “平局!”
elif (player == “石头” and computer == “剪刀”) or \
(player == “剪刀” and computer == “布”) or \
(player == “布” and computer == “石头”):
return “你赢了!”
else:
return “你输了!”
def play_game():
choices = [“石头”, “剪刀”, “布”]
player_choice = input(“请输入你的选择(石头/剪刀/布):”).strip()
while player_choice not in choices:
print("无效输入,请重新输入。")
player_choice = input("请输入你的选择(石头/剪刀/布):").strip()
computer_choice = get_computer_choice()
print(f"计算机的选择是:{computer_choice}")
print(determine_winner(player_choice, computer_choice))
if name == “main”:
play_game()
2. JavaScript (HTML + JavaScript) 版本
html
石头剪刀布游戏
石头 剪刀 布<script>
function getComputerChoice() {
const choices = ["石头", "剪刀", "布"];
return choices[Math.floor(Math.random() * choices.length)];
}
function determineWinner(player, computer) {
if (player === computer) {
return "平局!";
} else if ((player === "石头" && computer === "剪刀") ||
(player === "剪刀" && computer === "布") ||
(player === "布" && computer === "石头")) {
return "你赢了!";
} else {
return "你输了!";
}
}
function playGame(playerChoice) {
const computerChoice = getComputerChoice();
document.getElementById('result').innerText = `计算机的选择是:${computerChoice}<br>${determineWinner(playerChoice, computerChoice)}`;
}
</script>
3. Java 版本 (控制台应用程序) java import java.util.Random; import java.util.Scanner;
public class RockPaperScissors {
public static String getComputerChoice() {
String[] choices = {"石头", "剪刀", "布"};
Random rand = new Random();
return choices[rand.nextInt(choices.length)];
}
public static String determineWinner(String player, String computer) {
if (player.equals(computer)) {
return "平局!";
} else if ((player.equals("石头") && computer.equals("剪刀")) ||
(player.equals("剪刀") && computer.equals("布")) ||
(player.equals("布") && computer.equals("石头"))) {
return "你赢了!";
} else {
return "你输了!";
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的选择(石头/剪刀/布):");
String playerChoice = scanner.nextLine().trim();
while (!playerChoice.equals("石头") && !playerChoice.equals("剪刀") && !playerChoice.equals("布")) {
System.out.println("无效输入,请重新输入。");