Exemplo n.º 1
0
        public static nRecord MakeSaveRuleAnyRecord()
        {
            Game.NewGame(Players.O);
            while (true)
            {
                nBoard board  = Game.GetBoard();
                nPoint putPos = null;

                // 여기에서 규칙 우선순위별로 배열한다.
                putPos = GetCliffPos(board, Game.GetCurrentTurn());
                if (putPos == null)
                {
                    putPos = GetRandomVoidPos(board);
                }

                Game.PutNext(putPos, true);
                if (Game.IsGameEnd())
                {
                    break;
                }
            }
            var record = Game.GetRecord();

            return(record);
        }
Exemplo n.º 2
0
 /// <summary>
 /// 보드의 한 부분을 포지션으로 가져옵니다.
 /// </summary>
 /// <param name="position"></param>
 /// <returns></returns>
 public static nPiece GetPiece(nPoint position)
 {
     if (IsAvailablePosition(position) == false)
     {
         return(null);
     }
     return(board[position]);
 }
Exemplo n.º 3
0
 public static int[] GetWDLCount(nBoard board, nPoint position)
 {
     int[] WDL = new int[3];
     if (GetAIPieceArr(board) != null)
     {
         WDL[0] = GetAIPieceArr(board)[position.x, position.y].winCount;
         WDL[1] = GetAIPieceArr(board)[position.x, position.y].drawCount;
         WDL[2] = GetAIPieceArr(board)[position.x, position.y].loseCount;
     }
     return(WDL);
 }
Exemplo n.º 4
0
 public static float GetWinAverage(nBoard board, nPoint position)
 {
     if (GetAIPieceArr(board) != null)
     {
         return(GetAIPieceArr(board)[position.x, position.y].WinAverage);
     }
     else
     {
         return(0);
     }
 }
Exemplo n.º 5
0
        /// <summary>
        /// 다음 수를 AI가 선택해서 포지션을 반환합니다.
        /// </summary>
        /// <param name="board"></param>
        /// <returns></returns>
        public static nPoint GetPutNextPosition(nBoard board)
        {
            if (IsSameLineExist(board))
            {
                return(null);
            }
            if (Tool.IsFullBoard(board))
            {
                return(null);
            }
            // board에서 게임 끝날 라인이 있는지 확인한다.

            var AIBoardList = brain[board.PuttingCount];
            int listIndex   = 0;

            nPoint putPos;

            putPos = GetCliffPos(board, Game.GetCurrentTurn());

            if (putPos == null)
            {    // 이 보드를 가지고 있다면?
                if (IsBrainGettedBoard(board, AIBoardList, out listIndex))
                {
                    // 최고의 승률 포지션 구하기
                    float  bestWinAverage    = 0;
                    nPoint bestWinAveragePos = new nPoint();
                    Tool.DoTicTacToeArray((x, y) =>
                    {
                        if (board[x, y].player != Players.NULL)
                        {
                            return(true);
                        }

                        float nowWinAverage = AIBoardList[listIndex][x, y].WinAverage;
                        if (nowWinAverage >= bestWinAverage)
                        {
                            bestWinAverage      = nowWinAverage;
                            bestWinAveragePos.x = x;
                            bestWinAveragePos.y = y;
                        }
                        return(true);
                    });

                    putPos = bestWinAveragePos;
                }
                else
                {
                    putPos = GetAnyVoidPos(board);
                }
            }

            return(putPos);
        }
Exemplo n.º 6
0
        public nBoard(nBoard board)
        {
            Tool.DoTicTacToeArray((x, y) =>
            {
                pieces[x, y]          = new nPiece();
                pieces[x, y].position = new nPoint(x, y);
                pieces[x, y].player   = board[x, y].player;


                lastPutPosition = new nPoint(board.lastPutPosition.x, board.lastPutPosition.y);
                return(true);
            });
        }
Exemplo n.º 7
0
        private static bool IsCanNext(nPoint position)
        {
            if (board[position.x, position.y].player != Players.NULL)
            {
                return(false);
            }

            if (currentTurn == Players.NULL)
            {
                return(false);
            }

            return(true);
        }
