当然,我会继续为你提供几种不同编程语言实现的简单小游戏代码示例。这次,我们将实现一个经典的“石头、剪刀、布”游戏。

在这里插入图片描述

  1. 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

当然,我会继续为你提供几种不同编程语言实现的简单小游戏代码示例。这次,我们将实现一个经典的“石头、剪刀、布”游戏。

  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 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值