// Returns the allowed player Tokennames for the given Gamestate on a specific Field on the GameField // = The Tokens that are allowed to be placed on the Field which have to be Bigger than the current highest Token on the Field // @param availableTokens All available tokens of the Player for the GameState private static List <string> GetAllowedTokensForField(StringState state, Field field, List <string> availableTokens) { List <string> allowedTokens = new List <string>(); // Is already a Token on the Field? if (state.ContainsKey(field)) { // Iterate through available Tokens foreach (string token in availableTokens) { // Is the available Token "bigger" than the Token on the Field? if (TypeConverter.GetValueForTokenName(token) > TypeConverter.GetValueForTokenName(state[field].Peek())) { // Token is allowed allowedTokens.Add(token); } } } else { // No Token on the Field = all available Tokens are allowed allowedTokens = availableTokens; } return(allowedTokens); }
// Return a new state with the given move on the given state with new reference private static StringState GetStateWithMove(StringState state, MoveString move) { // Deep clone previous state StringState resultState = TypeConverter.DeepCloneState(state); // Remove the token from the previous field (if already placed) resultState = RemoveTokenFromGameState(resultState, move.Token); // If the field already has a token if (resultState.ContainsKey(move.Field)) { Stack <string> tokenStack = new Stack <string>(resultState[move.Field]); tokenStack.Push(move.Token); // Place the allowed Token above the old Token resultState.Remove(move.Field); resultState.Add(move.Field, tokenStack); } else { // Otherwise add the Field with a new Stack with the allowed Token Stack <string> tokenStack = new Stack <string>(); tokenStack.Push(move.Token); resultState.Add(move.Field, tokenStack); } return(resultState); }
// Returns the evaluation value for three Fields on the given State for the given Player // (Used an array as fields parameter for iteration) private static int EvaluateThreeFields(StringState state, Player player, Field[] fields) { int res = 0; int currentValue = 0; // Count the tokens for each player int playerTokenCounter = 0; int opponentTokenCounter = 0; // Iterate through all three fields foreach (Field field in fields) { // Get the tokenString on the peek of the field or use an empty string string tokenString = state.ContainsKey(field) ? state[field].Peek() : ""; // Evaluate the tokens on the field currentValue = EvaluateToken(player, tokenString); // The result is positive for the players token if (currentValue > 0) { // Inc player token counter playerTokenCounter++; res += currentValue * (playerTokenCounter + 1); } // The result is negative for opponent token else if (currentValue < 0) { // Inc opponent counter opponentTokenCounter++; // Add current Value * opponent counter res += currentValue * (opponentTokenCounter + 1); } // Otherwise no change of res } return(res); }
// Check the GameField for Three same tokens in a Row // Return the winner or null public static Player?CheckWinner(StringState GameField) { int fieldIndex = 0; Player?winner = null; // Check Horizontal Winner for (fieldIndex = 0; fieldIndex < 8; fieldIndex += 3) { // Check if fields are available (if they are they contain at least one token) if (GameField.ContainsKey((Field)fieldIndex) && GameField.ContainsKey((Field)fieldIndex + 1) && GameField.ContainsKey((Field)fieldIndex + 2)) { // Check the parent name (= Player Name) of the peek (= highest Token) on the Fields winner = ComparePlayerOnFields(GameField[(Field)fieldIndex].Peek(), GameField[(Field)fieldIndex + 1].Peek(), GameField[(Field)fieldIndex + 2].Peek()); // return winner directly when detected if (winner != null) { return(winner); } } } // Check Vertical Winner for (fieldIndex = 0; fieldIndex < 3; fieldIndex++) { // Check if fields are available (if they are they contain at least one token) if (GameField.ContainsKey((Field)fieldIndex) && GameField.ContainsKey((Field)fieldIndex + 3) && GameField.ContainsKey((Field)fieldIndex + 6)) { // Check the parent name (= Player Name) of the peek (= highest Token) on the Fields winner = ComparePlayerOnFields(GameField[(Field)fieldIndex].Peek(), GameField[(Field)fieldIndex + 3].Peek(), GameField[(Field)fieldIndex + 6].Peek()); // return winner directly when detected if (winner != null) { return(winner); } } } // Check Diagonal Winner if (GameField.ContainsKey(Field.TopLeft) && GameField.ContainsKey(Field.Middle) && GameField.ContainsKey(Field.BottomRight)) { // Check the parent name (= Player Name) of the peek (= highest Token) on the Fields winner = ComparePlayerOnFields(GameField[Field.TopLeft].Peek(), GameField[Field.Middle].Peek(), GameField[Field.BottomRight].Peek()); // return winner directly when detected if (winner != null) { return(winner); } } if (GameField.ContainsKey(Field.TopRight) && GameField.ContainsKey(Field.Middle) && GameField.ContainsKey(Field.BottomLeft)) { // Check the parent name (= Player Name) of the peek (= highest Token) on the Fields winner = ComparePlayerOnFields(GameField[Field.TopRight].Peek(), GameField[Field.Middle].Peek(), GameField[Field.BottomLeft].Peek()); } // return winner or null when no winner detected return(winner); }