C # Object-Oriented Programming-guessing games

Source: Internet
Author: User

C # Object-Oriented Programming-guessing games
1. Requirements

Now we want to create a game where players and computers can play the game with guesses. players can play the game with their fists, computers can play the game, and computers can automatically determine whether to win or lose.
2. Demand Analysis
Analyze the objects as needed, including the Player, Computer, and Judge ). Players are controlled by users. numbers are used to represent: 1 stone, 2 scissors, and 3 computers. The computers generate random referees to determine whether to win or lose Based on the situations of players and computers.
3. Implementation of class objects
Player 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? 1. scissors 2. stone 3. cloth "); int result = ReadInt (1, 3); string fist = IntToFist (result); Console. writeLine ("Player: {0} has 1 {1}", name, fist); return result ;}////// Convert the number entered by the user into the corresponding fist /////////
  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 ;}////// Receive data from the console and verify the validity ////////////
  Private int ReadInt (int min, int max) {while (true) {// obtain user input data from the Console string str = Console. readLine (); // converts a user-input string to Int type int result; if (int. tryParse (str, out result) {// judge the input range. if (result> = min & result <= max) {return result;} else {Console. writeLine ("Enter 1 number in the range of {0}-{1}", min, max); continue ;}} else {Console. writeLine ("enter an integer ");}}}}

Computer sample code
Class Computer {// generate a Random number, so that the Computer Random ran = new Random (); public int ShowFist () {int result = ran. next (1, 4); Console. writeLine ("computer output: {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 ;}}


Example code of the referee class. This class uses a special method to determine the result.


Class Judge {public void Determine (int p1, int p2) {// 1 scissors 2 stones 3 cloth // 1 3 1-3 =-2 when the player has 1 scissors, the computer has 3 scissors, player win // 2 1 2-1 = 1 when the player outputs 2 stones, the computer outputs 1 scissors, player win // 3 2 3-2 = 1 when the player has 3 cloths, the computer has 2 stones, player win if (p1-p2 =-2 | p1-p2 = 1) {Console. writeLine ("Player victory! ");} Else if (p1 = p2) {Console. WriteLine (" Draw ");} else {Console. WriteLine (" Player failed! ");}}}

4. Object implementation
        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();             }        }


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.