Beispiel #1
0
	def MainLoop(self):
		hilo=0
		datito="123"
		s=0
		sc=0
		s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		#s.settimeout(20)
		verify = 2
		if self.redis=='server':
			verify=0
			serv=ventanaServer()
			(direccion)=serv.GetipServer()
			if direccion == '0':
				pygame.quit()
				sys.exit(0)
			else :
				s.bind((direccion, 9999))
				s.listen(1)
				s.settimeout(20)
				#s.setblocking (0)
				print "Esperando conexion..."
				sc,adr = s.accept()
				#sc.settimeout(20)
				#sc.setblocking (0)
		elif self.redis=='client':
			verify=1
			clie=ventanaClient()
			(direccion1)=clie.GetipClient()
			if direccion1 == '0':
				pygame.quit()
				sys.exit(0)
			else :
				s.connect((direccion1, 9999))
		currentPlayerIndex = 0
		turnCount = 0
		sinComer = 0
		reySoloBlanco = 0
		reySoloNegro = 0
		reySolo=0
		ayniN = 0
		ayniB = 0
		while not self.Rules.IsCheckmate(self.Board.GetState(),self.player[currentPlayerIndex].color,ayniB,ayniN):
			board = self.Board.GetState()
			currentColor = self.player[currentPlayerIndex].GetColor()
			#hardcoded so that player 1 is always white
			if currentColor == 'blanco':
				turnCount = turnCount + 1
			self.Gui.PrintMessage("")
			baseMsg = "TURNO %s - %s (%s)" % (str(turnCount),self.player[currentPlayerIndex].GetName(),currentColor)
			self.Gui.PrintMessage("-----%s-----" % baseMsg)
			self.Gui.Draw(board)
			if self.Rules.IsInCheck(board,currentColor):
				self.Gui.PrintMessage("Peligro..."+self.player[currentPlayerIndex].GetName()+" ("+self.player[currentPlayerIndex].GetColor()+") esta en jaque")
				jak = jake()
				(ka) = jak.aviso()
			if currentColor== 'negro'and verify ==0 :#self.player[currentPlayerIndex].GetType() == 'server' and turnCount%2!=0: blanco espera movimiento
				"""#s.settimeout(20)
				#s.setblocking (0)
				#hilo=False
				print "ejecutando el treaht"
				thread.start_new_thread(self.reDato,(s,sc))
				hilo=0
				print hilo,"bucle infinito"
				while (hilo==0):
						pass
				#hilo=True
				print "salio de bucle",hilo
				recibido=datito"""
				recibido = sc.recv(1024)
				if recibido=="empate":
						empa=confirmarDerrota()
						(empat)=empa.confirmacionEmpate()
						if empat==1:
							sc.send("aceptar")
							em=empate()
							(emp)=em.mostrarEmpate()
							self.Gui.EndGame(board)
						elif empat==2:
							sc.send("no")
							recibido = sc.recv(1024)
				elif recibido=="salir":
						vi=victoria()
						(vic)=vi.mostrarVictoria()
						self.Gui.EndGame(board)
				else:
						numero=int(recibido)
						Aa=numero/1000
						AA=numero%1000
						Ee=AA/100
						EE=AA%100
						Ii=EE/10
						II=EE%10
						moveTuple=(Aa,Ee),(Ii,II)

			elif currentColor== 'blanco'and verify==0 :#self.player[currentPlayerIndex].GetType() == 'client' and turnCount%2==0: blanco hace su movimiento
				moveTuple = self.Gui.GetPlayerInput(board,currentColor,verify,s,sc,ayniB,ayniN)
				(a,e),(i,o)=moveTuple
				A=str(a)
				E=str(e)
				I=str(i)
				O=str(o)
				mensaje=A+E+I+O
				sc.send(mensaje)
			elif currentColor== 'blanco'and verify==1: #negro espera movimiento
				recibido = s.recv(1024)
				if recibido=="empate":
						empa=confirmarDerrota()
						(empat)=empa.confirmacionEmpate()
						if empat==1:
							s.send("aceptar")
							em=empate()
							(emp)=em.mostrarEmpate()
							self.Gui.EndGame(board)
						elif empat==2:
							s.send("no")
							recibido = s.recv(1024)
				elif recibido=="salir":
						vi=victoria()
						(vic)=vi.mostrarVictoria()
						self.Gui.EndGame(board)
				else:
						numero=int(recibido)
						Aa=numero/1000
						AA=numero%1000
						Ee=AA/100
						EE=AA%100
						Ii=EE/10
						II=EE%10
						moveTuple=(Aa,Ee),(Ii,II)
			elif currentColor== 'negro'and verify==1: #blanco hace su movimiento
				moveTuple = self.Gui.GetPlayerInput(board,currentColor,verify,s,sc,ayniB,ayniN)
				(a,e),(i,o)=moveTuple
				A=str(a)
				E=str(e)
				I=str(i)
				O=str(o)
				mensaje=A+E+I+O
				s.send(mensaje)
			else:
				moveTuple = self.Gui.GetPlayerInput(board,currentColor,verify,s,sc,ayniB,ayniN)
			moveReport,contadorsito,solito,ayni = self.Board.MovePiece(moveTuple) #moveReport = string like "White Bishop moves from A1 to C3" (+) "and captures ___!"
			self.Gui.PrintMessage(moveReport)
			currentPlayerIndex = (currentPlayerIndex+1)%2 #this will cause the currentPlayerIndex to toggle between 1 and 0
			if ayni==1:
				ayniB=ayniB+1
				print ayniB,"y",ayniN
			elif ayni==2:
				ayniN=ayniN+1
				print ayniB,"y",ayniN
			if contadorsito==1:
				sinComer=sinComer+1
			else:
				sinComer=0
			if solito==1:
				reySoloNegro=reySoloNegro+1
			elif solito==0:
				reySoloBlanco=reySoloBlanco+1
			if reySoloBlanco==14 or reySoloNegro==14:
				reySolo=reySolo+1
			if sinComer==28 or reySolo==15:
				em=empate()
				(emp)=em.mostrarEmpate()
				self.Gui.EndGame(board)

		s.close()
		self.Gui.PrintMessage("--------------------------------------------------     JAKE MATE")
		winnerIndex = (currentPlayerIndex+1)%2
		self.Gui.PrintMessage(self.player[winnerIndex].GetName()+" ("+self.player[winnerIndex].GetColor()+") gano el juego")
		if (self.player[winnerIndex].GetColor()=='blanco' and verify==0) or (self.player[winnerIndex].GetColor()=='negro' and verify==1):
			vi = victoria()
			(vic) = vi.mostrarVictoria()
		elif (self.player[winnerIndex].GetColor()=='negro' and verify==0) or (self.player[winnerIndex].GetColor()=='blanco' and verify==1):
			dec = derrota()
			(derr) = dec.mostrarDerrota()
		else:
			vi = victoria()
			(vic) = vi.mostrarVictoria()
		self.Gui.EndGame(board)
