Beispiel #1
0
    def reset(self):
        # clear the board
        for i in range(0, 8):
            for j in range(0, 8):
                self.board[i][j] = None

        # black pieces
        for i in range(0, 8):
            self.board[1][i] = Pawn(self.black)
        self.board[0][0] = Rook(self.black)
        self.board[0][1] = Knight(self.black)
        self.board[0][2] = Bishop(self.black)
        self.board[0][3] = Queen(self.black)
        self.board[0][4] = King(self.black)
        self.board[0][5] = Bishop(self.black)
        self.board[0][6] = Knight(self.black)
        self.board[0][7] = Rook(self.black)

        # white pieces
        for i in range(0, 8):
            self.board[6][i] = Pawn(self.white)
        self.board[7][0] = Rook(self.white)
        self.board[7][1] = Knight(self.white)
        self.board[7][2] = Bishop(self.white)
        self.board[7][3] = Queen(self.white)
        self.board[7][4] = King(self.white)
        self.board[7][5] = Bishop(self.white)
        self.board[7][6] = Knight(self.white)
        self.board[7][7] = Rook(self.white)
Beispiel #2
0
 def makeBuilders(self):
     randNumOfHills = random.randrange(3, 5)
     for i in range(randNumOfHills):
         queen = Queen(self._grid)
         antHill = queen.selectLocation().addInitRooms().addInitFood(
         ).addInitAnts(self.antIDCount).build()
         x = antHill.getXCoord()
         y = antHill.getYCoord()
         self._holderX = x
         self._holderY = y
         self._grid[y][x].setFood(1)
         self._grid[y][x].setHill(antHill)
         self._hills.append(antHill)
         self.antIDCount += 20
Beispiel #3
0
 def test_run(self):
     c = Queen.Queen_challenge()
     value = c.random_chromosome()
     fit = c.fitness(value)
     max_fit = c.probability(value,fit)
     population = value
     parent1 = population[0]
     parent2 = population[1]
     self.assertIn(parent1, population)
     self.assertIn(parent2, population)
Beispiel #4
0
    def loadSprites(self, levelNumber):
        #Used for finding centers of sprites
        x_offset = SIZE / 2
        y_offset = SIZE / 2
        #Get game level, layout and sprites
        gameLevel = Level.Level()
        self.layout = gameLevel.getLevel(levelNumber)
        sprites = gameLevel.returnSprites()
        #Create group for multiple tree and bee sprites
        self.treeSprites = pygame.sprite.Group()
        self.beeSprites = pygame.sprite.Group()

        #Create game levels, objects
        for y in range(len(self.layout)):
            for x in range(len(self.layout[y])):
                #Finding center point of each sprite
                center = [(x * SIZE) + x_offset, (y * SIZE + y_offset)]

                if self.layout[y][x] == gameLevel.TREE:
                    tree = BasicSprite.Sprite(center,
                                              sprites[gameLevel.TREE - 1])
                    self.treeSprites.add(tree)
                elif self.layout[y][x] == gameLevel.HORNET:
                    self.hornet = Hornet.Hornet(center,
                                                sprites[gameLevel.HORNET - 1])

                elif self.layout[y][x] == gameLevel.HIVE:
                    self.hive = BasicSprite.Sprite(center,
                                                   sprites[gameLevel.HIVE - 1])

                elif self.layout[y][x] == gameLevel.BEE:
                    bee = Bee.Bee(center, sprites[gameLevel.BEE - 1])
                    self.beeSprites.add(bee)
                elif self.layout[y][x] == gameLevel.QUEEN:
                    queen = Queen.Queen(center, sprites[gameLevel.QUEEN - 1])
                    self.beeSprites.add(queen)
                elif self.layout[y][x] == gameLevel.LARVA:
                    larva = Larva.Larva(center, sprites[gameLevel.LARVA - 1])
                    self.beeSprites.add(larva)

        #Rendering hornet and hive sprite
        self.hornetSprite = pygame.sprite.RenderPlain((self.hornet))
        self.hiveSprite = pygame.sprite.RenderPlain((self.hive))
Beispiel #5
0
 def test_random_chromosome(self):
     c = Queen.Queen_challenge()
     value = c.random_chromosome()
     self.assertAlmostEqual(len(value), 8)
Beispiel #6
0
 def test_result(self):
     c = Queen.Queen_challenge()
     c.run()
     result = c.res
     self.assertIsInstance(result, str)
