C # Object-oriented programming-scissors games

Source: Internet
Author: User

1. Requirements
now to make a game, the player and the computer to play scissors game, players punch, computer punch, the computer automatically judge winning or losing.
2. Demand Analysis
according to the needs, to analyze the object, can be analyzed: Player object (player), computer object (computer), referee Object (Judge). Player Punch is controlled by the user and is represented by a number: 1 stones, 2 scissors, 3 computer punches are randomly generated by the computer judge wins or loses according to the punch situation of the player and the computer.
3. Implementation of Class objects
Player class Sample Code
    Class Player {string name;            public string Name {get {return Name;}        set {name = value;}  } public int showfist () {Console.WriteLine ("Excuse me, what do you want to punch?")            1. Scissors 2. Stone 3. Cloth ");            int result = READINT (1, 3);            String fist = inttofist (result);            Console.WriteLine ("Player: {0} out of 1 {1}", name, fist);        return result; }///<summary>///Convert the number entered by the user into the corresponding Fist///</summary>//<param name= "Input" > </param>//<returns></returns> private string inttofist (int input) {s Tring result = string.            Empty;                    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>//<param name= "Max" ></param>///<returns></returns> private int ReadInt (int min,int max) {while (true) {//Gets user input data from the console Stri                ng str = console.readline ();                Converts a user-entered string into int type int result; if (int. TryParse (str, out result)) {//Determine the input range if (Result >= min &&amp ;                    Result <= max) {return result;                        } else {Console.WriteLine ("Please enter the number of 1 {0}-{1} ranges", Min, Max);                    Continue }} else {Console.WriteLine ("Please enter an integer"); }             }        }    }

Computer class Sample Code
    Class computer    {        //generates a random number to allow the computer to randomly punch random        ran = new Random ();        public int showfist ()        {            int result = ran. Next (1, 4);            Console.WriteLine ("Computer 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;        }    }


Referee Class Sample Code This class uses a special way to determine the result


    Class Judge    {public        void determine (int p1, int p2)        {            //1 scissors   2 stone 3 cloth            //1 3   1-3=-2 in case of player 1 scissors , the computer out 3 cloth, the player wins            //2 1   2-1=1   in the Player 2 stone case, the computer out 1 scissors, the player wins            //3 2   3-2=1   in the player out 3 cloth case, the computer out 2 stone, the player wins            if (p1-p2 = =-2 | | p1-p2 = = 1)            {                Console.WriteLine ("Player wins!");            }            else if (P1 = = p2)            {                Console.WriteLine ("Draw");            }            else            {                Console.WriteLine ("Player failed!");            }        }    }

4. Implementation of the object
        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 ();             }        }


C # Object-oriented programming-scissors games

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.