This is a very early project in my Python career. Only watched a few tutorials and then jumped in so ELI5 please.
It runs a couple of turns but then it just freezes and im not sure why. It takes turns and then says when there is a winner. There is a bot with real bot logic.
The game starts by defining a spots array which is what the users see. There is 10 spots so it was easier for me to use the array spots 1 for the 1st spot (bad use of arrays, i know). There is a similar thing with spotBooleans which is used for checking whether a spot is taken. The numbers in the 90's are defaults. When a player places a spot, it turns that into an int 1, and enemy will be an int 2.
Most of the action is at the bottom with the while playing loop where it prints most of the data and asks you where to go. Then it checks for winners after you go, and goes to trigger the bot which uses basic algorithms and logic to try to win like a human would. Currently has nothing for stopping human moves. Then it checks if the bot is the winner.
Somewhere here it just stops in its tracks and I can not find why.
Again, I know this is poor code. I know some Java but I am not good at Python so far. Just trying to get the logic right and then the clean code will come up next along with simplifying things.
import random
import time
spots = ["", "-", "-", "-", "-", "-", "-", "-", "-", "-"]
spotBooleans = [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
start = False
whosturn = 1
while True:
spots = ["", "-", "-", "-", "-", "-", "-", "-", "-", "-"]
spotBooleans = [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
start = False
whosturn = 1
while start == False:
playerchoice = input("Would you like to be 'x' or 'o'")
x = False
o = False
if playerchoice == "x" or playerchoice == "X":
x = True
print("Player is X")
start = True
elif playerchoice == "o" or playerchoice == "O" or playerchoice == "0":
o = True
print("Player is O")
start = True
else:
print("Try again.\n\n")
start = False
print("Starting: ")
playing = True
def gorobot(whosturn):
global x
global spots
global spotBooleans
global playing
global start
if x:
robotsymbol = "o"
else:
robotsymbol = "x"
while whosturn == 2:
if spotBooleans[1] == spotBooleans[2] == 2:
if spots[3] == "-":
spots[3] = robotsymbol
spotBooleans[3] = 2
whosturn = 1
elif spotBooleans[3] == spotBooleans[2] == 2:
if spots[1] == "-":
spots[1] = robotsymbol
spotBooleans[1] = 2
whosturn = 1
elif spotBooleans[1] == spotBooleans[3] == 2:
if spots[2] == "-":
spots[2] = robotsymbol
spotBooleans[2] = 2
whosturn = 1
elif spotBooleans[4] == spotBooleans[5] == 2:
if spots[6] == "-":
spots[6] = robotsymbol
spotBooleans[6] = 2
whosturn = 1
elif spotBooleans[4] == spotBooleans[6] == 2:
if spots[5] == "-":
spots[5] = robotsymbol
spotBooleans[5] = 2
whosturn = 1
elif spotBooleans[5] == spotBooleans[6] == 2:
if spots[4] == "-":
spots[4] = robotsymbol
spotBooleans[4] = 2
whosturn = 1
elif spotBooleans[7] == spotBooleans[8] == 2:
if spots[9] == "-":
spots[9] = robotsymbol
spotBooleans[9] = 2
whosturn = 1
elif spotBooleans[7] == spotBooleans[9] == 2:
if spots[8] == "-":
spots[8] = robotsymbol
spotBooleans[8] = 2
whosturn = 1
elif spotBooleans[8] == spotBooleans[9] == 2:
if spots[7] == "-":
spots[7] = robotsymbol
spotBooleans[7] = 2
whosturn = 1
elif spotBooleans[1] == spotBooleans[5] == 2:
if spots[9] == "-":
spots[9] = robotsymbol
spotBooleans[9] = 2
whosturn = 1
elif spotBooleans[9] == spotBooleans[5] == 2:
if spots[1] == "-":
spots[1] = robotsymbol
spotBooleans[1] = 2
whosturn = 1
elif spotBooleans[9] == spotBooleans[1] == 2:
if spots[5] == "-":
spots[5] = robotsymbol
spotBooleans[5] = 2
whosturn = 1
elif spotBooleans[3] == spotBooleans[5] == 2:
if spots[7] == "-":
spots[7] = robotsymbol
spotBooleans[7] = 2
whosturn = 1
elif spotBooleans[7] == spotBooleans[5] == 2:
if spots[3] == "-":
spots[3] = robotsymbol
spotBooleans[3] = 2
whosturn = 1
elif spotBooleans[7] == spotBooleans[3] == 2:
if spots[5] == "-":
spots[5] = robotsymbol
spotBooleans[5] = 2
whosturn = 1
elif spotBooleans[3] == spotBooleans[6] == 2:
if spots[9] == "-":
spots[9] = robotsymbol
spotBooleans[9] = 2
whosturn = 1
elif spotBooleans[3] == spotBooleans[9] == 2:
if spots[6] == "-":
spots[6] = robotsymbol
spotBooleans[6] = 2
whosturn = 1
elif spotBooleans[6] == spotBooleans[9] == 2:
if spots[3] == "-":
spots[3] = robotsymbol
spotBooleans[3] = 2
whosturn = 1
elif spotBooleans[1] == spotBooleans[4] == 2:
if spots[7] == "-":
spots[7] = robotsymbol
spotBooleans[7] = 2
whosturn = 1
elif spotBooleans[1] == spotBooleans[7] == 2:
if spots[4] == "-":
spots[4] = robotsymbol
spotBooleans[4] = 2
whosturn = 1
elif spotBooleans[4] == spotBooleans[7] == 2:
if spots[1] == "-":
spots[1] = robotsymbol
spotBooleans[1] = 2
whosturn = 1
elif spotBooleans[2] == spotBooleans[5] == 2:
if spots[8] == "-":
spots[8] = robotsymbol
spotBooleans[8] = 2
whosturn = 1
elif spotBooleans[2] == spotBooleans[8] == 2:
if spots[5] == "-":
spots[5] = robotsymbol
spotBooleans[5] = 2
whosturn = 1
elif spotBooleans[5] == spotBooleans[8] == 2:
if spots[2] == "-":
spots[2] = robotsymbol
spotBooleans[2] = 2
whosturn = 1
else:
botMove = random.randint(0, 9)
if spots[botMove] == "-":
spots[botMove] = robotsymbol
spotBooleans[botMove] = 2
whosturn = 1
else:
print("The Bot is a moron! " + str(botMove))
def checkforwinners():
if spotBooleans[1] == spotBooleans[2] == spotBooleans[3]:
print("Winner!")
return True
elif spotBooleans[4] == spotBooleans[5] == spotBooleans[6]:
print("Winner!")
return True
elif spotBooleans[7] == spotBooleans[8] == spotBooleans[9]:
print("Winner!")
return True
elif spotBooleans[1] == spotBooleans[4] == spotBooleans[7]:
print("Winner!")
return True
elif spotBooleans[2] == spotBooleans[5] == spotBooleans[8]:
print("Winner!")
return True
elif spotBooleans[3] == spotBooleans[6] == spotBooleans[9]:
print("Winner!")
return True
elif spotBooleans[1] == spotBooleans[5] == spotBooleans[9]:
print("Winner!")
return True
elif spotBooleans[3] == spotBooleans[5] == spotBooleans[7]:
print("Winner!")
return True
else:
print("No Winners!")
print(spots)
print(spotBooleans)
return False
while playing:
print(str(spots[1]) + " " + str(spots[2]) + " " + str(spots[3]))
print(str(spots[4]) + " " + str(spots[5]) + " " + str(spots[6]))
print(str(spots[7]) + " " + str(spots[8]) + " " + str(spots[9]))
print("\n")
spotChoice = input("Where would you like to place your " + playerchoice + "?")
spot = int(spotChoice)
if spots[spot] == "-":
if x:
spots[spot] = "x"
spotBooleans[spot] = 1
if checkforwinners():
playing = False
start = False
break
whosturn = 2
gorobot(2)
else:
spots[spot] = "o"
spotBooleans[spot] = 1
if checkforwinners():
playing = False
start = False
break
whosturn = 2
gorobot(2)
if checkforwinners():
playing = False
start = False
Aucun commentaire:
Enregistrer un commentaire