SoFunction
Updated on 2025-03-06

C# object-oriented programming method for implementing rock-scissors game

class Player
{
 
    string name;
    public string Name
    {
        get { return name; }
        set { name = value; }
    }
 
    public int ShowFist()
    {
("May I ask, what punch do you want to use? 1. Scissors    2. Stone    3. Cloth");
        int result = ReadInt(1, 3);
        string fist = IntToFist(result);
("Player: {0} has 1 {1}", name, fist);
        return result;
    }
 
    /// <summary>
/// Convert the number entered by the user into the corresponding fist
    /// </summary>
    /// <param name="input">
    /// <returns></returns>
    private string IntToFist(int input)
    {
        string result = ;
 
        switch (input)
        {
            case 1:
result = "scissors";
                break;
            case 2:
result = "stone";
                break;
            case 3:
result = "cloth";
                break;
        }
        return result;
    }
 
    /// <summary>
/// Receive data from the console and verify validity
    /// </summary>
    /// <param name="min">
    /// <param name="max">
    /// <returns></returns>
    private int ReadInt(int min,int max)
    {
        while (true)
        {
//Get user input data from the console
            string str = ();
 
//Convert the string entered by the user to Int type
            int result;
            if ((str, out result))
            {
//Judge the input range
                if (result >= min && result <= max)
                {
                    return result;
                }
                else
                {
("Please enter 1 number in the range of {0}-{1}", min, max);
                    continue;
                }
            }
            else
            {
("Please enter integer");
            }
        }
    }
}