import pygame, time, os
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode((240,640))
pygame.display.set_caption('py-ssance 3')
remiseazero = 0, 0, 0
horloge = pygame.time.Clock()


def plateau():

	screen.fill(remiseazero)
	for i in range(3):
		ligne = 80 * ( i + 1 )
		pygame.draw.line(screen, (200, 200, 200), (ligne, 0), (ligne, 640), 3)

	for i in range(7):
		ligne = 80 * ( i + 1 )
		pygame.draw.line(screen, (200, 200, 200), (0, ligne), (320, ligne), 3)


	pygame.display.flip()


def pion(reponse, jeton, joueur):
	
	colone = reponse * 80 + 40 
	ligne = 640 - ( ( jeton - 1 ) * 80 + 40 )
  
	if ( joueur == 1 ):
		couleur = (200, 0, 0)
	else:
		couleur = (0, 0, 200)


	pygame.draw.circle(screen, (couleur), (colone, ligne), 30, 10)
	pygame.display.flip()


def victoire():
	for i in range(8):

		if ( colone0[i] == colone0[i+1] ) and ( colone0[i+1] == colone0[i+2] ) and ( colone0[i] != 0 ):
			gameover()

		if ( colone1[i] == colone1[i+1] ) and ( colone1[i+1] == colone1[i+2] ) and ( colone1[i] != 0 ):
			gameover()

		if ( colone2[i] == colone2[i+1] ) and ( colone2[i+1] == colone2[i+2] ) and ( colone2[i] != 0 ):
			gameover()

		if ( colone0[i] == colone1[i] ) and ( colone1[i] == colone2[i] ) and ( colone0[i] != 0 ):
			gameover()

		if ( colone0[i] == colone1[i+1] ) and ( colone1[i+1] == colone2[i+2] ) and ( colone0[i] != 0 ):
			gameover()

		if ( colone2[i] == colone1[i+1] ) and ( colone1[i+1] == colone0[i+2] ) and ( colone2[i] != 0 ):
			gameover()



def ajoutjeton(numcolone, numligne, joueur):
	
	if ( numcolone == 0 ):
		colone0[numligne] = joueur + 1
		return

	if ( numcolone == 1 ):
		colone1[numligne] = joueur + 1
		return

	if ( numcolone == 2 ):
		colone2[numligne] = joueur + 1
		return


def lecture():
	os.system("clear")
	for i in range(8):
		ii = 8 - i
		print colone0[ii], colone1[ii], colone2[ii]
	return


def gameover():
	os.system("clear")
	print " Game Over "
	pygame.time.wait(3500)
	exit()



def main():

	jeton0 = 0
	jeton1 = 0
	jeton2 = 0
	tourdejeu = 0

	while 1:
	
		joueur = tourdejeu % 2
		tourdejeu = tourdejeu + 1
		erreur = True	
		
		while ( erreur == True ):
	
			print " "
			print " "
			lecture()
			print "Au tour du joueur ", joueur + 1

			while 1:
				reponse = raw_input( "Colone 1 - 2 - 3 ? = " )
				try:
					reponse = int(reponse)

				except ValueError:
					print " "

				else:
					break
					

			reponse = reponse - 1
			
			if ( reponse == 0 ):
				jeton0 = jeton0 + 1
				if ( jeton0 > 8 ):
					jeton0 = 8
				else:
					erreur = False
				jeton = jeton0
		
			if ( reponse == 1 ):
				jeton1 = jeton1 + 1
				if ( jeton1 > 8 ):
					jeton1 = 8
				else:
					erreur = False
				jeton = jeton1
	
			if ( reponse == 2 ):
				jeton2 = jeton2  + 1
				if ( jeton2 > 8 ):
					jeton2 = 8
				else:
					erreur = False
				jeton = jeton2
	
	
		ajoutjeton(reponse, jeton, joueur)
		pion(reponse, jeton, joueur)
		lecture()
		victoire()

	

colone0 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
colone1 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
colone2 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]


plateau()
main()