Beispiel #7
0
    def makeAIMove(self, selectedPiece, selectedSquare, grid, whitePieces,
                   blackPieces):
        #Delete the piece to move from grid
        grid[selectedPiece.ypos][selectedPiece.xpos].piece = None

        #Checks if we just captured a piece
        if (selectedSquare.piece is not None):
            if (selectedSquare.piece.color == "White"):
                self.remove(whitePieces, selectedSquare.piece)
            else:
                self.remove(blackPieces, selectedSquare.piece)

        grid[selectedSquare.ypos][
            selectedSquare.xpos].piece = selectedPiece.newPosition(
                selectedPiece.color, selectedSquare.xpos, selectedSquare.ypos,
                selectedPiece.pieceImage, selectedPiece.canvasImage, False)

        #change the position of piece in white or black pieces list
        if (selectedPiece.color == "White"):
            self.remove(whitePieces, selectedPiece)
            whitePieces.append(
                grid[selectedSquare.ypos][selectedSquare.xpos].piece)

        else:
            self.remove(blackPieces, selectedPiece)
            blackPieces.append(
                grid[selectedSquare.ypos][selectedSquare.xpos].piece)

        #Checks if we just castled king-side, if so we want to move the rook as well
        if type(
                selectedPiece
        ) == King and selectedPiece.xpos == 4 and selectedSquare.xpos == 6:
            castleRook = grid[selectedPiece.ypos][7].piece
            grid[selectedPiece.ypos][5].piece = castleRook.newPosition(
                selectedPiece.color, 5, selectedPiece.ypos,
                castleRook.pieceImage, castleRook.canvasImage, False)

            grid[selectedPiece.ypos][7].piece = None

            if (castleRook.color == "White"):
                self.remove(whitePieces, castleRook)
                whitePieces.append(grid[selectedPiece.ypos][5].piece)
            else:
                self.remove(blackPieces, castleRook)
                blackPieces.append(grid[selectedPiece.ypos][5].piece)

        #Checks if we just castled queen-side, if so we want to move the rook as well
        if type(
                selectedPiece
        ) == King and selectedPiece.xpos == 4 and selectedSquare.xpos == 2:
            castleRook = self.grid[selectedPiece.ypos][0].piece
            grid[selectedPiece.ypos][3].piece = castleRook.newPosition(
                selectedPiece.color, 3, selectedPiece.ypos,
                castleRook.pieceImage, castleRook.canvasImage, False)

            grid[selectedPiece.ypos][0].piece = None

            if (castleRook.color == "White"):
                self.remove(whitePieces, castleRook)
                whitePieces.append(grid[selectedPiece.ypos][3].piece)
            else:
                self.remove(blackPieces, castleRook)
                blackPieces.append(grid[selectedPiece.ypos][3].piece)

        #Checks if we just promoted a pawn
        if (type(selectedPiece) == Pawn
                and (selectedSquare.ypos == 0 or selectedSquare.ypos == 7)):
            if (selectedSquare.ypos == 7):
                self.remove(
                    blackPieces,
                    grid[selectedSquare.ypos][selectedSquare.xpos].piece)
                grid[selectedSquare.ypos][selectedSquare.xpos].piece = Queen(
                    selectedPiece.color, selectedSquare.xpos,
                    selectedSquare.ypos, self.bqPhoto, None, False)
                blackPieces.append(
                    grid[selectedSquare.ypos][selectedSquare.xpos].piece)
            else:
                self.remove(
                    whitePieces,
                    grid[selectedSquare.ypos][selectedSquare.xpos].piece)
                grid[selectedSquare.ypos][selectedSquare.xpos].piece = Queen(
                    selectedPiece.color, selectedSquare.xpos,
                    selectedSquare.ypos, self.wqPhoto, None, False)
                self.whitePieces.append(
                    self.grid[selectedSquare.ypos][selectedSquare.xpos].piece)

        return grid, whitePieces, blackPieces
