Wordle

Game logic

Game Logic code
import random
round_count = 20
def init_game(game_settings, ref_data):
    return {
        "player_turn_idx": 0,
        "answers": [random.choice(ref_data["words"]).upper() for i in range(round_count)],
        "round_index": 0,
        "guesses_each_round": [{
            "guesses": []
        } for i in range(round_count)],
        "scores": []
    }

def guess(state, ref_data, word):
    if len(word) != 5:
        raise InvalidActionError("Guesses should be 5 letters long")
    word = word.upper()
    round_index = state["round_index"]
    target = state["answers"][round_index]
    letter_bag = list(target)
    result = ['', '', '', '', '']

    for i in range(len(word)):
        if word[i] == target[i]:
            result[i] = 'G'
            letter_bag.remove(word[i])
    for i in range(len(word)):
        if result[i] == 'G':
            continue
        if word[i] in letter_bag:
            result[i] = 'Y'
            letter_bag.remove(word[i])
        else:
            result[i] = 'B'
    guesses_this_round = state["guesses_each_round"][round_index]["guesses"]
    guesses_this_round.append({
            "guess": word,
            "result": "".join(result)
        })

    if word == target or len(guesses_this_round) == 6:
        if round_index < round_count - 1:
            state["round_index"] += 1
        if word == target:
            if len(guesses_this_round) == 1:
                state["scores"].append(10)
            elif len(guesses_this_round) == 2:
                state["scores"].append(10)
            elif len(guesses_this_round) == 3:
                state["scores"].append(6)
            elif len(guesses_this_round) == 4:
                state["scores"].append(3)
            elif len(guesses_this_round) == 5:
                state["scores"].append(2)
            elif len(guesses_this_round) == 6:
                state["scores"].append(1)
        else:
            state["scores"].append(-5)

    return state

def get_game_result(state, ref_data):
    round_index = state["round_index"]
    target = state["answers"][round_index]
    guesses_this_round = state["guesses_each_round"][round_index]["guesses"]
    if (round_index == round_count - 1 and 
        len(guesses_this_round) > 0 and 
        (len(guesses_this_round) == 6 or guesses_this_round[-1]["guess"] == target)):
        return {
            "game_result": "SinglePlayerCompleted",
            "winner_idx": 0,
            "score": sum(state["scores"])
        }

    return {
        "game_result": "NoWinnerYet",
    }


def get_player_states(state, ref_data):
    player_states = []
    round_index = state["round_index"]
    target = state["answers"][round_index]
    guesses_this_round = state["guesses_each_round"][round_index]["guesses"]
    for i in range(1):
        obfuscated_answers = []
        for i in range(len(state["answers"])):
            if round_index > i:
                obfuscated_answers.append(state["answers"][i])
            elif round_index == i and len(state["guesses_each_round"][round_index]["guesses"]) == 6:
                obfuscated_answers.append(state["answers"][i])
            elif round_index == i and len(guesses_this_round) > 0 and guesses_this_round[-1]["guess"] == target:
                obfuscated_answers.append(state["answers"][i])
            else:
                obfuscated_answers.append("*****")
        player_states.append({
            "player_turn_idx": state["player_turn_idx"],
            "round_index": state["round_index"],
            "guesses_each_round": state["guesses_each_round"],
            "scores": state["scores"],
            "answers": obfuscated_answers
        })
    return player_states

Game state schema

message State {
  int32 player_turn_idx = 1;
  int32 round_index = 2;
  repeated GuessesEachRound = 3;
  repeated int32 scores = 4;
  repeated string answers = 5;
}
message GuessesEachRound {
  repeated Guess guesses = 1;
}
message Guess {
  string guess = 1;
  string result = 2;
}
message refData {
  repeated string words = 1;
}

Action schema

guess(string word)

Start building now

Last updated