Untitled
unknown
plain_text
a year ago
6.0 kB
1
Indexable
Never
using System; using System.Windows.Forms; namespace TicTacToe { public partial class Form1 : Form { private const int SIZE = 3; private Button[,] buttons; private Button multiPlayerButton; private Button networkPlayerButton; private Game game; private Game.SquareState currentPlayer; private NetworkPlayer networkPlayer; private bool isNetworkGame; private bool isMyTurnInNetworkGame; private GameMode gameMode; public Form1() { InitializeComponent(); game = new Game(); GameModeForm gameModeForm = new GameModeForm(game); gameModeForm.ShowDialog(); buttons = new Button[SIZE, SIZE]; currentPlayer = Game.SquareState.X; networkPlayer = null; isNetworkGame = false; isMyTurnInNetworkGame = false; // this.FormClosing += Form1_FormClosing; multiPlayerButton = new Button(); multiPlayerButton.Text = "Multi Player"; multiPlayerButton.Click += MultiPlayerButton_Click; multiPlayerButton.SetBounds(200, 300, 100, 50); networkPlayerButton = new Button(); networkPlayerButton.Text = "Network Player"; networkPlayerButton.Click += NetworkPlayerButton_Click; networkPlayerButton.SetBounds(350, 300, 100, 50); this.Controls.Add(multiPlayerButton); this.Controls.Add(networkPlayerButton); this.Load += new EventHandler(Form1_Load); // Přidáme inicializaci tlačítek for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { buttons[i, j] = new Button(); } } // Add this block if (game.GetGameMode() == GameMode.Network) { NetworkPlayerButton_Click(null, null); } else { MultiPlayerButton_Click(null, null); } // End of block } void Form1_Load(object sender, EventArgs e) { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { buttons[i, j] = new Button(); buttons[i, j].SetBounds(50 + 80 * i, 50 + 80 * j, 70, 70); buttons[i, j].Click += new EventHandler(this.Square_Click); buttons[i, j].Name = "Square_" + i.ToString() + "_" + j.ToString(); this.Controls.Add(buttons[i, j]); // Přidává tlačítka do formuláře } } } private void MultiPlayerButton_Click(object sender, EventArgs e) { networkPlayer = null; isNetworkGame = false; ResetGame(); } private void NetworkPlayerButton_Click(object sender, EventArgs e) { NetworkGameForm networkGameForm = new NetworkGameForm(game); networkGameForm.ShowDialog(); if (networkGameForm.DialogResult == DialogResult.OK) { string username = networkGameForm.Username; int arenaNumber = networkGameForm.ArenaNumber; networkPlayer = new NetworkPlayer("127.0.0.1", 12345 /*+ arenaNumber*/); networkPlayer.Connect(); isNetworkGame = true; ResetGame(); isMyTurnInNetworkGame = true; } } private void ResetGame() { game.ResetBoard(); currentPlayer = Game.SquareState.X; isMyTurnInNetworkGame = false; ResetButtonLabels(); } private void Square_Click(object sender, EventArgs e) { Button button = sender as Button; string[] splitName = button.Name.Split('_'); int x = Int32.Parse(splitName[1]); int y = Int32.Parse(splitName[2]); if (game.GetState(x, y) != Game.SquareState.Empty) { return; } if (!isNetworkGame || isMyTurnInNetworkGame) { game.MakeMove(x, y, currentPlayer); button.Text = currentPlayer.ToString(); currentPlayer = (currentPlayer == Game.SquareState.X) ? Game.SquareState.O : Game.SquareState.X; } if (isNetworkGame) { networkPlayer.SendMove(x, y); isMyTurnInNetworkGame = false; } if (game.CheckForWinner()) { MessageBox.Show("Player " + currentPlayer.ToString() + " wins!"); game.ResetBoard(); ResetButtonLabels(); return; } if (game.IsBoardFull()) { MessageBox.Show("It's a draw!"); game.ResetBoard(); ResetButtonLabels(); return; } if (game.CheckForWinner()) { MessageBox.Show("Player " + currentPlayer.ToString() + " wins!"); game.ResetBoard(); ResetButtonLabels(); return; } if (game.IsBoardFull()) { MessageBox.Show("It's a draw!"); game.ResetBoard(); ResetButtonLabels(); } } private void ResetButtonLabels() { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { buttons[i, j].Text = ""; } } } } }