Beispiel #2
0
	def GetPlayerInput(self,board,currentColor,verify,s,sc,ayniB,ayniN):
		#returns ((from_row,from_col),(to_row,to_col))
		ayniBlanco=ayniB
		ayniNegro=ayniN
		fromSquareChosen = 0
		toSquareChosen = 0
		while not fromSquareChosen or not toSquareChosen:
			squareClicked = []
			pygame.event.set_blocked(MOUSEMOTION)
			e = pygame.event.wait()
			if e.type is KEYDOWN:
				if e.key is K_ESCAPE:
					fromSquareChosen = 0
					fromTuple = []
			if e.type is MOUSEBUTTONDOWN:
				(mouseX,mouseY) = pygame.mouse.get_pos()
				squareClicked = self.ConvertToChessCoords((mouseX,mouseY))
				if squareClicked[0]<0 or squareClicked[0]>9 or squareClicked[1]<0 or squareClicked[1]>9:
					squareClicked = [] #not a valid chess square
				elif squareClicked[0]==8 or squareClicked[1]==8:
					defs = exits()
					(confirm)=defs.confirmacion()
					if confirm == 1:
						if verify==0 :
							sc.send("empate")
							empatear=sc.recv(1024)
							if empatear=="aceptar":
							    em=empate()
							    (emp)=em.mostrarEmpate()
							    pygame.quit()
							    sc.close()
							    sys.exit(0)
							elif empatear=="no":
							    squareClicked = []
						elif verify==1:
							s.send("empate")
							empatar=sc.recv(1024)
							if empatar=="aceptar":
							    em=empate()
							    (emp)=em.mostrarEmpate()
							    pygame.quit()
							    s.close()
							    sys.exit(0)
							elif empatar=="no":
							    squareClicked = []
						else:
							em=empate()
							(emp)=em.mostrarEmpate()
							pygame.quit()
							sys.exit(0)
					elif confirm == 2:
						squareClicked = []
				elif squareClicked[0]==9 or squareClicked[1]==9:
					defs = exits()
					(confirm)=defs.confirmacion()
					if confirm == 1:
						if verify==0 :
							sc.send("salir")
							der=derrota()
							(derro)=der.mostrarDerrota()
							pygame.quit()
							sc.close()
							sys.exit(0)
						elif verify==1:
							s.send("salir")
							der=derrota()
							(derro)=der.mostrarDerrota()
							pygame.quit()
							s.close()
							sys.exit(0)
						else:
							der=derrota()
							(derro)=der.mostrarDerrota()
							pygame.quit()
							sys.exit(0)
					elif confirm == 2:
						squareClicked = []
			if e.type is QUIT: #the "x" kill button
				pygame.quit()
				sys.exit(0)



			if not fromSquareChosen and not toSquareChosen:
				self.Draw(board)
				if squareClicked != []:
					(r,c) = squareClicked
					if currentColor == 'negro' and 'b' in board[r][c]:
						if len(self.Rules.GetListOfValidMoves(board,currentColor,squareClicked,ayniBlanco,ayniNegro))>0:
							fromSquareChosen = 1
							fromTuple = squareClicked
					elif currentColor == 'blanco' and 'w' in board[r][c]:
						if len(self.Rules.GetListOfValidMoves(board,currentColor,squareClicked,ayniBlanco,ayniNegro))>0:
							fromSquareChosen = 1
							fromTuple = squareClicked

			elif fromSquareChosen and not toSquareChosen:
				possibleDestinations = self.Rules.GetListOfValidMoves(board,currentColor,fromTuple,ayniBlanco,ayniNegro)
				self.Draw(board,possibleDestinations)
				if squareClicked != []:
					(r,c) = squareClicked
					if squareClicked in possibleDestinations:
						toSquareChosen = 1
						toTuple = squareClicked
					elif currentColor == 'negro' and 'b' in board[r][c]:
						if squareClicked == fromTuple:
							fromSquareChosen = 0
						elif len(self.Rules.GetListOfValidMoves(board,currentColor,squareClicked,ayniBlanco,ayniNegro))>0:
							fromSquareChosen = 1
							fromTuple = squareClicked
						else:
							fromSquareChosen = 0 #piece is of own color, but no possible moves
					elif currentColor == 'blanco' and 'w' in board[r][c]:
						if squareClicked == fromTuple:
							fromSquareChosen = 0
						elif len(self.Rules.GetListOfValidMoves(board,currentColor,squareClicked,ayniBlanco,ayniNegro))>0:
							fromSquareChosen = 1
							fromTuple = squareClicked
						else:
							fromSquareChosen = 0
					else: #blank square or opposite color piece not in possible destinations clicked
						fromSquareChosen = 0

		return (fromTuple,toTuple)
