tic tac toe - Why my TicTacToe game doesn't complete running the codes? (Python 2.7.9) -
when run codes,it gets stuck after choose wether play first or not. so, here codes:
#tictactoe game import random null = '' new = ['','','','','','','','',''] player = '' computer = '' #x or o signs def sign(player, computer): player = raw_input('please choose either x or o:') while player not in ('x','x','o','o'): print ('not appropriate choice!') player = raw_input('please choose either x or o;') if player == 'x' or player == 'x': print ('you have chosen x!') computer = 'o' else: print ('you have chosen o!') computer = 'x' return player.upper(), computer.upper() #which player play first def whoplaysfirst(): shift = none while shift not in ('yes','yes','yes','no','no','no'): shift = raw_input('do want play first? yes or no:') if shift == 'yes' or shift == 'yes' or shift == 'yes': return 1 elif shift == 'no' or shift == 'no' or shift == 'no': return 0 else: print ('it invaild choice!') def theboard(move): print ("=================================") print ("[*] : : [*]") print "[*] " , move[1] , " : " , move[2] , " : " , move[3] , " [*]" print ("[*] : : [*]") print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~") print ("[*] : : [*]") print "[*] " , move[4] , " : " , move[5] , " : " , move[6] , " [*]" print ("[*] : : [*]") print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~") print ("[*] : : [*]") print "[*] " , move[7] , " : " , move[8] , " : " , move[9] , " [*]" print ("[*] : : [*]") print ("=================================") def winner_computer(): print ("sorry lost!") def winner_player(): print("you won!") def firstmove_player(player, computer, new): while winn(player, computer, new) none: turn = player_turn(player, new) new[int(turn)] = player theboard(new) if winn(player, computer, new) != none: break else: pass print("the computer choose..") c_turn = computer_turn(player, computer, new) print c_turn new[int(c_turn)] = computer theboard(new) pr = winn(player, computer, new) if pr == 1: winner_player() elif pr == 0: winner_computer() else: print("it tie :/") def firstmove_computer(player, computer, new): while not winn(player, computer, new): print("the computer take..") c_turn = computer_turn(player, computer, new) print c_turn new[c_turn] = computer theboard(new) if winn(player, computer, new) != none: break else: pass turn = player_turn(player, new) new[int(move)] = player theboard(new) pr = winn(player, computer, new) if pr == 1: winner_player() elif pr == 0: winner_computer() else: print("it tie :/") def winn(player, computer, new): possibilities = ((1,2,3),(4,5,6),(7,8,9),(1,4,7),(2,5,8),(3,6,9),(1,5,9),(3,5,7)) in possibilities: if new[i[1]] == new[i[2]] == new[i[3]] != null: winner = new[i[1]] if winner == player: return 1 elif winner == computer: return 0 if null not in new: return 'tie!' if null not in new: return 'tie' return none def player_turn(player, new): move = raw_input("which box want put in?:") while true: if move not in ('1','2','3','4','5','6','7','8','9'): print("that invalid move") move = raw_input("which box want put in?:") elif new[int(move)] != null: print ("this box taken, please choose one") move = raw_input("which box want put in?:") else: return int(move) def computer_turn(player, computer, new): choicest = [9, 7, 5, 1, 3] blank = [] in range(0,9): if new[i] == null: blank.append(i) in blank: new[i] = computer if winn(player, computer, new) 0: return new[i] = null in blank: new[i] = player if winn(player, computer, new) 1: return new[i] = null return int(blank[random.randrange(len(blank))]) def enterance(): #intro. game print ("welcome to...") print ("############ ############## #############") print ("############ ############## #############") print (" #### wwwwwwwww #### xxxxxxxxx ####") print (" ####ooo www*****w #### xxx&&&&&& #### wwwwwwwwwww") print (" ####ooo wwwwwwwww #### xxxxxxxxx #### www www") print (" #### www ####ggggggg xxx ####dddddddd wwwwwwwwwww") print (" ####o#o www ####g@@@@@g xxx ####ddddddkd ww%%%%%%%ww") print (" ####o#o www ####g@@@@@g xxx ####d dkd wwwwwwwwwww") print (" ####o#o wwwwwwwww ####g@@@@@@ggggggxxxxx ####d dkd ww") print (" ####o#o www*****w ####g@@@@@@@@@@@gx&&&& ####ddddddkd ww") print (" ####o#o wwwwwwwww ####ggggggg ggggggxxxxx ####ddddddkd wwwwwwwwwww") print ("all have choose number can make move") print ("and here how game board like:") print (" ") print ("====================================") print ("[*] : : [*]") print ("[*] 1 : 2 : 3 [*]") print ("[*] : : [*]") print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~") print ("[*] : : [*]") print ("[*] 4 : 5 : 6 [*]") print ("[*] : : [*]") print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~") print ("[*] : : [*]") print ("[*] 7 : 8 : 9 [*]") print ("[*] : : [*]") print ("====================================") print (" let's start game!! ") def main(player, computer, new): enterance() move = sign(player, computer) player = move[0] computer = move[1] b = whoplaysfirst() if b == 1: print ("you play first.") print ("lets start. here our game board!") theboard(new) firstmove_player(player, computer, new) elif b == 0: print ("the computer play first") print ("lets begin game. our game board!") theboard(new) firstmove_computer(player, computer, new) else: pass main(player, computer, new) raw_input("tab enter exit")
and here error message when codes stuck @ point:
traceback (most recent call last): file "c:\users\hany\documents\nona cs\mytictactoe3.py", line 214, in <module> main(player, computer, new) file "c:\users\hany\documents\nona cs\mytictactoe3.py", line 203, in main theboard(new) file "c:\users\hany\documents\nona cs\mytictactoe3.py", line 50, in theboard print "[*] " , move[7] , " : " , move[8] , " : " , move[9] , " [*]" indexerror: list index out of range
can please tell i've done wrong , how fix it? appreciate thanks!
your variable new
is list , first index of list 0. should change def theboard(move)
. should start move[0]
, not move[1]
because move[9]
ist 9th index of list maximum 8.
the same problem should in def winn(player, computer, new)
new[i[1]]
.
see this learn how use lists.
edit: have same problem in def player_turn(player, new)
. when correct there too, should work.
Comments
Post a Comment