Created
July 6, 2017 21:40
-
-
Save bootandy/999c0ed8877147ff6d95ccb4fbb1c3c5 to your computer and use it in GitHub Desktop.
Simple Os and Xs game
This file contains hidden or 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
from __future__ import absolute_import, division, print_function | |
import random | |
import copy | |
EMPTY = '_' | |
def winner(spaces): | |
winning_tile = filter(lambda a:a, [_is_row_winner(row) for row in spaces]) | |
if winning_tile: | |
return winning_tile[0] | |
return False | |
def _is_row_winner(row): | |
if len(set(row)) == 1 and row[0] != EMPTY: | |
return row[0] | |
return False | |
class Board(object): | |
spaces = [] | |
for i in range(0, 3): | |
spaces.append([EMPTY, EMPTY, EMPTY]) | |
def print(self): | |
print("The board:") | |
for space in self.spaces: | |
for point in space: | |
print(point + ' ', end='') | |
print('\n') | |
def play_in_position(self, x, y, player_char): | |
if not self.is_space_free(x, y): | |
raise Exception('tried to play in illegal space') | |
self.spaces[y][x] = player_char | |
def is_space_free(self, x, y): | |
if y >= len(self.spaces) or x >= len(self.spaces[0]): | |
return False | |
return self.spaces[y][x] == EMPTY | |
def winner(self): | |
diags1 = [self.spaces[i][i] for i in range(0, 3)] | |
diags2 = [self.spaces[i][2-i] for i in range(0, 3) ] | |
return ( | |
winner(self.spaces) or winner(zip(*self.spaces)) or | |
_is_row_winner(diags1) or _is_row_winner(diags2) | |
) | |
def game_inplay(self): | |
return not self.winner() and self.spaces_left() | |
def spaces_left(self): | |
return any([EMPTY in space for space in self.spaces]) | |
def play(self): | |
self.print() | |
hp = HumanPlayer('X') | |
RandomPlayer('O') | |
players = [hp, hp2] | |
play_index = 0 | |
while self.game_inplay(): | |
player = players[play_index] | |
x, y = player.get_next_move(self) | |
if self.is_space_free(x, y): | |
self.play_in_position(x, y, player.n) | |
b.print() | |
play_index = (play_index + 1) % 2 | |
else: | |
print('Illegal move') | |
if self.winner(): | |
print('Winner was: {}'.format(self.winner())) | |
else: | |
print('Game drawn') | |
def __deepcopy__(self, memo): | |
newone = type(self)() | |
newone.__dict__.update(self.__dict__) | |
newone.spaces = copy.deepcopy(self.spaces, memo) | |
return newone | |
class HumanPlayer(object): | |
def __init__(self, n): | |
self.n = n | |
def get_next_move(self, board): | |
print('Player {} play your move:'.format(self.n)) | |
x = input('Enter x:') - 1 # TODO: Check input valid! | |
y = input('Enter y:') - 1 | |
return x, y | |
class RandomPlayer(object): | |
def __init__(self, n): | |
self.n = n | |
def get_next_move(self, board): | |
return random.randint(0, 3), random.randint(0, 3) | |
b = Board() | |
b.play() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment