Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
4.9 kB
0
Indexable
Never
using System;
using System.Windows.Forms;
using static TicTacToe.Game;

namespace TicTacToe
{
    public partial class Form1 : Form
    {
        private const int SIZE = 5;
        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]);
                }
            }
            multiPlayerButton.Visible = false;
            vsAIButton.Visible = false;
        }

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

        private void VsAIButton_Click(object sender, EventArgs e)
        {
            gameMode = GameMode.AI;
            AIPlayer aiPlayer = new AIPlayer(game, AIPlayer.Difficulty.Easy, Game.SquareState.O); // set default difficulty to Easy
            game.SetAIPlayer(aiPlayer);  // set the AI player in the game
            ResetGame(gameMode);
        }



        private void ResetGame(GameMode gameMode)
        {
            game.ResetBoard();
            if (gameMode == GameMode.Local)
            {
                game.SetAIPlayer(null);
            }
            currentPlayer = Game.SquareState.X;
            UpdateBoard();
        }


        private void UpdateBoard()
        {
            for (int i = 0; i < SIZE; i++)
            {
                for (int j = 0; j < SIZE; j++)
                {
                    switch (game.GetState(i, j))
                    {
                        case Game.SquareState.X:
                            buttons[i, j].Text = "X";
                            break;
                        case Game.SquareState.O:
                            buttons[i, j].Text = "O";
                            break;
                        default:
                            buttons[i, j].Text = "";
                            break;
                    }
                }
            }
        }


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

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

            if (moveMade && gameMode == GameMode.AI)
            {
                game.MakeAIMove();
                UpdateBoard();
            }

            if (game.CheckForWinner())
            {
                MessageBox.Show("Player " + currentPlayer + " wins!");
                ResetGame(gameMode); // pass the current game mode
                return;
            }

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



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