Skip to content

Instantly share code, notes, and snippets.

@xavdid
Last active August 29, 2015 13:56
Show Gist options
  • Save xavdid/d07ef908fe023f2e1225 to your computer and use it in GitHub Desktop.
Save xavdid/d07ef908fe023f2e1225 to your computer and use it in GitHub Desktop.
Neat tic tac toe variant!
# A of tic-tac-toe-ception written on an airplane
class TTT:
def __init__(self, n):
#stuff
# 0,1,2,
# 3,4,5,
# 6,7,8
self.board = [
0,0,0,
0,0,0,
0,0,0
]
self.winner = False
self.board_num = n
# hard coding wins is only cool because it's a very finite set.
# probably a better way algorithimically to do it, but whatever
self.wins = {
'col0':[0,3,6],
'col1':[1,4,7],
'col2':[2,5,8],
'row0':[0,1,2],
'row1':[3,4,5],
'row2':[6,7,8],
'diag':[0,4,8],
'anti_diag':[6,4,2]
}
def move(self, player, i):
# enters move, checks winner. if winner, report
# right mow, inout is checked outside the function, should be checked here
if not self.square_empty(i):
print 'Square occupied, pick again'
return False
else:
self.board[i] = player
if self.check_win(player, i):
print "Player",player,"wins board",self.board_num
self.winner = player
return True
def check_win(self, player, i):
# print 'checking win'
c = 0
for k in self.wins:
# only checking wins in which move appears
if i in self.wins[k]:
# check all positions
for m in self.wins[k]:
if self.board[m] == player:
c += 1
else:
break
if c == 3:
return True
else:
c = 0
return False
def square_empty(self, i):
# print "checking for empty, it was",self.board[i],not self.board[i]
return not self.board[i]
def get_line(self, k):
return ' '.join([str(self.board[(k*3)]),"|",str(self.board[(k*3)+1]),"|",str(self.board[(k*3)+2])])
def print_board(self):
print ''
print self.board[0],"|",self.board[1],"|",self.board[2]
print "---------"
print self.board[3],"|",self.board[4],"|",self.board[5]
print "---------"
print self.board[6],"|",self.board[7],"|",self.board[8]
print ''
def print_big(d):
games = [
['game0','game1','game2'],
['game3','game4','game5'],
['game6','game7','game8']
]
h_sep = '='
h_sep_size = 43
v_sep = ' || '
print ''
i = 2
for g in games:
for row in range(3):
print ' ',' '*9,v_sep,' '*9,v_sep
print ' ', d[g[0]].get_line(row),v_sep, d[g[1]].get_line(row), v_sep, d[g[2]].get_line(row)
print ' ',' '*9,v_sep,' '*9,v_sep
if i > 0:
print h_sep * h_sep_size
i -= 1
print ''
## MAIN ##
d = {}
players = [1,2]
b = False
meta = TTT("Big")
board = 'x'
for i in range(0,9):
d['game'+str(i)] = TTT(i)
while not meta.winner:
# TODO: sanitize input better!
while board == 'x':
boar = raw_input("Select a board: ")
try:
boar = int(boar)
if boar >= 0 and boar < 9:
board = int(boar)
else:
print 'Invalid board, try again'
except:
print 'Enter a number 0-9'
print "Player %s, select a move for board %s" % (players[b], board)
m = raw_input("--> ")
if m == 'print':
print 'Board %s' % board
d['game'+str(board)].print_board()
elif m == 'meta':
meta.print_board()
elif m == 'big':
print_big(d)
else:
try:
m = int(m)
if m >= 0 and m < 9:
# makes the move, if possible
if d['game'+str(board)].move(players[b],m):
# checks if that was a winning move, marks meta ifso
if d['game'+str(board)].winner:
meta.move(players[b],board)
b = not b
# sends move to next board, it able
if not d['game'+str(m)].winner:
board = m
else:
board = 'x'
else:
print 'Invalid board, try again'
except:
print 'Enter a number 0-9'
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment