📅  最后修改于: 2023-12-03 14:39:47.414000             🧑  作者: Mango
该项目是一个基于控制台的石头剪刀布游戏,使用C#编写。该游戏允许两个程序员进行对战,每个程序员选择石头、剪刀或布,然后比较选择以确定游戏的结果。
该项目由以下几个关键部分组成:
以下是一个简化版本的代码示例:
using System;
enum Move
{
Rock,
Paper,
Scissors
}
class Player
{
public string Name { get; private set; }
public Move Move { get; private set; }
public Player(string name)
{
Name = name;
}
public void MakeMove(Move move)
{
Move = move;
}
}
class Game
{
private Player _player1;
private Player _player2;
public Game(Player player1, Player player2)
{
_player1 = player1;
_player2 = player2;
}
public void Play()
{
Console.WriteLine($"{_player1.Name} make your move (0 for Rock, 1 for Paper, 2 for Scissors):");
int player1Move = int.Parse(Console.ReadLine());
_player1.MakeMove((Move)player1Move);
Console.Clear();
Console.WriteLine($"{_player2.Name} make your move (0 for Rock, 1 for Paper, 2 for Scissors):");
int player2Move = int.Parse(Console.ReadLine());
_player2.MakeMove((Move)player2Move);
Console.Clear();
// Determine the winner and display the result
if (_player1.Move == _player2.Move)
{
Console.WriteLine("It's a tie!");
}
else if (
(_player1.Move == Move.Rock && _player2.Move == Move.Scissors) ||
(_player1.Move == Move.Scissors && _player2.Move == Move.Paper) ||
(_player1.Move == Move.Paper && _player2.Move == Move.Rock))
{
Console.WriteLine($"{_player1.Name} wins!");
}
else
{
Console.WriteLine($"{_player2.Name} wins!");
}
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Welcome to Rock Paper Scissors Game!");
Console.Write("Enter Player 1 name: ");
Player player1 = new Player(Console.ReadLine());
Console.Write("Enter Player 2 name: ");
Player player2 = new Player(Console.ReadLine());
Game game = new Game(player1, player2);
game.Play();
Console.WriteLine("Thanks for playing!");
}
}
以上示例中,我们定义了Player
类和Game
类,并在Main
方法中初始化了两个玩家和一个游戏实例。在游戏中,我们通过读取控制台的输入来获取每个玩家的选择,并根据规则确定胜利者。
现在,你可以邀请另一个程序员一起玩这个有趣的石头剪刀布游戏啦!