Immediate Help with python program!

Jon Clements joncle at googlemail.com
Wed Dec 9 19:18:14 EST 2009


On Dec 9, 11:55 pm, Daniel <dkeepe... at yahoo.com> wrote:
> i am making a tic-tac-toe game using python. i am pretty new to it,
> but cant seem to figure this one out.
> Here is my code:
>
> X = "X"
> O = "O"
> empty = " "
> tie = "Tie"
> squares = 9
>
> def display():
>     print """Welcome to Tic-Tac-Toe. Player will play against the
> computer.
>             \nYou will move by typing in the number to the
> corresponding square below:
>
>                   0 | 1 | 2
>                 -------------
>                   3 | 4 | 5
>                 -------------
>                   6 | 7 | 8 \n"""
>
> def select():
>     question = ask("Do you want to go first? (y/n)")
>     if question == "y":
>         player = X
>         computer = O
>     else:
>         computer = X
>         player = O
>     return computer, player
>
> def newBoard():
>     board = []
>     for i in range(squares):
>         board.append(empty)
>     return board
>
> def displayBoard(board):
>     print "\n\t", board[0], "|", board[1], "|", board[2]
>     print "\t", "---------"
>     print "\t", board[3], "|", board[4], "|", board[5]
>     print "\t", "---------"
>     print "\t", board[6], "|", board[7], "|", board[8], "\n"
>
> def boardMoves(board):
>     moves = []
>     for i in range(squares):
>         if board[i] == empty:
>             moves.append(i)
>     return moves
>
> def findWinner(board):
>     win = ((0, 1, 2),
>            (3, 4, 5),
>            (6, 7, 8),
>            (0, 3, 6),
>            (1, 4, 7),
>            (2, 5, 8),
>            (0, 4, 8),
>            (2, 4, 6))
>
>     for i in win:
>         if board[i[0]] == board[i[1]] == board[i[2]] != empty:
>             winner = board[i[0]]
>             return winner
>         if empty not in board:
>             return tie
>         return None
>
> def askMove(question, low, high):
>     response = None
>     while response not in range(low, high):
>         response = int(raw_input(question))
>     return response
>
> def playerMove(board, player):
>     legal = boardMoves(board)
>     move = None
>     while move not in legal:
>         move = askMove("Pick a number where you want to move(0-8):",
> 0, squares)
>         if move not in legal:
>             print "\nThat move is taken already. Pick another."
>     return move
>
> def compMove(board, computer, player):
>     board = board[:]
>     strategy = (4, 0, 2, 6, 8, 1, 3, 5, 7)
>     print "Computer chooses:",
>
>     # if computer can win, take that move
>     for move in boardMoves(board):
>         board[move] = computer
>         if findWinner(board) == computer:
>             print move
>             return move
>         board[move] = empty
>
>     # if human can win, block that move
>     for move in boardMoves(board):
>         board[move] = player
>         if findWinner(board) == player:
>             print move
>             return move
>         board[move] = empty
>
>     # If no one can win pick best open square
>     for move in strategy:
>         if move in boardMoves(board):
>             print move
>             return move
>
> def nextTurn(turn):
>     if turn == X:
>         return 0
>     else:
>         return X
>
> def gameWinner(winner, computer, player):
>     if winner == computer:
>         print "Computer Wins. Better luck next time"
>     elif winner == player:
>         print "You win. Good job!"
>     elif winner == tie:
>         print "Tie game. Play again."
>
> def main():
>     display()
>     computer, player = select()
>     turn = X
>     board = newBoard()
>     displayBoard(board)
>
>     while not findWinner(board):
>         if turn == player:
>             move = playerMove(board, player)
>             board[move] = player
>         else:
>             move = compMove(board, computer, player)
>             board[move] = computer
>         displayBoard(board)
>         turn = nextTurn(turn)
>     winner = findWinner(board)
>     gameWinner(winner, computer, player)
>
> main()
>
> Here is my problem:
> If you hit 'n' at the beginning prompt, the computer does four moves
> automatically and wins- you can't input anything. If you hit 'y' at
> the beginning prompt, you can play but cannot win. I got three x's in
> a row and it didn't let me win. It just keeps letting
> you input numbers until the computer wins even if you have three in a
> row.
>
> If anyone can help please do asap.
> Thank you!

Someone's homework assignment is overdue/due very soon? And, I don't
believe for a second this is your code. In fact, just searching for
(the obvious Java based) function names leads me to believe you've
'butchered' it from Java code (do you not think your teacher/lecturer
can do the same?).

Someone might well help out, but I'd be surprised if you got a "here's
how to fix it response", as from my POV you haven't done any work.

Of course, I'm occasionally wrong,

Jon.










More information about the Python-list mailing list