Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
4.8 kB
2
Indexable
using System;
using System.Windows.Forms;
using static TicTacToe.Game;

namespace TicTacToe
{
    public partial class Form1 : Form
    {
        private const int SIZE = 3;
        private Button[,] buttons;
        private Button multiPlayerButton;
        private Button vsAIButton;
        private Game game;
        private Game.SquareState currentPlayer;
        private GameMode gameMode;

        public Form1()
        {
            InitializeComponent();
            game = new Game();
            GameModeForm gameModeForm = new GameModeForm(game);
            gameModeForm.ShowDialog();
            gameMode = gameModeForm.gameMode; // Přidáno
            buttons = new Button[SIZE, SIZE];
            currentPlayer = Game.SquareState.X;

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

            vsAIButton = new Button();
            vsAIButton.Text = "AI Game";
            vsAIButton.Click += VsAIButton_Click;
            vsAIButton.SetBounds(200, 350, 100, 50);

            this.Controls.Add(multiPlayerButton);
            this.Controls.Add(vsAIButton);
            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)
        {
            gameMode = GameMode.Local;
            ResetGame();
        }

        private void VsAIButton_Click(object sender, EventArgs e)
        {
            gameMode = GameMode.AI;
            ResetGame();
        }

        private void ResetGame()
        {
            game.ResetBoard();
            currentPlayer = Game.SquareState.X;
            ResetButtonLabels();
        }

       /* private void UpdateBoard()
        {
            for (int i = 0; i < SIZE; i++)
            {
                for (int j = 0; j < SIZE; j++)
                {
                    buttons[i, j].Text = game.GetState(i, j).ToString();
                }
            }
        }*/

        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;
            }

            game.MakeMove(x, y, currentPlayer);
            button.Text = currentPlayer.ToString();

            if (game.CheckForWinner())
            {
                MessageBox.Show("Player " + currentPlayer.ToString() + " wins!");
                ResetGame();
                return;
            }
            if (game.IsBoardFull())
            {
                MessageBox.Show("It's a draw!");
                ResetGame();
                return;
            }

            currentPlayer = currentPlayer == Game.SquareState.X ? Game.SquareState.O : Game.SquareState.X;

            if (gameMode == GameMode.AI && currentPlayer == Game.SquareState.O)
            {
                var aiMove = game.GetBestMove();
                game.MakeMove(aiMove.Item1, aiMove.Item2, currentPlayer);
                buttons[aiMove.Item1, aiMove.Item2].Text = currentPlayer.ToString();
                currentPlayer = Game.SquareState.X;

                if (game.CheckForWinner())
                {
                    MessageBox.Show("AI wins!");
                    ResetGame();
                    return;
                }
                if (game.IsBoardFull())
                {
                    MessageBox.Show("It's a draw!");
                    ResetGame();
                    return;
                }
            }
        }



        private void ResetButtonLabels()
        {
            for (int i = 0; i < SIZE; i++)
            {
                for (int j = 0; j < SIZE; j++)
                {
                    buttons[i, j].Text = "";
                }
            }
        }
    }
}