+ 1

The Game never ends! Please Help!

from random import randint as rnd from time import sleep stop=2 b=[[None,None,None],[None,None,None],[None,None,None]] def printBoard(board): for x in range(0,len(board)): r='' for y in range(0,len(board[x])): if board[x][y] == 1: r += ' X ' elif board[x][y] == 0: r += ' O ' else: r+= ' - ' print(r) def win(board): for x in range(len(board)): if board[x][0] == board[x][1] and board[x][1] == board[x][2]: if board[x][0] == 1: print("Computer Wins") stop=1 elif board[x][0] == 0: print("Player Wins") stop=1 for y in range(len(board[0])): if board[0][y] == board[1][y] and board[1][y] == board[2][y]: if board[0][y] == 1: print("Computer Wins") stop=1 elif board[0][y] == 0: print("Player Wins") stop=1 if board[0][0] == board[1][1] and board[1][1] == board[2][2]: if board[0][0] == 1: print("Computer Wins") stop=1 elif board[0][0] == 0: print("Player Wins") stop=1 if board[0][2] == board[1][1] and board[1][1] == board[2][0]: if board[0][2] == 1: print("Computer Wins") stop=1 elif board[0][2] == 0: print("Player Wins") stop=1 def putrnd(board): x=rnd(0,len(board)-1) y=rnd(0,len(board[0])-1) if board[x][y] == None: board[x][y]=1 else: putrnd(board) while stop==2: print("This your beautiful Tic Tac Toe board:") printBoard(b) print("Now, where do you want your O to be?") xc=int(input("Enter the x-coord: ")) yc=int(input("Enter the y-coord: ")) if b[xc][yc] == None: b[xc][yc] = 0 print("Hmm....\nNice try bro\nLet me think where to put my X.....") sleep(rnd(0,3)) else: print(". .\n | \n _ \nSorry the place is already filled, you lose a chance") putrnd(b) win(b)

30th Jun 2017, 4:06 PM
Prabhakar Dev
Prabhakar Dev - avatar
3 ответов
+ 4
Hmm... are you sure your methods are updating the *global* stop variable? ;)
30th Jun 2017, 6:00 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 4
Just add: global stop to each method's definition, to be sure.
1st Jul 2017, 2:37 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
0
Hmm..... I see what you are pointing out to.... Is there any other way?
1st Jul 2017, 10:50 AM
Prabhakar Dev
Prabhakar Dev - avatar