Hardware Reference
In-Depth Information
Listing 3-5 Two-Player Tic-Tac-Toe Game
#!/usr/bin/env python
# Tic-Tac-Toe 5 - 2 player game
board = [ '1', '2', '3', '4', '5', '6', '7', '8', '9' ]
wins = [ [0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6],;
[1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6] ]
def play() :
printBoard()
print 'Tic-Tac-Toe'
print 'two players'
while True :
wipeBoard()
player_turn = 'X'
while checkWin(swapPlayer(player_turn)) == False ;
and canMove() == True :
getMove(player_turn)
printBoard()
player_turn = swapPlayer(player_turn)
if checkWin(swapPlayer(player_turn)):
print 'Player',swapPlayer(player_turn),'wins ;
... New Game'
else:
print 'A draw. ... New game'
def swapPlayer(player):
if player == 'X' :
player = 'O'
else:
player = 'X'
return player
def getMove(player):
global board
correct_number = False
while correct_number == False :
square = raw_input('Square to place the '+ player + ' ')
try:
square = int(square)
except:
square = -2
square -= 1 # make input number match internal numbers
if square >= 0 and square < 10 : # number in range
if board[square] == ' ' : # if it is blank
Search WWH ::




Custom Search