Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.6 kB
0
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 Game game;
        private Game.SquareState currentPlayer;

        private GameMode gameMode;



        public Form1()
        {
            InitializeComponent();
            game = new Game();
            GameModeForm gameModeForm = new GameModeForm(game);
            gameModeForm.ShowDialog();
            buttons = new Button[SIZE, SIZE];
            game = new Game();
            currentPlayer = Game.SquareState.X;



            multiPlayerButton = new Button();
            multiPlayerButton.Text = "Multi Player";
            multiPlayerButton.Click += MultiPlayerButton_Click;
            multiPlayerButton.SetBounds(200, 300, 100, 50);

          


            this.Controls.Add(multiPlayerButton);
            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 MultiPlayerButton_Click(object sender, EventArgs e)
        {
            ResetGame();
        }

       

        private void ResetGame()
        {
            game.ResetBoard();
            currentPlayer = Game.SquareState.X;
             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 (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 = "";
                }
            }
        }
    }
}