Beispiel #8
0
    def initialize_pieces(self, canvas):

        self.bpPhoto = PhotoImage(file="blackpawn.png")
        self.wpPhoto = PhotoImage(file="whitepawn.png")
        self.brPhoto = PhotoImage(file="blackrook.png")
        self.wrPhoto = PhotoImage(file="whiterook.png")
        self.bnPhoto = PhotoImage(file="blackknight.png")
        self.wnPhoto = PhotoImage(file="whiteknight.png")
        self.bbPhoto = PhotoImage(file="blackbishop.png")
        self.wbPhoto = PhotoImage(file="whitebishop.png")
        self.bqPhoto = PhotoImage(file="blackqueen.png")
        self.wqPhoto = PhotoImage(file="whitequeen.png")
        self.bkPhoto = PhotoImage(file="blackking.png")
        self.wkPhoto = PhotoImage(file="whiteking.png")

        for i in range(8):
            c = canvas.create_image(
                39.5 + i * 75, 114.5,
                image=self.bpPhoto)  #Initializes black pawns
            self.grid[1][i].piece = Pawn("Black", i, 1, self.bpPhoto, c, True)
            self.blackPieces.append(self.grid[1][i].piece)

        for i in range(8):
            c = canvas.create_image(
                39.5 + i * 75, 487.5,
                image=self.wpPhoto)  #Initializes white pawns
            self.grid[6][i].piece = Pawn("White", i, 6, self.wpPhoto, c, True)
            self.whitePieces.append(self.grid[6][i].piece)

        c = canvas.create_image(39.5, 39.5,
                                image=self.brPhoto)  #Initializes black rooks
        self.grid[0][0].piece = Rook("Black", 0, 0, self.brPhoto, c, True)
        self.blackPieces.append(self.grid[0][0].piece)
        c = canvas.create_image(564.5, 39.5, image=self.brPhoto)
        self.grid[0][7].piece = Rook("Black", 7, 0, self.brPhoto, c, True)
        self.blackPieces.append(self.grid[0][7].piece)

        c = canvas.create_image(39.5, 564.5,
                                image=self.wrPhoto)  #Initializes white rooks
        self.grid[7][0].piece = Rook("White", 0, 7, self.wrPhoto, c, True)
        self.whitePieces.append(self.grid[7][0].piece)
        c = canvas.create_image(564.5, 564.5, image=self.wrPhoto)
        self.grid[7][7].piece = Rook("White", 7, 7, self.wrPhoto, c, True)
        self.whitePieces.append(self.grid[7][7].piece)

        c = canvas.create_image(114.5, 39.5,
                                image=self.bnPhoto)  #Initializes black knights
        self.grid[0][1].piece = Knight("Black", 1, 0, self.bnPhoto, c, True)
        self.blackPieces.append(self.grid[0][1].piece)
        c = canvas.create_image(489.5, 39.5, image=self.bnPhoto)
        self.grid[0][6].piece = Knight("Black", 6, 0, self.bnPhoto, c, True)
        self.blackPieces.append(self.grid[0][6].piece)

        c = canvas.create_image(114.5, 564.5,
                                image=self.wnPhoto)  #Initializes white knights
        self.grid[7][1].piece = Knight("White", 1, 7, self.wnPhoto, c, True)
        self.whitePieces.append(self.grid[7][1].piece)
        c = canvas.create_image(489.5, 564.5, image=self.wnPhoto)
        self.grid[7][6].piece = Knight("White", 6, 7, self.wnPhoto, c, True)
        self.whitePieces.append(self.grid[7][6].piece)

        c = canvas.create_image(189.5, 39.5,
                                image=self.bbPhoto)  #Initializes black bishops
        self.grid[0][2].piece = Bishop("Black", 2, 0, self.bbPhoto, c, True)
        self.blackPieces.append(self.grid[0][2].piece)
        c = canvas.create_image(414.5, 39.5, image=self.bbPhoto)
        self.grid[0][5].piece = Bishop("Black", 5, 0, self.bbPhoto, c, True)
        self.blackPieces.append(self.grid[0][5].piece)

        c = canvas.create_image(189.5, 564.5,
                                image=self.wbPhoto)  #Initializes white bishops
        self.grid[7][2].piece = Bishop("White", 2, 7, self.wbPhoto, c, True)
        self.whitePieces.append(self.grid[7][2].piece)
        c = canvas.create_image(414.5, 564.5, image=self.wbPhoto)
        self.grid[7][5].piece = Bishop("White", 5, 7, self.wbPhoto, c, True)
        self.whitePieces.append(self.grid[7][5].piece)

        c = canvas.create_image(264.5, 39.5,
                                image=self.bqPhoto)  #Initializes black queen
        self.grid[0][3].piece = Queen("Black", 3, 0, self.bqPhoto, c, True)
        self.blackPieces.append(self.grid[0][3].piece)
        c = canvas.create_image(264.5, 564.5,
                                image=self.wqPhoto)  #Initializes white queen
        self.grid[7][3].piece = Queen("White", 3, 7, self.wqPhoto, c, True)
        self.whitePieces.append(self.grid[7][3].piece)

        c = canvas.create_image(339.5, 39.5,
                                image=self.bkPhoto)  #Initializes black king
        self.grid[0][4].piece = King("Black", 4, 0, self.bkPhoto, c, True)
        self.blackPieces.append(self.grid[0][4].piece)
        c = canvas.create_image(339.5, 564.5,
                                image=self.wkPhoto)  #Initializes white king
        self.grid[7][4].piece = King("White", 4, 7, self.wkPhoto, c, True)
        self.whitePieces.append(self.grid[7][4].piece)
