C Object oriented Programming guessing game implementation method

  • 2020-12-07 04:22:11
  • OfStack

This article gives an example of C# Object-oriented programming guessing game implementation. Share to everybody for everybody reference. Specific implementation methods are as follows:

1. The demand

Now to make a game, players and computer guessing game, players punch, computer punch, the computer automatically judge the win or lose.

2. Demand analysis

According to the requirements, 1 object can be analyzed: player object (Player), computer object (Computer), referee object (Judge). The player punching is controlled by the user and represented by numbers: 1 stone, 2 scissors, 3 cloth computer punching is randomly generated by the computer judge to judge the winning or losing according to the punching situation of the player and the computer.

3. Implementation of class objects

. Player class example code:

class Player
{
 
    string name;
    public string Name
    {
        get { return name; }
        set { name = value; }
    }
 
    public int ShowFist()
    {
        Console.WriteLine(" Excuse me, , What are you going to punch ?  1. scissors      2. stone     3. cloth ");
        int result = ReadInt(1, 3);
        string fist = IntToFist(result);
        Console.WriteLine(" The player :{0} Out of the 1 a {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 = string.Empty;
 
        switch (input)
        {
            case 1:
                result = " scissors ";
                break;
            case 2:
                result = " stone ";
                break;
            case 3:
                result = " cloth ";
                break;
        }
        return result;
    }
 
    /// <summary>
    /// Receives data from the console and validates it
    /// </summary>
    /// <param name="min">
    /// <param name="max">
    /// <returns></returns>
    private int ReadInt(int min,int max)
    {
        while (true)
        {
            // Get the user input data from the console
            string str = Console.ReadLine();
 
            // Converts a string entered by the user to Int type
            int result;
            if (int.TryParse(str, out result))
            {
                // Determine the range of inputs
                if (result >= min && result <= max)
                {
                    return result;
                }
                else
                {
                    Console.WriteLine(" Please enter the 1 a {0}-{1} Range of several ", min, max);
                    continue;
                }
            }
            else
            {
                Console.WriteLine(" Please enter an integer ");
            }
        }
    }
}

Ii. Computer class example code:
class Computer
{
    // generate 1 Random number, let the computer random punch
    Random ran = new Random();
    public int ShowFist()
    {
        int result = ran.Next(1, 4);
        Console.WriteLine(" The computer is out :{0}", IntToFist(result));
        return result;
    }
 
    private string IntToFist(int input)
    {
        string result = string.Empty;
 
        switch (input)
        {
            case 1:
                result = " scissors ";
                break;
            case 2:
                result = " stone ";
                break;
            case 3:
                result = " cloth ";
                break;
        }
        return result;
    }
}

This class determines the result in a special way:
class Judge
{
    public void Determine(int p1, int p2)
    {
        //1 scissors    2 stone 3 cloth
        //1 3   1-3=-2 The player out 1 The scissors case is out of the computer 3 Cloth, the player wins
        //2 1   2-1=1   The player out 2 In the case of stone, the computer comes out 1 Scissors, player wins
        //3 2   3-2=1   The player out 3 Cloth the case out of the computer 2 Stone, player wins
        if (p1 - p2 == -2 || p1 - p2 == 1)
        {
            Console.WriteLine(" Players win !");
        }
        else if (p1 == p2)
        {
            Console.WriteLine(" A draw ");
        }
        else
        {
            Console.WriteLine(" Players failed !");
        }
    }
}

. Object realization:
static void Main(string[] args)
{
    Player p1 = new Player() { Name="Tony"};
    Computer c1 = new Computer();
    Judge j1 = new Judge();
    while (true)
    {
        int res1 = p1.ShowFist();
        int res2 = c1.ShowFist();
        j1.Determine(res1, res2);
        Console.ReadKey();
    }
}

Hopefully this article has helped you with your C# programming.


Related articles: