2017-04-06 6 views
0

このコードは、ユーザーが何度入力した回数だけ実行されるランダムウォーカーであると考えられています。ウォーカーがゲームボードの端に達すると、歩行者は出発地点に戻りますが、彼は失います。今私のコードの実行が、勝敗はあなたが二回if x == "down":をチェックしているランダムウォーカーはPythonを実行する時間ではありません

# 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

プログラムを実行する必要が倍のユーザー帰属量を等しいいけません。最初の番号をif x == "left":に変更します。

将来のデバッグのヒント:とelse: passelse: assert Falseに置き換えて、このようなバグに気づくことをお勧めします。

関連する問題