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

在这里插入图片描述

Python 版本lnkryy.cn
Python 版本的“石头剪刀布”游戏是一个控制台应用。

python
import random

choices = [‘石头’, ‘剪刀’, ‘布’]

def get_player_choice():
while True:
choice = input("请输入你的选择(石头/剪刀/布): ").strip().lower()
if choice in choices:
return choice
else:
print(“无效的选择,请重新输入。”)

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 rock_paper_scissors():
print(“欢迎来到石头剪刀布游戏!”)
player_choice = get_player_choice()
computer_choice = random.choice(choices)
print(f"你的选择是:{player_choice},电脑的选择是:{computer_choice}")
result = determine_winner(player_choice, computer_choice)
print(result)

if name == “main”:
rock_paper_scissors()
JavaScript 版本(网页版)
这个JavaScript代码可以嵌入到HTML文件中,用于在浏览器中运行。

html

石头剪刀布游戏

石头剪刀布游戏

开始游戏 C# 版本(控制台应用) 这个C#示例是一个简单的控制台应用程序,不包含Unity或任何图形用户界面。

csharp
using System;

class Program
{
static string[] choices = { “石头”, “剪刀”, “布” };

static string GetPlayerChoice()  
{  
    Console.Write("请输入你的选择(石头/剪刀/布): ");  
    string choice = Console.ReadLine().Trim().ToLower();  
    if (!Array.Exists(choices, element => element == choice))  
    {  
        Console.WriteLine("无效的选择,请重新运行程序。");  
        Environment.Exit(1); // 或者其他错误处理  
    }  
    return choice;  
}  

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

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

Python 版本
Python 版本的“石头剪刀布”游戏是一个控制台应用。

python
import random

choices = [‘石头’, ‘剪刀’, ‘布’]

def get_player_choice():
while True:
choice = input("请输入你的选择(石头/剪刀/布): ").strip().lower()
if choice in choices:
return choice
else:
print(“无效的选择,请重新输入。”)

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 rock_paper_scissors():
print(“欢迎来到石头剪刀布游戏!”)
player_choice = get_player_choice()
computer_choice = random.choice(choices)
print(f"你的选择是:{player_choice},电脑的选择是:{computer_choice}")
result = determine_winner(player_choice, computer_choice)
print(result)

if name == “main”:
rock_paper_scissors()
JavaScript 版本(网页版)
这个JavaScript代码可以嵌入到HTML文件中,用于在浏览器中运行。

html

石头剪刀布游戏

石头剪刀布游戏

开始游戏 C# 版本(控制台应用) 这个C#示例是一个简单的控制台应用程序,不包含Unity或任何图形用户界面。

csharp
using System;

class Program
{
static string[] choices = { “石头”, “剪刀”, “布” };

static string GetPlayerChoice()  
{  
    Console.Write("请输入你的选择(石头/剪刀/布): ");  
    string choice = Console.ReadLine().Trim().ToLower();  
    if (!Array.Exists(choices, element => element == choice))  
    {  
        Console.WriteLine("无效的选择,请重新运行程序。");  
        Environment.Exit(1); // 或者其他错误处理  
    }  
    return choice;  
}  

static string DetermineWinner(string playerChoice, string computerChoice)  
{  
    if (playerChoice == computerChoice)  
        return "平局!";  
    if ((playerChoice == "石头" && computerChoice == "剪刀") ||  
        (playerChoice == "剪刀" && computerChoice == "布") ||  
        (playerChoice == "布" && computerChoice
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值