簡體   English   中英

訪問先前移動的用戶輸入

[英]Access user input from previous move

我正在創建一個簡單的游戲,它接受用戶輸入(0 或 1)並將其與計算機輸入進行比較。 根據用戶在先前移動中的輸入,計算機將決定下 0 或 1。

select_difficulty = int(input("Choose the type of game (1: Easy; 2: Difficult): "))

moves = int(input("Enter the number of moves: "))

MS = 0
PS = 0
xi = 1234

throw00 = []
throw01 = []
throw10 = []
throw11 = []

條件如下:

throw00 = count of the number of times the human player chose 0 given that in the previous move he chose 0 throw01 = count of the number of times the human player chose 0 given in the previous move he chose 1時選擇 0 throw10 = count of the number of times the human player chose 1 given that his previous move was 0鑒於他/她先前的出價為throw10 = count of the number of times the human player chose 1 given that his previous move was 0 throw11 = count of the number of times the human player chose 1 given his/her previous bid was 1

可能會發生以下情況: 如果玩家的最后一次投擲為 0:

If throw10 > throw00: then the computer chooses 1

If throw10 < throw00: then the computer chooses 0

If throw10 = throw00: then the computer chooses randomly 0 or 1

如果玩家的最后一次投擲是 1:

If throw11 > throw01: then the computer chooses 1

If throw11 < throw01: then the computer chooses 0

If throw11 = throw01: then the computer chooses randomly 0 or 1.

我已經嘗試通過將玩家移動保存在列表中,然后從該列表中訪問上一個移動(一回合后)來實現這一點。

if select_difficulty == 2:
    for turn in range(moves):
    player_move_dif = [int(n) for n in input("Choose your move number %s (0 or 1):" % (turn+1)).split()]`
    player_previous_move = player_move_dif[1]

基於這兩個變量上的這兩個變量,我然后 append 將 player_move 選擇(0 或 1)到相應的列表中,而“計算機”又使用該列表來選擇他的移動

if player_move_dif == 0 and player_previous_move == 0:
            throw00.append(player_move_dif)
        elif player_move_dif == 0 and player_previous_move == 1:
            throw01.append(player_move_dif)
        elif player_move_dif == 1 and player_previous_move == 0:
            throw10.append(player_move_dif)
        else:
            throw11.append(player_move_dif)
        
        #define computer behavior depening on previous player move
        if player_previous_move == 0:
                if len(throw10) > len(throw00):
                    computer_move = 1
                elif len(throw10) < len(throw00):
                    computer_move = 0
                else:
                    computer_move,xi = linear_congruence(xi) 
        elif player_previous_move == 1:
                if len(throw11) > len(throw01):
                    computer_move = 1
                elif len(throw11) < len(throw01):
                    computer_move = 0
                else:
                    computer_move,xi = linear_congruence(xi)  
        else:
            computer_move,xi = linear_congruence(xi)   

這不起作用,因為我需要玩家移動的值(作為整數)用於打印語句以顯示游戲如何進行

 if player_move_dif == computer_move:
               MS = MS + 1
               print("player = %d machine = %d - Machine wins!" % (player_move_dif, computer_move))
               print("You: %d Computer: %d" % (PS, MS))
        else:
               PS = PS + 1
               print("player = %d machine = %d - Player wins!" % (player_move_dif, computer_move))
               print("You: %d Computer: %d" % (PS, MS))
        
        print('PLAYER: ' + '*'*PS)
        print('MACHINE: ' + '*'*MS)
        
    if turn == moves and PS > MS:
        print("The game has ended, you win!")
    elif PS == MS:
        print("The game has ended, it is a draw!")
    else:
        print("the game has ended, the machine wins!")

這種方式目前會導致 TypeError %d 格式:需要一個數字,而不是列表。 我懷疑我走錯了路,如何以我現在嘗試的方式解決這個問題,因為我找不到解決方案來以我想要的方式定義變量,同時能夠在以后根據需要訪問它們。

此外我不確定游戲的邏輯是否正確實現,我發現了一些導致您錯誤的問題。

如果要將玩家的移動保存到列表中,則必須在 for 循環之前創建一個空列表,否則會一次又一次地覆蓋它。 你也遇到了問題,在第一步沒有以前的移動,所以你首先需要一個起始編號:

player_move_dif = [] #create an empty list out of your for-loop

for i, turn in enumerate(range(moves)): #use enumerate() to count the loops
    if i > 0: #first move has no previous one
        player_move_dif.append(input("Choose your move number %s (0 or 1):" % (turn+1)).split()))
        player_previous_move = int(player_move_dif[i-1])
    else:
        player_move_dif.append(input("Choose your start number %s (0 or 1):" % (turn+1)).split()))
        continue #no action after the first move

然后,您可以通過移動列表的索引訪問當前移動。 在您的情況下,列表的最后一個索引是玩家所做的最后一步:

if player_move_dif == computer_move:
   MS = MS + 1
   print("player = %d machine = %d - Machine wins!" % (int(player_move_dif[-1]), computer_move)) #pick the last move from play_move_dif
   print("You: %d Computer: %d" % (PS, MS))
else:
   PS = PS + 1
   print("player = %d machine = %d - Player wins!" % (int(player_move_dif[-1]), computer_move)) #pick the last move from play_move_dif
   print("You: %d Computer: %d" % (PS, MS))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM