Guest User

Bulls and Cows Game - Optimized

a guest
Jan 14th, 2022
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.06 KB | None | 0 0
  1. /*
  2.  * Created by SharpDevelop.
  3.  * User: Admin
  4.  * Date: 12.1.2022 г.
  5.  * Time: 11:18
  6.  *
  7.  * To change this template use Tools | Options | Coding | Edit Standard Headers.
  8.  */
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Drawing;
  12. using System.Windows.Forms;
  13.  
  14. namespace bulls_cows
  15. {
  16.     /// <summary>
  17.     /// Description of MainForm.
  18.     /// </summary>
  19.     public partial class MainForm : Form
  20.     {
  21.         public int pcNumber = 0;
  22.         public List<int> pcDigits = new List<int>();
  23.         public int tries = 10;
  24.        
  25.         public MainForm()
  26.         {
  27.             //
  28.             // The InitializeComponent() call is required for Windows Forms designer support.
  29.             //
  30.             InitializeComponent();
  31.            
  32.             //
  33.             // TODO: Add constructor code after the InitializeComponent() call.
  34.             //
  35.         }
  36.         void LinkLabel1LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  37.         {
  38.             Info info = new Info();
  39.             info.Show();
  40.         }
  41.        
  42.        
  43.         void ButtonNewGameClick(object sender, EventArgs e)
  44.         {
  45.             tries = 10;
  46.            
  47.             labelPcNumber.Text = "";
  48.             textBoxUserNumber.Text = "";
  49.             labelTries.Text = "";
  50.             listBoxResult.Items.Clear();
  51.            
  52.             buttonCheck.Enabled = true;
  53.             textBoxUserNumber.Enabled = true;
  54.            
  55.             Random r = new Random();
  56.        
  57.             while (true)
  58.             {
  59.                 pcNumber = r.Next(1000, 10000);
  60.                
  61.                 if (Different(pcNumber, true)) break;
  62.             }
  63.            
  64.            
  65.         }
  66.        
  67.        
  68.         void ButtonCheckClick(object sender, EventArgs e)
  69.         {
  70.             int userNumber = 0;
  71.            
  72.             try
  73.             {
  74.                 userNumber = int.Parse(textBoxUserNumber.Text);
  75.             }
  76.             catch
  77.             {                                  
  78.                 MessageBox.Show("Въведи 4-цифрено число");
  79.                 textBoxUserNumber.Text = "";
  80.                 textBoxUserNumber.Select();
  81.                 return;
  82.             }
  83.            
  84.             textBoxUserNumber.Text = "";
  85.             textBoxUserNumber.Select();
  86.            
  87.             //if (textBoxUserNumber.Text.Length != 4)
  88.             if (userNumber < 1000 || userNumber > 9999)
  89.             {
  90.                 MessageBox.Show("Числото трябва да е 4-цифрено");
  91.                 return;
  92.             }
  93.                                    
  94.            
  95.             if (!Different(userNumber, false))
  96.             {
  97.                 MessageBox.Show("Цифрите трябва да са различни");
  98.                 return;
  99.             }
  100.            
  101.             int bulls = 0;
  102.             int cows = 0;
  103.             tries--;
  104.            
  105.             int a = userNumber / 1000;
  106.             int b = userNumber % 1000 / 100;
  107.             int c = userNumber % 100 / 10;
  108.             int d = userNumber % 10;
  109.            
  110.             if (pcDigits[0] == a) bulls++;
  111.             else if (pcDigits.Contains(a)) cows++;
  112.            
  113.             if (pcDigits[1] == b) bulls++;
  114.             else if (pcDigits.Contains(b)) cows++;
  115.            
  116.             if (pcDigits[2] == c) bulls++;
  117.             else if (pcDigits.Contains(c)) cows++;
  118.            
  119.             if (pcDigits[3] == d) bulls++;
  120.             else if (pcDigits.Contains(d)) cows++;
  121.            
  122.             labelTries.Text = "Остават " + tries + " опита";
  123.             if (tries == 1)
  124.             {
  125.                 labelTries.Text = "Остава 1 опит";
  126.             }
  127.            
  128.            
  129.             if (bulls == 4)
  130.             {
  131.                 labelTries.Text = "ПЕЧЕЛИШ !!!";
  132.                 labelPcNumber.Text = pcNumber.ToString();
  133.                 labelPcNumber.ForeColor = Color.Green;
  134.                 buttonCheck.Enabled = false;
  135.                 textBoxUserNumber.Enabled = false;
  136.             }
  137.            
  138.             string bullsWord = bulls == 1 ? "бик" : "бика";
  139.             string cowsWord = cows == 1 ? "крава" : "крави";
  140.            
  141.             if (tries > 0)
  142.             {
  143.                 string result = string.Format("{0}: {1} {2}, {3} {4}",
  144.                     userNumber, bulls, bullsWord, cows, cowsWord);
  145.                 listBoxResult.Items.Add(result);
  146.             }
  147.             else
  148.             {
  149.                 if (bulls != 4)
  150.                 {
  151.                     labelTries.Text = "ГУБИШ !!!";
  152.                     labelPcNumber.Text = pcNumber.ToString();
  153.                     labelPcNumber.ForeColor = Color.Red;
  154.                     buttonCheck.Enabled = false;
  155.                     textBoxUserNumber.Enabled = false;
  156.                 }
  157.  
  158.             }
  159.            
  160.         }
  161.        
  162.        
  163.         public bool Different(int number, bool pc)
  164.         {
  165.             int a = number / 1000;
  166.             int b = number % 1000 / 100;
  167.             int c = number % 100 / 10;
  168.             int d = number % 10;
  169.            
  170.             List<int> digits = new List<int>();
  171.            
  172.             if (!digits.Contains(a)) digits.Add(a);
  173.             if (!digits.Contains(b)) digits.Add(b);
  174.             if (!digits.Contains(c)) digits.Add(c);
  175.             if (!digits.Contains(d)) digits.Add(d);
  176.            
  177.             if (pc) pcDigits = digits;
  178.            
  179.             return digits.Count == 4;          
  180.         }
  181.        
  182.        
  183.     }
  184. }
  185.  
Advertisement
Add Comment
Please, Sign In to add comment