python - Maximum Recursion Depth Exceeded (unable to determine cause of infinite loop) -


hopefully out there can make sense of messed code. getting python in free time , decided make tic tac toe game.

the problem toward end of game, if human player not win, computer's turn function goes infinite loop , crashes code.

code:

import random board = [[' ',' ',' ',],[' ',' ',' ',],[' ',' ',' ',]]  def printboard():     spacercount = 0     print "    |     |  "     row in board:         print row[0] + "   | " + row[1] + "   | " + row[2]         if spacercount < 2:             print "---------------"             print "    |     |  "             spacercount += 1     print "\n"     print "____________________ \n \n"   def userturn():     x = raw_input("choose row: ")     y = raw_input("choose column: ")     print "\n"     if board[int(x)][int(y)] == " ":         board[int(x)][int(y)] = "x"         printboard()         if checkwin("x") == true:             print "player has won!"         elif checkfull() == true:             print "it's tie!"         else:             computerturn()     else:         print "this space taken, try again"         userturn()  def computerturn():     x = random.randint(1,2)     y = random.randint(1,2)     print "\n"     if board[int(x)][int(y)] == " ":         board[int(x)][int(y)] = "o"         printboard()         if checkwin("o") == true:             print "computer has won!"         elif checkfull() == true:             print "it's tie!"         else:             userturn()     else:         computerturn()  def checkwin(le):     return ((board[0][2] == le , board[1][1] == le , board[2][0] == le) or     (board[0][0] == le , board[1][1] == le , board[2][2] == le) or     (board[0][0] == le , board[1][0] == le , board[2][0] == le) or     (board[0][1] == le , board[1][1] == le , board[2][1] == le) or     (board[0][2] == le , board[1][2] == le , board[2][2] == le) or     (board[0][0] == le , board[0][1] == le , board[0][2] == le) or     (board[1][0] == le , board[1][1] == le , board[1][2] == le) or     (board[2][0] == le , board[2][1] == le , board[2][2] == le))  def checkfull():     x in board:         if x[0] != " " , x[1] != " " , x[2] != " ":             return true         else:             return false  printboard() userturn() 

i must have gone through computerturn function hundred times trying determine why infinite loop started, have not been successful.

any appreciated :) code straightforward enough comments not needed if add them in

first, empty board:

board = [[' ',' ',' ',],[' ',' ',' ',],[' ',' ',' ',]] 

a single space character denotes empty square.

next, first thing program after printing board call userturn(). user makes kind of move, and, if succeeds, program calls computerturn(). but, how computer move?

it looks empty squares in part of board (not of - second , third columns , rows because of randint(1,2) instead of randint(0,2)). if randomly-selected square occupied, computer tries go again. once of squares checks occupied, never able try go again. repeatedly until it's stopped maximum recursion depth.

how fix this? instead of having computer repeatedly pick random move entire board , making keep trying until picks valid move, we'll limit moves ones available.

def computerturn():     available = [(x, y) x,row in enumerate(board)                  y,column in enumerate(row)                  if board[x][y] == ' ']                 # list of actual available moves     x,y = random.choice() # select actual available moves     # x = random.randint(0,2) # don't want     # y = random.randint(0,2) # don't want     print "\n"     # don't need following conditional     #if board[x][y] == " ": # don't need int() calls     board[x][y] = "o" # thing successful move should     printboard() # unindent stuff     if checkwin("o"): # don't need compare boolean true         print "computer has won!"     elif checkfull():         print "it's tie!"     else:         userturn()      #else: # let's take out whole recursive call , fix random move     #    computerturn() 

and "clean" version comments , obsolete code removed:

def computerturn():     available = [(x, y) x,row in enumerate(board)                  y,column in enumerate(row)                  if board[x][y] == ' ']     x,y = random.choice()     print "\n"     board[x][y] = "o"     printboard()     if checkwin("o"):         print "computer has won!"     elif checkfull():         print "it's tie!"     else:         userturn() 

we hardly need comments - can see what's going on code itself.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -