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
Related
I just started to learn python a few days ago. I made this for practice but the win counter does not increase after a win. I would also like to make the games run continually one after another. Any help would be appreciated.
print("Welcome to Rock | Paper | Scissors")
player1_wincount = 0
player2_wincount = 0
x = input("Would you like to play a game?\n")
if x == "Yes" or "yes" or "YES" or "Yeah" or "yeah" or "Yup" or "yup" or "Yessir" or "yessir":
x = 1
player1_name = input("Player 1, please enter your name:\n")
player1_age = input("Player 1, please enter your age:\n")
player2_name = input("Player 2, please enter your name:\n")
player2_age = input("Player 2, please enter your age:\n")
else: x = -1
while x > 0:
round = 1
print("Round", round)
player1_selection = input("Player 1, please select from the following options:\n1:Rock\n2:Paper\n3:Scissors\n4:EXIT")
player2_selection = input("Player 2, please select from the following options:\n1:Rock\n2:Paper\n3:Scissors\n4:EXIT")
round + 1
if player1_selection == "1" and player2_selection == 1:
print("Tie!")
elif player1_selection == "1" and player2_selection == 2:
print("Player 2 Wins!")
player2_wincount += 1
elif player1_selection == "1" and player2_selection == 3:
print("Player 1 Wins")
player1_wincount += 1
elif player1_selection == "2" and player2_selection == 1:
print("Player 1 Wins!")
player1_wincount += 1
elif player1_selection == "2" and player2_selection == 2:
print("Tie!")
elif player1_selection == "2" and player2_selection == 3:
print("Player 2 Wins!")
player2_wincount += 1
elif player1_selection == "3" and player2_selection == 1:
print("Player 2 Wins!")
player2_wincount += 1
elif player1_selection == "3" and player2_selection == 2:
print("Player 1 Wins!")
player1_wincount += 1
elif player1_selection == "3" and player2_selection == 3:
print("Tie!")
else:
x = -1
break
print (player1_name,"has", player1_wincount, "wins!\n", player2_name, "has", player2_wincount, "wins!")
You made two small mistakes in your case, the indentation of your if-else statements, and placing quotes around player 2's selections.
print("Welcome to Rock | Paper | Scissors")
player1_wincount = 0
player2_wincount = 0
x = input("Would you like to play a game?\n")
if x == "Yes" or "yes" or "YES" or "Yeah" or "yeah" or "Yup" or "yup" or "Yessir" or "yessir":
x = 1
player1_name = input("Player 1, please enter your name:\n")
player1_age = input("Player 1, please enter your age:\n")
player2_name = input("Player 2, please enter your name:\n")
player2_age = input("Player 2, please enter your age:\n")
else: x = -1
while x > 0:
round = 1
print("Round", round)
player1_selection = input("Player 1, please select from the following options:\n1:Rock\n2:Paper\n3:Scissors\n4:EXIT")
player2_selection = input("Player 2, please select from the following options:\n1:Rock\n2:Paper\n3:Scissors\n4:EXIT")
round + 1
if player1_selection == "1" and player2_selection == "1":
print("Tie!")
elif player1_selection == "1" and player2_selection == "2":
print("Player 2 Wins!")
player2_wincount += 1
elif player1_selection == "1" and player2_selection == "3":
print("Player 1 Wins")
player1_wincount += 1
elif player1_selection == "2" and player2_selection == "1":
print("Player 1 Wins!")
player1_wincount += 1
elif player1_selection == "2" and player2_selection == "2":
print("Tie!")
elif player1_selection == "2" and player2_selection == "3":
print("Player 2 Wins!")
player2_wincount += 1
elif player1_selection == "3" and player2_selection == "1":
print("Player 2 Wins!")
player2_wincount += 1
elif player1_selection == "3" and player2_selection == "2":
print("Player 1 Wins!")
player1_wincount += 1
elif player1_selection == "3" and player2_selection == "3":
print("Tie!")
else:
x = -1
break
print (player1_name,"has", player1_wincount, "wins!\n", player2_name, "has", player2_wincount, "wins!")
Additionally the code for this program can be more condensed using for loops.
print("Welcome to Rock | Paper | Scissors")
player1_wincount = 0
player2_wincount = 0
round = 1
x = input("Would you like to play a game?\n")
if x == "Yes" or "yes" or "YES" or "Yeah" or "yeah" or "Yup" or "yup" or "Yessir" or "yessir":
a = 1
player1_name = input("Player 1, please enter your name:\n")
# player1_age = input("Player 1, please enter your age:\n")
player2_name = input("Player 2, please enter your name:\n")
# player2_age = input("Player 2, please enter your age:\n")
else:
a = -1
while a > 0:
print("Round", round)
player1_selection = int(input("Player 1, please select from the following options:\n1:Rock\n2:Paper\n3:Scissors\n4:EXIT\n"))
player2_selection = int(input("Player 2, please select from the following options:\n1:Rock\n2:Paper\n3:Scissors\n4:EXIT\n"))
if player1_selection == 1 and player2_selection == 1:
print("Tie!")
elif player1_selection == 1 and player2_selection == 2:
print("Player 2 Wins!")
player2_wincount += 1
elif player1_selection == 1 and player2_selection == 3:
print("Player 1 Wins")
player1_wincount += 1
elif player1_selection == 2 and player2_selection == 1:
print("Player 1 Wins!")
player1_wincount += 1
elif player1_selection == 2 and player2_selection == 2:
print("Tie!")
elif player1_selection == 2 and player2_selection == 3:
print("Player 2 Wins!")
player2_wincount += 1
elif player1_selection == 3 and player2_selection == 1:
print("Player 2 Wins!")
player2_wincount += 1
elif player1_selection == 3 and player2_selection == 2:
print("Player 1 Wins!")
player1_wincount += 1
elif player1_selection == 3 and player2_selection == 3:
print("Tie!")
else:
a = -1
break
round += 1
print (player1_name,"has", player1_wincount, "wins!\n", player2_name, "has", player2_wincount, "wins!")
There were a few mistakes in the code, well done for your project considering its your first few days of coding though!
First of all, all the if/elif/else conditions weren't indented into the while loop
The conditions of the if/elif statements weren´t of the same type. This means that the player 1 selection was always checking for the string "1" and player 2 selection was checking for a number/integer. I changed the input type to int, you can do it the other way around though if you wish
The round 1 had to be definded outside of the while loop as it got redefined as round 1 every time the loop restarted.
I am making a rock paper scissors program in python.
I have written a function which should return the result of a game but it only returns the else condition, what do?
Here is the code of the program.
import random
def emove():
computer=["r","p","s"]
return(random.choice(computer))
def winner():
if user == "r" and emove == "p":
return("Computer won!")
elif user == "p" and emove == "r":
return("You won!")
elif user == "p" and emove == "s":
return("Computer won!")
elif user == "s" and emove == "p":
return("You won!")
elif user == "r" and emove == "s":
return("You won!")
elif user == "s" and emove == "p":
return("You won!")
elif user == emove:
return("Tie")
else:
return("Wrong input")
user=input("Welcome to Rock Paper Scissors, to play enter the initial letter of the choice you make:").lower()
while user:
print("Computer chose",emove(),",you chose", (user), winner() )
playagain = input("Play again? (y/n): ").lower()
if playagain =="n":
print("Thanks for playing")
break
emove is a function , so emove == "p" always return Flase
So I suggest you the following minor changes:
import random
def emove():
computer=["r","p","s"]
return(random.choice(computer))
def winner():
if user == "r" and em == "p":
return("Computer won!")
elif user == "p" and em == "r":
return("You won!")
elif user == "p" and em == "s":
return("Computer won!")
elif user == "s" and em == "p":
return("You won!")
elif user == "r" and em == "s":
return("You won!")
elif user == "s" and em == "p":
return("You won!")
elif user == em:
return("Tie")
else:
return("Wrong input")
user=input("Welcome to Rock Paper Scissors, to play enter the initial letter of the choice you make:").lower()
while user:
em=emove()
print("Computer chose",em,",you chose", (user), winner() )
playagain = input("Play again? (y/n): ").lower()
if playagain =="n":
print("Thanks for playing")
break
Because you are missing function parantheses emove() inside winner() function.
def winner():
print(user,emove())
var = emove() # storing random choice in variable.
if user == "r" and var == "p":
return ("Computer won!")
elif user == "p" and var == "r":
return ("You won!")
elif user == "p" and var == "s":
return ("Computer won!")
elif user == "s" and var == "p":
return ("You won!")
elif user == "r" and var == "s":
return ("You won!")
elif user == "s" and var == "p":
return ("You won!")
elif user == var:
return ("Tie")
else:
return ("Wrong input")
Your code is almost right, but you have a few things wrong with your implementation. Here is a version of your code that I think does everything you desire:
import random
def computer_move():
computer = ["r", "p", "s"]
return (random.choice(computer))
def winner(emove):
if user == "r" and emove == "p":
return ("Computer won!")
elif user == "p" and emove == "r":
return ("You won!")
elif user == "p" and emove == "s":
return ("Computer won!")
elif user == "s" and emove == "p":
return ("You won!")
elif user == "r" and emove == "s":
return ("You won!")
elif user == "s" and emove == "p":
return ("You won!")
elif user == emove:
return ("Tie")
else:
return ("Wrong input")
while True:
user = input("Welcome to Rock Paper Scissors, to play enter the initial letter of the choice you make:").lower()
emove = computer_move()
print("Computer chose", emove, ",you chose", user, winner(emove))
playagain = input("Play again? (y/n): ").lower()
if playagain == "n":
print("Thanks for playing")
break
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:
I'm having a problem with my python program and I've spent too many time trying to fix it but I can't. I was hoping you could help me.
Anyway, the problem is in:
def choose_winnerPvsP(p1,p2):
When I run it the part of Player vs Computer woks perfectly, but the part of Player vs Player gives me this error: "Local variable player_1score referenced before assignment"
I can't understand why the if's are never checked.
PS: I'm a beginner and I know that the code could be more compact, but for now I just want this thing working. Thanks in advance
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from random import *
from time import sleep
import sys
import os
os.system("cls") #ou "clear" se estiver em linux
print "******Welcome to Rock Paper Scissors Game!******\n"
playerOrComputer = str(raw_input("Would you like to play against a friend or a computer?(F or C) \n" ))
p1_score = 0
p2_score = 0
def player_turn(nome_player):
print "What do you want to do, " + nome_player + "?"
pchoice = str(raw_input("ROCK (R) PAPER (P) SCISSORS (S) or Quit\n>> "))
if pchoice == "R" or pchoice == "r":
print "You chosed ROCK"
elif pchoice == "P" or pchoice == "p":
print "You chosed PAPER"
elif pchoice == "S" or pchoice == "s":
print "You chosed SCISSORS"
elif pchoice == "quit" or pchoice == "Quit":
sys.exit()
else:
print "I didn't understand! Please repeat."
player_turn()
return pchoice
def player_turn2p(nome_player1,p2):
print "What do you want to do, " + nome_player1 + "?"
pchoice1 = str(raw_input("ROCK (R) PAPER (P) SCISSORS (S)\n>> "))
if pchoice1 == "R" or pchoice1 == "r":
pchoice1 = "ROCK"
print "You chosed ROCK"
sleep(1)
os.system("cls")
elif pchoice1 == "P" or pchoice1 == "p":
pchoice1 = "PAPER"
print "You chosed PAPER"
sleep(1)
os.system("cls")
elif pchoice1 == "S" or pchoice1 == "s":
pchoice1 = "SCISSORS"
print "You chosed SCISSORS"
sleep(1)
os.system("cls")
elif pchoice1 == "quit" or pchoice == "Quit":
sys.exit()
else:
print "I didn't understand!"
player_turn2p(nome_player1,p2)
print "What do you want to do, " + p2 + "?"
pchoice2 = str(raw_input("ROCK (R) PAPER (P) SCISSORS (S)\n>> "))
if pchoice2 == "R" or pchoice2 == "r":
print p2 + " chosed ROCK\n" + nome_player1 + " chosed " + pchoice1
elif pchoice2 == "P" or pchoice2 == "p":
print p2 + " chosed PAPER\n" + nome_player1 + " chosed " + pchoice1
elif pchoice2 == "S" or pchoice2 == "s":
print p2 + " chosed SCISSORS\n" + nome_player1 + " chosed " + pchoice1
elif pchoice1 == "quit" or pchoice == "Quit":
sys.exit()
else:
print "I didn't understand!"
player_turn2p(nome_player1,p2)
return pchoice1, pchoice2
def computer_turn():
choicecomp = randint(1,3)
if choicecomp == 1:
choicecomp = "ROCK"
elif choicecomp == 2:
choicecomp = "PAPER"
elif choicecomp == 3:
choicecomp = "SCISSORS"
return choicecomp
#1-Rock 2-Paper 3-Scissors
def choose_winnerPvsP(p1,p2):
player_choice1, player_choice2= player_turn2p(p1,p2)
if player_choice1 == "R" and player_choice2 == "R" or player_choice1 == "r" and player_choice2 == "r" or player_choice1 == "R" and player_choice2 == "r" or player_choice1 == "r" and player_choice2 == "R":
player_1score = "lose"
player_2score = "win"
print "******It's a draw!******"
elif player_choice1 == "R" and player_choice2 == "P" or player_choice1 == "r" and player_choice2 == "p" or player_choice1 == "R" and player_choice2 == "p" or player_choice1 == "r" and player_choice2 == "P":
player_1score = "lose"
player_2score = "win"
print "******" + p2 + " wins!******"
elif player_choice1 == "R" and player_choice2 == "S" or player_choice1 == "r" and player_choice2 == "s" or player_choice1 == "R" and player_choice2 == "s" or player_choice1 == "r" and player_choice2 == "S":
player_1score = "win"
player_2score = "lose"
print "******" + p1 + " wins!******"
elif player_choice1 == "P" and player_choice2 == "R" or player_choice1 == "p" and player_choice2 == "r" or player_choice1 == "P" and player_choice2 == "r" or player_choice1 == "p" and player_choice2 == "R":
player_1score = "win"
player_2score = "lose"
print "******" + p1 + " wins!******"
elif player_choice1 == "P" and player_choice2 == "P" or player_choice1 == "p" and player_choice2 == "p" or player_choice1 == "P" and player_choice2 == "p" or player_choice1 == "p" and player_choice2 == "P":
player_1score = "draw"
player_2score = "draw"
print "******It's a draw!******"
elif player_choice1 == "P" and player_choice2 == "S" or player_choice1 == "p" and player_choice2 == "s" or player_choice1 == "P" and player_choice2 == "s" or player_choice1 == "p" and player_choice2 == "S":
player_1score = "lose"
player_2score = "win"
print "******" + p2 + " wins!******"
elif player_choice1 == "S" and player_choice2 == "R" or player_choice1 == "s" and player_choice2 == "r" or player_choice1 == "S" and player_choice2 == "r" or player_choice1 == "s" and player_choice2 == "R":
player_1score = "lose"
player_2score = "win"
print "******" + p2 + " wins!******"
elif player_choice1 == "S" and player_choice2 == "P" or player_choice1 == "s" and player_choice2 == "p" or player_choice1 == "S" and player_choice2 == "p" or player_choice1 == "s" and player_choice2 == "P":
player_1score = "win"
player_2score = "lose"
print "******" + p1 + " wins!******"
elif player_choice1 == "S" and player_choice2 == "S" or player_choice1 == "s" and player_choice2 == "s" or player_choice1 == "S" and player_choice2 == "s" or player_choice1 == "s" and player_choice2 == "S":
player_1score = "draw"
player_2score = "draw"
print "******It's a draw!******"
return player_1score, player_2score
def choose_winnerPvsC(player_name,player_choice, computer_choice):
if player_choice == "R" and computer_choice == "ROCK" or player_choice == "r" and computer_choice == "ROCK":
computer_score = "draw"
player_score = "draw"
print "******It's a draw!****** "
elif player_choice == "R" and computer_choice == "PAPER" or player_choice == "r" and computer_choice == "PAPER":
computer_score = "win"
player_score = "lose"
print "******I win!****** "
elif player_choice == "R" and computer_choice == "SCISSORS" or player_choice == "r" and computer_choice == "SCISSORS":
player_score = "win"
computer_score = "lose"
print "******" + player_name + " wins!****** "
elif player_choice == "P" and computer_choice == "ROCK" or player_choice == "p" and computer_choice == "ROCK":
player_score = "win"
computer_score = "lose"
print "******" + player_name + " wins!****** "
elif player_choice == "P" and computer_choice == "PAPER" or player_choice == "p" and computer_choice == "PAPER":
computer_score = "draw"
player_score = "draw"
print "******It's a draw!******"
elif player_choice == "P" and computer_choice == "SCISSORS" or player_choice == "p" and computer_choice == "SCISSORS":
computer_score = "win"
player_score = "lose"
print "******I win!****** "
elif player_choice == "S" and computer_choice == "ROCK" or player_choice == "s" and computer_choice == "ROCK":
computer_score = "win"
player_score = "lose"
print "******I win!****** "
elif player_choice == "S" and computer_choice == "PAPER" or player_choice == "s" and computer_choice == "PAPER":
player_score = "win"
computer_score = "lose"
print "******" + player_name + " wins!****** "
elif player_choice == "S" and computer_choice == "SCISSORS" or player_choice == "s" and computer_choice == "SCISSORS":
computer_score = "draw"
player_score = "draw"
print "******It's a draw!****** "
return player_score, computer_score
def loopPvsP():
p1score = 0
p2score = 0
while True:
player_1score, player_2score = choose_winnerPvsP(p1,p2)
if player_1score == "win":
p1score += 1
elif player_2score == "win":
p2score += 1
print p1 + ":",p1score
print p2 + ":",p2score
if p1score == 5:
print "-+-+-+-+-"+ p1.upper() + "IS THE BIG WINNER!!-+-+-+-+-"
break
elif p2score == 5:
print "-+-+-+-+-"+ p2.upper() + "IS THE BIG WINNER!!-+-+-+-+-"
break
def loopPvsC():
pscore = 0
cscore = 0
while True:
pchoice = player_turn(p1c)
choicecomp = computer_turn()
print "I chosed "+ choicecomp + "!"
player_score, computer_score = choose_winnerPvsC(p1c,pchoice, choicecomp)
if computer_score == "win":
cscore += 1
elif player_score == "win":
pscore += 1
print "Computer:",cscore
print p1c + ":",pscore
if pscore == 5:
print "-+-+-+-+-YOU ARE THE BIG WINNER!!-+-+-+-+-"
break
elif cscore == 5:
print "-+-+-+-+-I'M THE BIG WINNER!!-+-+-+-+-"
break
if playerOrComputer == "f" or playerOrComputer == "F":
p1 = str(raw_input("Player 1: "))
p2 = str(raw_input("Player 2: "))
os.system("cls")
loopPvsP()
elif playerOrComputer == "c" or playerOrComputer == "C":
p1c = str(raw_input("Player: "))
os.system("cls")
loopPvsC()
else:
print "That's not what I asked!"
Meh. Debuggers are overrated -- I go years without using one in Python. Just add a final else clause to your big if/elif statement:
else:
print "Did not find answer"
print repr(player_choice), repr(computer_choice)
If that doesn't hit, then it means you have a typo somewhere else, perhaps. Anyway, you can certainly add more debugging statements -- one to each elif clause, if need be.
Which brings up another point -- you have a lot of if/elif clauses.
You might want to investigate things like dicts with values you can add together, or functions you can call, to reduce the number of if/else statements you need.
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.