Reputation: 3557
I'm trying to make this game and I'm having an issue checking for wins (in my checkWin()
function). Right now I'm using X
as a test case and only checking the first column. My problem is even if there aren't X
's in the first column it will always tell me I win. I sorta understand why it's doing that, but I don't know how to only let win
be True
if the boxes are populated with X
's. I'm still working on a few kinks with this program, but I wanted to get that straightened out.
'''Tic-tac-toe game'''
import time
import random
def printBoard():
print "\n"
print " 1 | 2 | 3 "
print "____|____|____"
print " 4 | 5 | 6 "
print "____|____|____"
print " 7 | 8 | 9 "
print " | | "
print "\n"
def makeMove():
move = raw_input("\nOf the boxes numbered above, choose a move: ")
move = int(move)
return move
def boardCurrent(takenSpots):
print "",takenSpots[0]," |",takenSpots[1]," |",takenSpots[2]," "
print "____|____|____"
print "",takenSpots[3]," |",takenSpots[4]," |",takenSpots[5]," "
print "____|____|____"
print "",takenSpots[6]," |",takenSpots[7]," |",takenSpots[8]," "
print " | | "
print "\n"
def compMove(takenSpots):
move = random.randint(0,8)
if takenSpots[move] == " ":
takenSpots[move] = "O"
else:
compMove(takenSpots)
return takenSpots
def takeSpot(move):
takenSpots[move - 1] = "X"
return takenSpots
def checkWin(takenSpots):
win = False
for i in range(len(takenSpots)):
if takenSpots[i % 3 == 0]:
win = True
if win == True:
print "You win!"
else:
pass
def main():
print "\nWelcome to tic-tac-toe.\n"
printBoard()
person = makeMove()
boardCurrent(takeSpot(person))
print "Now the computer will go...\n"
time.sleep(1)
compMove(takenSpots)
boardCurrent(takenSpots)
boardCurrent(takeSpot(makeMove()))
print "Now the computer will go...\n"
time.sleep(1)
compMove(takenSpots)
boardCurrent(takenSpots)
boardCurrent(takeSpot(makeMove()))
checkWin(takenSpots)
boardCurrent(takeSpot(makeMove()))
boardCurrent(takeSpot(makeMove()))
takenSpots = [" "," "," "," "," "," "," "," "," "]
main()
Upvotes: 0
Views: 91
Reputation: 280837
for i in range(len(takenSpots)):
if takenSpots[i % 3 == 0]:
win = True
This loop is messed up. First, takenSpots
is a list of all spots; you never check that the player actually took any spots. Second, your if
checks whether any of the spots in the list are in the left column, not whether the spots form 3 in a row. Third, you have nothing to indicate who won. You need to either check for a specific player's victory, or have a more informative return value that indicates which player won.
The simple way to perform the check is to make a list of the indices for each row, column, and diagonal, then iterate over those and check whether anyone is occupying an entire line:
win_positions = [
(0, 3, 6),
(1, 4, 7),
(2, 5, 8),
(0, 1, 2),
(3, 4, 5),
(6, 7, 8),
(0, 4, 8),
(2, 4, 6),
]
for line in win_positions:
if all(takenSpots[position] == player for position in line):
return some_sort_of_indicator_that_that_player_won
Upvotes: 1
Reputation: 164
I made a Tic-Tac-Toe game a while ago. I used two giant If / And statements to check for a winner on either team. Not the best way of doing it, but it worked in the end.
It was in visual basic, but it looked somewhat like this:
if (conerTL & middle & conerBR == "X" or etc...) {}else if (etc.. == "O"){}
Upvotes: 0