Reputation: 90
I am trying to make a tic-tac-toe game however when I try implement two players, the printed out result (current game state) is not as expected. for example when I try to put the letter 'x' in box 1 it will add random letters/numbers in different spots. I have already tried this with only one player and it wrks perfectly.
import random
a = ' '
b = ' '
c = ' '
d = ' '
e = ' '
f = ' '
g = ' '
h = ' '
i = ' '
def gamestateboard(a, b, c, d, e, f, g, h, i):
x = [a, b, c]
y = [d, e, f]
z = [g, h, i]
print(x)
print(y)
print(z)
gamestateboard(a, b, c, d, e, f, g, h, i)
def checkwin():
if a == 0 and b == 0 and c == 0:
print('Winner')
if a == 0 and d == 0 and g == 0:
print('Winner')
if g == 0 and h == 0 and i == 0:
print('Winner')
if c == 0 and e == 0 and g == 0:
print('Winner')
if a == 0 and e == 0 and i == 0:
print('Winner')
if c == 0 and f == 0 and i == 0:
print('Winner')
for i in range(1000000):
inputs = str(input('Enter a position: '))
if i%2 == 0:
if inputs == '1':
a = 0
gamestateboard(a, b, c, d, e, f, g, h, i)
checkwin()
if inputs == '2':
b = 0
gamestateboard(a, b, c, d, e, f, g, h, i)
checkwin()
if inputs == '3':
c = 0
gamestateboard(a, b, c, d, e, f, g, h, i)
checkwin()
if inputs == '4':
d = 0
gamestateboard(a, b, c, d, e, f, g, h, i)
checkwin()
if inputs == '5':
e = 0
gamestateboard(a, b, c, d, e, f, g, h, i)
checkwin()
if inputs == '6':
f = 0
gamestateboard(a, b, c, d, e, f, g, h, i)
checkwin()
if inputs == '7':
g = 0
gamestateboard(a, b, c, d, e, f, g, h, i)
checkwin()
if inputs == '8':
h = 0
gamestateboard(a, b, c, d, e, f, g, h, i)
checkwin()
if inputs == '9':
i = 0
gamestateboard(a, b, c, d, e, f, g, h, i)
checkwin()
else:
if inputs == '1':
a = 'X'
gamestateboard(a, b, c, d, e, f, g, h, i)
checkwin()
if inputs == '2':
b = 'X'
gamestateboard(a, b, c, d, e, f, g, h, i)
checkwin()
if inputs == '3':
c = 'X'
gamestateboard(a, b, c, d, e, f, g, h, i)
checkwin()
if inputs == '4':
d = 'X'
gamestateboard(a, b, c, d, e, f, g, h, i)
checkwin()
if inputs == '5':
e = 'X'
gamestateboard(a, b, c, d, e, f, g, h, i)
checkwin()
if inputs == '6':
f = 'X'
gamestateboard(a, b, c, d, e, f, g, h, i)
checkwin()
if inputs == '7':
g = 'X'
gamestateboard(a, b, c, d, e, f, g, h, i)
checkwin()
if inputs == '8':
h = 'X'
gamestateboard(a, b, c, d, e, f, g, h, i)
checkwin()
if inputs == '9':
i = 'X'
gamestateboard(a, b, c, d, e, f, g, h, i)
checkwin()
Upvotes: 1
Views: 84
Reputation: 1175
You are using i as your loop variable but also the name of a variable you use to draw in your game world.
I recommend moving away from having these variables and using indexes.
gameworld =[ [' ',' ',' '] for i in range(3)]
def draw_world():
for i in range(3):
print(gameworld[i])
then you can ask for coordinates or divide and use the modulus to use a single value like you did before. 4 // 3 = 1 4 % 3 = 1 position 4 is at [1][1]
This means you don't need your giant if input == list.
edit: Oh yeah you probably also want to do a loop with something like
no_winner = True
while no_winner:
...
you can set no_winner to False if check_win returns true and then you game doens't go on forever
edit 2: Almost forgot you have an indenting error with the else for i%2==0 though I'm guessing that may be just from when you copied and pasted into your question. You also never check to see if someone else has already taken a square so when your code does actually work a player can overwrite another player's move.
Upvotes: 1