def algebraic(self): return cartesian_to_algebra((self.actualx, self.actualy))
def algebraic_newpos(self): return cartesian_to_algebra((self.x, self.y))
def __str__(self): return (str(self.x) + "," + str(self.y) + " " + "".join(cartesian_to_algebra((self.x, self.y))))
def eventpos_to_algebraic(self, pos): x = (pos[0] - 60) // 60 y = (pos[1] - 60) // 60 # TODO utils cant return anything greater than 8 or g alg = cartesian_to_algebra((x, y)) return alg
def test_conversion(self): self.assertEquals(cartesian_to_algebra([7, 0]), "h8") self.assertEquals(cartesian_to_algebra([0, 0]), "a8") self.assertEquals(cartesian_to_algebra([0, 7]), "a1") self.assertEquals(cartesian_to_algebra([7, 7]), "h1") self.assertEquals(cartesian_to_algebra([3, 3]), "d5")
def gameOn(self, board): """ This method has a while loop until game exit is called""" # contains main logic and connections self.paintBoard(board) # paints the board after every change lastColor = "black" # only white goes first selectedPiece = None orgpos = None board.storeOnFile() # opens file for storing while not self.gameExit: for event in pygame.event.get(): if event.type == pygame.QUIT: gameExit = True pygame.quit() quit() # quits game elif event.type == pygame.MOUSEBUTTONDOWN: mousepos = pygame.mouse.get_pos() newpos = self.eventpos_to_algebraic(mousepos) # clicks on piece if selectedPiece: # previous click selected the piece orgpos = selectedPiece.currpos valid_move = selectedPiece.move_piece(Pos(newpos)) self.paintErrorText(" ") if valid_move: # only update if its a valid move board.updateMoveOnFile( selectedPiece, str( "".join(cartesian_to_algebra((orgpos.x, orgpos.y))) ), newpos, ) else: selectedPiece=None if valid_move: self.paintBoard(board) colors2 = ["white", "black"] colors2.remove(lastColor) # shows which color piece just went print("last color", str(colors2[0])) self.paintMoveText( str(colors2[0]) + " : " + str( "".join(cartesian_to_algebra((orgpos.x, orgpos.y))) ) + " " + str(newpos) ) print("".join(cartesian_to_algebra((orgpos.x, orgpos.y)))) lastColor = selectedPiece.color selectedPiece = None else: self.paintErrorText("Invalid Move") else: # this click is the first click currentPiece, _ = board.piece_from_algebraic_coord(newpos) if currentPiece: selectedPiece = currentPiece else: continue if lastColor: if currentPiece.color != lastColor: selectedPiece = currentPiece else: # Could use a enum for string colors1 = ["white", "black"] colors1.remove(lastColor) self.paintErrorText( "Please select a %s piece" % str(colors1[0]) ) selectedPiece = None pygame.display.update()
def eventpos_to_algebraic(self, pos): # returns the mouse coords into algebraic coords x = (pos[0] - 60) // 60 y = (pos[1] - 60) // 60 alg = cartesian_to_algebra((x, y)) return alg