Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 1. Napravite igru križić-kružić (iks-oks) korištenjem znanja stečenih na ovoj
- // laboratorijskoj vježbi. Omogućiti pokretanje igre, unos imena dvaju igrača, ispis
- // koji igrač je trenutno na potezu, igranje igre s iscrtavanjem križića i kružića na
- // odgovarajućim mjestima te ispis dijaloga s porukom o pobjedi, odnosno
- // neriješenom rezultatu kao i praćenje ukupnog rezultata.
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace LV7___Analiza
- {
- public partial class TicTacToe : Form
- {
- bool turn = true; // true = X turn; flase = O turn
- public TicTacToe()
- {
- InitializeComponent();
- }
- private void NewGame()
- {
- label_turn.Text = "Turn: " + textBox_player1.Text;
- button1.Text = "";
- button2.Text = "";
- button3.Text = "";
- button4.Text = "";
- button5.Text = "";
- button6.Text = "";
- button7.Text = "";
- button8.Text = "";
- button9.Text = "";
- button1.Enabled = true;
- button2.Enabled = true;
- button3.Enabled = true;
- button4.Enabled = true;
- button5.Enabled = true;
- button6.Enabled = true;
- button7.Enabled = true;
- button8.Enabled = true;
- button9.Enabled = true;
- }
- private void button_click(object sender, EventArgs e)
- {
- Button b = (Button)sender;
- if (turn)
- {
- label_turn.Text = "Turn: " + textBox_player2.Text;
- b.Text = "X";
- }
- else
- {
- label_turn.Text = "Turn: " + textBox_player1.Text;
- b.Text = "O";
- }
- turn = !turn;
- b.Enabled = false;
- 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"))
- {
- MessageBox.Show(textBox_player1.Text + " has won!");
- NewGame();
- }
- 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"))
- {
- MessageBox.Show(textBox_player2.Text + " has won!");
- NewGame();
- }
- 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"))
- {
- MessageBox.Show(textBox_player1.Text + " has won!");
- NewGame();
- }
- 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"))
- {
- MessageBox.Show(textBox_player2.Text + " has won!");
- NewGame();
- }
- else if ((button1.Text == "X" && button5.Text == "X" && button9.Text == "X") || (button3.Text == "X" && button5.Text == "X" && button7.Text == "X"))
- {
- MessageBox.Show(textBox_player1.Text + " has won!");
- NewGame();
- }
- else if ((button1.Text == "O" && button5.Text == "O" && button9.Text == "O") || (button3.Text == "O" && button5.Text == "O" && button7.Text == "O"))
- {
- MessageBox.Show(textBox_player2.Text + " has won!");
- NewGame();
- }
- else
- {
- if (!button1.Enabled && !button2.Enabled && !button3.Enabled && !button4.Enabled && !button5.Enabled && !button6.Enabled && !button7.Enabled && !button8.Enabled && !button9.Enabled)
- {
- MessageBox.Show("Tie!");
- NewGame();
- }
- }
- }
- private void button10_Click(object sender, EventArgs e)
- {
- Application.Exit();
- }
- private void button_start_Click(object sender, EventArgs e)
- {
- if (textBox_player1.Text.Length == 0 || (textBox_player2.Text.Length == 0))
- {
- MessageBox.Show("Invalid player name!");
- }
- else
- {
- NewGame();
- }
- }
- private void TicTacToe_Load(object sender, EventArgs e)
- {
- button1.Enabled = false;
- button2.Enabled = false;
- button3.Enabled = false;
- button4.Enabled = false;
- button5.Enabled = false;
- button6.Enabled = false;
- button7.Enabled = false;
- button8.Enabled = false;
- button9.Enabled = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement