Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- namespace lab2
- {
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- //TIC TAC
- private List<Button> lstButtons = new List<Button>();
- private string nextChar = "X";
- private Color nextColor = Colors.Blue;
- private string[,] helperMatrix = new string[3, 3];
- //HANGMAN
- private List<UIElement> lstParts = new List<UIElement>();
- private List<Label> lstLetters = new List<Label>();
- private int counter = 0;
- private string[] words = { "bike", "mountain", "table", "computer" };
- private string currentWord = "";
- public MainWindow()
- {
- InitializeComponent();
- //TICTAC
- for (int i = 0; i < 9; i++)
- {
- helperMatrix[i / 3, i % 3] = "";
- lstButtons.Add(new Button());
- grid1.Children.Add(lstButtons[i]);
- lstButtons[i].SetValue(Grid.RowProperty, i / 3);
- lstButtons[i].SetValue(Grid.ColumnProperty, i % 3);
- lstButtons[i].Click += new RoutedEventHandler(Button_Click);
- }
- //HANGMAN
- lstParts.Add(head);
- lstParts.Add(body);
- lstParts.Add(leftLeg);
- lstParts.Add(rightLeg);
- lstParts.Add(leftArm);
- lstParts.Add(rightArm);
- hangManTab.KeyUp += new KeyEventHandler(hangManTab_KeyUp);
- ResetHangman();
- }
- private void ResetHangman()
- {
- Random rnd = new Random();
- foreach (UIElement ui in lstParts)
- {
- ui.Visibility = Visibility.Hidden;
- counter = 0;
- }
- pnlLetters.Children.Clear();
- lstLetters.Clear();
- currentWord = words[rnd.Next(words.Length)];
- for (int i = 0; i < currentWord.Length; i++)
- {
- Label lbl = new Label();
- lbl.BorderBrush = new SolidColorBrush(Colors.Black);
- lbl.BorderThickness = new Thickness(1);
- lbl.VerticalContentAlignment = System.Windows.VerticalAlignment.Top;
- lbl.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
- lbl.FontSize = 30;
- lbl.Height = 50;
- lbl.Width = 40;
- lbl.Margin = new Thickness(2);
- lstLetters.Add(lbl);
- pnlLetters.Children.Add(lbl);
- }
- }
- private void hangManTab_KeyUp(object sender, KeyEventArgs e)
- {
- char key;
- Char.TryParse(e.Key.ToString(), out key);
- if (Char.IsLetter(key))
- {
- key = char.ToLower(key);
- if (currentWord.Contains(key))
- {
- for (int i = 0; i < currentWord.Length; i++)
- {
- if (currentWord[i] == key)
- {
- lstLetters[i].Content = currentWord[i].ToString();
- }
- }
- }
- else
- {
- if (counter < lstParts.Count)
- lstParts[counter++].Visibility = Visibility.Visible;
- }
- if (IsWonHangman())
- {
- MessageBox.Show("You won :)");
- ResetHangman();
- }
- else if (IsOverHangman())
- {
- MessageBox.Show("Game over :(");
- ResetHangman();
- }
- }
- }
- private bool IsOverHangman()
- {
- Console.WriteLine(lstParts.Count);
- if (counter == 6)
- return true;
- return false;
- }
- private bool IsWonHangman()
- {
- foreach (Label lbl in lstLetters)
- {
- if (lbl.Content == null)
- return false;
- else if (lbl.Content.ToString() == "")
- return false;
- }
- return true;
- }
- private void Button_Click(object sender, RoutedEventArgs e) {
- Button btn = sender as Button;
- btn.IsEnabled = false;
- Label lbl = new Label();
- lbl.Content = nextChar;
- lbl.Foreground = new SolidColorBrush(nextColor);
- lbl.FontSize = 30;
- lbl.VerticalAlignment = System.Windows.VerticalAlignment.Center;
- lbl.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
- btn.Content = lbl;
- helperMatrix[(int) btn.GetValue(Grid.RowProperty), (int) btn.GetValue(Grid.ColumnProperty)] = nextChar;
- if(nextChar=="X"){
- nextChar = "O";
- nextColor = Colors.Red;
- }
- else{
- nextChar = "X";
- nextColor = Colors.Blue;
- }
- if (isWon()) {
- MessageBox.Show("Pobeda!");
- resetTic();
- }
- else if (isDraw()) {
- MessageBox.Show("Nereseno!");
- resetTic();
- }
- }
- private bool isWon() {
- bool won = false;
- if (helperMatrix[0, 0] != "" && helperMatrix[0, 1] != "" && helperMatrix[0, 2] != "" && helperMatrix[0, 0] == helperMatrix[0, 1] && helperMatrix[0, 1] == helperMatrix[0, 2]) { won = true; }
- if (helperMatrix[1, 0] != "" && helperMatrix[1, 1] != "" && helperMatrix[1, 2] != "" && helperMatrix[1, 0] == helperMatrix[1, 1] && helperMatrix[1, 1] == helperMatrix[1, 2]) { won = true; }
- if (helperMatrix[2, 0] != "" && helperMatrix[2, 1] != "" && helperMatrix[2, 2] != "" && helperMatrix[2, 0] == helperMatrix[2, 1] && helperMatrix[2, 1] == helperMatrix[2, 2]) { won = true; }
- if (helperMatrix[0, 0] != "" && helperMatrix[1, 0] != "" && helperMatrix[2, 0] != "" && helperMatrix[0, 0] == helperMatrix[1, 0] && helperMatrix[1, 0] == helperMatrix[2, 0]) { won = true; }
- if (helperMatrix[0, 1] != "" && helperMatrix[1, 1] != "" && helperMatrix[2, 1] != "" && helperMatrix[0, 1] == helperMatrix[1, 1] && helperMatrix[1, 1] == helperMatrix[2, 1]) { won = true; }
- if (helperMatrix[0, 2] != "" && helperMatrix[1, 2] != "" && helperMatrix[2, 2] != "" && helperMatrix[0, 2] == helperMatrix[1, 2] && helperMatrix[1, 2] == helperMatrix[2, 2]) { won = true; }
- if (helperMatrix[0, 0] != "" && helperMatrix[1, 1] != "" && helperMatrix[2, 2] != "" && helperMatrix[0, 0] == helperMatrix[1, 1] && helperMatrix[1, 1] == helperMatrix[2, 2]) { won = true; }
- if (helperMatrix[0, 2] != "" && helperMatrix[1, 1] != "" && helperMatrix[2, 0] != "" && helperMatrix[0, 2] == helperMatrix[1, 1] && helperMatrix[1, 1] == helperMatrix[2, 0]) { won = true; }
- return won;
- }
- private bool isDraw()
- {
- bool draw = true;
- for (int i = 0; i < 3; i++)
- {
- for (int j = 0; j < 3; j++) {
- if (helperMatrix[i, j] == "") {
- draw = false;
- break;
- }
- }
- if (draw == false)
- break;
- }
- return draw;
- }
- private void resetTic() {
- foreach (Button btn in lstButtons)
- {
- btn.IsEnabled = true;
- btn.Content = "";
- }
- for (int i = 0; i < 3; i++)
- for (int j = 0; j < 3; j++)
- helperMatrix[i, j] = "";
- nextChar = "X";
- nextColor = Colors.Blue;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement