Immediate Help with python program!

D

Daniel

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 == 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!
 
J

Jon Clements

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 == 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.
 
D

Daniel

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 == 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)

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.


Just lookin for some help man. It is my code. but im here because i
needed help and i dont know whats wrong with it. Figured someone would
offer a hand. Thanks anyway.
 
I

Intchanter / Daniel Fackrell

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.

I also got the impression that it was a homework assignment. I'll
just add that trying to bring attention to your request for help with
exclamation points and words that indicate urgency is pretty bad form
in a volunteer support community.
 
M

MRAB

Daniel said:
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:
[snip]
You problem is due to your choice of variable names, because '0' looks
too much like 'O'.

You also don't define 'ask'.
 
D

Daniel

Daniel said:
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:

[snip]
You problem is due to your choice of variable names, because '0' looks
too much like 'O'.

You also don't define 'ask'.

Appreciate the help guys. Sorry about postin it here, mistake on my
part. Thanks again.
 
G

Grant Edwards

Ahem. This is a newsgroup/mailing list, not IM. I happen to have seen
this within half an hour of you posting it, but that's just luck.
Expecting an "immediate" response is unrealistic.

Furthermore, this is comp.lang.python, a group right up there in pedantry
terms with cam.misc. Wandering in and demanding "immediate" help is just
begging for half a dozen replies that give you detailed and mind-boggling
versions of exactly what you asked for, especially if it's got nothing to
do with the answer you actually need.

An exclamation point in a subject line also tends to act as an
excellent lighting rod...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top