I realise that many other people have tried and failed to make naughts and crosses (by looking at the suggested question) but I'm assuming they don't have the same problem as me.
Here is my code:
import random, time, sys
raw3 = ["-", "-", "-"]
raw2 = ["-", "-", "-"]
raw1 = ["-", "-", "-"]
def draw():
str3 = ""
str2 = ""
str1 = ""
for i in raw3:
str3 = (str3 + i + " ")
for i in raw2:
str2 = (str2 + i + " ")
for i in raw1:
str1 = (str1 + i + " ")
str_board = str3+str("\n"*2)+str(str2)+str("\n"*2)+str(str1)
print(str_board)
def playerX_turn():
c_choice_in = input("Type the coordinates in the format y/x\n(e.g. 2/1)")
c_choice_list = list(c_choice_in)
c_choice_x = int(c_choice_list[0]); c_choice_y = int(c_choice_list[2])
print(c_choice_x, c_choice_y)
if c_choice_x == 3 and raw3[c_choice_y - 1] == "-":
raw3[c_choice_y - 1] = "X"
elif c_choice_x == 2 and raw2[c_choice_y - 1] == "-":
raw2[c_choice_y - 1] = "X"
elif c_choice_x == 1 and raw1[c_choice_y - 1] == "-":
raw1[c_choice_y - 1] = "X"
def playerO_turn():
c_choice_in = input("Type the coordinates in the format y/x\n(e.g. 2/1)")
c_choice_list = list(c_choice_in)
c_choice_x = int(c_choice_list[0]); c_choice_y = int(c_choice_list[2])
print(c_choice_x, c_choice_y)
if c_choice_x == 3 and raw3[c_choice_y - 1] == "-":
raw3[c_choice_y - 1] = "O"
elif c_choice_x == 2 and raw2[c_choice_y - 1] == "-":
raw2[c_choice_y - 1] = "O"
elif c_choice_x == 1 and raw1[c_choice_y - 1] == "-":
raw1[c_choice_y - 1] = "O"
def check4win():
winner = None
if winner == None or winner == False and raw3[0] == "X" or raw3[2] == "X" and raw2[1] == "X" and raw1[0] == "X" or raw1[2] == "X":
winner = "playerX"
column_checker = 0
for i in range(0,3):
if winner == None or winner == False and raw3[column_checker] == "X" and raw2[column_checker] == "X" and raw1[column_checker] == "X":
winner = "playerX"
else:
column_checker += 1
if winner == None or winner == False:
if raw3[0] == "X" and raw3[1] == "X" and raw3[2] == "X":
winner = "playerX"
elif raw2[0] == "X" and raw2[1] == "X" and raw2[2] == "X":
winner = "playerX"
elif raw1[0] == "X" and raw1[1] == "X" and raw1[2] == "X":
winner = "playerX"
### ### ### ###
if winner == None or winner == False and raw3[0] == "Y" or raw3[2] == "Y" and raw2[1] == "Y" and raw1[0] == "Y" or raw1[2] == "Y":
winner = "playerY"
column_checker = 0
for i in range(0,3):
if winner == None or winner == False and raw3[column_checker] == "Y" and raw2[column_checker] == "Y" and raw1[column_checker] == "Y":
winner = "playerY"
else:
column_checker += 1
if winner == None or winner == False:
if raw3[0] == "Y" and raw3[1] == "Y" and raw3[2] == "Y":
winner = "playerY"
elif raw2[0] == "Y" and raw2[1] == "Y" and raw2[2] == "Y":
winner = "playerY"
elif raw1[0] == "Y" and raw1[1] == "Y" and raw1[2] == "Y":
winner = "playerY"
### ### ### ###
if winner != None or winner != False:
if winner == "playerX":
print("X won the game!\n(Sorry, O!)")
elif winner == "playerY":
print("Y won the game!\n(Bad luck, X!)")
sys.exit()
def main():
game_over = False
while game_over == False:
playerX_turn()
draw()
check4win()
playerO_turn()
draw()
check4win()
main()
So my first problem is that this is an example of output from it:
>>>
Type the coordinates in the format y/x
(e.g. 2/1)1,1
1 1
- - -
- - -
X - -
X won the game!
(Sorry, O!)
>>>
Which clearly is not correct...
It's probably a stupid mistake somewhere but I'm too lazy to go looking through it properly, but I've had a brief look through the check4win() function, but to no avail.
Another (less important) thing is that I have a feeling this code could be made neater... Maybe something with main()?
Related
I made a rock, paper, scissors game.
It works fine, as I wanted to, but it doesn't store entered value when runs second time, how do I fix that.
See the while loop below where the program goes again if the user enters y.
import random
tools =["Rock","Scissors","Paper"]
r = "".join(tools[0])
s = "".join(tools[1])
p = "".join(tools[-1])
def computer_predicion():
computer_pred = random.choice(tools)
return computer_pred
computer = computer_predicion()
def my_predicion():
my_predicion = input("Choose (R)Rock,(S)Scissors,(P)Paper:")
if my_predicion == "R" or my_predicion == "r":
my_predicion = r
return my_predicion
elif my_predicion == "S" or my_predicion == "s":
my_predicion = s
return my_predicion
elif my_predicion == "P" or my_predicion == "p":
my_predicion = p
return my_predicion
else:
print("Debils ir?")
human=my_predicion()
def game():
message_win = ("You won!")
message_lose = ("You lost!")
message = "Computer:%s\nUser:%s"%(computer, human)
if computer == r and human == r :
print(message+"\nIt's draw")
elif computer == p and human == p:
print(message + "\nIt's draw")
elif computer == s and human == s:
print(message + "\nIt's draw")
elif computer == r and human == p:
print(message+'\n'+message_win)
elif computer == p and human == r:
print(message+'\n'+message_lose)
elif computer == r and human == s:
print(message+'\n'+message_lose)
elif computer == s and human == r:
print(message+'\n'+message_win)
elif computer == p and human == s:
print(message+'\n'+message_win)
elif computer == s and human == p:
print(message+'\n'+message_lose)
else:
pass
c = True
while c: //Here code runs second time if user inputs Y or y.
game()
h = input("Continue?(Y/N):")
if h == "Y" or h == "y":
my_predicion()
computer_predicion()
pass
elif h == "N" or h == "n":
c = False
else:
print("Wrong symbol!")
The value is not being stored in the second loop because
computer = computer_predicion()
human = my_predicion()
are displayed outside of the while loop
Here is your working code, I migrated the two variable assignment inside the while loop
import random
tools =["Rock","Scissors","Paper"]
r="".join(tools[0])
s="".join(tools[1])
p="".join(tools[-1])
def computer_predicion():
computer_pred = random.choice(tools)
return computer_pred
def my_predicion():
my_predicion = input("Choose (R)Rock,(S)Scissors,(P)Paper:")
if my_predicion=="R" or my_predicion =="r":
my_predicion = r
return my_predicion
elif my_predicion=="S" or my_predicion =="s":
my_predicion = s
return my_predicion
elif my_predicion=="P" or my_predicion =="p":
my_predicion = p
return my_predicion
else:
print("Debils ir?")
def game():
message_win = ("You won!")
message_lose = ("You lost!")
message = "Computer:%s\nUser:%s"%(computer,human)
if computer ==r and human==r :
print(message+"\nIt's draw")
elif computer == p and human == p:
print(message + "\nIt's draw")
elif computer == s and human == s:
print(message + "\nIt's draw")
elif computer == r and human==p:
print(message+'\n'+message_win)
elif computer == p and human==r:
print(message+'\n'+message_lose)
elif computer == r and human==s:
print(message+'\n'+message_lose)
elif computer == s and human==r:
print(message+'\n'+message_win)
elif computer == p and human == s:
print(message+'\n'+message_win)
elif computer == s and human==p:
print(message+'\n'+message_lose)
else:
pass
c=True
while c : #Here code runs second time if user inputs Y or y.
computer = computer_predicion()
human = my_predicion()
game()
h = input("Continue?(Y/N):")
if h=="Y" or h=="y":
pass
elif h=="N" or h=="n":
c=False
else:
print("Wrong symbol!")
I am trying to make a Tic Tac Toe that is played by two bots, the tic tac toe works perfectly and shows me who wins but it breaks when they draw, I have tried different solutions as to trying to fix it but I have no clue how it's done. Basically the bots choose a random number from a list of 1-9 and that number corresponds to a place on the board which then turns into an "O" or an "X". If they get three in a row they win. For every move, the number generated randomly will be deducted from the list and that would make the other bot choose a number that hasn't been used yet and vice versa. The issue here is when the game draws(list becomes empty) I have no idea how to stop it and prompt the user with a message that says draw or X/O won.
This is the program:
import random,time
xoro = ["X","O"]
line1 = [1,2,3]
line2 = [4,5,6]
line3 = [7,8,9]
pause = False
starter = ""
botlist = [1,2,3,4,5,6,7,8,9]
def displayboard():
print(" ")
print(" ")
print(line1)
print(line2)
print(line3)
def xbot():
botnumber = random.choice(botlist)
if botnumber in line1:
for n,i in enumerate(line1):
if botnumber == i:
line1[n] = 'X'
botlist.remove(i)
return("X")
elif botnumber in line2:
for n,i in enumerate(line2):
if botnumber == i:
line2[n] = 'X'
botlist.remove(i)
return "X"
elif botnumber in line3:
for n,i in enumerate(line3):
if botnumber == i:
line3[n] = 'X'
botlist.remove(i)
return "X"
def obot():
botnumber = random.choice(botlist)
if botnumber in line1:
for n,i in enumerate(line1):
if botnumber == i:
line1[n] = 'O'
botlist.remove(i)
return("O")
elif botnumber in line2:
for n,i in enumerate(line2):
if botnumber == i:
line2[n] = 'O'
botlist.remove(i)
return "O"
elif botnumber in line3:
for n,i in enumerate(line3):
if botnumber == i:
line3[n] = 'O'
botlist.remove(i)
return "O"
def checkwin():
if line1[0] == "X" and line1[1] == "X" and line1[2] == "X":
return "X"
pause = True
elif line2[0] == "X" and line2[1] == "X" and line2[2] == "X":
return "X"
pause = True
elif line3[0] == "X" and line3[1] == "X" and line3[2] == "X":
return "X"
pause = True
elif line1[0] == "X" and line2[0] == "X" and line3[0] == "X":
return "X"
pause = True
elif line1[1] == "X" and line2[1] == "X" and line3[1] == "X":
return "X"
pause = True
elif line1[2] == "X" and line2[2] == "X" and line3[2] == "X":
return "X"
pause = True
elif line1[0] == "X" and line2[1] == "X" and line3[2] == "X":
return "X"
pause = True
elif line1[2] == "X" and line2[1] == "X" and line3[0] == "X":
return "X"
pause = True
elif line1[0] == "O" and line1[1] == "O" and line1[2] == "O":
return "O"
pause = True
elif line2[0] == "O" and line2[1] == "O" and line2[2] == "O":
return "O"
pause = True
elif line3[0] == "O" and line3[1] == "O" and line3[2] == "O":
return "O"
pause = True
elif line1[0] == "O" and line2[0] == "O" and line3[0] == "O":
return "O"
pause = True
elif line1[1] == "O" and line2[1] == "O" and line3[1] == "O":
return "O"
pause = True
elif line1[2] == "O" and line2[2] == "O" and line3[2] == "O":
return "O"
pause = True
elif line1[0] == "O" and line2[1] == "O" and line3[2] == "O":
return "O"
pause = True
elif line1[2] == "O" and line2[1] == "O" and line3[0] == "O":
return "O"
pause = True
def checkdraw():
if not botlist:
return True
else:
return False
returned = ""
def start():
return(random.choice(xoro))
winner = checkwin()
draw = checkdraw()
while (winner == None) or (draw == False):
displayboard()
if returned == "X":
returned = obot()
else:
returned = xbot()
winner = checkwin()
draw = checkdraw()
displayboard()
if winner == "X":
print(f"Winner is {winner}")
elif winner == "O":
print(f"Winner is {winner}")
else:
print(f"Game draw? {draw}")
The main issue is right here at the end.
Thank you very much for reading.
The problem is your while loop condition.
It should not be an or but an and. You want to continue the loop when both those conditions hold (i.e. there is no winner, and it is not a draw), and exit when either one does not hold (i.e. there is a winner or it is a draw):
while winner is None and not draw:
So I started a project for TicTacToe, and also I am a beginner so I don't know how to do a lot of stuff. This the result of the half hour of coding.
I seem to have problems with debugging why Player2 isn't taking turn.
So it asks to add more details, then I will write a few more lines, and it still asking me to add more lines.
Finally...
#Simple TicTacToe(Not so much)
#November 26, 2018
#1 is X, 2 is O
data = ["1","2","3","4","5","6","7","8","9"]
play = True
replay = False
chs = ""
ppl = 0
def ques():
global chs
global replay
a = int(input(f"{chs}:Postion(Classic Computer Number Pad Order, in number form,1-9): "))
if chs == "Player 1":
if data[a-1] != "X" and data[a-1] != "O":
data[a-1] = "X"
replay = False
else:
print("PLACE TAKEN! CHOOSE ANOTHER ONE")
replay = True
elif chs == "Player 2":
if data[a-1] != "O" and data[a-1] != "X":
data[a-1] = "O"
replay = False
else:
print("PLACE TAKEN! CHOOSE ANOTHER ONE")
replay = True
if chs == "Player 1" and replay == True:
chs = "Player 2"
elif chs == "Player 2" and replay == True:
chs = "Player 1"
def board():
print(str(data[6])+"|"+str(data[7])+"|"+str(data[8]))
print(str(data[3])+"|"+str(data[4])+"|"+str(data[5]))
print(str(data[0])+"|"+str(data[1])+"|"+str(data[2]))
def checkX():
if data[0] == "X" and data[1] == "X" and data[2] == "X":
print("Player 1 WON")
return True
elif data[3] == "X" and data[4] == "X" and data[5] == "X":
print("Player 1 WON")
return True
elif data[6] == "X" and data[7] == "X" and data[8] == "X":
print("Player 1 WON")
return True
elif data[0] == "X" and data[3] == "X" and data[6] == "X":
print("Player 1 WON")
return True
elif data[1] == "X" and data[4] == "X" and data[7] == "X":
print("Player 1 WON")
return True
elif data[2] == "X" and data[5] == "X" and data[8] == "X":
print("Player 1 WON")
return True
elif data[0] == "X" and data[4] == "X" and data[8] == "X":
print("Player 1 WON")
return True
elif data[2] == "X" and data[4] == "X" and data[6] == "X":
print("Player 1 WON")
return True
def checkO():
if data[0] == "O" and data[1] == "O" and data[2] == "O":
print("Player 2 WON")
return True
elif data[3] == "O" and data[4] == "O" and data[5] == "O":
print("Player 2 WON")
return True
elif data[6] == "O" and data[7] == "O" and data[8] == "O":
print("Player 2 WON")
return True
elif data[0] == "O" and data[3] == "O" and data[6] == "O":
print("Player 2 WON")
return True
elif data[1] == "O" and data[4] == "O" and data[7] == "O":
print("Player 2 WON")
return True
elif data[2] == "O" and data[5] == "O" and data[8] == "O":
print("Player 2 WON")
return True
elif data[0] == "O" and data[4] == "O" and data[8] == "O":
print("Player 2 WON")
return True
elif data[2] == "O" and data[4] == "O" and data[6] == "O":
print("Player 2 WON")
return True
def main():
global chs
player = input("Which shape go first?('x'for player1 or 'o'for player2): ")
if player == "x" or player == "X":
print("Player 1 will go first.")
chs = "Player 1"
elif player == "o" or player == "O":
print("Player 2 will go first.")
chs = "Player 2"
while play:
ques()
board()
if checkX():
break
if checkO():
break
print("\n"*10)
main()
It looks like you're trying to change the player in section 2 there.
But in section 1 of this code, you're setting replay to false. Then it never gets to change to the next player in section 2.
Perhaps you meant to check for the 'play' variable in section 2?
# section 1
if chs == "Player 1":
if data[a-1] != "X" and data[a-1] != "O":
data[a-1] = "X"
replay = False # after making their turn replay is false.
else:
print("PLACE TAKEN! CHOOSE ANOTHER ONE")
replay = True
elif chs == "Player 2":
if data[a-1] != "O" and data[a-1] != "X":
data[a-1] = "O"
replay = False
else:
print("PLACE TAKEN! CHOOSE ANOTHER ONE")
replay = True
# section 2
if chs == "Player 1" and replay == True: # replay is checked here, but is false
chs = "Player 2" # never gets run
elif chs == "Player 2" and replay == True: # replay is checked again here, but is false once more
chs = "Player 1" # never gets run
You never tell your program to change the player. You may fix it by alternating the value of chs after every turn, for example:
if chs == "Player 1":
if data[a-1] != "X" and data[a-1] != "O":
data[a-1] = "X"
replay = False
chs = "Player 2"
else:
print("PLACE TAKEN! CHOOSE ANOTHER ONE")
replay = True
elif chs == "Player 2":
if data[a-1] != "O" and data[a-1] != "X":
data[a-1] = "O"
replay = False
chs = "Player 1"
else:
print("PLACE TAKEN! CHOOSE ANOTHER ONE")
replay = True
I'm having trouble trying to resolve this error:
SyntaxWarning: name 'meaning5' is assigned to before global declaration
Basically my program needs to allow the user to input their name, the program then calculates the users lucky number based on the assignment of a=1, b=2 etc.
This is my code so far:
from time import sleep
tempNumb = 0
tempLNN1a = 0
tempLNN1b = 0
tempLNN2a = 0
tempLNN2b = 0
varLNN1 = 0
varLNN2 = 0
LNN = 0
tempLNNa = 0
tempLNNb = 0
templetter = "nothing"
meaning1 = "Natural leader"
meaning2 = "Natural peacemaker"
meaning3 = "Creative and optimistic"
meaning4 = "Hard worker"
meaning5 = "Value freedom"
meaning6 = "Carer and a provider"
meaning7 = "Thinker"
meaning8 = "Have diplomatic skills"
meaning9 = "Selfless and generous"
global templetter
global tempNumb
global tempLNN1a
global tempLNN1b
global tempLNN2a
global tempLNN2b
global varLNN1
global varLNN1
global LNN
global tempLNNa
global tempLNNb
global meaning1
global meaning2
global meaning3
global meaning4
global meaning5
global meaning6
global meaning7
global meaning8
global meaning9
def mainprogram():
sleep(1)
print("-----------------------")
print("Welcome to LUCKY NAME \n NUMBERS")
print("-----------------------")
sleep(1)
firstname = input("Please input your first \nname in all capitals\n")
if firstname == firstname.upper():
print("-----------------------")
sleep(1)
surname = input("Please input your surname \nin all capitals\n")
if surname == surname.upper():
print("-----------------------")
print("Calculating your Lucky \nName Number...")
for i in range(len(firstname)):
templetter = firstname[i]
calculate()
tempfirstname()
for i in range(len(surname)):
templetter = surname[i]
calculate()
tempsurname()
finalcalculate()
def calculate():
if templetter == "A":
tempNumb = 1
elif templetter == "B":
tempNumb = 2
elif templetter == "C":
tempNumb = 3
elif templetter == "D":
tempNumb = 4
elif templetter == "E":
tempNumb = 5
elif templetter == "F":
tempNumb = 6
elif templetter == "G":
tempNumb = 7
elif templetter == "H":
tempNumb = 8
elif templetter == "I":
tempNumb = 9
elif templetter == "J":
tempNumb = 1
elif templetter == "K":
tempNumb = 2
elif templetter == "L":
tempNumb = 3
elif templetter == "M":
tempNumb = 4
elif templetter == "N":
tempNumb = 5
elif templetter == "O":
tempNumb = 6
elif templetter == "P":
tempNumb = 7
elif templetter == "Q":
tempNumb = 8
elif templetter == "R":
tempNumb = 9
elif templetter == "S":
tempNumb = 1
elif templetter == "T":
tempNumb = 2
elif templetter == "U":
tempNumb = 3
elif templetter == "V":
tempNumb = 4
elif templetter == "W":
tempNumb = 5
elif templetter == "X":
tempNumb = 6
elif templetter == "Y":
tempNumb = 7
elif templetter == "Z":
tempNumb = 8
else:
"You managed to break it."
mainprogram()
def tempfirstname():
varLNN1 = varLNN1 + tempNumb
def tempsurname():
varLNN2 = varLNN2 + tempNumb
def finalcalculate():
varLNN1 = str(varLNN1)
varLNN2 = str(varLNN2)
tempLNN1a = varLNN1[0]
tempLNN1b = varLNN1[1]
tempLNN2a = varLNN2[0]
tempLNN2b = varLNN2[1]
varLNN1 = int(tempLNN1a) + int(tempLNN1b)
varLNN2 = int(tempLNN2a) + int(tempLNN2b)
LNN = varLNN1 + varLNN2
LNN = str(LNN)
tempLNNa = LNN[0]
tempLNNb = LNN[1]
LNN = int(tempLNNa) + int(tempLNNb)
if LNN == 1 or "1":
print("Your Lucky Name Number is - " + str(LNN) + " and it means you are a " + meaning1)
loop()
elif LNN == 2 or "2":
print("Your Lucky Name Number is - " + str(LNN) + " and it means you are a " + meaning2)
loop()
elif LNN == 3 or "3":
print("Your Lucky Name Number is - " + str(LNN) + " and it means you are " + meaning3)
loop()
elif LNN == 4 or "4":
print("Your Lucky Name Number is - " + str(LNN) + " and it means you are a " + meaning4)
loop()
elif LNN == 5 or "5":
print("Your Lucky Name Number is - " + str(LNN) + " and it means you " + meaning5)
loop()
elif LNN == 6 or "6":
print("Your Lucky Name Number is - " + str(LNN) + " and it means you are a " + meaning6)
loop()
elif LNN == 7 or "7":
print("Your Lucky Name Number is - " + str(LNN) + " and it means you are a " + meaning7)
loop()
elif LNN == 8 or "8":
print("Your Lucky Name Number is - " + str(LNN) + " and it means you " + meaning8)
loop()
elif LNN == 9 or "9":
print("Your Lucky Name Number is - " + str(LNN) + " and it means you are " + meaning9)
loop()
else:
print("Somehow your lucky name number is too high...")
mainprogram()
Python is different than C, you don't have to declare a variable global, if you are using a global variable in a function, there you need to use global keyword.
For example:
meaning5 = "Value freedom"
def somefunction():
global meaning5
print meaning5
and as linsug has said use lists.
Simply define all the global variable in a single function like :
def global_variables():
global var_1
global var_2
global var_3
global var_4
.
global var_n
And before calling anything else in main, call this function first:
if __name__ == "__main__":
global_variables()
# Rest of your code.
Might it help!. Thanx
in these days i have been struggling with minimax algorithm and i can say i finally understood it ( thank to another post in stackoverflow). Therefore i opened my editor and i tried to implement it into a EXTREMELY simple ( do not blame me for code please :P) tic tac toe, just to give a try out. everything is working, but the computer move function always retrieve me -1. i'm not asking you to give me code, just the "why" it does that. i have searched throught the code many times, but found nothing . The code is probably really similar to another one i found on the web. here's my code :
# COMPUTER AI
def computer_move():
best_move = minimax_recurse(game_board,active_player, 0)
print "The best move is ", best_move
make_move(game_board,best_move, active_player)
print "COMPUTER MOVE DONE"
def minimax_recurse(game_board,player,depth):
winner = is_winner(game_board)
if winner == active_player :
return 1
elif winner is not active_player :
return -1
elif len(get_move_list(game_board)) == 0 :
return 0
if player == player1 :
other_player = player2
other_player = player1
if player == active_player :
alpha = -1
alpha = 1
movelist = get_move_list(game_board)
for move in movelist :
board2 = game_board
make_move(board2,move,player)
subalpha = minimax_recurse(board2, other_player, depth + 1)
if player == active_player :
if depth == 0 and alpha <= subalpha:
best_move = move
alpha = max(alpha,subalpha)
return alpha
else :
alpha = min(alpha,subalpha)
return alpha
# BOARD FUNCTIONS
game_board = ([1,2,3],[4,5,6],[7,8,9])
def print_board(board) :
for row in board :
print row
def make_move(game_board,player_move,active_player):
x = 0
y = 0
player_move = int(player_move)
if player_move == 1 :
x = 0
y = 0
elif player_move == 2 :
x = 0
y = 1
elif player_move == 3 :
x = 0
y = 2
elif player_move == 4 :
x = 1
y = 0
elif player_move == 5 :
x = 1
y = 1
elif player_move == 6 :
x = 1
y = 2
elif player_move == 7 :
x = 2
y = 0
elif player_move == 8 :
x = 2
y = 1
elif player_move == 9 :
x = 2
y = 2
elif player_move >= 10 :
print "value is too high"
skip = False
return board
if game_board[x][y] == "O" or game_board[x][y] == "X" :
print "move not avaiable"
return game_board
game_board[x][y] = active_player
return game_board
def is_winner(board):
for i in range (0,3) :
if board[i][0] == player1 and board[i][1] == player1 and board[i][2] == player1 :
return player1
if board[i][0] == player2 and board[i][1] == player2 and board[i][2] == player2 :
return player2
# checking for obliqual, that's quite bad and slow check but it works
if board[0][0] == player1 and board[1][1] == player1 and board[2][2] == player1 :
return player1
if board[0][0] == player2 and board[1][1] == player2 and board[2][2] == player2 :
return player2
if board[2][0] == player1 and board[1][1] == player1 and board[0][2] == player1 :
return player1
if board[2][0] == player2 and board[1][1] == player2 and board[0][2] == player2 :
return player2
return None
def get_move_list (game_board) :
move = [0]
for row in game_board :
for i in row :
if isinstance(i,int) == True :
move.append(i)
move.remove(0)
return move
# Main Loop
player1 = "X"
player2 = "O"
print_board(game_board)
while True :
active_player = player1
# this is for player move
print get_move_list(game_board)
player_move = int(raw_input("Please insert your move >>> "))
make_move(game_board,player_move,active_player)
print_board(game_board)
if is_winner(game_board) == player1 :
print "Player1 is the winner"
break
if is_winner(game_board) == player2 :
print "Player2 is the winner"
break
print get_move_list(game_board)
# computer time
active_player = player2
computer_move()
print_board(game_board)
if is_winner(game_board) == player1 :
print "Player1 is the winner"
break
if is_winner(game_board) == player2 :
print "Player2 is the winner"
break
Without debugging all your code, one thing that seems wrong is your use of "best_move" variable (global uninitialized) as both a holder for a move as well as holder for -1/0/+1 result of minimax_recurse. So it's being overwritten by your minmax algorithm. You need more variables, clearer initialization of them, and consistent usage.
The root cause of the -1 always returning is that winner is not active_player returns True when winner is None. You could use a variable to keep track of the other (inactive) player, or you could use the ternary operator: elif winner is (player1 if player2 == active_player else player2)
Though that's not the only issue:
if player == active_player :
alpha = -1
alpha = 1
That will always set alpha to 1. The lines directly above that have the same problem. The variable naming issue pointed out in the other answer is also true.