Python code

import random
import sys

def drawBoard(board):
#this function prints out the board that is passed. Returns none.
HLINE=’ +—+—+—+—+—+—+—+—+’
VLINE=’ | | | | | | | | |’

print (‘ 1 2 3 4 5 6 7 8 9’)
print(HLINE)
for y in range(8):
print(VLINE)
print(y+1, end=”)
for x in range(8):
print(‘| %s’ % (board[x][y]), end=”)
print(‘|’)
print(VLINE)
print(HLINE)

def resetBoard(board):
#Blanks out the board it is passed, except for the original starting position

for x in range(8):
for y in range(8):
board[x][y]=”

#starting peices
board[3][3]=’X’
board[3][4]=’O’
board[4][3]=’O’
board[4][4]=’X’

def getNewBoard():
#creates a brand new, blank data structure

board=[]
for i in range(8):
board.append([”] * 8)
return board

def isValidMove(board, tile, xstart, ystart):
# Returns False if the player’s move on space xstart, ystart is invalid.
# If it is a valid move, returns a list of spaces that would become the player’s if they made a move here.

if board[xstart][ystart] !=’ ‘ or not isOnBoard(xstart, ystart):
return False
board[xstart][ystart] = tile # temporarily set the tile on the board.
if tile == ‘X’:
otherTile = ‘O’
else:
otherTile = ‘X’

tilesToFlip=[]
for xdirection, ydirection in [[0, 1], [1, 1], [1, 0], [1, -1], [0, -1], [-1, -1], [-1, 0], [-1, 1]]:
x, y = xstart, ystart
x +=xdirection #first step in the direction
y +=ydirection #first step in the direction
if isOnBoard(x,y) and board[x][y] == otherTile:
#There is a peice belonging to the other player next to our piece

x +=xdirection
y +=ydirection
if not isOnBoard(x,y):
continue
while board[x][y] == otherTile:
x +=xdirection
y += ydirection
if not isOnBoard(x,y): #break out of while loop , then continue in fro loop
break
if not isOnBoard(x,y):
continue
if board[x][y] == tile:
#there are pieces to flip over. Go in the reverse direction untill we reach the original space, noting allthe tiles along the way
while True:
x -=xdirection
y -=ydirection
if x == xstart and y ==ystart :
break
tilesToFlip.append([x,y])
board[xstart][ystart] =”#restore the empty space

if len(tilesToFlip)==0: #If no tiles were flipped , this is not a valid move.
return False
return tilesToFlip

def isOnBoard(x, y):
#retunrs True if the coordinates are located on the board
return x>=0 and x=0 and y bestScore:
bestMove = [x, y]
bestScore = score
return bestMove

def showPoints(playerTile, computerTile):
# Prints out the current score.
scores = getScoreOfBoard(mainBoard)
print(‘You have %s points. The computer has %s points.’ %(scores[playerTile], scores[computerTile]))

print(‘Welcome to Reversi!’)
while True:
# Reset the board and game.
mainBoard = getNewBoard()
resetBoard(mainBoard)
playerTile, computerTile = enterPlayerTile()
showHints = False
turn = whoGoesFirst()
print(‘The ‘ + turn + ‘ will go first.’)
while True:
if turn == ‘player’:
# Player’s turn.
if showHints:
validMovesBoard = getBoardWithValidMoves(mainBoard, playerTile)
drawBoard(validMovesBoard)
else:
drawBoard(mainBoard)
showPoints(playerTile, computerTile)
move = getPlayerMove(mainBoard, playerTile)
if move == ‘quit’:
print(‘Thanks for playing!’)
sys.exit() # terminate the program
elif move == ‘hints’:
showHints = not showHints
continue
else:
makeMove(mainBoard, playerTile, move[0], move[1])
if getValidMoves(mainBoard, computerTile) == []:
break
else:
turn = ‘computer’
else:
# Computer’s turn.
drawBoard(mainBoard)
showPoints(playerTile, computerTile)
input(‘Press Enter to see the computer\’s move.’)
x, y = getComputerMove(mainBoard, computerTile)
makeMove(mainBoard, computerTile, x, y)
if getValidMoves(mainBoard, playerTile) == []:
break
else:
turn = ‘player’
# Display the final score.
drawBoard(mainBoard)
scores = getScoreOfBoard(mainBoard)
print(‘X scored %s points. O scored %s points.’ % (scores[‘X’], scores[‘O’]))
if scores[playerTile] > scores[computerTile]:
print(‘You beat the computer by %s points! Congratulations!’ % (scores[playerTile] – scores[computerTile]))
elif scores[playerTile] < scores[computerTile]:
print('You lost. The computer beat you by %s points.' % (scores[computerTile] – scores[playerTile]))
else:
print('The game was a tie!')
if not playAgain():
break

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s