- Python 版本mmcml.com
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 rock_paper_scissors():
print(“欢迎来到石头、剪刀、布游戏!”)
player_choice = input(“请输入你的选择(石头、剪刀、布):”).strip()
while player_choice not in ['石头', '剪刀', '布']:
print("无效输入,请重新输入(石头、剪刀、布):")
player_choice = input().strip()
computer_choice = get_computer_choice()
print(f"计算机的选择是:{computer_choice}")
print(determine_winner(player_choice, computer_choice))
if name == “main”:
rock_paper_scissors()
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 choose(playerChoice) {
const computerChoice = getComputerChoice();
document.getElementById('result').innerText = `计算机的选择是:${computerChoice}\n${determineWinner(playerChoice, computerChoice)}`;
}
</script>
3. Java 版本 (控制台应用程序) java import java.util.Random; import java.util.Scanner;
public class RockPaperScissorsGame {
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("欢迎来到石头、剪刀、布游戏!");
System.out.print("请输入你的选择(石头、剪刀、布):");
String playerChoice = scanner.nextLine().trim();
while (!playerChoice.equals("石头") && !playerChoice.equals("剪刀") && !playerChoice
当然,我会继续为你提供几种不同编程语言实现的简单小游戏代码示例。这次,我们将实现一个经典的“石头、剪刀、布”游戏。
- 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 rock_paper_scissors():
print(“欢迎来到石头、剪刀、布游戏!”)
player_choice = input(“请输入你的选择(石头、剪刀、布):”).strip()
while player_choice not in ['石头', '剪刀', '布']:
print("无效输入,请重新输入(石头、剪刀、布):")
player_choice = input().strip()
computer_choice = get_computer_choice()
print(f"计算机的选择是:{computer_choice}")
print(determine_winner(player_choice, computer_choice))
if name == “main”:
rock_paper_scissors()
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 choose(playerChoice) {
const computerChoice = getComputerChoice();
document.getElementById('result').innerText = `计算机的选择是:${computerChoice}\n${determineWinner(playerChoice, computerChoice)}`;
}
</script>
3. Java 版本 (控制台应用程序) java import java.util.Random; import java.util.Scanner;
public class RockPaperScissorsGame {
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("欢迎来到石头、剪刀、布游戏!");
System.out.print("请输入你的选择(石头、剪刀、布):");
String playerChoice = scanner.nextLine().trim();
while (!playerChoice.equals("石头") && !playerChoice.equals("剪刀") && !playerChoice