# global constants
X = 'X'
O = 'O'
EMPTY = ' ';
TIE = 'T';
NO_ONE = 'N';
def instructions():
print "Welcome to the ultimate man-machine showdown: Tic-Tac-Toe.";
print "--where human brain is pit against silicon processor";
print "Make your move known by entering a number, 0 - 8. The number";
print "corresponds to the desired board position, as illustrated:";
print "0 | 1 | 2";
print "---------";
print "3 | 4 | 5\n";
print "---------";
print "6 | 7 | 8\n\n";
print "Prepare yourself, human. The battle is about to begin.";
def askYesNo(question):
response =''
while (response != 'y' and response != 'n'):
print question , " (y/n): ",
response = raw_input()
return response;
def askNumber( question, high, low):
while True:
print question , " (" , low , " - " , high , "): ",
number = int(raw_input())
if (number > high or number < low):
pass
else:
break
return number;
def humanPiece():
global X,O,EMPTY,TIE,NO_ONE
go_first = askYesNo("Do you require the first move?");
if (go_first == 'y'):
print "Then take the first move. You will need it."
return X;
else:
print "Your bravery will be your undoing. . . I will go first.";
return O;
def opponent( piece):
global X,O,EMPTY,TIE,NO_ONE
if (piece == X):
return O;
else:
return X;
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];
print "\n";
def winner(board):
global X,O,EMPTY,TIE,NO_ONE
# all possible winning rows
WINNING_ROWS = [ [0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]]
TOTAL_ROWS = 8;
# if any winning row has three values that are the same (and not EMPTY),
# then we have a winner
for row in range (TOTAL_ROWS):
if ( (board[WINNING_ROWS[row][0]] != EMPTY) and
(board[WINNING_ROWS[row][0]] == board[WINNING_ROWS[row][1]]) and
(board[WINNING_ROWS[row][1]] == board[WINNING_ROWS[row][2]]) ):
return board[WINNING_ROWS[row][0]];
# since nobody has won, check for a tie (no empty squares left)
if EMPTY not in board:
return TIE;
return NO_ONE;
def isLegal(move,board):
global X,O,EMPTY,TIE,NO_ONE
return (board[move] == EMPTY);
def humanMove(board,human):
global X,O,EMPTY,TIE,NO_ONE
move = askNumber("Where will you move?", len(board),0);
while (not isLegal(move, board)):
print "\nThat square is already occupied, foolish human.\n";
move = askNumber("Where will you move?", len(board),0);
print "Fine. . .\n";
return move;
def computerMove(board, computer):
global X,O,EMPTY,TIE,NO_ONE
move = 0;
found = False;
#if computer can win on next move, that's the move to make
while (not found and move < len(board)):
if (isLegal(move, board)):
board[move] = computer;
found = winner(board) == computer;
board[move] = EMPTY;
if (not found):
move += 1
#otherwise, if human can win on next move, that's the move to make
if (not found):
move = 0;
human = opponent(computer);
while (not found and move < len(board)):
if (isLegal(move, board)):
board[move] = human;
found = winner(board) == human;
board[move] = EMPTY;
if (not found):
move += 1
#otherwise, moving to the best open square is the move to make
if (not found):
move = 0;
i = 0;
BEST_MOVES = [4, 0, 2, 6, 8, 1, 3, 5, 7] #pick best open square
while (not found and i < len(board)):
move = BEST_MOVES[i];
if (isLegal(move, board)):
found = True;
i += 1
print "I shall take square number " , move
return move;
def announceWinner( winner, computer, human):
if (winner == computer):
print winner , "'s won!";
print "As I predicted, human, I am triumphant once more -- proof";
print "that computers are superior to humans in all regards.";
elif (winner == human):
print winner , "'s won!";
print "No, no! It cannot be! Somehow you tricked me, human.";
print "But never again! I, the computer, so swear it!";
else:
print "It's a tie.\n";
print "You were most lucky, human, and somehow managed to tie me.\n";
print "Celebrate. . . for this is the best you will ever achieve.\n";
NUM_SQUARES = 9;
board = [EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY]
instructions();
human = humanPiece();
computer = opponent(human);
turn = X;
displayBoard(board);
while (winner(board) == NO_ONE):
if (turn == human):
move = humanMove(board, human);
board[move] = human;
else:
move = computerMove(board, computer);
board[move] = computer;
displayBoard(board);
turn = opponent(turn);
announceWinner(winner(board), computer, human);