public TicTacToeState() { Fields = new TicTacToeFieldState[3][]; Fields[0] = new TicTacToeFieldState[3]; Fields[1] = new TicTacToeFieldState[3]; Fields[2] = new TicTacToeFieldState[3]; }
private static Int32 GetSign(TicTacToeFieldState state) { if (state == TicTacToeFieldState.Cross) { return(1); } else if (state == TicTacToeFieldState.Circle) { return(-1); } else { throw new ArgumentException(state.ToString()); } }
public Int32 Evaluate(IGameState gameState, GamePlayer player) { TicTacToeState state = (TicTacToeState)gameState; for (Int32 q = 0; q < Lines.Length; q++) { Int32 crosses = 0; Int32 circles = 0; Coords[] line = Lines[q]; for (Int32 w = 0; w < line.Length; w++) { Coords coords = line[w]; TicTacToeFieldState fieldState = state.Fields[coords.X][coords.Y]; if (fieldState == TicTacToeFieldState.Cross) { crosses++; } else if (fieldState == TicTacToeFieldState.Circle) { circles++; } } if (crosses == 3) { return(WinValue * GetSign(TicTacToeFieldState.Cross)); } if (circles == 3) { return(WinValue * GetSign(TicTacToeFieldState.Circle)); } } return(0); }
//this function asks the helper about the game state void ShowGameState() { //first we need to convert the text to a form that the helper understands char[] text = label5.Text.ToCharArray(); List <TicTacToeFieldState> board = new List <TicTacToeFieldState>(); for (int i = 0; i < text.Length; i++) { try { TicTacToeFieldState fieldState = TicTacToeHelper.GetFieldFromChar(text[i]); board.Add(fieldState); } catch { } } //get the game state TicTacToeGameState gameState = helper.CheckGameState(board.ToArray()); if (gameState == TicTacToeGameState.Continuable) { label7.Text = "game is continuable."; } else if (gameState == TicTacToeGameState.Tied) { label7.Text = "game is tied."; } else if (gameState == TicTacToeGameState.XWon) { label7.Text = "x has won the game."; } else if (gameState == TicTacToeGameState.OWon) { label7.Text = "o has won the game."; } }
public TicTacToeGameState CheckGameState(TicTacToeFieldState[] board) { if (board.Length != BoardLength * BoardLength) { throw new ArgumentOutOfRangeException("board", "The array should be as long as the board!"); } //first assume the game is tied TicTacToeGameState gameState = TicTacToeGameState.Tied; //check if the game is continuable for (int i = 0; i < board.Length; i++) { //if there's an empty field, the game is continuable. if (board[i] == TicTacToeFieldState.None) { gameState = TicTacToeGameState.Continuable; break; } } //check if x or o has won the game for (int i = 0; i < wins.GetLength(0); i++) { TicTacToeFieldState firstField = board[wins[i, 0]]; //no need to do the win check if the field we picked is empty if (firstField != TicTacToeFieldState.None) { //assume this is going to be a victory line bool victory = true; for (int k = 1; k < wins.GetLength(1); k++) { TicTacToeFieldState field = board[wins[i, k]]; //if selected field isn't the same as the first picked one, //then this wasn't a winning line. if (firstField != field) { victory = false; break; } } //if a winning line was found, change gameState if (victory) { if (firstField == TicTacToeFieldState.X) { gameState = TicTacToeGameState.XWon; } else { gameState = TicTacToeGameState.OWon; } break; } } } //the true game state is found and we can return it return(gameState); }
public Int32 Evaluate(IGameState gameState, IGameMove gameMove, IGameState newGameState) { TicTacToeState state = (TicTacToeState)newGameState; TicTacToeMove move = (TicTacToeMove)gameMove; Int32 sign = GetSign(move.Symbol); #region horizontal lines Int32 verticalCounter = 0; Int32 verticalOponentCounter = 0; for (Int32 q = 0; q < 3; q++) { TicTacToeFieldState field = state.Fields[q][move.Y]; if (field == move.Symbol) { verticalCounter++; } else if (field != TicTacToeFieldState.Empty) { verticalOponentCounter++; } } if (verticalCounter >= 3) { return(WinValue * sign); } if (verticalOponentCounter >= 3) { return(WinValue * (-sign)); } #endregion horizontal lines #region vertical lines Int32 horizontalCounter = 0; Int32 horizontalOponentCounter = 0; for (Int32 q = 0; q < 3; q++) { TicTacToeFieldState field = state.Fields[move.X][q]; if (field == move.Symbol) { horizontalCounter++; } else if (field != TicTacToeFieldState.Empty) { horizontalOponentCounter++; } } if (horizontalCounter >= 3) { return(WinValue * sign); } if (horizontalOponentCounter >= 3) { return(WinValue * -sign); } #endregion vertical lines #region diagonal lines Int32 diagonalCounterA = 0; Int32 oponentDiagonalCounterA = 0; if (move.X == move.Y) //// check diagonal A { Coords[] diagonal = Lines[0]; for (Int32 q = 0; q < 3; q++) { Coords coords = diagonal[q]; TicTacToeFieldState field = state.Fields[coords.X][coords.Y]; if (field == move.Symbol) { diagonalCounterA++; } else if (field != TicTacToeFieldState.Empty) { oponentDiagonalCounterA++; } } } if (diagonalCounterA >= 3) { return(WinValue * sign); } if (oponentDiagonalCounterA >= 3) { return(WinValue * -sign); } Int32 diagonalCounterB = 0; Int32 oponentDiagonalCounterB = 0; if (move.X + move.Y == 2) //// check diagonal B { Coords[] diagonal = Lines[1]; for (Int32 q = 0; q < 3; q++) { Coords coords = diagonal[q]; TicTacToeFieldState field = state.Fields[coords.X][coords.Y]; if (field == move.Symbol) { diagonalCounterB++; } else if (field != TicTacToeFieldState.Empty) { oponentDiagonalCounterB++; } } } if (diagonalCounterB >= 3) { return(WinValue * sign); } if (oponentDiagonalCounterB >= 3) { return(WinValue * -sign); } #endregion diagonal lines if (horizontalOponentCounter == 2 && horizontalCounter == 1) { return(DefendingBonus * sign);; } if (verticalOponentCounter == 2 && verticalCounter == 1) { return(DefendingBonus * sign); } if (oponentDiagonalCounterB == 2 && diagonalCounterB == 1) { return(DefendingBonus * sign); } if (oponentDiagonalCounterA == 2 && diagonalCounterA == 1) { return(DefendingBonus * sign); } Int32 linesWithTwo = 0; if (horizontalCounter == 2 && horizontalOponentCounter == 0) { linesWithTwo++; } if (verticalCounter == 2 && verticalOponentCounter == 0) { linesWithTwo++; } if (diagonalCounterB == 2 && oponentDiagonalCounterB == 0) { linesWithTwo++; } if (diagonalCounterA == 2 && oponentDiagonalCounterA == 0) { linesWithTwo++; } Int32 resultValue = 0; if (linesWithTwo > 0) { resultValue = linesWithTwo * LineWithTwoBonus * sign; } if (move.X == 1 && move.Y == 1) { resultValue = MiddleFieldBonus * sign; } return(resultValue); }