protected static nPiece[] GetLine(nBoard board, int line, char XYD) { nPiece[] retValue = new nPiece[3]; if (XYD == 'X') { retValue[0] = board[0, line]; retValue[1] = board[1, line]; retValue[2] = board[2, line]; } else if (XYD == 'Y') { retValue[0] = board[line, 0]; retValue[1] = board[line, 1]; retValue[2] = board[line, 2]; } else if (XYD == 'D') { if (line == 0) { retValue[0] = board[0, 0]; retValue[1] = board[1, 1]; retValue[2] = board[2, 2]; } else if (line == 1) { retValue[0] = board[2, 0]; retValue[1] = board[1, 1]; retValue[2] = board[0, 2]; } } return(retValue); }
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); }
protected static List <nPiece[]> GetCliffLine(nBoard board) { List <nPiece[]> cliffLineArrList = new List <nPiece[]>(); Tool.DoTicTacToeArray((x, y) => { nPiece[] lineArr; for (int i = 0; i < 3; i++) { lineArr = GetLine(board, i, 'X'); if (IsThisLineCliff(lineArr)) { cliffLineArrList.Add(lineArr); } lineArr = GetLine(board, i, 'Y'); if (IsThisLineCliff(lineArr)) { cliffLineArrList.Add(lineArr); } if (i < 2) { lineArr = GetLine(board, i, 'D'); if (IsThisLineCliff(lineArr)) { cliffLineArrList.Add(lineArr); } } } return(true); }); return(cliffLineArrList); }
public nAIBoard(nBoard board) { Tool.DoTicTacToeArray((x, y) => { nBoard newBoard = new nBoard(board); AIPieceArr[x, y] = new nAIPiece(); AIPieceArr[x, y].piece = newBoard[x, y]; return(true); }); }
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); }
public static float GetWinAverage(nBoard board, nPoint position) { if (GetAIPieceArr(board) != null) { return(GetAIPieceArr(board)[position.x, position.y].WinAverage); } else { return(0); } }
public static bool IsFullBoard(nBoard board) { foreach (var piece in board.pieces) { if (piece.player == Players.NULL) { return(false); } } return(true); }
protected static nPoint GetRandomVoidPos(nBoard board) { randIntSeed++; Random randSeed = new Random(); Random rand = new Random(randSeed.Next() * randIntSeed); var voidList = GetVoidPosList(board); return(voidList[rand.Next(voidList.Count)]); }
private static nAIPiece[,] GetAIPieceArr(nBoard board) { int listIndex = 0; var AIBoardList = brain[board.PuttingCount]; if (IsBrainGettedBoard(board, AIBoardList, out listIndex)) { return(AIBoardList[listIndex].AIPieceArr); } return(null); }
private static nBoard AIBoardToBoard(nAIBoard aiBoard) { nBoard newBoard = new nBoard(); Tool.DoTicTacToeArray((x, y) => { newBoard.pieces[x, y] = aiBoard[x, y].piece; return(true); }); return(newBoard); }
protected static bool IsSameLineExist(nBoard board) { if (Tool.GetSameLine(board) == null) { return(false); } else { return(true); } }
/// <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); }
/// <summary> /// 새로운 게임을 시작합니다. /// </summary> public static bool NewGame(char startPlayer) { if ((startPlayer != Players.O) && (startPlayer != Players.X)) { return(false); } currentTurn = startPlayer; board = new nBoard(); Recorder.NewRecord(); return(true); }
protected static bool IsSameLine(nBoard board, int line, char XYD) { var linePieces = GetLine(board, line, XYD); if (linePieces[0].player == linePieces[1].player && linePieces[1].player == linePieces[2].player) { if (linePieces[0].player != Players.NULL) { return(true); } } return(false); }
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); }); }
protected static List <nPoint> GetVoidPosList(nBoard board) { List <nPoint> posList = new List <nPoint>(); Tool.DoTicTacToeArray((x, y) => { if (board[x, y].player == Players.NULL) { posList.Add(new nPoint(x, y)); } return(true); }); return(posList); }
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); } }
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); }
private static bool IsBrainGettedBoard(nBoard board, List <nAIBoard> AIBoardList, out int listIndex) { bool IsSame = false; listIndex = 0; // 보드의 O X 반전 for (int reverse = 0; reverse < 2; reverse++) { for (int i = 0; i < AIBoardList.Count; i++) { IsSame = true; for (int y = 0; y < 3; y++) { for (int x = 0; x < 3; x++) { char AIPlayer = AIBoardList[i][x, y].piece.player; if (reverse == 1) { AIPlayer = GetOtherPlayer(AIPlayer); } if (AIPlayer != board[x, y].player) { IsSame = false; goto BreakXY; } } } if (IsSame == true) { listIndex = i; return(true); } BreakXY :; } } return(false); }
public static nPiece[] GetSameLine(nBoard board) { for (int i = 0; i < 3; i++) { if (IsSameLine(board, i, 'X')) { return(GetLine(board, i, 'X')); } else if (IsSameLine(board, i, 'Y')) { return(GetLine(board, i, 'Y')); } else if (i < 2) { if (IsSameLine(board, i, 'D')) { return(GetLine(board, i, 'D')); } } } return(null); }
public static void RecordNext(nBoard board) { record.AddRecord(board); }
protected static nPoint GetAnyVoidPos(nBoard board) { return(GetVoidPosList(board)[0]); }
public void AddRecord(nBoard board) { nBoard newBoard = new nBoard(board); recordList.Add(newBoard); }