Beispiel #9
0
    def makeMove(self, selectedPiece, selectedSquare):
        #Delete the piece to move from canvas and grid
        self.board.delete(selectedPiece.canvasImage)
        self.grid[selectedPiece.ypos][selectedPiece.xpos].piece = None

        #Checks if we just captured a piece
        if (selectedSquare.piece is not None):
            self.board.delete(selectedSquare.piece.canvasImage)
            if (selectedSquare.piece.color == "White"):
                self.remove(self.whitePieces, selectedSquare.piece)
            else:
                self.remove(self.blackPieces, selectedSquare.piece)

        #Create new image on new position
        newx = 39.5 + selectedSquare.xpos * 75
        newy = 39.5 + selectedSquare.ypos * 75
        c = self.board.create_image(newx, newy, image=selectedPiece.pieceImage)
        self.grid[selectedSquare.ypos][
            selectedSquare.xpos].piece = selectedPiece.newPosition(
                selectedPiece.color, selectedSquare.xpos, selectedSquare.ypos,
                selectedPiece.pieceImage, c, False)

        #change the position of piece in white or black pieces list
        if (selectedPiece.color == "White"):
            self.remove(self.whitePieces, selectedPiece)
            self.whitePieces.append(
                self.grid[selectedSquare.ypos][selectedSquare.xpos].piece)

        else:
            self.remove(self.blackPieces, selectedPiece)
            self.blackPieces.append(
                self.grid[selectedSquare.ypos][selectedSquare.xpos].piece)

        #Checks if we just castled king-side, if so we want to move the rook as well
        if type(
                selectedPiece
        ) == King and selectedPiece.xpos == 4 and selectedSquare.xpos == 6:
            #Create new image on new position
            newx = 39.5 + 5 * 75
            newy = 39.5 + selectedPiece.ypos * 75
            castleRook = self.grid[selectedPiece.ypos][7].piece
            c = self.board.create_image(newx,
                                        newy,
                                        image=castleRook.pieceImage)
            self.grid[selectedPiece.ypos][5].piece = castleRook.newPosition(
                selectedPiece.color, 5, selectedPiece.ypos,
                castleRook.pieceImage, c, False)

            self.board.delete(
                self.grid[selectedPiece.ypos][7].piece.canvasImage)
            self.grid[selectedPiece.ypos][7].piece = None

            if (castleRook.color == "White"):
                self.remove(self.whitePieces, castleRook)
                self.whitePieces.append(self.grid[selectedPiece.ypos][5].piece)
            else:
                self.remove(self.blackPieces, castleRook)
                self.blackPieces.append(self.grid[selectedPiece.ypos][5].piece)

        #Checks if we just castled queen-side, if so we want to move the rook as well
        if type(
                selectedPiece
        ) == King and selectedPiece.xpos == 4 and selectedSquare.xpos == 2:
            #Create new image on new position
            newx = 39.5 + 3 * 75
            newy = 39.5 + selectedPiece.ypos * 75
            castleRook = self.grid[selectedPiece.ypos][0].piece
            c = self.board.create_image(newx,
                                        newy,
                                        image=castleRook.pieceImage)
            self.grid[selectedPiece.ypos][3].piece = castleRook.newPosition(
                selectedPiece.color, 3, selectedPiece.ypos,
                castleRook.pieceImage, c, False)

            self.board.delete(
                self.grid[selectedPiece.ypos][0].piece.canvasImage)
            self.grid[selectedPiece.ypos][0].piece = None

            if (castleRook.color == "White"):
                self.remove(self.whitePieces, castleRook)
                self.whitePieces.append(self.grid[selectedPiece.ypos][3].piece)
            else:
                self.remove(self.blackPieces, castleRook)
                self.blackPieces.append(self.grid[selectedPiece.ypos][3].piece)

        #Checks if we just promoted a pawn
        if (type(selectedPiece) == Pawn
                and (selectedSquare.ypos == 0 or selectedSquare.ypos == 7)):
            promote = simpledialog.askstring(
                title="Test", prompt="What do you want to promote to?")
            if (promote == "Knight"):
                if (selectedSquare.ypos == 7):
                    self.remove(
                        self.blackPieces, self.grid[selectedSquare.ypos][
                            selectedSquare.xpos].piece)
                    self.board.delete(c)
                    self.grid[selectedSquare.ypos][
                        selectedSquare.xpos].piece = None
                    c = self.board.create_image(newx, newy, image=self.bnPhoto)
                    self.grid[selectedSquare.ypos][
                        selectedSquare.xpos].piece = Knight(
                            selectedPiece.color, selectedSquare.xpos,
                            selectedSquare.ypos, self.bnPhoto, c, False)
                    self.blackPieces.append(self.grid[selectedSquare.ypos][
                        selectedSquare.xpos].piece)

                else:
                    self.remove(
                        self.whitePieces, self.grid[selectedSquare.ypos][
                            selectedSquare.xpos].piece)
                    self.board.delete(c)
                    self.grid[selectedSquare.ypos][
                        selectedSquare.xpos].piece = None
                    c = self.board.create_image(newx, newy, image=self.wnPhoto)
                    self.grid[selectedSquare.ypos][
                        selectedSquare.xpos].piece = Knight(
                            selectedPiece.color, selectedSquare.xpos,
                            selectedSquare.ypos, self.wnPhoto, c, False)
                    self.whitePieces.append(self.grid[selectedSquare.ypos][
                        selectedSquare.xpos].piece)
            else:
                if (selectedSquare.ypos == 7):
                    self.remove(
                        self.blackPieces, self.grid[selectedSquare.ypos][
                            selectedSquare.xpos].piece)
                    self.board.delete(c)
                    self.grid[selectedSquare.ypos][
                        selectedSquare.xpos].piece = None
                    c = self.board.create_image(newx, newy, image=self.bqPhoto)
                    self.grid[selectedSquare.ypos][
                        selectedSquare.xpos].piece = Queen(
                            selectedPiece.color, selectedSquare.xpos,
                            selectedSquare.ypos, self.bqPhoto, c, False)
                    self.blackPieces.append(self.grid[selectedSquare.ypos][
                        selectedSquare.xpos].piece)
                else:
                    self.remove(
                        self.whitePieces, self.grid[selectedSquare.ypos][
                            selectedSquare.xpos].piece)
                    self.board.delete(c)
                    self.grid[selectedSquare.ypos][
                        selectedSquare.xpos].piece = None
                    c = self.board.create_image(newx, newy, image=self.wqPhoto)
                    self.grid[selectedSquare.ypos][
                        selectedSquare.xpos].piece = Queen(
                            selectedPiece.color, selectedSquare.xpos,
                            selectedSquare.ypos, self.wqPhoto, c, False)
                    self.whitePieces.append(self.grid[selectedSquare.ypos][
                        selectedSquare.xpos].piece)
    def movePiece(self, event):
        x = (event.x - 2) // 75
        y = (event.y - 2) // 75
        selectedSquare = self.grid[y][x]
        selectedPiece = selectedSquare.piece

        if (self.selectPiece == True and selectedPiece is not None
                and selectedPiece.color
                == self.turn):  #If we click on a valid piece
            self.board.delete(selectedPiece.canvasImage)
            selectedPiece.canvasImage = None
            self.selectedRectangle = self.board.create_rectangle(
                2 + x * 75,
                2 + y * 75,
                2 + x * 75 + 75,
                2 + y * 75 + 75,
                fill="red")
            #Fill the background square red and redraw piece image
            c = self.board.create_image(39.5 + x * 75,
                                        39.5 + y * 75,
                                        image=selectedPiece.pieceImage)
            selectedPiece.canvasImage = c
            self.selectPiece = False
            self.pieceToMove = selectedPiece

            #get the piece's possible moves
            self.possibleMoves = self.removeCheckMoves(
                self.pieceToMove.getMoves(self.grid), self.pieceToMove,
                self.grid, self.whitePieces, self.blackPieces)

        elif (self.selectPiece == False
              and selectedSquare in self.possibleMoves):
            #Delete the piece to move from canvas and grid
            self.board.delete(self.pieceToMove.canvasImage)
            self.grid[self.pieceToMove.ypos][
                self.pieceToMove.xpos].piece = None

            #Checks if we just captured a piece
            if (selectedPiece is not None):
                self.board.delete(selectedPiece.canvasImage)
                if (selectedPiece.color == "White"):
                    self.whitePieces.remove(selectedPiece)
                else:
                    self.blackPieces.remove(selectedPiece)

            #Create new image on new position
            newx = 39.5 + selectedSquare.xpos * 75
            newy = 39.5 + selectedSquare.ypos * 75
            c = self.board.create_image(newx,
                                        newy,
                                        image=self.pieceToMove.pieceImage)
            self.grid[selectedSquare.ypos][
                selectedSquare.xpos].piece = self.pieceToMove.newPosition(
                    self.pieceToMove.color, selectedSquare.xpos,
                    selectedSquare.ypos, self.pieceToMove.pieceImage, c, False)

            #change the position of piece in white or black pieces list
            if (self.pieceToMove.color == "White"):
                self.whitePieces.remove(self.pieceToMove)
                self.whitePieces.append(
                    self.grid[selectedSquare.ypos][selectedSquare.xpos].piece)

            else:
                self.blackPieces.remove(self.pieceToMove)
                self.blackPieces.append(
                    self.grid[selectedSquare.ypos][selectedSquare.xpos].piece)

            #Checks if we just castled king-side, if so we want to move the rook as well
            if type(
                    self.pieceToMove
            ) == King and self.pieceToMove.xpos == 4 and selectedSquare.xpos == 6:
                #Create new image on new position
                newx = 39.5 + 5 * 75
                newy = 39.5 + self.pieceToMove.ypos * 75
                castleRook = self.grid[self.pieceToMove.ypos][7].piece
                c = self.board.create_image(newx,
                                            newy,
                                            image=castleRook.pieceImage)
                self.grid[
                    self.pieceToMove.ypos][5].piece = castleRook.newPosition(
                        self.pieceToMove.color, 5, self.pieceToMove.ypos,
                        castleRook.pieceImage, c, False)

                self.board.delete(
                    self.grid[self.pieceToMove.ypos][7].piece.canvasImage)
                self.grid[self.pieceToMove.ypos][7].piece = None

                if (castleRook.color == "White"):
                    self.whitePieces.remove(castleRook)
                    self.whitePieces.append(
                        self.grid[self.pieceToMove.ypos][5].piece)
                else:
                    self.blackPieces.remove(castleRook)
                    self.blackPieces.append(
                        self.grid[self.pieceToMove.ypos][5].piece)

            #Checks if we just castled queen-side, if so we want to move the rook as well
            if type(
                    self.pieceToMove
            ) == King and self.pieceToMove.xpos == 4 and selectedSquare.xpos == 2:
                #Create new image on new position
                newx = 39.5 + 3 * 75
                newy = 39.5 + self.pieceToMove.ypos * 75
                castleRook = self.grid[self.pieceToMove.ypos][0].piece
                c = self.board.create_image(newx,
                                            newy,
                                            image=castleRook.pieceImage)
                self.grid[
                    self.pieceToMove.ypos][3].piece = castleRook.newPosition(
                        self.pieceToMove.color, 3, self.pieceToMove.ypos,
                        castleRook.pieceImage, c, False)

                self.board.delete(
                    self.grid[self.pieceToMove.ypos][0].piece.canvasImage)
                self.grid[self.pieceToMove.ypos][0].piece = None

                if (castleRook.color == "White"):
                    self.whitePieces.remove(castleRook)
                    self.whitePieces.append(
                        self.grid[self.pieceToMove.ypos][3].piece)
                else:
                    self.blackPieces.remove(castleRook)
                    self.blackPieces.append(
                        self.grid[self.pieceToMove.ypos][3].piece)

            #Checks if we just promoted a pawn
            if (type(self.pieceToMove) == Pawn and
                (selectedSquare.ypos == 0 or selectedSquare.ypos == 7)):
                promote = simpledialog.askstring(
                    title="Test", prompt="What do you want to promote to?")
                if (promote == "Knight"):
                    if (selectedSquare.ypos == 7):
                        self.blackPieces.remove(self.grid[selectedSquare.ypos][
                            selectedSquare.xpos].piece)
                        self.board.delete(c)
                        self.grid[selectedSquare.ypos][
                            selectedSquare.xpos].piece = None
                        c = self.board.create_image(newx,
                                                    newy,
                                                    image=self.bnPhoto)
                        self.grid[selectedSquare.ypos][
                            selectedSquare.xpos].piece = Knight(
                                self.pieceToMove.color, selectedSquare.xpos,
                                selectedSquare.ypos, self.bnPhoto, c, False)
                        self.blackPieces.append(self.grid[selectedSquare.ypos][
                            selectedSquare.xpos].piece)

                    else:
                        self.whitePieces.remove(self.grid[selectedSquare.ypos][
                            selectedSquare.xpos].piece)
                        self.board.delete(c)
                        self.grid[selectedSquare.ypos][
                            selectedSquare.xpos].piece = None
                        c = self.board.create_image(newx,
                                                    newy,
                                                    image=self.wnPhoto)
                        self.grid[selectedSquare.ypos][
                            selectedSquare.xpos].piece = Knight(
                                self.pieceToMove.color, selectedSquare.xpos,
                                selectedSquare.ypos, self.wnPhoto, c, False)
                        self.whitePieces.append(self.grid[selectedSquare.ypos][
                            selectedSquare.xpos].piece)
                else:
                    if (selectedSquare.ypos == 7):
                        self.blackPieces.remove(self.grid[selectedSquare.ypos][
                            selectedSquare.xpos].piece)
                        self.board.delete(c)
                        self.grid[selectedSquare.ypos][
                            selectedSquare.xpos].piece = None
                        c = self.board.create_image(newx,
                                                    newy,
                                                    image=self.bqPhoto)
                        self.grid[selectedSquare.ypos][
                            selectedSquare.xpos].piece = Queen(
                                self.pieceToMove.color, selectedSquare.xpos,
                                selectedSquare.ypos, self.bqPhoto, c, False)
                        self.blackPieces.append(self.grid[selectedSquare.ypos][
                            selectedSquare.xpos].piece)
                    else:
                        self.whitePieces.remove(self.grid[selectedSquare.ypos][
                            selectedSquare.xpos].piece)
                        self.board.delete(c)
                        self.grid[selectedSquare.ypos][
                            selectedSquare.xpos].piece = None
                        c = self.board.create_image(newx,
                                                    newy,
                                                    image=self.wqPhoto)
                        self.grid[selectedSquare.ypos][
                            selectedSquare.xpos].piece = Queen(
                                self.pieceToMove.color, selectedSquare.xpos,
                                selectedSquare.ypos, self.wqPhoto, c, False)
                        self.whitePieces.append(self.grid[selectedSquare.ypos][
                            selectedSquare.xpos].piece)

            #Checks if someone is in check

            if self.getKing(self.blackPieces).yesCheck(self.grid) or \
                self.getKing(self.whitePieces).yesCheck(self.grid):
                self.isCheck = True
            else:
                self.isCheck = False

            #Reset everything else
            self.selectPiece = True
            self.pieceToMove = None
            self.board.delete(self.selectedRectangle)
            self.selectedRectangle = None
            self.possibleMoves = []

            #Changes the turn to the opposing person's turn
            if (self.turn == "White"):
                self.turn = "Black"
            else:
                self.turn = "White"

            #Checks if we have a draw
            if (self.noMoreMoves() and not self.isCheck):
                tkinter.messagebox.showinfo("Game Over", "Draw!")
                self.master.quit()

            #Checks if we have a win
            if (self.noMoreMoves() and self.isCheck):
                tkinter.messagebox.showinfo("Game Over", "Check Mate!")
                self.master.quit()

        #If we made an illegal move, do nothing
        else:
            self.selectPiece = True
            self.pieceToMove = None
            self.board.delete(self.selectedRectangle)
            self.selectedRectangle = None
