When I run my codes,it gets stuck after I choose wether to play first or not. So, here is the 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 the 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 will play first
def WhoPlaysFirst():
shift = None
while shift not in ('yes','Yes','YES','no','No','NO'):
shift = raw_input('Do you want to 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 is 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 you lost!")
def winner_Player():
print("You won!")
def FirstMove_Player(Player, Computer, new):
while winn(Player, Computer, new) is None:
Turn = Player_Turn(Player, new)
new[int(Turn)] = Player
TheBoard(new)
if winn(Player, Computer, new) != None:
break
else:
pass
print("The computer will 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 is a tie :/")
def FirstMove_Computer(Player, Computer, new):
while not winn(Player, Computer, new):
print("The computer will 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 is a 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))
for i 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 you want to put in?:")
while True:
if Move not in ('1','2','3','4','5','6','7','8','9'):
print("That is invalid move")
Move = raw_input("Which box you want to put in?:")
elif new[int(Move)] != null:
print ("This box is already taken, please choose another one")
Move = raw_input("Which box you want to put in?:")
else:
return int(Move)
def Computer_Turn(Player, Computer, new):
choicest = [9, 7, 5, 1, 3]
blank = []
for i in range(0,9):
if new[i] == null:
blank.append(i)
for i in blank:
new[i] = Computer
if winn(Player, Computer, new) is 0:
return i
new[i] = null
for i in blank:
new[i] = Player
if winn(Player, Computer, new) is 1:
return i
new[i] = null
return int(blank[random.randrange(len(blank))])
def Enterance():
#Intro. to the 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 you have to do is to choose the number so that you can make your move")
print ("And here is how your Game Board will look 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 THE GAME!! ")
def Main(Player, Computer, new):
Enterance()
Move = sign(Player, Computer)
Player = Move[0]
Computer = Move[1]
b = WhoPlaysFirst()
if b == 1:
print ("You will play first.")
print ("Lets start. Here is our Game Board!")
TheBoard(new)
FirstMove_Player(Player, Computer, new)
elif b == 0:
print ("The computer will play first")
print ("Lets begin the game. That is our Game Board!")
TheBoard(new)
FirstMove_Computer(Player, Computer, new)
else:
pass
Main(Player, Computer, new)
raw_input("Tab Enter to exit")
And here is the error message i get when the codes stuck at some 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 someone please tell what I've done wrong and how to fix it? I would appreciate it thanks!
Your variable new
is a List and the first index of a list is 0.
You should change your def TheBoard(Move)
. It should start with Move[0]
and not Move[1]
because Move[9]
ist the 9th index of your list but the maximum is 8.
The same problem should be in def winn(Player, Computer, new)
with new[i[1]]
.
See also THIS to learn how to use lists.
Edit:
You have the same problem in def Player_Turn(Player, new)
.
When you correct it there too, it should work.