Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 23 additions & 25 deletions tic_tac_toe.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@

def display_instructions():
# Displaying game Instructions.
print \
"""
print("""
Welcome to Tic-Tac-Toe, a showdown between a Human Brain and an
Intelligent Computer.

Expand All @@ -25,20 +24,20 @@ def display_instructions():
----------
6 | 7 | 8

"""
""")

def ask_yes_no(question):
#Ask a yes or no question
response = None
while response not in ("y" , "n"):
response = raw_input(question).lower()
response = input(question).lower()
return response

def ask_number(question, low, high):
#Ask for a number withing a range
response = None
while response not in range(low,high):
response = int(raw_input(question))
response = int(input(question))
return response


Expand All @@ -48,12 +47,12 @@ def pieces():
go_first = ask_yes_no("Do you want to go first? (y/n)")

if go_first == 'y':
print "\nThen take the first move. You will need it."
print("\nThen take the first move. You will need it.")
human = X
computer = O

else:
print "\nYou are brave! I'll go first."
print("\nYou are brave! I'll go first.")
computer = X
human = O

Expand All @@ -71,11 +70,11 @@ def new_board():
def display_board(board):
#Display 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"
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 legal_moves(board):
#If an empty square is found, that is a legal move, otherwise not a legal move
Expand Down Expand Up @@ -108,34 +107,34 @@ def human_move(board, human):
while move not in legal:
move = ask_number("Where will you move? (0-8): ", 0, NUM_SQUARES)
if move not in legal:
print "That square is already occupied, foolish human. Choose another.\n"
print "Cool."
print("That square is already occupied, foolish human. Choose another.\n")
print("Cool.")
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 ",
print("I shall take square number ")

for move in legal_moves(board):
board[move] = computer
if winner(board) == computer:
print move
print(move)
return move
board[move] = EMPTY

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

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


Expand All @@ -147,17 +146,16 @@ def next_turn(turn):

def congrat_winner(the_winner, computer, human):
if the_winner != TIE:
print the_winner, "won!\n"
print(the_winner, "won!\n")
else:
print "It's a tie. \n"
print("It's a tie. \n")

if the_winner == computer:
print "I triumph once again. It proves that computers are superior to humans!"
print("I triumph once again. It proves that computers are superior to humans!")
elif the_winner == human:
print "No, no! It cannot be. Somehow you tricked me, human! \n"\
"But never again. I, the computer, so swears it."
print("No, no! It cannot be. Somehow you tricked me, human! \n","But never again. I, the computer, so swears it.")
elif the_winner == TIE:
print "Lucky Human. You managed to tie me. Go celebrate for this is the best you will ever achieve."
print("Lucky Human. You managed to tie me. Go celebrate for this is the best you will ever achieve.")

def main():
display_instructions()
Expand All @@ -183,7 +181,7 @@ def main():
#START

main()
raw_input("Press enter to exit")
input("Press enter to exit")