/// <summary> /// Play a turn on the board. /// </summary> /// <param name="context">The Tic Tac Toe environment's context.</param> /// <returns> /// The play turn result. /// </returns> public TicTacToeBotPlayTurnResult PlayTurn(ITicTacToeEnvironmentContext context) { var spaces = context.Board.GetSpaces(); // If center space is empty, use it. if (spaces[1, 1].SpaceType == SpaceType.Empty) { return(new TicTacToeBotPlayTurnResult(1, 1)); } // If any corners spaces is empty. if (spaces[0, 0].SpaceType == SpaceType.Empty) { return(new TicTacToeBotPlayTurnResult(0, 0)); } if (spaces[0, 2].SpaceType == SpaceType.Empty) { return(new TicTacToeBotPlayTurnResult(0, 2)); } if (spaces[2, 0].SpaceType == SpaceType.Empty) { return(new TicTacToeBotPlayTurnResult(2, 0)); } if (spaces[2, 2].SpaceType == SpaceType.Empty) { return(new TicTacToeBotPlayTurnResult(2, 2)); } // If any non corners spaces is empty. if (spaces[0, 1].SpaceType == SpaceType.Empty) { return(new TicTacToeBotPlayTurnResult(0, 1)); } if (spaces[1, 2].SpaceType == SpaceType.Empty) { return(new TicTacToeBotPlayTurnResult(1, 2)); } if (spaces[2, 1].SpaceType == SpaceType.Empty) { return(new TicTacToeBotPlayTurnResult(2, 1)); } return(new TicTacToeBotPlayTurnResult(1, 0)); }
private void Play(IBot bot, ITicTacToeBotAbility botAbility, ITicTacToeEnvironmentContext context) { var spaces = m_board.GetSpaces(); var p = botAbility.PlayTurn(context); if (spaces[p.RowIndex, p.ColumnIndex].SpaceType == SpaceType.Empty) { m_board.SetSpace(p.RowIndex, p.ColumnIndex, context.MySpaceType); return; } else { var msg = String.Format(CultureInfo.CurrentUICulture, "O bot '{0}' executou uma atualização inválida. Tentativa de jogar na linha {1} e coluna {2}.", bot.Name, p.RowIndex, p.ColumnIndex); throw new InvalidOperationException(msg); } }
/// <summary> /// Play a turn on the board. /// </summary> /// <param name="context">The Tic Tac Toe environment's context.</param> /// <returns> /// The play turn result. /// </returns> public TicTacToeBotPlayTurnResult PlayTurn(ITicTacToeEnvironmentContext context) { m_spaces = context.Board.GetSpaces(); // If has a victory space to play. var result = CheckVictorySpace(context.MySpaceType); if (result != null) { return(result); } // If need to block a opponent victory space. result = CheckVictorySpace(context.OpponentSpaceType); if (result != null) { return(result); } // If center space is empty, use it. result = CheckCenterSpace(); if (result != null) { return(result); } // If any corners spaces is empty. result = CheckCornerSpaces(); if (result != null) { return(result); } // If any non corners spaces is empty. return(CheckNonCornerSpaces()); }