Advertisement
jovanovski

КИ Лаб2 C#

Mar 2nd, 2014
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.31 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15.  
  16. namespace lab2
  17. {
  18.     /// <summary>
  19.     /// Interaction logic for MainWindow.xaml
  20.     /// </summary>
  21.     public partial class MainWindow : Window
  22.     {
  23.         //TIC TAC
  24.         private List<Button> lstButtons = new List<Button>();
  25.         private string nextChar = "X";
  26.         private Color nextColor = Colors.Blue;
  27.         private string[,] helperMatrix = new string[3, 3];
  28.  
  29.         //HANGMAN
  30.         private List<UIElement> lstParts = new List<UIElement>();
  31.         private List<Label> lstLetters = new List<Label>();
  32.         private int counter = 0;
  33.         private string[] words = { "bike", "mountain", "table", "computer" };
  34.         private string currentWord = "";
  35.  
  36.         public MainWindow()
  37.         {
  38.             InitializeComponent();
  39.             //TICTAC
  40.             for (int i = 0; i < 9; i++)
  41.             {
  42.                 helperMatrix[i / 3, i % 3] = "";
  43.                 lstButtons.Add(new Button());
  44.                 grid1.Children.Add(lstButtons[i]);
  45.                 lstButtons[i].SetValue(Grid.RowProperty, i / 3);
  46.                 lstButtons[i].SetValue(Grid.ColumnProperty, i % 3);
  47.                 lstButtons[i].Click += new RoutedEventHandler(Button_Click);
  48.             }
  49.  
  50.  
  51.             //HANGMAN
  52.             lstParts.Add(head);
  53.             lstParts.Add(body);
  54.             lstParts.Add(leftLeg);
  55.             lstParts.Add(rightLeg);
  56.             lstParts.Add(leftArm);
  57.             lstParts.Add(rightArm);
  58.             hangManTab.KeyUp += new KeyEventHandler(hangManTab_KeyUp);
  59.             ResetHangman();
  60.         }
  61.  
  62.         private void ResetHangman()
  63.         {
  64.             Random rnd = new Random();
  65.             foreach (UIElement ui in lstParts)
  66.             {
  67.                 ui.Visibility = Visibility.Hidden;
  68.                 counter = 0;
  69.             }
  70.             pnlLetters.Children.Clear();
  71.             lstLetters.Clear();
  72.             currentWord = words[rnd.Next(words.Length)];
  73.             for (int i = 0; i < currentWord.Length; i++)
  74.             {
  75.                 Label lbl = new Label();
  76.                 lbl.BorderBrush = new SolidColorBrush(Colors.Black);
  77.                 lbl.BorderThickness = new Thickness(1);
  78.                 lbl.VerticalContentAlignment = System.Windows.VerticalAlignment.Top;
  79.                 lbl.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
  80.                 lbl.FontSize = 30;
  81.                 lbl.Height = 50;
  82.                 lbl.Width = 40;
  83.                 lbl.Margin = new Thickness(2);
  84.                 lstLetters.Add(lbl);
  85.                 pnlLetters.Children.Add(lbl);
  86.             }
  87.         }
  88.  
  89.         private void hangManTab_KeyUp(object sender, KeyEventArgs e)
  90.         {
  91.             char key;
  92.             Char.TryParse(e.Key.ToString(), out key);
  93.             if (Char.IsLetter(key))
  94.             {
  95.                 key = char.ToLower(key);
  96.                 if (currentWord.Contains(key))
  97.                 {
  98.                     for (int i = 0; i < currentWord.Length; i++)
  99.                     {
  100.                         if (currentWord[i] == key)
  101.                         {
  102.                             lstLetters[i].Content = currentWord[i].ToString();
  103.                         }
  104.                     }
  105.                 }
  106.                 else
  107.                 {
  108.                     if (counter < lstParts.Count)
  109.                         lstParts[counter++].Visibility = Visibility.Visible;
  110.                 }
  111.                 if (IsWonHangman())
  112.                 {
  113.                     MessageBox.Show("You won :)");
  114.                     ResetHangman();
  115.                 }
  116.                 else if (IsOverHangman())
  117.                 {
  118.                     MessageBox.Show("Game over :(");
  119.                     ResetHangman();
  120.                 }
  121.             }
  122.         }
  123.  
  124.         private bool IsOverHangman()
  125.         {
  126.             Console.WriteLine(lstParts.Count);
  127.             if (counter == 6)
  128.             return true;
  129.             return false;
  130.         }
  131.         private bool IsWonHangman()
  132.         {
  133.  
  134.             foreach (Label lbl in lstLetters)
  135.             {
  136.                 if (lbl.Content == null)
  137.                     return false;
  138.                 else if (lbl.Content.ToString() == "")
  139.                     return false;
  140.             }
  141.             return true;
  142.         }
  143.  
  144.         private void Button_Click(object sender, RoutedEventArgs e) {
  145.             Button btn = sender as Button;
  146.             btn.IsEnabled = false;
  147.             Label lbl = new Label();
  148.             lbl.Content = nextChar;
  149.             lbl.Foreground = new SolidColorBrush(nextColor);
  150.             lbl.FontSize = 30;
  151.             lbl.VerticalAlignment = System.Windows.VerticalAlignment.Center;
  152.             lbl.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
  153.             btn.Content = lbl;
  154.             helperMatrix[(int) btn.GetValue(Grid.RowProperty), (int) btn.GetValue(Grid.ColumnProperty)] = nextChar;
  155.  
  156.             if(nextChar=="X"){
  157.                 nextChar = "O";
  158.                 nextColor = Colors.Red;
  159.             }
  160.             else{
  161.                 nextChar = "X";
  162.                 nextColor = Colors.Blue;
  163.             }
  164.  
  165.             if (isWon()) {
  166.                 MessageBox.Show("Pobeda!");
  167.                 resetTic();
  168.             }
  169.             else if (isDraw()) {
  170.                 MessageBox.Show("Nereseno!");
  171.                 resetTic();
  172.             }
  173.            
  174.         }
  175.  
  176.         private bool isWon() {
  177.             bool won = false;
  178.  
  179.             if (helperMatrix[0, 0] != "" && helperMatrix[0, 1] != "" && helperMatrix[0, 2] != "" && helperMatrix[0, 0] == helperMatrix[0, 1] && helperMatrix[0, 1] == helperMatrix[0, 2]) { won = true; }
  180.             if (helperMatrix[1, 0] != "" && helperMatrix[1, 1] != "" && helperMatrix[1, 2] != "" && helperMatrix[1, 0] == helperMatrix[1, 1] && helperMatrix[1, 1] == helperMatrix[1, 2]) { won = true; }
  181.             if (helperMatrix[2, 0] != "" && helperMatrix[2, 1] != "" && helperMatrix[2, 2] != "" && helperMatrix[2, 0] == helperMatrix[2, 1] && helperMatrix[2, 1] == helperMatrix[2, 2]) { won = true; }
  182.  
  183.             if (helperMatrix[0, 0] != "" && helperMatrix[1, 0] != "" && helperMatrix[2, 0] != "" && helperMatrix[0, 0] == helperMatrix[1, 0] && helperMatrix[1, 0] == helperMatrix[2, 0]) { won = true; }
  184.             if (helperMatrix[0, 1] != "" && helperMatrix[1, 1] != "" && helperMatrix[2, 1] != "" && helperMatrix[0, 1] == helperMatrix[1, 1] && helperMatrix[1, 1] == helperMatrix[2, 1]) { won = true; }
  185.             if (helperMatrix[0, 2] != "" && helperMatrix[1, 2] != "" && helperMatrix[2, 2] != "" && helperMatrix[0, 2] == helperMatrix[1, 2] && helperMatrix[1, 2] == helperMatrix[2, 2]) { won = true; }
  186.  
  187.             if (helperMatrix[0, 0] != "" && helperMatrix[1, 1] != "" && helperMatrix[2, 2] != "" && helperMatrix[0, 0] == helperMatrix[1, 1] && helperMatrix[1, 1] == helperMatrix[2, 2]) { won = true; }
  188.             if (helperMatrix[0, 2] != "" && helperMatrix[1, 1] != "" && helperMatrix[2, 0] != "" && helperMatrix[0, 2] == helperMatrix[1, 1] && helperMatrix[1, 1] == helperMatrix[2, 0]) { won = true; }
  189.  
  190.             return won;
  191.         }
  192.  
  193.         private bool isDraw()
  194.         {
  195.             bool draw = true;
  196.  
  197.             for (int i = 0; i < 3; i++)
  198.             {
  199.                 for (int j = 0; j < 3; j++) {
  200.                     if (helperMatrix[i, j] == "") {
  201.                         draw = false;
  202.                         break;
  203.                     }
  204.                 }
  205.  
  206.                 if (draw == false)
  207.                     break;
  208.             }
  209.  
  210.                 return draw;
  211.         }
  212.  
  213.         private void resetTic() {
  214.             foreach (Button btn in lstButtons)
  215.             {
  216.                 btn.IsEnabled = true;
  217.                 btn.Content = "";
  218.             }
  219.             for (int i = 0; i < 3; i++)
  220.                 for (int j = 0; j < 3; j++)
  221.                     helperMatrix[i, j] = "";
  222.             nextChar = "X";
  223.             nextColor = Colors.Blue;
  224.         }
  225.        
  226.     }
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement