# Torbert, 12.2.2004

from os import system, listdir
from random import choice
import time

black, white, empty, outer = 1, 2, 0, 3
directions = [-11, -10, -9, -1, 1, 9, 10, 11]

def printState(board, lastMove, currTest, b, w, s, bl, wl):
	if board[currTest] == black:
		if currTest == lastMove:
			return bl
		else:
			return b
	if board[currTest] == white:
		if currTest == lastMove:
			return wl
		else:
			return w
	if board[currTest] == empty:
		return s

def display(state, lastMove):
	start = chr(27) + "[32;42m" + chr(27) + "[1m"
	end = chr(27) + "[0m"
	LeftUpperCorner = start + "+" + end
	LeftSide = start + "|" + end
	LeftInterSection = start + "+" + end
	LeftLowerCorner = start + "+" + end
	RightUpperCorner = start + "+" + end
	RightSide = start + "|" + end
	RightInterSection = start + "+" + end
	RightLowerCorner = start + "+" + end
	LowerSide = start + "-" + end
	LowerInterSection = start + "+" + end
	UpperSide = start + "-" + end
	UpperInterSection = start + "+" + end
	VerticalBar = start + "|" + end
	HorizontalBar = start + "-" + end
	MiddleInterSection = start + "+" + end

#	LeftUpperCorner = "╔"
#	LeftSide = "║"
#	LeftInterSection = "╟"
#	LeftLowerCorner = "╚"
#	RightUpperCorner = "╗"
#	RightSide = "║"
#	RightInterSection = "╢"
#	RightLowerCorner = "╝"
#	LowerSide = "═"
#	LowerInterSection = "╧"
#	UpperSide = "═"
#	UpperInterSection = "╤"
#	VerticalBar = "│"
#	HorizontalBar = "─"
#	MiddleInterSection = "┼"

	blackMarker = chr(27) + "[30;42m" + chr(27) + "[1m" + " * " + chr(27) + "[0m"
	whiteMarker = chr(27) + "[37;42m" + chr(27) + "[1m" + " * " + chr(27) + "[0m"
	spaceMarker = chr(27) + "[31;42m" + chr(27) + "[1m" + "   " + chr(27) + "[0m"
	blackLast = chr(27) + "[30;46m" + chr(27) + "[1m" + " * " + chr(27) + "[0m"
	whiteLast = chr(27) + "[37;46m" + chr(27) + "[1m" + " * " + chr(27) + "[0m"

	string = ""

	string += LeftUpperCorner
	string += UpperSide
	string += UpperSide
	string += UpperSide
	for i in range(7):
		string += UpperInterSection
		string += UpperSide
		string += UpperSide
		string += UpperSide
	string += RightUpperCorner
	string += "\n"

	for i in range(7):
		string += LeftSide
		for j in range(7):
			string += printState(state, lastMove, 10*i+j+11, blackMarker, whiteMarker, spaceMarker, blackLast, whiteLast)
			string += VerticalBar
		string += printState(state, lastMove, 10*i+18, blackMarker, whiteMarker, spaceMarker, blackLast, whiteLast)
		string += RightSide
		string += "\n"

		string += LeftInterSection
		string += HorizontalBar
		string += HorizontalBar
		string += HorizontalBar
		for i in range(7):
			string += MiddleInterSection
			string += HorizontalBar
			string += HorizontalBar
			string += HorizontalBar
		string += RightInterSection
		string += "\n"

	string += LeftSide
	for j in range(7):
		string += printState(state, lastMove, 81+j, blackMarker, whiteMarker, spaceMarker, blackLast, whiteLast)
		string += VerticalBar
	string += printState(state, lastMove, 88, blackMarker, whiteMarker, spaceMarker, blackLast, whiteLast)
	string += RightSide
	string += "\n"



	string += LeftLowerCorner
	string += LowerSide
	string += LowerSide
	string += LowerSide
	for i in range(7):
		string += LowerInterSection
		string += LowerSide
		string += LowerSide
		string += LowerSide
	string += RightLowerCorner
	string += "\n"
	

	#print string.encode('utf-8')
	print string

def show(board):
	print "  " + "".join(map(str,range(1,9)))
	for row in range(10, 90, 10):
		s = str(row) + "" 
		for col in range(1, 9):
			square = board[row + col]
			if square is empty:
				s += chr(27) + "[32;42"
			elif square is white:
				s += chr(27) + "[37;42"
			else:
				s += chr(27) + "[30;42"
			s += "m"
			s += chr(27) + "[1m";
			if square is empty:
				s += " "
			else:
				s += "+"
		print s + chr(27) + "[0m"
	print

def bracket(board, player, square):
	opp = opponent_color(player)
	for d in directions:
		k = square + d
		
		if board[k] is not opp:
			continue
		while board[k] is opp:
			k = k + d
		if board[k] is player:
			k = k - d
			while k != square:
				board[k] = player
				k = k - d

def would_bracket(board, player, square):
	opp = opponent_color(player)
	for d in directions:
		k = square + d
		if board[k] is not opp:
			continue
		while board[k] is opp:
			k = k + d
		if board[k] is player:
			return True
	return False

def opponent_color(player):
	if player is black: 
		return white
	return black

def count(board, player):
	total = 0
	for row in range(10, 90, 10):
		for col in range(1, 9):
			square = board[row + col]
			if square is player:	
				total = total + 1
	return total

def terminal_test(state):
	found1 = False
	found2 = False
	found0 = False
	for i in state:
		if i is 0:
			found0 = True
		if i is 1:
			found1 = True
		if i is 2:
			found2 = True

	if found1 == False or found2 == False or found0 == False:
		return True
	return False

def reportMove(state, move, player):
	state[move] = player
	bracket(state, player, move)
	return state[:]

#INPUT OPTIONS


def getMove(board, player, pType):
	if pType == "H":
		return user(board, player)
	if pType == "R":
		return rand(board, player)
	if pType == "M":
		return AI(board, player)

def makeBoard():
	board = [empty] * 100
	board[0:10] = [outer] * 10
	board[90:100] = [outer] * 10
	for k in range(10, 90, 10):
		board[k + 0] = outer
		board[k + 9] = outer 
	board[44], board[55] = white, white
	board[45], board[54] = black, black
	return board

def makeReadable(f):
	answer = ""
	answer += f[:f.index('V')]
	answer += " v. "
	answer += f[f.index('V')+1:]
	return answer

def main():
	system("clear")
	files = [k[:-4] for k in listdir("Games") if k[-3:] == "ogf"]
	k = 1
	print "The Games:"
	for f in files:
		print "%3d. %s" % (k, makeReadable(f))
		k += 1
	print 
	game = files[int(raw_input("Game:"))-1]
	gamefile = open("Games/" + game + ".ogf", "r")
	text = gamefile.readlines()
	gamefile.close()
	goodtext = [i[0:-1] for i in text]
	player1 = goodtext[0]
	player2 = goodtext[1]
	board = makeBoard()
	
	#CREATE BOARD
	player = black
	system("clear")
	display(board, 0)
	for k in range(2,len(goodtext)):
		if goodtext[k] == "None":
			square = None
		else:
			square = int(goodtext[k])
		if square is not None:
			board[square] = player
			bracket(board, player, square)
		system("clear")
		display(board, square)
		print player1 + " (B): "+str(count(board, black))
		print player2 + " (W): "+str(count(board, white))
		time.sleep(1.5)
		player = opponent_color(player)
	print

if __name__ == "__main__":
	main()