Beispiel #11
0
def initializeQueens(gameList):
    gameList[0][4] = Queen.Queen(BLACK)
    gameList[7][4] = Queen.Queen(WHITE)
    def reset(self):

        # Instantiate the chessboard
        self.chessboard = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                           [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                           [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0],
                           [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]

        # Set the board pieces
        # White Main Pieces
        self.chessboard[0][0] = Square(Coordinate("a1"),
                                       Rook(ChessColour.WHITE))
        self.chessboard[1][0] = Square(Coordinate("b1"),
                                       Knight(ChessColour.WHITE))
        self.chessboard[2][0] = Square(Coordinate("c1"),
                                       Bishop(ChessColour.WHITE))
        self.chessboard[3][0] = Square(Coordinate("d1"),
                                       Queen(ChessColour.WHITE))
        self.chessboard[4][0] = Square(Coordinate("e1"),
                                       King(ChessColour.WHITE))
        self.chessboard[5][0] = Square(Coordinate("f1"),
                                       Bishop(ChessColour.WHITE))
        self.chessboard[6][0] = Square(Coordinate("g1"),
                                       Knight(ChessColour.WHITE))
        self.chessboard[7][0] = Square(Coordinate("h1"),
                                       Rook(ChessColour.WHITE))

        # White Pawns
        self.chessboard[0][1] = Square(Coordinate("a2"),
                                       Pawn(ChessColour.WHITE))
        self.chessboard[1][1] = Square(Coordinate("b2"),
                                       Pawn(ChessColour.WHITE))
        self.chessboard[2][1] = Square(Coordinate("c2"),
                                       Pawn(ChessColour.WHITE))
        self.chessboard[3][1] = Square(Coordinate("d2"),
                                       Pawn(ChessColour.WHITE))
        self.chessboard[4][1] = Square(Coordinate("e2"),
                                       Pawn(ChessColour.WHITE))
        self.chessboard[5][1] = Square(Coordinate("f2"),
                                       Pawn(ChessColour.WHITE))
        self.chessboard[6][1] = Square(Coordinate("g2"),
                                       Pawn(ChessColour.WHITE))
        self.chessboard[7][1] = Square(Coordinate("h2"),
                                       Pawn(ChessColour.WHITE))

        # The blank squares
        for j in range(0, 8):
            for i in range(2, 6):
                self.chessboard[j][i] = Square(
                    Coordinate("{}{}".format(chr(j + 97), i + 1)), None)

        # Black Main Pieces
        self.chessboard[0][7] = Square(Coordinate("a8"),
                                       Rook(ChessColour.BLACK))
        self.chessboard[1][7] = Square(Coordinate("b8"),
                                       Knight(ChessColour.BLACK))
        self.chessboard[2][7] = Square(Coordinate("c8"),
                                       Bishop(ChessColour.BLACK))
        self.chessboard[3][7] = Square(Coordinate("d8"),
                                       Queen(ChessColour.BLACK))
        self.chessboard[4][7] = Square(Coordinate("e8"),
                                       King(ChessColour.BLACK))
        self.chessboard[5][7] = Square(Coordinate("f8"),
                                       Bishop(ChessColour.BLACK))
        self.chessboard[6][7] = Square(Coordinate("g8"),
                                       Knight(ChessColour.BLACK))
        self.chessboard[7][7] = Square(Coordinate("h8"),
                                       Rook(ChessColour.BLACK))

        # Black Pawns
        self.chessboard[0][6] = Square(Coordinate("a7"),
                                       Pawn(ChessColour.BLACK))
        self.chessboard[1][6] = Square(Coordinate("b7"),
                                       Pawn(ChessColour.BLACK))
        self.chessboard[2][6] = Square(Coordinate("c7"),
                                       Pawn(ChessColour.BLACK))
        self.chessboard[3][6] = Square(Coordinate("d7"),
                                       Pawn(ChessColour.BLACK))
        self.chessboard[4][6] = Square(Coordinate("e7"),
                                       Pawn(ChessColour.BLACK))
        self.chessboard[5][6] = Square(Coordinate("f7"),
                                       Pawn(ChessColour.BLACK))
        self.chessboard[6][6] = Square(Coordinate("g7"),
                                       Pawn(ChessColour.BLACK))
        self.chessboard[7][6] = Square(Coordinate("h7"),
                                       Pawn(ChessColour.BLACK))
if __name__ == "__main__":

    print("\n\n\n\n\n\n")
    print '\t\t\033[1;41m[WELCOME TO DONKEY KONG]\033[1;m'
    print("\n\n\n\n")
    print "\033[1;42mPlay it on full screen\033[1;m",
    print "\t\t\033[1;42mPress any key to Continue...\033[1;m"
    input = getchar()
    score = 0
    while True:

        draw_layout = Board(30,90)
        draw_layout.create_board();
        coins = Coins(random.randint(6,8))
        coins.create_coins(draw_layout.get_Matrix())
        queen = Queen()
        queen.locate_queen(draw_layout.get_Matrix())
        player = Player(28,2,score)

        donkey = Donkey(4,6)
        donkey.locate_donkey(draw_layout.get_Matrix())
        track_time = 0

        fbList = []

        fb = FireBalls(donkey.getPosition(),draw_layout.get_Matrix())
        fbList.append(fb)

        player.print_player(draw_layout.get_Matrix())

        count = 1