Advertisement
amarek

OOP LV7 - Analiza

Nov 18th, 2019
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.26 KB | None | 0 0
  1. // 1. Napravite igru križić-kružić (iks-oks) korištenjem znanja stečenih na ovoj
  2. // laboratorijskoj vježbi. Omogućiti pokretanje igre, unos imena dvaju igrača, ispis
  3. // koji igrač je trenutno na potezu, igranje igre s iscrtavanjem križića i kružića na
  4. // odgovarajućim mjestima te ispis dijaloga s porukom o pobjedi, odnosno
  5. // neriješenom rezultatu kao i praćenje ukupnog rezultata.
  6.  
  7. using System;
  8. using System.Collections.Generic;
  9. using System.ComponentModel;
  10. using System.Data;
  11. using System.Drawing;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows.Forms;
  16.  
  17. namespace LV7___Analiza
  18. {
  19.     public partial class TicTacToe : Form
  20.     {
  21.         bool turn = true; // true = X turn; flase = O turn
  22.         public TicTacToe()
  23.         {
  24.             InitializeComponent();
  25.         }
  26.  
  27.         private void NewGame()
  28.         {
  29.             label_turn.Text = "Turn: " + textBox_player1.Text;
  30.  
  31.             button1.Text = "";
  32.             button2.Text = "";
  33.             button3.Text = "";
  34.             button4.Text = "";
  35.             button5.Text = "";
  36.             button6.Text = "";
  37.             button7.Text = "";
  38.             button8.Text = "";
  39.             button9.Text = "";
  40.  
  41.             button1.Enabled = true;
  42.             button2.Enabled = true;
  43.             button3.Enabled = true;
  44.             button4.Enabled = true;
  45.             button5.Enabled = true;
  46.             button6.Enabled = true;
  47.             button7.Enabled = true;
  48.             button8.Enabled = true;
  49.             button9.Enabled = true;
  50.         }
  51.  
  52.         private void button_click(object sender, EventArgs e)
  53.         {
  54.             Button b = (Button)sender;
  55.             if (turn)
  56.             {
  57.                 label_turn.Text = "Turn: " + textBox_player2.Text;
  58.                 b.Text = "X";
  59.             }
  60.             else
  61.             {
  62.                 label_turn.Text = "Turn: " + textBox_player1.Text;
  63.                 b.Text = "O";
  64.             }
  65.  
  66.             turn = !turn;
  67.             b.Enabled = false;
  68.  
  69.             if ((button1.Text == "X" && button2.Text == "X" && button3.Text == "X") || (button4.Text == "X" && button5.Text == "X" && button6.Text == "X") || (button7.Text == "X" && button8.Text == "X" && button9.Text == "X"))
  70.             {
  71.                 MessageBox.Show(textBox_player1.Text + " has won!");
  72.                 NewGame();
  73.             }
  74.             else if ((button1.Text == "O" && button2.Text == "O" && button3.Text == "O") || (button4.Text == "O" && button5.Text == "O" && button6.Text == "O") || (button7.Text == "O" && button8.Text == "O" && button9.Text == "O"))
  75.             {
  76.                 MessageBox.Show(textBox_player2.Text + " has won!");
  77.                 NewGame();
  78.             }
  79.             else if ((button1.Text == "X" && button4.Text == "X" && button7.Text == "X") || (button2.Text == "X" && button5.Text == "X" && button8.Text == "X") || (button3.Text == "X" && button6.Text == "X" && button9.Text == "X"))
  80.             {
  81.                 MessageBox.Show(textBox_player1.Text + " has won!");
  82.                 NewGame();
  83.             }
  84.             else if ((button1.Text == "O" && button4.Text == "O" && button7.Text == "O") || (button2.Text == "O" && button5.Text == "O" && button8.Text == "O") || (button3.Text == "O" && button6.Text == "O" && button9.Text == "O"))
  85.             {
  86.                 MessageBox.Show(textBox_player2.Text + " has won!");
  87.                 NewGame();
  88.             }
  89.             else if ((button1.Text == "X" && button5.Text == "X" && button9.Text == "X") || (button3.Text == "X" && button5.Text == "X" && button7.Text == "X"))
  90.             {
  91.                 MessageBox.Show(textBox_player1.Text + " has won!");
  92.                 NewGame();
  93.             }
  94.             else if ((button1.Text == "O" && button5.Text == "O" && button9.Text == "O") || (button3.Text == "O" && button5.Text == "O" && button7.Text == "O"))
  95.             {
  96.                 MessageBox.Show(textBox_player2.Text + " has won!");
  97.                 NewGame();
  98.             }
  99.             else
  100.             {
  101.                 if (!button1.Enabled && !button2.Enabled && !button3.Enabled && !button4.Enabled && !button5.Enabled && !button6.Enabled && !button7.Enabled && !button8.Enabled && !button9.Enabled)
  102.                 {
  103.                     MessageBox.Show("Tie!");
  104.                     NewGame();
  105.                 }
  106.             }
  107.         }
  108.  
  109.         private void button10_Click(object sender, EventArgs e)
  110.         {
  111.             Application.Exit();
  112.         }
  113.  
  114.         private void button_start_Click(object sender, EventArgs e)
  115.         {
  116.             if (textBox_player1.Text.Length == 0 || (textBox_player2.Text.Length == 0))
  117.             {
  118.                 MessageBox.Show("Invalid player name!");
  119.             }
  120.             else
  121.             {
  122.                 NewGame();
  123.             }
  124.         }
  125.  
  126.         private void TicTacToe_Load(object sender, EventArgs e)
  127.         {
  128.             button1.Enabled = false;
  129.             button2.Enabled = false;
  130.             button3.Enabled = false;
  131.             button4.Enabled = false;
  132.             button5.Enabled = false;
  133.             button6.Enabled = false;
  134.             button7.Enabled = false;
  135.             button8.Enabled = false;
  136.             button9.Enabled = false;
  137.         }
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement