Doing another fundamental c# run

Touching up on the fundamentals of c# again by going over a few courses that I bought on Udemy, but never got around. Quick and dirty Tic Tac Toe, bare minimum functional.

internal class Board
{
    string[,] _board;
    bool _isPlayer1 = true;
    string _playerName = "Player1";
    string _playerTic = "O";
    string _playerSymbol;
    

    public char Selection { get; set; }

    public string[,] GameBoard
    {
        get => _board;
        set => _board = value;
    }

    public Board()
    {
        _board = new string[,]        
        {
            { "1", "2", "3" },
            { "4", "5", "6" },
            { "7", "8", "9" }
        };
    }

    public void Run()
    {
        Console.WriteLine("Welcome to your tic tac toe game.");
        for (int i = 0; i < GameBoard.GetLength(0); i++)
        {
            for (int j = 0; j < GameBoard.GetLength(1); j++)
            {
                Console.Write("_" + GameBoard[i, j] + "_|");
            }
            Console.WriteLine("");
        }
    }

    public void ShowBoard()
    {
        for (int i = 0; i < GameBoard.GetLength(0); i++)
        {
            for (int j = 0; j < GameBoard.GetLength(1); j++)
            {
                Console.Write("_" + GameBoard[i, j] + "_|");
            }
            Console.WriteLine("");
        }
    }

    public void Start()
    {
        Console.WriteLine($"{_playerName}: It's your turn (enter #): ");
        Selection = Console.ReadKey().KeyChar;
        Console.WriteLine("_____________________");
        Console.WriteLine();
        Console.WriteLine();
        Select();

    }

    public void Select()
    {
        for (int i = 0; i < GameBoard.GetLength(0); i++)
        {
            for (int j = 0; j < GameBoard.GetLength(1); j++)
            {
                if (GameBoard[i, j] == Selection.ToString())
                {
                    GameBoard[i, j] = _playerTic;
                }
            }
        }

        WinCheck();
        ShowBoard();

        _isPlayer1 = !_isPlayer1;

        if (!_isPlayer1)
        {
            _playerName = "Player2";
            _playerTic = "X";
        }
        else
        {
            _playerName = "Player1";
            _playerTic = "O";
        }
        
        Start();
    }

    public void WinCheck()
    {
        _playerSymbol = _isPlayer1 ? "O" : "X";

        if (GameBoard[0, 0] == _playerSymbol && GameBoard[0, 1] == _playerSymbol &&
            GameBoard[0, 2] == _playerSymbol ||
            GameBoard[1, 0] == _playerSymbol && GameBoard[1, 1] == _playerSymbol &&
            GameBoard[1, 2] == _playerSymbol ||
            GameBoard[2, 0] == _playerSymbol && GameBoard[2, 1] == _playerSymbol &&
            GameBoard[2, 2] == _playerSymbol ||

            GameBoard[0, 0] == _playerSymbol && GameBoard[1, 0] == _playerSymbol &&
            GameBoard[2, 0] == _playerSymbol ||
            GameBoard[0, 1] == _playerSymbol && GameBoard[1, 1] == _playerSymbol &&
            GameBoard[2, 1] == _playerSymbol ||
            GameBoard[2, 0] == _playerSymbol && GameBoard[1, 2] == _playerSymbol &&
            GameBoard[2, 2] == _playerSymbol ||

            GameBoard[0, 0] == _playerSymbol && GameBoard[1, 1] == _playerSymbol &&
            GameBoard[2, 2] == _playerSymbol ||
            GameBoard[0, 2] == _playerSymbol && GameBoard[1, 1] == _playerSymbol &&
            GameBoard[2, 0] == _playerSymbol)
        {
            if (_isPlayer1)
            {
                Console.WriteLine("=============");
                Console.WriteLine("Player1 Wins!");
                Console.WriteLine("=============");
                Environment.Exit(0);
            }
            else
            {
                Console.WriteLine("=============");
                Console.WriteLine("Player2 Wins!");
                Console.WriteLine("=============");
                Environment.Exit(0);
            }
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *