lundi 7 août 2017

python tic tac toe problems

I am new to coding and has recently started learning python. My first challenge is to build a tic tac toe game. Below are my codes for the game, everything worked fine except that when I want to restart the game or end the game, the loop won't break or the game cannot start.Could anyone figure out whats wrong with my codes. thank you!

Matrix = [[' ' for i in range(3)]for j in range(3)]
for i in Matrix:
    print(i)
def check_done(value):
for a in range(0,3):
    if Matrix[0][a]==Matrix[1][a]==Matrix[2][a]==value\
    or Matrix[a][0]==Matrix[a][1]==Matrix[a][2]==value:
            print ('won')
            return True
    if Matrix[0][0]==Matrix[1][1]==Matrix[2][2]==value\
    or Matrix[2][0]==Matrix[1][1]==Matrix[0][2]==value:
        print('won')
        return True

   if ' ' not in Matrix[0] and ' ' not in Matrix[1] and ' ' not in     
   Matrix[2]:
    print('draw')
    return True

    return False

def replay():

 command =input('Enter r to restart, or e to end game: ')
 while True: 
    if command == 'r':
        player1_input()

    if command == 'e':
            return
            break

 else:
     print('Invalid command.')

def player1_input():
print('Player 1 insert your name here')
name1=input()
print('player 2 insert your name here')
name2=input()
while True:
    inputValid = False
    while inputValid == False:
        print(name1,'please place x coordinates')
        xinput=int(input())
        print(name1,'please place y coordinates')
        yinput=int(input())
        if yinput >= 0 & yinput <= 2 & xinput >=0 & xinput <= 2:
            if Matrix[yinput][xinput] == ' ':
                Matrix[yinput][xinput] = 'X'
                for i in Matrix:
                    print(i)
                if check_done('X'):
                    print(name1,'won')
                    replay()
                inputValid = True

    inputValid = False
    while inputValid == False:
        print(name2,'please place x coordinates')
        xinput=int(input())
        print(name2,'please place y coordinates')
        yinput=int(input())
        if yinput >= 0 & yinput <= 2 & xinput >=0 & xinput <= 2:
            if Matrix[yinput][xinput] == ' ':
                Matrix[yinput][xinput] = 'O'
                for i in Matrix:
                    print(i)
                if check_done('O'):
                    print(name2,'won')
                    replay()
                inputValid = True

 return True

Aucun commentaire:

Enregistrer un commentaire