Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.1 kB
0
Indexable
Never
using System;
using System.Collections.Generic;

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

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

        public void ResetBoard()
        {
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; 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 SquareState GetState(int x, int y)
        {
            return board[x, y];
        }

        public bool IsBoardFull()
        {
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; 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 < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    if (board[i, j] == SquareState.Empty)
                    {
                        availableMoves.Add((i, j));
                    }
                }
            }
            return availableMoves;
        }
    }
}