Skip to content

Instantly share code, notes, and snippets.

@akaptur
Created July 28, 2012 21:49
Show Gist options
  • Save akaptur/3194947 to your computer and use it in GitHub Desktop.
Save akaptur/3194947 to your computer and use it in GitHub Desktop.
__eq__
import random
import pdb
import sys
import pygame
from pygame.locals import QUIT, MOUSEBUTTONDOWN
class Game:
BLACK = (0,0,0)
WHITE = (255,255,255)
GREY = (140,140,140)
SCREEN = pygame.display.set_mode((400,450))
def __init__(self):
# self.gamestate = [[' ' for i in range(3)] for j in range(3)]
self.gamestate = [['X', 'X', 'X'],[' ', ' ', ' '],[' ', ' ', ' ']]
self.display_board()
def hardcode_state(self, inputstring):
""" This function is for debugging/testing equality. """
string_index = 0
for row in range(3):
for col in range(3):
self.gamestate[row][col] = inputstring[string_index]
string_index += 1
def main_loop(self):
self.display_board()
for event in pygame.event.get():
if event.type == MOUSEBUTTONDOWN:
self.user_move(event)
self.display_board()
pygame.time.wait(100) # small pause before AI move
if event.type == QUIT:
pygame.quit()
sys.exit()
def display_board(self):
Game.SCREEN.fill(Game.WHITE)
linewidth = 3
pygame.draw.line(Game.SCREEN, Game.BLACK, (150,50), (150,350), linewidth)
pygame.draw.line(Game.SCREEN, Game.BLACK, (250,50), (250,350), linewidth)
pygame.draw.line(Game.SCREEN, Game.BLACK, (50,150), (350,150), linewidth)
pygame.draw.line(Game.SCREEN, Game.BLACK, (50,250), (350,250), linewidth)
for row in range(3):
for col in range(3):
if self.gamestate[col][row] == 'X':
pygame.draw.line(Game.SCREEN, Game.BLACK, (row*100 + 75, col*100 + 75), (row*100 + 125, col*100+125), linewidth)
pygame.draw.line(Game.SCREEN, Game.BLACK, (row*100 + 125, col*100 + 75), (row*100 + 75, col*100+125), linewidth)
if self.gamestate[col][row] == 'O':
pygame.draw.circle(Game.SCREEN, Game.BLACK, (row*100 + 100, col*100 + 100),33, linewidth)
pygame.display.update()
def user_move(self, event):
xpos, ypos = event.pos
if xpos < 150:
xcell = 0
elif xpos < 250:
xcell = 1
elif xpos < 350:
xcell = 2
if ypos < 150:
ycell = 0
elif ypos < 250:
ycell = 1
elif ypos < 350:
ycell = 2
self.gamestate[xcell][ycell] = 'X'
def show_message(self, text):
""" Message displays a message on the screen.
Note that this function does not contain a display.update()."""
pygame.font.init()
myfont = pygame.font.SysFont('courier', 14, bold=1)
pygame.draw.rect(SCREEN, GREY, (50,375,300,50))
message = myfont.render(text,1,BLACK)
def legal_moves(self):
legal_moves = []
for row in range(3):
for col in range(3):
if self.gamestate[row][col] == ' ':
legal_moves.append([row,col])
return legal_moves
def rotate(self):
cells_one = [[0,0], [0,1], [0,2], [1,2], [2,2], [2,1], [2,0], [1,0]] #cells clockwise - ignore center
cells_two = [[0,2], [1,2], [2,2], [2,1], [2,0], [1,0], [0,0], [0,1]] #rotated by two
duplicate = Game()
# self.gamestate_two = [[' ' for i in range(3)] for j in range(3)]
for cell in range(len(cells_one)):
row, col = cells_one[cell]
row_two, col_two = cells_two[cell]
duplicate.gamestate[row_two][col_two] = self.gamestate[row][col]
# self.gamestate = self.gamestate_two
return duplicate
def reflect(self):
cells_one = [[0,1], [0,2], [1,2], [2,1], [2,0], [1,0]] #cells clockwise
cells_two = [[1,0], [2,0], [2,1], [1,2], [0,2], [0,1]]
self.gamestate_two = [[' ' for i in range(3)] for j in range(3)]
for cell in range(len(cells_one)):
row, col = cells_one[cell]
row_two, col_two = cells_two[cell]
self.gamestate_two[row_two][col_two] = self.gamestate[row][col]
self.gamestate = self.gamestate_two
return self
def __eq__(self, other):
# rotationally identical
other1 = other
rotations = 0
while rotations < 4:
if self.gamestate == other.gamestate:
return True
else:
other = other.rotate()
other2 = other.reflect()
rotations = 0
while rotations < 4:
if self.gamestate == other.gamestate:
return True
else:
other = other.rotate()
return False
def print_to_terminal(self):
for i in range(0,2):
for j in range(0,2):
print self.gamestate[i][j], '|',
print self.gamestate[i][2]
print " - - - "
for j in range(0,2):
print self.gamestate[2][j], '|',
print self.gamestate[2][2]
if __name__ == '__main__':
# game = Game()
# game.hardcode_state("XXX ")
# game.print_to_terminal()
# game.display_board()
game_two = Game()
game_two.hardcode_state(" X X X")
game_two.print_to_terminal()
game_two.rotate()
game_two.print_to_terminal()
# game_two.display_board
if game == game_two:
print "Rotational identity succeeds!"
# while True:
# game.main_loop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment