Created
May 3, 2023 14:18
-
-
Save anthonykasza/4e5ceb081a5bdbb9c35f77526190d3fd to your computer and use it in GitHub Desktop.
tictac
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
class Game(): | |
def __init__(self, size, debug=False, players=2): | |
self.debug = debug | |
self.board = [["-"] * size for i in range(size)] | |
self.players = players | |
self.symbols = {} | |
self.turns = 0 | |
self.next_turn = 0 | |
self.winner = None | |
def register_player(self, symbol): | |
if len(self.symbols.keys()) >= self.players: | |
if self.debug: print("too many players") | |
return False | |
if symbol in self.symbols: | |
if self.debug: print("symbol already reserved") | |
return False | |
self.symbols[len(self.symbols.keys())] = symbol | |
return True | |
def check_win(self,x,y): | |
size = len(self.board) | |
l2r = [] | |
r2l = [] | |
xline = [] | |
yline = [] | |
for i in range(size): | |
l2r.append(self.board[i][i]) | |
r2l.append(self.board[size-1-i][i]) | |
xline.append(self.board[x][i]) | |
# rebuilding these lists is pretty dumb but it works | |
yline.append(self.board[i][y]) | |
if "-" not in yline and len(set(yline)) == 1: | |
return True | |
if "-" not in xline and len(set(xline)) == 1: | |
return True | |
if ("-" not in l2r and len(set(l2r)) == 1) or ("-" not in r2l and len(set(r2l)) == 1): | |
return True | |
return False | |
def make_move(self, x, y, symbol): | |
if self.winner: | |
if self.debug: print("game is over. {} won the game".format(self.winner)) | |
return False | |
if symbol not in self.symbols.values(): | |
if self.debug: print("symbol not registered to play in this game") | |
return False | |
if symbol != self.symbols[self.next_turn]: | |
if self.debug: print("not your turn") | |
return False | |
if x > len(self.board) or y > len(self.board): | |
if self.debug: print("space outisde of board limits") | |
return False | |
if self.board[x][y] != "-": | |
if self.debug: print("space is already taken by {}".format(self.board[x][y])) | |
return False | |
self.board[x][y] = symbol | |
self.turns += 1 | |
if self.turns == len(self.board) * len(self.board): | |
print("we have a draw") | |
self.winner = ''.join(self.symbols.values()) | |
return True | |
if self.check_win(x,y): | |
self.winner = symbol | |
print("congrats, you win", symbol) | |
return True | |
self.next_turn = (self.next_turn + 1) % self.players | |
return True | |
def show_board(self): | |
for i in range(len(self.board)): | |
print(self.board[i]) | |
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
from random import randint | |
class Player(): | |
def __init__(self, game, symbol): | |
self.game = game | |
self.symbol = symbol | |
self.game.register_player(self.symbol) | |
def make_move(self): | |
have_moved = False | |
while (not have_moved): | |
x = randint(0, len(self.game.board)-1) | |
y = randint(0, len(self.game.board)-1) | |
have_moved = self.game.make_move(x, y, self.symbol) |
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
from game import Game | |
def test_sanity(): | |
g = Game(3, debug=True) | |
print("pass1") if g.register_player("x") else print("failed") | |
print("pass2") if g.register_player("o") else print("failed") | |
print("pass3") if not g.register_player("$") else print("failed") | |
print("pass4") if len(g.symbols) == g.players else print("failed") | |
print("pass5") if not g.make_move(0,0, "o") else print("failed") | |
print("pass6") if g.make_move(0,0, "x") else print("failed") | |
print("pass7") if not g.make_move(0,1, "x") else print("failed") | |
print("pass8") if not g.make_move(0,0, "o") else print("failed") | |
print("pass9") if not g.make_move(0,1, "$") else print("failed") | |
print("pass sanity") if g.make_move(0,1, "o") else print("failed") | |
def test_v_win(): | |
g = Game(3, debug=True) | |
print("pass1") if g.register_player("x") else print("failed") | |
print("pass2") if g.register_player("o") else print("failed") | |
print("pass3") if g.make_move(0,0, "x") else print("failed") | |
print("pass4") if g.make_move(2,2, "o") else print("failed") | |
print("pass5") if g.make_move(1,0, "x") else print("failed") | |
print("pass6") if g.make_move(2,1, "o") else print("failed") | |
print("pass7") if g.make_move(2,0, "x") else print("failed") | |
print("pass v win") if g.winner else print("failed") | |
def test_h_win(): | |
g = Game(3, debug=True) | |
print("pass1") if g.register_player("x") else print("failed") | |
print("pass2") if g.register_player("o") else print("failed") | |
print("pass3") if g.make_move(0,0, "x") else print("failed") | |
print("pass4") if g.make_move(2,2, "o") else print("failed") | |
print("pass5") if g.make_move(0,1, "x") else print("failed") | |
print("pass6") if g.make_move(2,1, "o") else print("failed") | |
print("pass7") if g.make_move(0,2, "x") else print("failed") | |
print("pass h win") if g.winner else print("failed") | |
def test_d_win1(): | |
g = Game(3, debug=True) | |
print("pass1") if g.register_player("x") else print("failed") | |
print("pass2") if g.register_player("o") else print("failed") | |
print("pass3") if g.make_move(0,0, "x") else print("failed") | |
print("pass4") if g.make_move(0,1, "o") else print("failed") | |
print("pass5") if g.make_move(1,1, "x") else print("failed") | |
print("pass6") if g.make_move(2,0, "o") else print("failed") | |
print("pass7") if g.make_move(2,2, "x") else print("failed") | |
print("pass d1 win") if g.winner == "x" else print("failed") | |
def test_d_win2(): | |
g = Game(3, debug=True) | |
print("pass1") if g.register_player("x") else print("failedA") | |
print("pass2") if g.register_player("o") else print("failedB") | |
print("pass3") if g.make_move(0,2, "x") else print("failedC") | |
print("pass4") if g.make_move(0,1, "o") else print("failedD") | |
print("pass5") if g.make_move(1,1, "x") else print("failedE") | |
print("pass6") if g.make_move(1,0, "o") else print("failedF") | |
print("pass7") if g.make_move(2,0, "x") else print("failedG") | |
print("pass d2 win") if g.winner == "x" else print("failedH") | |
def test_draw(): | |
g = Game(3, debug=True) | |
print("pass1") if g.register_player("x") else print("failed") | |
print("pass2") if g.register_player("o") else print("failed") | |
print("pass3") if g.make_move(0,0, "x") else print("failed") | |
print("pass4") if g.make_move(0,1, "o") else print("failed") | |
print("pass5") if g.make_move(0,2, "x") else print("failed") | |
print("pass6") if g.make_move(1,1, "o") else print("failed") | |
print("pass7") if g.make_move(1,0, "x") else print("failed") | |
print("pass8") if g.make_move(1,2, "o") else print("failed") | |
print("pass9") if g.make_move(2,1, "x") else print("failed") | |
print("pass10") if g.make_move(2,0, "o") else print("failed") | |
print("pass11") if g.make_move(2,2, "x") else print("failed") | |
print("pass draw") if g.winner else print("failed") | |
test_sanity() | |
test_draw() | |
test_d_win1() | |
test_d_win2() | |
test_h_win() | |
test_v_win() |
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
from game import Game | |
from player import Player | |
size = 3 | |
g = Game(size) | |
p1 = Player(g, "x") | |
p2 = Player(g, "o") | |
players = {"x": p1, "o": p2} | |
while (not g.winner): | |
player = players[g.symbols[g.next_turn]] | |
player.make_move() | |
print("----------------") | |
g.show_board() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment