Advertisement
Guest User

Bulls and Cows Game

a guest
Jan 14th, 2022
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.13 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> digits = 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.                 int a = pcNumber / 1000;
  61.                 int b = pcNumber % 1000 / 100;
  62.                 int c = pcNumber % 100 / 10;
  63.                 int d = pcNumber % 10;
  64.                
  65.                 digits.Clear();
  66.                
  67.                 if (!digits.Contains(a)) digits.Add(a);
  68.                 if (!digits.Contains(b)) digits.Add(b);
  69.                 if (!digits.Contains(c)) digits.Add(c);
  70.                 if (!digits.Contains(d)) digits.Add(d);
  71.                
  72.                 if (digits.Count == 4) break;
  73.             }
  74.            
  75.            
  76.         }
  77.        
  78.        
  79.         void ButtonCheckClick(object sender, EventArgs e)
  80.         {
  81.             int userNumber = 0;
  82.            
  83.             try
  84.             {
  85.                 userNumber = int.Parse(textBoxUserNumber.Text);
  86.             }
  87.             catch
  88.             {                                  
  89.                 MessageBox.Show("Въведи 4-цифрено число");
  90.                 textBoxUserNumber.Text = "";
  91.                 textBoxUserNumber.Select();
  92.                 return;
  93.             }
  94.            
  95.             textBoxUserNumber.Text = "";
  96.             textBoxUserNumber.Select();
  97.            
  98.             //if (textBoxUserNumber.Text.Length != 4)
  99.             if (userNumber < 1000 || userNumber > 9999)
  100.             {
  101.                 MessageBox.Show("Числото трябва да е 4-цифрено");
  102.                 return;
  103.             }
  104.            
  105.        
  106.             int a = userNumber / 1000;
  107.             int b = userNumber % 1000 / 100;
  108.             int c = userNumber % 100 / 10;
  109.             int d = userNumber % 10;
  110.            
  111.             List<int> userDigits = new List<int>();
  112.             if (!userDigits.Contains(a)) userDigits.Add(a);
  113.             if (!userDigits.Contains(b)) userDigits.Add(b);
  114.             if (!userDigits.Contains(c)) userDigits.Add(c);
  115.             if (!userDigits.Contains(d)) userDigits.Add(d);
  116.            
  117.             if (userDigits.Count != 4)
  118.             {
  119.                 MessageBox.Show("Цифрите трябва да са различни");
  120.                 return;
  121.             }
  122.            
  123.             int bulls = 0;
  124.             int cows = 0;
  125.             tries--;
  126.            
  127.             if (digits[0] == a) bulls++;
  128.             else if (digits.Contains(a)) cows++;
  129.            
  130.             if (digits[1] == b) bulls++;
  131.             else if (digits.Contains(b)) cows++;
  132.            
  133.             if (digits[2] == c) bulls++;
  134.             else if (digits.Contains(c)) cows++;
  135.            
  136.             if (digits[3] == d) bulls++;
  137.             else if (digits.Contains(d)) cows++;
  138.            
  139.             labelTries.Text = "Остават " + tries + " опита";
  140.             if (tries == 1)
  141.             {
  142.                 labelTries.Text = "Остава 1 опит";
  143.             }
  144.            
  145.            
  146.             if (bulls == 4)
  147.             {
  148.                 labelTries.Text = "ПЕЧЕЛИШ !!!";
  149.                 labelPcNumber.Text = pcNumber.ToString();
  150.                 labelPcNumber.ForeColor = Color.Green;
  151.                 buttonCheck.Enabled = false;
  152.                 textBoxUserNumber.Enabled = false;
  153.             }
  154.            
  155.             string bullsWord = bulls == 1 ? "бик" : "бика";
  156.             string cowsWord = cows == 1 ? "крава" : "крави";
  157.            
  158.             if (tries > 0)
  159.             {
  160.                 string result = string.Format("{0}: {1} {2}, {3} {4}",
  161.                     userNumber, bulls, bullsWord, cows, cowsWord);
  162.                 listBoxResult.Items.Add(result);
  163.             }
  164.             else
  165.             {
  166.                 if (bulls != 4)
  167.                 {
  168.                     labelTries.Text = "ГУБИШ !!!";
  169.                     labelPcNumber.Text = pcNumber.ToString();
  170.                     labelPcNumber.ForeColor = Color.Red;
  171.                     buttonCheck.Enabled = false;
  172.                     textBoxUserNumber.Enabled = false;
  173.                 }
  174.  
  175.             }
  176.            
  177.         }
  178.     }
  179. }
  180.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement