Matt
Matt

Reputation: 3557

Restart a tictactoe game after a win

In my checkWin() below I'm trying to give an option of quitting or restarting when the game has been won. With what I have currently it will allow me to 'restart' but it's actually just letting me continue to play with the current board. In other words, I can't figure out a way to start fresh. What's more is that I don't think what I've done in checkWin() is very clean. Looking for pointers on what to do.

(I'm aware there are other kinks in this script, but I'm working one thing at a time)

'''Tic-tac-toe game'''

import sys
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("\nIt's your turn. Choose a box between 1 and 9: \n")
    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):
    if takenSpots[move - 1] != " ":
        print "That spot is taken, choose another."
        takeSpot(makeMove())
    else:
        takenSpots[move - 1] = "X"
    return takenSpots

def checkWin(takenSpots):
    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] == "X" for position in line):
            win = True
            print "You win."
            if win == True:
                endgame = int(raw_input("Would you like to play another round?\n1) Yes\n2) No, this sucks. I want to quit."))
            if endgame == 1:
                main()
            if endgame == 2:
                sys.exit()
            return win
        if all(takenSpots[position] == "O" for position in line):
            win = True
            print "The computer won."
            return win

def clearBoard():
    takenSpots = [" "," "," "," "," "," "," "," "," "]
    return takenSpots

def main():
    boardCurrent(takeSpot(makeMove()))
    checkWin(takenSpots)
    print "Now the computer will go...\n"
    time.sleep(1)
    compMove(takenSpots)
    boardCurrent(takenSpots)
    checkWin(takenSpots)

takenSpots = [" "," "," "," "," "," "," "," "," "]

print "\nWelcome to tic-tac-toe."
win = False
printBoard()
main() 
while win == False:
    main()

Upvotes: 0

Views: 2347

Answers (1)

Totem
Totem

Reputation: 7349

Without showing you exactly how to do it, as you are capable of that I imagine, I would suggest you nest the whole program in a while loop like this:

while True:
    program...

And when a game is won or tied, ask the user for input, like whether to quit or restart:

Enter q to quit or r to restart # for instance

if q is entered, simply break from the while loop with the break statement, and end the program, or else, for a restart, loop back, redrawing the board and well, basically just reset any variable where necessary. With some thought for the loop it is not too difficult.

checkwin:

I would re-evaluate the usefulness/necessity of the variable 'win'. Is it really needed? If so, is it being used properly? Maybe if you implement the while loop solution this issue will become more apparent. I see you have a loop, while win == False, but I'm not sure that win always gets evaluated properly for the situation where the player wins.

Upvotes: 3

Related Questions