Exemplo n.º 8
0
 private static bool IsAvailablePosition(nPoint position)
 {
     if (position == null)
     {
         return(false);
     }
     if (0 <= position.x && position.x < 3)
     {
         if (0 <= position.y && position.y < 3)
         {
             return(true);
         }
     }
     return(false);
 }
Exemplo n.º 9
0
        private static void AddKnowledge(nBoard board, nPoint lastPos, char WLD)
        {
            int    listIndex   = 0;
            var    AIBoardList = brain[board.PuttingCount - 1];
            nBoard prevBoard   = new nBoard(board);

            prevBoard[lastPos].player = Players.NULL;

            // 하나 뺀 보드를 주어야 한다.
            bool IsGetted = IsBrainGettedBoard(prevBoard, AIBoardList, out listIndex);


            // 같은 것이 있을 때
            if (IsGetted)
            {
                if (WLD == 'W')
                {
                    AIBoardList[listIndex][lastPos].winCount++;
                }
                else if (WLD == 'L')
                {
                    AIBoardList[listIndex][lastPos].loseCount++;
                }
                else if (WLD == 'D')
                {
                    AIBoardList[listIndex][lastPos].drawCount++;
                }
            }
            // 같은 것이 없을 때
            else
            {
                nAIBoard newAIBoard = new nAIBoard(prevBoard);
                if (WLD == 'W')
                {
                    newAIBoard[lastPos].winCount++;
                }
                else if (WLD == 'L')
                {
                    newAIBoard[lastPos].loseCount++;
                }
                else if (WLD == 'D')
                {
                    newAIBoard[lastPos].drawCount++;
                }

                AIBoardList.Add(newAIBoard);
            }
        }
Exemplo n.º 10
0
        protected static nPoint GetCliffPos(nBoard board, char currentTurnPlayer)
        {
            nPoint retValue     = null;
            var    cliffArrList = GetCliffLine(board);



            if (cliffArrList.Count != 0)
            {
                foreach (var piece in cliffArrList)
                {
                    char   cliffPlayer = Players.NULL;
                    nPoint voidPoint   = null;
                    for (int i = 0; i < 3; i++)
                    {
                        if (piece[i].player != Players.NULL)
                        {
                            cliffPlayer = piece[i].player;
                        }
                        else
                        {
                            voidPoint = piece[i].position;
                        }
                    }

                    // 위험한 라인이 자신의 것 일때
                    if (currentTurnPlayer == cliffPlayer)
                    {
                        retValue = voidPoint;
                    }
                    // 위험한 라인이 상대의 것 일때
                    else
                    {
                        if (retValue == null)
                        {
                            retValue = voidPoint;
                        }
                    }
                }
            }
            return(retValue);
        }
Exemplo n.º 11
0
        /// <summary>
        /// 다음 수를 둡니다.
        /// </summary>
        /// <param name="position"></param>
        /// <returns></returns>
        public static bool PutNext(nPoint position, bool withAIGame)
        {
            if (position == null)
            {
                return(false);
            }
            if (IsAvailablePosition(position) == false)
            {
                return(false);
            }


            if (IsCanNext(position) == true)
            {
                // 바뀌기 이전을 기록한다.
                board.lastPutPosition = position;

                board[position].player = currentTurn;

                Recorder.RecordNext(board);

                ChangeTurn();


                if (IsSameLineExist(board) || Tool.IsFullBoard(board))
                {
                    currentTurn = Players.NULL;
                    SetWL();
                    Recorder.RecordEndGame(winner, loser);

                    if (withAIGame == false)
                    {
                        AI.Learn(Recorder.GetRecord());
                    }
                }
            }
            else
            {
                return(false);
            }
            return(true);
        }
Exemplo n.º 12
0
 public nPiece this[nPoint position]
 {
     get { return(pieces[position.x, position.y]); }
 }
Exemplo n.º 13
0
 public nAIPiece this[nPoint position]
 {
     get { return(AIPieceArr[position.x, position.y]); }
 }