mardi 2 août 2016

I have a nested if statement and the inside statement nullifies the outter

board=[[1,0,8,5,12,16,24],[7,0,6,2,20,4,18],[13,0,9,10,22,23,17],[19,0,11,3,14,15,21]]

def move():
while board[0][6]!= 0 and board[1][6]!=0 and board[2][6]!=0 and board[3][6]!=0: 
    user_choice= int(raw_input('>'))
    for lists in board:
        for i in range(len(lists)):
            if lists[i]== user_choice-1 and lists[i+1]==0:
                lists[i+1]= user_choice
                print print_board(board)                    
                for a in board:
                    for i in range(len(a)):
                        if a[i]==user_choice:
                            a[i]=0
                            print print_board(board)  

I am making a game where the user inputs a number (user_choice). The program will find the number 1 smaller than user_choice. Say the user picks 8, the game will find the number 7 on the board and if the number after 7 is 0 the program swaps the location of the 0 and the 8.

The code I have works except when the second if statement runs the output undoes the first if statement

here is a typical output for user_choice=8:

[[1,0,8,5,12,16,24],[7,8,6,2,20,4,18],[13,0,9,10,22,23,17],[19,0,11,3,14,15,21]]
None
[[1,0,8,5,12,16,24],[7,0,6,2,20,4,18],[13,0,9,10,22,23,17],[19,0,11,3,14,15,21]]
None
[[1,0,0,5,12,16,24],[7,0,6,2,20,4,18],[13,0,9,10,22,23,17],[19,0,11,3,14,15,21]]

The first output replaced the 0 after the 7 with an 8.
The second output seems to show the initial board.
The third output replaces the 8 in the initial board with a 0.

I want one output that both replaces the 0 after the 7 with an 8 and replaces the initial 8 with a 0.

Aucun commentaire:

Enregistrer un commentaire