Untitled

 avatar
unknown
plain_text
2 years ago
4.5 kB
2
Indexable
using System;
using System.Collections.Generic;

namespace TicTacToe
{
    public class Game
    {
        public enum SquareState { Empty, X, O }
        private SquareState[,] board;
        private GameMode gameMode;
        private const int SIZE = 5;

        public Game()
        {
            board = new SquareState[SIZE, SIZE];
            ResetBoard();
        }
        public void SetGameMode(GameMode gameMode)
        {
            this.gameMode = gameMode;
        }

        public void ResetBoard()
        {
            for (int i = 0; i < SIZE; i++)
            {
                for (int j = 0; j < SIZE; j++)
                {
                    board[i, j] = SquareState.Empty;
                }
            }
        }

        public bool MakeMove(int x, int y, SquareState state)
        {
            if (board[x, y] == SquareState.Empty)
            {
                board[x, y] = state;
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool MakeAIMove()
        {
            if (gameMode != GameMode.AI)
            {
                return false;
            }

            var aiMove = GetBestMove();
            if (aiMove.Item1 == -1 && aiMove.Item2 == -1)
            {
                return false;  // no available moves
            }

            board[aiMove.Item1, aiMove.Item2] = SquareState.O;
            return true;
        }

        public SquareState GetState(int x, int y)
        {
            return board[x, y];
        }

        public bool IsBoardFull()
        {
            for (int i = 0; i < SIZE; i++)
            {
                for (int j = 0; j < SIZE; j++)
                {
                    if (board[i, j] == SquareState.Empty)
                    {
                        return false;
                    }
                }
            }
            return true;
        }

        public bool CheckForWinner()
        {
            // horizontal checks
            for (int i = 0; i < 3; i++)
            {
                if (board[i, 0] != SquareState.Empty && board[i, 0] == board[i, 1] && board[i, 0] == board[i, 2])
                {
                    return true;
                }
            }
            // vertical checks
            for (int i = 0; i < 3; i++)
            {
                if (board[0, i] != SquareState.Empty && board[0, i] == board[1, i] && board[0, i] == board[2, i])
                {
                    return true;
                }
            }
            // diagonal checks
            if (board[0, 0] != SquareState.Empty && board[0, 0] == board[1, 1] && board[0, 0] == board[2, 2])
            {
                return true;
            }
            if (board[0, 2] != SquareState.Empty && board[0, 2] == board[1, 1] && board[0, 2] == board[2, 0])
            {
                return true;
            }
            return false;
        }

        public List<(int, int)> GetAvailableMoves()
        {
            List<(int, int)> availableMoves = new List<(int, int)>();
            for (int i = 0; i < SIZE; i++)
            {
                for (int j = 0; j < SIZE; j++)
                {
                    if (board[i, j] == SquareState.Empty)
                    {
                        availableMoves.Add((i, j));
                    }
                }
            }
            return availableMoves;
        }

        public (int, int) GetBestMove()
        {
            var availableMoves = GetAvailableMoves();
            if (availableMoves.Count == 0)
            {
                return (-1, -1);  // no available moves
            }

            // Check if there's a winning move
            foreach (var move in availableMoves)
            {
                board[move.Item1, move.Item2] = SquareState.O;
                if (CheckForWinner())
                {
                    board[move.Item1, move.Item2] = SquareState.Empty;  // reset the move
                    return move;
                }
                board[move.Item1, move.Item2] = SquareState.Empty;  // reset the move
            }

            // If no winning move, return a random move
            var random = new Random();
            return availableMoves[random.Next(availableMoves.Count)];
        }
    }
}
Editor is loading...