public override bool GetNextMove(TicTacToeSign sign, out int row, out int column) { Steps = 0; row = -1; column = -1; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { Steps++; if (board.CorrectMove(i, j)) { row = i; column = j; break; } } if (row != -1) { break; } } return(row != -1); }
public override bool GetNextMove(TicTacToeSign sign, out int row, out int column) { Steps = 0; row = -1; column = -1; List <Position> plist = new List <Position>(); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { Steps++; if (board.CorrectMove(i, j)) { plist.Add(new Position(i, j)); } } } if (plist.Count != 0) { Random rnd = new Random(); Position p = plist[rnd.Next(plist.Count)]; row = p.Row; column = p.Column; } return(plist.Count != 0); }
public override string ToString() { StringBuilder table = new StringBuilder(32); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { TicTacToeSign sign = board[i, j]; if (sign == TicTacToeSign.Sign_X) { table.Append('X'); } else if (sign == TicTacToeSign.Sign_O) { table.Append('O'); } else { table.Append(' '); } } table.Append(Environment.NewLine); } return(table.ToString()); }
public override bool GetNextMove(TicTacToeSign sign, out int row, out int column) { Steps = 0; row = -1; column = -1; int rowtmp = -1; int columntmp = -1; int max = -100; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (board.CorrectMove(i, j)) { Steps++; board.SetSign(i, j, sign); if (board.Winner() != TicTacToeResult.None) { row = i; column = j; board.SetSign(i, j, TicTacToeSign.Empty); break; } else { int e = board.FreeDirection(sign) - board.FreeDirection(TicTacToeHelper.Opponent(sign)); if (e > max) { max = e; rowtmp = i; columntmp = j; } } board.SetSign(i, j, TicTacToeSign.Empty); } } if (row != -1) break; } if (row == -1) { row = rowtmp; column = columntmp; } return (row != -1); }
void NextMove(GameAIAbstract gameAI, TicTacToeSign sign) { int row; int column; stopWatch.Restart(); if (gameAI.GetNextMove(sign, out row, out column)) { stopWatch.Stop(); TextBlockAITime.Text = stopWatch.ElapsedMilliseconds.ToString() + " ms"; if (gameAI is IGameAIMinimax) { TextBlockAISteps.Text = gameAI.Steps.ToString(); int bestResult = (gameAI as IGameAIMinimax).GetBestResult(); string bestText; switch (bestResult) { case -1: bestText = "A gépi játékos veszíteni fog"; break; case 0: bestText = "A gépi játékos döntetlent ér el"; break; case 1: bestText = "A gépi játékos győzni fog"; break; default: bestText = "???"; break; } TextBlockAIBestResult.Text = bestText; } playingBoard.SetSign(row, column, sign); gameBoard.SetSign(row, column, sign); TicTacToeResult winner = gameBoard.Winner(); if (winner != TicTacToeResult.None) { AnnounceWinner(winner); } } else { MessageBox.Show("Nincs következő lépés..."); } }
public int FreeDirection(TicTacToeSign sign) { if (sign == TicTacToeSign.Sign_X) { return(FreeX()); } else { return(FreeO()); } }
public TicTacToeResult Winner() { TicTacToeResult res = TicTacToeResult.None; TicTacToeSign sign = TicTacToeSign.Empty; if (((board[0, 0] == board[1, 1] && board[0, 0] == board[2, 2]) || (board[0, 0] == board[0, 1] && board[0, 0] == board[0, 2]) || (board[0, 0] == board[1, 0] && board[0, 0] == board[2, 0])) && board[0, 0] != TicTacToeSign.Empty) { sign = board[0, 0]; } else if (((board[0, 2] == board[1, 1] && board[0, 2] == board[2, 0]) || (board[0, 2] == board[1, 2] && board[0, 2] == board[2, 2])) && board[0, 2] != TicTacToeSign.Empty) { sign = board[0, 2]; } else if (board[1, 0] == board[1, 1] && board[1, 0] == board[1, 2] && board[1, 0] != TicTacToeSign.Empty) { sign = board[1, 0]; } else if (board[2, 0] == board[2, 1] && board[2, 0] == board[2, 2] && board[2, 0] != TicTacToeSign.Empty) { sign = board[2, 0]; } else if (board[0, 1] == board[1, 1] && board[0, 1] == board[2, 1] && board[0, 1] != TicTacToeSign.Empty) { sign = board[0, 1]; } if (sign == TicTacToeSign.Empty) { if (EmptyCount() == 0) { res = TicTacToeResult.Draw; } } else if (sign == TicTacToeSign.Sign_X) { res = TicTacToeResult.Player_X; } else { res = TicTacToeResult.Player_O; } return(res); }
public static TicTacToeSign Opponent(TicTacToeSign sign) { if (sign == TicTacToeSign.Sign_X) { return(TicTacToeSign.Sign_O); } else if (sign == TicTacToeSign.Sign_O) { return(TicTacToeSign.Sign_X); } else { return(TicTacToeSign.Empty); } }
private int PlayMax(TicTacToeBoard prevBoard, TicTacToeSign sign) { int max = -1; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (prevBoard.CorrectMove(i, j)) { TicTacToeBoard newBoard = new TicTacToeBoard(prevBoard); newBoard.SetSign(i, j, sign); Steps++; TicTacToeResult result = newBoard.Winner(); if (result != TicTacToeResult.None) { if (result == TicTacToeResult.Draw) { if (max < 0) { max = 0; } } else { max = 1; } } else { int max1 = PlayMin(newBoard, TicTacToeHelper.Opponent(sign)); if (max1 > max) { max = max1; } } } } } return(max); }
public void Set(int y, int x, TicTacToeSign sign) { switch (sign) { case TicTacToeSign.X: Field[y, x] = 1; Buttons[y, x].Action.Label = "[X]"; Buttons[y, x].Action.Payload = "{\"button\":\"S" + y + "_" + x + "\"}"; Buttons[y, x].Color = KeyboardButtonColor.Primary; break; case TicTacToeSign.O: Field[y, x] = 2; Buttons[y, x].Action.Label = "[O]"; Buttons[y, x].Action.Payload = "{\"button\":\"S" + y + "_" + x + "\"}"; Buttons[y, x].Color = KeyboardButtonColor.Negative; break; } }
public override bool GetNextMove(TicTacToeSign sign, out int row, out int column) { Steps = 0; row = -1; column = -1; for (int i = 0; i < bestMoves.Count; i++) { Steps++; if (board.GetSign(bestMoves[i].Row, bestMoves[i].Column) == TicTacToeSign.Empty) { row = bestMoves[i].Row; column = bestMoves[i].Column; break; } } return(row != -1); }
public void SetSign(TicTacToeSign sign) { switch (sign) { case TicTacToeSign.Empty: TextBlockSign.Text = " "; break; case TicTacToeSign.Sign_X: TextBlockSign.Text = "X"; TextBlockSign.Foreground = new SolidColorBrush(Colors.Black); break; case TicTacToeSign.Sign_O: TextBlockSign.Text = "O"; TextBlockSign.Foreground = new SolidColorBrush(Colors.Red); break; default: break; } }
private void NewGame() { gameBoard.Reset(); playingBoard.Reset(); CreateAI(); TextBlockAITime.Text = ""; TextBlockAISteps.Text = ""; TextBlockAIBestResult.Text = ""; TextBlockResult.Text = ""; if (gameAIX != null) { playerSign = TicTacToeSign.Sign_O; NextMove(gameAIX, TicTacToeSign.Sign_X); } else { playerSign = TicTacToeSign.Sign_X; } }
public override bool GetNextMove(TicTacToeSign sign, out int row, out int column) { Steps = 0; row = -1; column = -1; // Ha egy adott lépéssel megnyerné a gép a játékot, akkor ezt a lépést kell választani for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { Steps++; if (board.GetSign(i, j) == TicTacToeSign.Empty) { board.SetSign(i, j, sign); if (board.Winner() != TicTacToeResult.None) { row = i; column = j; board.SetSign(i, j, TicTacToeSign.Empty); break; } board.SetSign(i, j, TicTacToeSign.Empty); } } if (row != -1) { break; } } if (row != -1) { return(true); } // Ha egy adott lépéssel az ellenfél nyerné meg a játékot, akkor erre a helyre lépve blokkolni kell az ő lépését for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { Steps++; if (board.GetSign(i, j) == TicTacToeSign.Empty) { board.SetSign(i, j, TicTacToeHelper.Opponent(sign)); if (board.Winner() != TicTacToeResult.None) { row = i; column = j; board.SetSign(i, j, TicTacToeSign.Empty); break; } board.SetSign(i, j, TicTacToeSign.Empty); } } } if (row != -1) { return(true); } // Egyébként táblázat alapján választunk az üres helyek közül for (int i = 0; i < bestMoves.Count; i++) { Steps++; if (board.GetSign(bestMoves[i].Row, bestMoves[i].Column) == TicTacToeSign.Empty) { row = bestMoves[i].Row; column = bestMoves[i].Column; break; } } return(row != -1); }
public void SetSign(int row, int column, TicTacToeSign sign) { board[row, column] = sign; }
public abstract bool GetNextMove(TicTacToeSign sign, out int row, out int column);
public void SetSign(int position, TicTacToeSign sign) { board[(int)(position / 3), position % 3] = sign; }
public void SetSign(int row, int column, TicTacToeSign sign) { fields[row, column].SetSign(sign); }
public override bool GetNextMove(TicTacToeSign sign, out int row, out int column) { row = -1; column = -1; Steps = 0; int max = -1; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (board.CorrectMove(i, j)) { TicTacToeBoard newBoard = new TicTacToeBoard(board); newBoard.SetSign(i, j, sign); Steps++; TicTacToeResult result = newBoard.Winner(); if (result != TicTacToeResult.None) { if (result == TicTacToeResult.Draw) { if (max < 0) { max = 0; row = i; column = j; } } else { max = 1; row = i; column = j; } } else { int max1 = PlayMin(newBoard, TicTacToeHelper.Opponent(sign)); if (max1 > max) { max = max1; row = i; column = j; } } } } } Debug.WriteLine(String.Format("Sor: {0} Oszlop: {1} Érték: {2} Vizsgálatok: {3}", row, column, max, Steps)); // MessageBox.Show(String.Format("Sor: {0} Oszlop: {1} Érték: {2} Vizsgálatok: {3}", row, column, max, count)); BestResult = max; return(row != -1); }