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

在这里插入图片描述

Python 版本hellohailuo.com
python
import random

def get_computer_choice():
choices = [‘石头’, ‘剪刀’, ‘布’]
return random.choice(choices)

def determine_winner(player_choice, computer_choice):
if player_choice == computer_choice:
return ‘平局!’
elif (player_choice == ‘石头’ and computer_choice == ‘剪刀’) or \
(player_choice == ‘剪刀’ and computer_choice == ‘布’) or \
(player_choice == ‘布’ and computer_choice == ‘石头’):
return ‘你赢了!’
else:
return ‘你输了!’

def play_game():
print(“欢迎来到’石头、剪刀、布’游戏!”)
player_choice = input("请输入你的选择(石头/剪刀/布): “)
computer_choice = get_computer_choice()
print(f"计算机的选择是:{computer_choice}”)
result = determine_winner(player_choice, computer_choice)
print(result)

if name == “main”:
play_game()
JavaScript 版本(网页版)
html

石头、剪刀、布游戏

石头、剪刀、布游戏

开始游戏

C# 版本(控制台应用) csharp using System;

class Program
{
static string GetComputerChoice()
{
string[] choices = { “石头”, “剪刀”, “布” };
Random random = new Random();
return choices[random.Next(choices.Length)];
}

static string DetermineWinner(string playerChoice, string computerChoice)  
{  
    if (playerChoice == computerChoice)  
        return "平局!";  
    else if (  
        (playerChoice == "石头" && computerChoice == "剪刀") ||  
        (playerChoice == "剪刀" && computerChoice == "布") ||  
        (playerChoice == "布" && computerChoice == "石头")  
    )  
        return "你赢了!";  
    else  
        return "你输了!";  
}  

static void Main(string[] args)  
{  
    Console.WriteLine("欢迎来到'石头、剪刀、布'游戏!");  
    Console.Write("请输入你的选择(石头/剪刀/布): ");  
    string playerChoice = Console.ReadLine();  
    string computerChoice = GetComputerChoice();  
    Console.WriteLine($"计算机的选择是:{computerChoice}");  
    string result = DetermineWinner(playerChoice, computerChoice);  
    Console.WriteLine(result);  
}  

}
这三个示例展示了如何使用Python、JavaScript(网页版)和C#(控制台应用)来实现一个简单的“石头、剪刀、布”游戏。每个版本都包含了获取玩家和计算机选择、判断胜负以及输出结果当然,我会为你提供几种不同编程语言实现的简单小程序游戏代码示例。这次,我们将实现一个“石头、剪刀、布”游戏,这是一个经典的两人游戏。

Python 版本
python
import random

def get_computer_choice():
choices = [‘石头’, ‘剪刀’, ‘布’]
return random.choice(choices)

def determine_winner(player_choice, computer_choice):
if player_choice == computer_choice:
return ‘平局!’
elif (player_choice == ‘石头’ and computer_choice == ‘剪刀’) or \
(player_choice == ‘剪刀’ and computer_choice == ‘布’) or \
(player_choice == ‘布’ and computer_choice == ‘石头’):
return ‘你赢了!’
else:
return ‘你输了!’

def play_game():
print(“欢迎来到’石头、剪刀、布’游戏!”)
player_choice = input("请输入你的选择(石头/剪刀/布): “)
computer_choice = get_computer_choice()
print(f"计算机的选择是:{computer_choice}”)
result = determine_winner(player_choice, computer_choice)
print(result)

if name == “main”:
play_game()
JavaScript 版本(网页版)
html

石头、剪刀、布游戏

石头、剪刀、布游戏

开始游戏

C# 版本(控制台应用) csharp using System;

class Program
{
static string GetComputerChoice()
{
string[] choices = { “石头”, “剪刀”, “布” };
Random random = new Random();
return choices[random.Next(choices.Length)];
}

static string DetermineWinner(string playerChoice, string computerChoice)  
{  
    if (playerChoice == computerChoice)  
        return "平局!";  
    else if (  
        (playerChoice == "石头" && computerChoice == "剪刀") ||  
        (playerChoice == "剪刀" && computerChoice == "布") ||  
        (playerChoice == "布" && computerChoice == "石头")  
    )  
        return "你赢了!";  
    else  
        return "你输了!";  
}  

static void Main(string[] args)  
{  
    Console.WriteLine("欢迎来到'石头、剪刀、布'游戏!");  
    Console.Write("请输入你的选择(石头/剪刀/布): ");  
    string playerChoice = Console.ReadLine();  
    string computerChoice = GetComputerChoice();  
    Console.WriteLine($"计算机的选择是:{computerChoice}");  
    string result = DetermineWinner(playerChoice, computerChoice);  
    Console.WriteLine(result);  
}  

}
这三个示例展示了如何使用Python、JavaScript(网页版)和C#(控制台应用)来实现一个简单的“石头、剪刀、布”游戏。每个版本都包含了获取玩家和计算机选择、判断胜负以及输出结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值