0

The code is supposed to be a random walker that runs for how ever many times the user inputs and if the walker manages to reach the edge of the game board, he wins or if the walker ever returns to his starting location, he loses. Right now my code runs but the wins and losses dont equal the user imputed amount of times the program should run

    # Project 3 yay
#right now my win loss ratio is wrong///very off
from random import choice #choice makes a random selection from list
path = []
path_len_list = []
string_outcome = "ok"
def move_man():
    """Function moves the walker left, right, up, or down according to the random number selected"""
    directions = ["left", "down", "right", "up"]
    position = [3, 3]
    #print(path)
    path.clear()
    global string_outcome
    string_outcome = ""
    #Randomly generate a integer 1 - 4
    while string_outcome != "win" and string_outcome != "loss":
        x = choice(directions)
        path.append(list(position))
        if x == "down": #decrement x position
            x = position[0] - 1
            y = position[1]
            position[0], position[1] = (x, y)
            string_outcome = check_position_1(position)
            #print(string_outcome)
        elif x == "right": #increment y position
            x = position[0]
            y = position[1] + 1
            position[0], position[1] = (x, y)
            string_outcome = check_position_1(position)
            #print(string_outcome)
        elif x == "up": #increment x positon
            x = position[0] + 1
            y = position[1]
            position[0], position[1] = (x, y)
            string_outcome = check_position_1(position)
            #print(string_outcome)
        elif x == "down": #decrement y postion
            x = position[0]
            y = position[1] - 1
            position[0], position[1] = (x, y)
            string_outcome = check_position_1(position)
            #print(string_outcome)
        else:
            break
    write_to_file(path, string_outcome)
    return string_outcome
def check_position_1(position):
    """Checks whether the move results in a win, loss, or if the walker continues walking. Keeps tally of the amounts of wins and losses."""
    if (position[0] == 0 or position[0] == 6 or position[1] == 0 or position[1] == 6):
        print("Winning condition")
        string_outcome = "win"
        path_len = len(path)
        path_len_list.append(path_len)
        return string_outcome
    elif (position[0] == 3 and position[1] == 3):
        string_outcome = "loss"
        print("Losing condition")
        path_len = len(path)
        path_len_list.append(path_len)
        return string_outcome
    else:
        string_outcome = "ok"
        return string_outcome

def write_to_file(path, string_outcome): #right now its only writing the last case not all of them
    """Iterates through each path and writes the coordinates of every win and loses to a file"""
    with open("log.txt", "a") as my_file:
        print(string_outcome, file=my_file)# print text to my_file
        print(path, file=my_file)


def get_win_percentage(number_of_win, number_of_loss):
    """Totals wins and loses, divides the amount of wins by the total, and then displays the percentage of wins"""
    win_sum = number_of_win + number_of_loss
    win_percentage = number_of_win / win_sum
    win_percentage = round(win_percentage, 2)
    win_percentage = win_percentage * 100
    return win_percentage

def get_average_game_length(path_len_list, number_of_games):
    """Calculates the average amount of steps taken for all the games ran"""
    average_game_length = sum(path_len_list) / (number_of_games)
    print(path_len_list)
    average_game_length = round(average_game_length, 3)
    return average_game_length


def walking_game():
    "main function, gets inputs, calls other functions and prints out information to user"""
    global string_outcome
    number_of_win = 0
    number_of_loss = 0
    print("This program simulates a random walk on a grid.")
    number_of_games = int(input("How many games should be simulated? "))
    for i in range(number_of_games):
        move_man()
        if string_outcome == "win":
            number_of_win = number_of_win + 1
            #print(number_of_win)
        elif string_outcome == "loss":
            number_of_loss = number_of_loss + 1 
            #print(number_of_loss)
        else:
            pass
    win_percentage = get_win_percentage(number_of_win, number_of_loss)
    average_game_length = get_average_game_length(path_len_list, number_of_games)
    print("There were", number_of_win, "number of wins and", number_of_loss, "number of losses, resulting in a win percentage of", win_percentage, "%")
    print("The average game length was", average_game_length)
    print("Log of games written to log.txt")


walking_game()

1 Answers1

1

You're checking if x == "down": twice. Change the first one to if x == "left":.

Debugging tip for the future: Replace your else: break and else: pass with else: assert False to notice such bugs.

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149