Beispiel #3
0
    def MainLoop(self):
        hilo = 0
        datito = "123"
        s = 0
        sc = 0
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        #s.settimeout(20)
        verify = 2
        if self.redis == 'server':
            verify = 0
            serv = ventanaServer()
            (direccion) = serv.GetipServer()
            if direccion == '0':
                pygame.quit()
                sys.exit(0)
            else:
                s.bind((direccion, 9999))
                s.listen(1)
                s.settimeout(20)
                #s.setblocking (0)
                print "Esperando conexion..."
                sc, adr = s.accept()
                #sc.settimeout(20)
                #sc.setblocking (0)
        elif self.redis == 'client':
            verify = 1
            clie = ventanaClient()
            (direccion1) = clie.GetipClient()
            if direccion1 == '0':
                pygame.quit()
                sys.exit(0)
            else:
                s.connect((direccion1, 9999))
        currentPlayerIndex = 0
        turnCount = 0
        sinComer = 0
        reySoloBlanco = 0
        reySoloNegro = 0
        reySolo = 0
        ayniN = 0
        ayniB = 0
        while not self.Rules.IsCheckmate(self.Board.GetState(),
                                         self.player[currentPlayerIndex].color,
                                         ayniB, ayniN):
            board = self.Board.GetState()
            currentColor = self.player[currentPlayerIndex].GetColor()
            #hardcoded so that player 1 is always white
            if currentColor == 'blanco':
                turnCount = turnCount + 1
            self.Gui.PrintMessage("")
            baseMsg = "TURNO %s - %s (%s)" % (
                str(turnCount), self.player[currentPlayerIndex].GetName(),
                currentColor)
            self.Gui.PrintMessage("-----%s-----" % baseMsg)
            self.Gui.Draw(board)
            if self.Rules.IsInCheck(board, currentColor):
                self.Gui.PrintMessage(
                    "Peligro..." + self.player[currentPlayerIndex].GetName() +
                    " (" + self.player[currentPlayerIndex].GetColor() +
                    ") esta en jaque")
                jak = jake()
                (ka) = jak.aviso()
            if currentColor == 'negro' and verify == 0:  #self.player[currentPlayerIndex].GetType() == 'server' and turnCount%2!=0: blanco espera movimiento
                """#s.settimeout(20)
				#s.setblocking (0)
				#hilo=False
				print "ejecutando el treaht"
				thread.start_new_thread(self.reDato,(s,sc))
				hilo=0
				print hilo,"bucle infinito"
				while (hilo==0):
						pass
				#hilo=True
				print "salio de bucle",hilo
				recibido=datito"""
                recibido = sc.recv(1024)
                if recibido == "empate":
                    empa = confirmarDerrota()
                    (empat) = empa.confirmacionEmpate()
                    if empat == 1:
                        sc.send("aceptar")
                        em = empate()
                        (emp) = em.mostrarEmpate()
                        self.Gui.EndGame(board)
                    elif empat == 2:
                        sc.send("no")
                        recibido = sc.recv(1024)
                elif recibido == "salir":
                    vi = victoria()
                    (vic) = vi.mostrarVictoria()
                    self.Gui.EndGame(board)
                else:
                    numero = int(recibido)
                    Aa = numero / 1000
                    AA = numero % 1000
                    Ee = AA / 100
                    EE = AA % 100
                    Ii = EE / 10
                    II = EE % 10
                    moveTuple = (Aa, Ee), (Ii, II)

            elif currentColor == 'blanco' and verify == 0:  #self.player[currentPlayerIndex].GetType() == 'client' and turnCount%2==0: blanco hace su movimiento
                moveTuple = self.Gui.GetPlayerInput(board, currentColor,
                                                    verify, s, sc, ayniB,
                                                    ayniN)
                (a, e), (i, o) = moveTuple
                A = str(a)
                E = str(e)
                I = str(i)
                O = str(o)
                mensaje = A + E + I + O
                sc.send(mensaje)
            elif currentColor == 'blanco' and verify == 1:  #negro espera movimiento
                recibido = s.recv(1024)
                if recibido == "empate":
                    empa = confirmarDerrota()
                    (empat) = empa.confirmacionEmpate()
                    if empat == 1:
                        s.send("aceptar")
                        em = empate()
                        (emp) = em.mostrarEmpate()
                        self.Gui.EndGame(board)
                    elif empat == 2:
                        s.send("no")
                        recibido = s.recv(1024)
                elif recibido == "salir":
                    vi = victoria()
                    (vic) = vi.mostrarVictoria()
                    self.Gui.EndGame(board)
                else:
                    numero = int(recibido)
                    Aa = numero / 1000
                    AA = numero % 1000
                    Ee = AA / 100
                    EE = AA % 100
                    Ii = EE / 10
                    II = EE % 10
                    moveTuple = (Aa, Ee), (Ii, II)
            elif currentColor == 'negro' and verify == 1:  #blanco hace su movimiento
                moveTuple = self.Gui.GetPlayerInput(board, currentColor,
                                                    verify, s, sc, ayniB,
                                                    ayniN)
                (a, e), (i, o) = moveTuple
                A = str(a)
                E = str(e)
                I = str(i)
                O = str(o)
                mensaje = A + E + I + O
                s.send(mensaje)
            else:
                moveTuple = self.Gui.GetPlayerInput(board, currentColor,
                                                    verify, s, sc, ayniB,
                                                    ayniN)
            moveReport, contadorsito, solito, ayni = self.Board.MovePiece(
                moveTuple
            )  #moveReport = string like "White Bishop moves from A1 to C3" (+) "and captures ___!"
            self.Gui.PrintMessage(moveReport)
            currentPlayerIndex = (
                currentPlayerIndex + 1
            ) % 2  #this will cause the currentPlayerIndex to toggle between 1 and 0
            if ayni == 1:
                ayniB = ayniB + 1
                print ayniB, "y", ayniN
            elif ayni == 2:
                ayniN = ayniN + 1
                print ayniB, "y", ayniN
            if contadorsito == 1:
                sinComer = sinComer + 1
            else:
                sinComer = 0
            if solito == 1:
                reySoloNegro = reySoloNegro + 1
            elif solito == 0:
                reySoloBlanco = reySoloBlanco + 1
            if reySoloBlanco == 14 or reySoloNegro == 14:
                reySolo = reySolo + 1
            if sinComer == 28 or reySolo == 15:
                em = empate()
                (emp) = em.mostrarEmpate()
                self.Gui.EndGame(board)

        s.close()
        self.Gui.PrintMessage(
            "--------------------------------------------------     JAKE MATE")
        winnerIndex = (currentPlayerIndex + 1) % 2
        self.Gui.PrintMessage(self.player[winnerIndex].GetName() + " (" +
                              self.player[winnerIndex].GetColor() +
                              ") gano el juego")
        if (self.player[winnerIndex].GetColor() == 'blanco' and verify
                == 0) or (self.player[winnerIndex].GetColor() == 'negro'
                          and verify == 1):
            vi = victoria()
            (vic) = vi.mostrarVictoria()
        elif (self.player[winnerIndex].GetColor() == 'negro' and verify
              == 0) or (self.player[winnerIndex].GetColor() == 'blanco'
                        and verify == 1):
            dec = derrota()
            (derr) = dec.mostrarDerrota()
        else:
            vi = victoria()
            (vic) = vi.mostrarVictoria()
        self.Gui.EndGame(board)