Created
March 5, 2020 20:01
-
-
Save obakanue/70fbd92086811827295c736aef14e524 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
def main(): | |
global mainBoard | |
global playerTile | |
global computerTile | |
global pointsMatrix | |
pointsMatrix = [[10, -10, 3, 3, 3, 3, -10, 10], | |
[-10, -10, -3, -3, -3, -3, -10, -10], | |
[3, -3, 1, 1, 1, 1, -3, 3], | |
[3, -3, 1, 1, 1, 1, -3, 3], | |
[3, -3, 1, 1, 1, 1, -3, 3], | |
[3, -3, 1, 1, 1, 1, -3, 3], | |
[-10, -10, -3, -3, -3, -3, -10, -10], | |
[10, -10, 3, 3, 3, 3, -10, 10]] | |
print("A game of Othello") | |
while True: | |
mainBoard = get_new_board() | |
playerTile, computerTile = enter_player_tile() | |
turn = who_goes_first(playerTile) | |
print('The ' + turn + ' will go first.') | |
while True: | |
if turn == 'player': | |
validMovesBoard = get_valid_moves_board(mainBoard, playerTile) | |
draw_board(validMovesBoard) | |
show_points(playerTile, computerTile) | |
move = get_player_move(mainBoard, playerTile) | |
if move == 'quit': | |
print("Thanks for playing!") | |
sys.exit() | |
else: | |
make_move(mainBoard, playerTile, move[0], move[1]) | |
if get_valid_moves(mainBoard, computerTile) == []: | |
break | |
else: | |
turn = 'computer' | |
else: | |
draw_board(mainBoard) | |
show_points(playerTile, computerTile) | |
input("Press Enter to see the computer\'s move.") | |
x, y = get_computer_move(mainBoard, computerTile) | |
make_move(mainBoard, computerTile, x, y) | |
if get_valid_moves(mainBoard, playerTile) == []: | |
break | |
else: | |
turn = 'player' | |
draw_board(mainBoard) | |
scores = get_score(mainBoard) | |
print(f"'@' scored {scores['@']} points. 'O' scored {scores['O']} points.") | |
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 play_again(): | |
print("Thanks for playing!") | |
break | |
def make_move(board, tile, xstart, ystart): | |
tilesToFlip = is_valid_move(board, tile, xstart, ystart) | |
if tilesToFlip == False: | |
return False | |
board[xstart][ystart] = tile | |
for x, y in tilesToFlip: | |
board[x][y] = tile | |
return True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment