using System; using System.Windows.Forms; namespace TicTacToe { public partial class Form1 : Form { private const int SIZE = 3; private Button[,] buttons; private Button singlePlayerButton; private Button multiPlayerButton; private Button networkPlayerButton; private Game game; private Game.SquareState currentPlayer; private AIPlayer aiPlayer; private NetworkPlayer networkPlayer; private bool isNetworkGame; private bool isMyTurnInNetworkGame; public Form1() { InitializeComponent(); buttons = new Button[SIZE, SIZE]; game = new Game(); currentPlayer = Game.SquareState.X; aiPlayer = null; networkPlayer = null; isNetworkGame = false; isMyTurnInNetworkGame = false; singlePlayerButton = new Button(); singlePlayerButton.Text = "Single Player"; singlePlayerButton.Click += SinglePlayerButton_Click; singlePlayerButton.SetBounds(50, 300, 100, 50); 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(singlePlayerButton); this.Controls.Add(multiPlayerButton); this.Controls.Add(networkPlayerButton); this.Load += new EventHandler(Form1_Load); } 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]); } } } private void SinglePlayerButton_Click(object sender, EventArgs e) { aiPlayer = new AIPlayer(game, currentPlayer); networkPlayer = null; isNetworkGame = false; ResetGame(); } private void MultiPlayerButton_Click(object sender, EventArgs e) { aiPlayer = null; networkPlayer = null; isNetworkGame = false; ResetGame(); } private void NetworkPlayerButton_Click(object sender, EventArgs e) { // Here you should add code for creating and setting up the NetworkPlayer aiPlayer = null; networkPlayer = new NetworkPlayer("127.0.0.1", 12345); // Replace with your network settings isNetworkGame = true; ResetGame(); } 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 (currentPlayer == Game.SquareState.O && aiPlayer != null && !isNetworkGame) // AI turn { (int aiX, int aiY) = aiPlayer.MakeMove(); Button aiButton = buttons[aiX, aiY]; aiButton.Text = currentPlayer.ToString(); currentPlayer = Game.SquareState.X; 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 = ""; } } } } }