当然,这次我将为你提供几种不同编程语言中实现的一个简单小游戏代码示例,我们将创建一个“石头剪刀布”游戏。在这个游戏中,玩家将与计算机对战,通过输入“石头”、“剪刀”或“布”来决出胜负。

在这里插入图片描述

  1. 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("无效输入,请重新输入。");

当然,这次我将为你提供几种不同编程语言中实现的一个简单小游戏代码示例,我们将创建一个“石头剪刀布”游戏。在这个游戏中,玩家将与计算机对战,通过输入“石头”、“剪刀”或“布”来决出胜负。

  1. 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("无效输入,请重新输入。");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值