활동/내일배움캠프 Unity

[내배캠] TIL 틱택토 만들기

김진우 개발일지 2024. 9. 19. 13:12

C# 텍스트 틱택토 전체 코드

using System;

namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int boardX = 3;
            int boardY = 3;
            TicTacToe ticTacToe = new TicTacToe(boardX, boardY);

            TicTacToe.BoardState turn = TicTacToe.BoardState.Player1;
            while (true)
            {
                ticTacToe.PrintBoard();

                Console.Write("Select Num(1~9) : ");
                int selectIndex = int.Parse(Console.ReadLine()) - 1;
                if (!ticTacToe.SetBoardState(turn, selectIndex))
                {
                    Console.WriteLine("You choose wrong number");
                } 
                else if(ticTacToe.IsWinOnIndex(selectIndex))
                {
                    string playerName = turn == TicTacToe.BoardState.Player1 ? "Player1" : "Player2";
                    Console.Write($"{playerName} is win");
                    break;
                }
                else if(turn == TicTacToe.BoardState.Player1)
                {
                    turn = TicTacToe.BoardState.Player2;
                }
                else
                {
                    turn = TicTacToe.BoardState.Player1;
                }
            }
        }
    }

    class TicTacToe
    {
        public enum BoardState
        {
            None = 0,
            Player1,
            Player2
        }
        BoardState[,] board;
        public TicTacToe(int x, int y)
        {
            board = new BoardState[x, y];
        }
        public void PrintBoard()
        {
            int x = board.GetLength(0);
            int y = board.GetLength(1);
            for (int i = 0; i < x; i++)
            {
                for (int j = 0; j < y; j++)
                {
                    if (board[j, i] == BoardState.Player1)
                    {
                        Console.Write("o ");
                    }
                    else if (board[j, i] == BoardState.Player2)
                    {
                        Console.Write("x ");
                    }
                    else
                    {
                        Console.Write("_ ");
                    }
                }
                Console.WriteLine();
            }
        }

        public bool SetBoardState(BoardState state, int index)
        {
            int x = index % board.GetLength(0);
            int y = index / board.GetLength(0);
            if (board[x, y] != BoardState.None)
            {
                return false;
            }

            board[x, y] = state;
            return true;
        }

        int GetCountOfSameStateByDirection(int currentX, int currentY, int directionX, int directionY)
        {
            if ((currentX == 0 && directionX == -1) ||
                (currentY == 0 && directionY == -1) ||
                (currentX == board.GetLength(0)-1 && directionX == 1) ||
                (currentY == board.GetLength(1)-1 && directionY == 1))
            {
                return 0;
            }

            int nextStateX = currentX + directionX;
            int nextStateY = currentY + directionY;
            if (board[currentX, currentY] == board[nextStateX, nextStateY])
            {
                return 1 + GetCountOfSameStateByDirection(nextStateX, nextStateY, directionX, directionY);
            }
            else
            {
                return GetCountOfSameStateByDirection(nextStateX, nextStateY, directionX, directionY);
            }
        }

        public bool IsWinOnIndex(int index)
        {
            int boardX = board.GetLength(0);
            int boardY = board.GetLength(1);
            int x = index % boardX;
            int y = index / boardX;

            if (GetCountOfSameStateByDirection(x, y, -1, 0) + GetCountOfSameStateByDirection(x, y, 1, 0) + 1 == boardX ||
                GetCountOfSameStateByDirection(x, y, 0, -1) + GetCountOfSameStateByDirection(x, y, 0, 1) + 1 == boardY)
            { // check horizontal and vertical
                return true;
            }

            //check diagonal 1->9
            int sameStateCount = GetCountOfSameStateByDirection(x, y, -1, -1) + GetCountOfSameStateByDirection(x, y, 1, 1) + 1;
            if(sameStateCount == boardX || sameStateCount == boardY)
            {
                return true;
            }

            //check diagonal 7->3
            sameStateCount = GetCountOfSameStateByDirection(x, y, -1, 1) + GetCountOfSameStateByDirection(x, y, 1, -1) + 1;
            if (sameStateCount == boardX || sameStateCount == boardY)
            {
                return true;
            }

            return false;
        }
    }
}