Im trying to add six functions which are six effects to this tic tac toe game.
For instance a remove all pieces function. Also, Im trying to restart the DIE roll after every turn made.

If I can get the remove all pieces function(reall) to work I can get other functions to work as well.

``````import time
import random

DIE = random.randrange(3) + 1
X = "X"
O = "O"
SPACE = ""
TIE = "TIE"
DINNER = 9

def dispinst():
print """

0 | 1 | 2
---------
3 | 4 | 5
---------
6 | 7 | 8
"""

def yes_no(question):
""" Ask a yes/no question """
response = None
while response not in ("y", "n"):
response = raw_input(question).lower()
return response

""" Ask for a number wihin range """
response = None
while response not in range(low, high):
response = int(input(question))
return response

def pieces():
""" This is for who goes first """
fmove = yes_no ("Would you like to go first? y/n: ")
if fmove == "y":
print "\nYou will go first"
human = X
computer = O
else:
print "\nThe Computer will go first"
computer = X
human = O
return computer, human

def newboard():
""" Create a new board """
board = []
for square in range(DINNER):
board.append(SPACE)
return board

def dispboard(board):
""" Display game board on screen """
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 reall(board):
"""remove all pieces"""

def legal_moves(board):
moves = []
for square in range(DINNER):
if board [square] == SPACE:
moves.append(square)
return moves

def winner(board):
"""Determine the game winner."""
WAYS_TO_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 row in WAYS_TO_WIN:
if board[row[0]] == board[row[1]] == board[row[2]] != SPACE:
winner = board[row[0]]
return winner

if SPACE not in board:
return TIE

return None

def human_move(board, human):
legal = legal_moves(board)
rall = reall(board)
move = None
while move not in legal:
move = asknum("Where will you move? (0 - 8): ", 0, DINNER)
if move not in legal:
print "\nThat square is already occupied, foolish human. Choose another.\n"
print "Fine.."
return move

def computer_move(board, computer, human):
board = board[:]

BEST_MOVES = (4, 0, 2, 6, 8, 1, 3, 5, 7)

print "I shall take square number"

# if computer can win, take that move
for move in legal_moves(board):
board[move] = computer
if winner(board) == computer:
print move
return move
# done checking this move, undo it
board[move] = SPACE

for move in legal_moves(board):
board[move] = human
if winner(board) == human:
print move
return move
board[move] = SPACE

for move in BEST_MOVES:
if move in legal_moves(board):
print move
return move

def next_turn(turn):
if turn == X:
return O
else:
return X

def congrat_winner(the_winner, computer, human):
if the_winner != TIE:
print the_winner, "Won!\n"
else:
print "CEASE THIS FOOLISHNESS!\n"

if the_winner == computer:
print "It was not by my hand that I was once again given flesh. \n" \

elif the_winner == human:
print "NOoo, IT CANNOT BE!!!, AAAAAGGGGHHHHHHHHuaaaa!!!! \n"

elif the_winner == TIE:
print "You were lucky, human and somehow managed to tie me. \n" \

def main():
print "Welcome",
time.sleep(0.5)
print ".",
time.sleep(0.5)
print ".",
time.sleep(0.5)
print "."
time.sleep(0.8)
print "TO DIE!"
time.sleep(2)
dispinst()
computer, human = pieces()
turn = X
board = newboard()
dispboard(board)

while not winner(board):
if turn == human:
print "Your dice roll is", DIE
move = human_move(board, human)
board[move] = human
else:
move = computer_move(board, computer, human)
board[move] = computer
dispboard(board)
turn = next_turn(turn)

the_winner = winner(board)
congrat_winner(the_winner, computer, human)

main ()
raw_input("\n\nPress any key to exit")``````

## All 3 Replies

What problems are you having so far?

None, I just have no idea how to add a remove all pieces function.
Im assuming I should borrow from newboard function to do that.

Also, How do I restart the dice roll after every turn?

Okay, I have the dice roll working.
Now Im trying to figure out how to clear the board or take off a X or O off the board.

``````def dieroll(board):
DIE=random.randrange(2) + 1
print "Your dice roll is", DIE
if DIE == 1:
print "This is effect one"
elif DIE == 2:
print "This is effect two"``````

``````while not winner(board):
if turn == human:
dieroll(board)
move = human_move(board, human)
board[move] = human
else:
move = computer_move(board, computer, human)
board[move] = computer
dispboard(board)
turn = next_turn(turn)``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.