コード例 #1
0
    def test_HitAi_RandomPartOfPlaneHit_CellTypeChangedToX(self):
        player_table = PlayTable()
        AI_table = PlayTable()
        service = ServiceGame(player_table,AI_table)
        service.InitializeTable("ui", "white", "AI")
        service.InitializeTable("ui","grey","NAI")
        service.AddAiPlane(3, 0, "ui")
        service.HitAi(1, 1)
        
        correct_table = []
        for i in range(8):
            row = []
            for j in range(8):
                if ((i == 2 and j == 1) or (i == 2 and j == 3)) or (i == 3 and ( j == 1 or j == 2 or j == 3)) or ((i == 4 and j == 1) or (i == 4 and j == 3)) or (i == 5 and j == 1):
                    cell = ButtonForRepo("1",i,j,"blue","AI")
                elif i == 1 and j == 1:
                    cell = ButtonForRepo("X",i,j,"X","AI")
                elif i == 3 and j == 0:
                    cell = ButtonForRepo("1",i,j,"pink","AI")
                else:
                    cell = ButtonForRepo("0",i,j,"white","AI")
                row.append(cell)
            correct_table.append(row)

        table = service.GetAiTable()
        self.assertEqual(table, correct_table)
コード例 #2
0
ファイル: Services.py プロジェクト: CostinOnciu/Planes
    def AddPlayerPlane(self, row, column, Type, color, player, frame=None):
        '''
        
        :param row:
        :param column:
                these 2 make the position that the player choses for the cockpit of one of it's 2 planes 
        :param Type: UI or GUI because they are different types of entities
        :param color: the color of the table (it s different for the player and the AI)
        :param player: AI or NAI meaning that it is AI or not
        :param frame: if the type of the table is GUI then it needs a frame on what tkinter it's going to place all the buttons
            
            the method will make the positions corresponding to the plane based on the position of the cockpit to be part of the plane
        '''
        row_neighbours = [0, -2, -1, 0, 1, 2, 0, -1, 0, 1]
        column_neighbours = [0, 1, 1, 1, 1, 1, 2, 3, 3, 3]

        position = [row, column]
        if position not in self.__choice:
            return

        if Type == "ui":
            for i in range(len(row_neighbours)):
                if self.__player_table.get_entity(
                        row + row_neighbours[i],
                        column + column_neighbours[i]).get_Type() != "0":
                    return
            for i in range(len(row_neighbours)):
                if i == 0:
                    cell = ButtonForRepo("1", row + row_neighbours[i],
                                         column + column_neighbours[i], "pink",
                                         player)
                else:
                    cell = ButtonForRepo("1", row + row_neighbours[i],
                                         column + column_neighbours[i], "blue",
                                         player)
                self.__player_table.AddSingleEntity(cell)
        else:
            for i in range(len(row_neighbours)):
                if self.__player_table.get_entity(
                        row + row_neighbours[i], column +
                        column_neighbours[i]).get_Type()["bg"] == "blue":
                    return
            for i in range(len(row_neighbours)):
                button = Button(frame, bg="blue", text="", fg="blue")
                cell = ButtonForRepo(button, row + row_neighbours[i],
                                     column + column_neighbours[i], "blue",
                                     player)
                if i != 0:
                    event = partial(self.PlayerPlaneHited, [cell, frame])
                else:
                    event = partial(self.PlayerPlaneDown, [cell, frame])
                button.bind("<Button-2>", event)
                self.__player_table.AddSingleEntity(cell, frame)
コード例 #3
0
ファイル: Services.py プロジェクト: CostinOnciu/Planes
    def InitializeTable(self,
                        Type,
                        color,
                        player,
                        frame=None,
                        StartPosition=None):
        '''
        
        :param Type: UI or GUI because they are different types of entities
        :param color: the color of the table (it s different for the player and the AI)
        :param player: AI or NAI meaning that it is AI or not
        :param frame: if the type of the table is GUI then it needs a frame on what tkinter it's going to place all the buttons
        :param StartPosition: if the type of the table is GUI then the position of the Buttons will differ based on what table they belong
        
        :return ---
            the tables will be initialized with entities that represent that every cell is not a plane
        '''
        for i in range(8):
            for j in range(8):
                if Type == "ui":
                    cell = ButtonForRepo("0", i, j, color, player)
                else:
                    button = Button(frame, bg=color, text="", fg=color)
                    button.grid(row=i, column=j + StartPosition)
                    button.config(height=3, width=6)
                    cell = ButtonForRepo(button, i, j, color, player)

                    if player == "NAI":
                        event = partial(self.GuiAddPlayerPlane,
                                        [i, j, "gui", color, player, frame])
                        button.bind("<Button-1>", event)

                        event = partial(self.NotPlane, [cell, frame])
                        button.bind("<Button-2>", event)
                    else:
                        event = partial(self.HitNotPlane, [cell, frame])
                        button.bind("<Button-1>", event)

                if player == "NAI":
                    self.__player_table.Initialize(cell)
                else:
                    self.__ai_table.Initialize(cell)
コード例 #4
0
 def test_AddAiPlane_ValidInput_PlaneAddedOnTheTable(self):
     player_table = PlayTable()
     AI_table = PlayTable()
     service = ServiceGame(player_table,AI_table)
     service.InitializeTable("ui", "white", "AI")
     service.AddAiPlane(3, 0, "ui")
     correct_table = []
     for i in range(8):
         row = []
         for j in range(8):
             if (i == 1 and j == 1) or ((i == 2 and j == 1) or (i == 2 and j == 3)) or (i == 3 and ( j == 1 or j == 2 or j == 3)) or ((i == 4 and j == 1) or (i == 4 and j == 3)) or (i == 5 and j == 1):
                 cell = ButtonForRepo("1",i,j,"blue","AI")
             elif i == 3 and j == 0:
                 cell = ButtonForRepo("1",i,j,"pink","AI")
             else:
                 cell = ButtonForRepo("0",i,j,"white","AI")
             row.append(cell)
         correct_table.append(row)
     table = service.GetAiTable()
     self.assertEqual(table, correct_table)
コード例 #5
0
 def test_InitializeTable_ValidInput_TableInitilizedCorectly(self):
     player_table = PlayTable()
     AI_table = PlayTable()
     service = ServiceGame(player_table,AI_table)
     service.InitializeTable("ui", "grey", "NAI")
     table = service.GetPlayerTable()
     correct_table = []
     for i in range(8):
         row = []
         for j in range(8):
             cell = ButtonForRepo("0",i,j,"grey","NAI")
             row.append(cell)
         correct_table.append(row)
     self.assertEqual(table, correct_table)
コード例 #6
0
ファイル: Services.py プロジェクト: CostinOnciu/Planes
    def HitNotPlane(self, *args):
        '''
        :args the position of the cell
            this is for the AI table and just for GUI 
            it's the event that occurs when a button that is not part from a plane is clicked
        '''
        row = args[0][0].get_row()
        column = args[0][0].get_column()

        button = Button(args[0][1], bg="green", text="", fg="white")
        button.grid(row=row, column=column + 30)
        button.config(height=3, width=6)
        cell = ButtonForRepo(button, row, column, "green", "AI")

        self.__ai_table.AddSingleEntity(cell, args[0][1], 30)
        self.HitPlayer("gui")
コード例 #7
0
ファイル: Services.py プロジェクト: CostinOnciu/Planes
    def HitCockpit(self, *args):
        '''
        :args the position of the cell
            this is for the AI table and just for GUI 
            it's the event that occurs when a button that is a cockpit of a plane is clicked
        '''
        row = args[0][0].get_row()
        column = args[0][0].get_column()
        self.__thinking_choice = []
        self.__ai_planes_down += 1

        row_neighbours = [0, -2, -1, 0, 1, 2, 0, -1, 0, 1]
        column_neighbours = [0, 1, 1, 1, 1, 1, 2, 3, 3, 3]

        for i in range(len(row_neighbours)):
            button = Button(args[0][1], bg="red", text="-", fg="white")
            button.grid(row=row + row_neighbours[i],
                        column=column + column_neighbours[i] + 30)
            button.config(height=3, width=6)
            cell = ButtonForRepo(button, row + row_neighbours[i],
                                 column + column_neighbours[i], "red", "AI")

        self.__ai_table.AddSingleEntity(cell, args[0][1], 30)

        if self.__ai_planes_down == 2:
            button = Button(args[0][1],
                            bg="yellow",
                            text="You won!",
                            fg="black")
            button.grid(row=3, column=7)
            button.config(height=3, width=6)

            args[0][1].after(1000, self.close, args[0][1])
            #print("You won")
            #return

        self.HitPlayer("gui")
コード例 #8
0
ファイル: Services.py プロジェクト: CostinOnciu/Planes
    def AddAiPlane(self, row, column, Type, frame=None):
        '''        
        :param row: 
        :param column:
                    these 2 represent the random position where the AI plane will have it's cockpit  
        :param Type: UI or GUI because they are different types of entities
        :param frame: if the type of the table is GUI then it needs a frame on what tkinter it's going to place all the buttons
        
        :return ----
                it will add on the corresponding positions of cells that will be part of the plane that has the cockpit 
                on the position given entities that will say that the positions are part of a plane 
        '''
        row_neighbours = [0, -2, -1, 0, 1, 2, 0, -1, 0, 1]
        column_neighbours = [0, 1, 1, 1, 1, 1, 2, 3, 3, 3]

        to_remove = []

        number_of_choises = len(self.__choice)
        index1 = 0
        while index1 < number_of_choises:
            index2 = 0
            ok = False
            while index2 < 10 and ok != True:
                index3 = 0
                while index3 < 10 and ok != True:
                    if (self.__choice[index1][0] + row_neighbours[index2]) == (
                            row + row_neighbours[index3]):
                        if (self.__choice[index1][1] +
                                column_neighbours[index2]) == (
                                    column + column_neighbours[index3]):
                            to_remove.append(self.__choice[index1])
                            ok = True
                    index3 += 1
                index2 += 1
            index1 += 1

        for i in to_remove:
            self.__choice.remove(i)
        row_copy = copy.deepcopy(row)
        column_copy = copy.deepcopy(column)

        if Type == "ui":
            for i in range(len(row_neighbours)):
                if i != 0:
                    cell = ButtonForRepo("0", row + row_neighbours[i],
                                         column + column_neighbours[i], "blue",
                                         "AI")
                else:
                    cell = ButtonForRepo("0", row + row_neighbours[i],
                                         column + column_neighbours[i], "pink",
                                         "AI")

                self.__ai_table.AddSingleEntity(cell)
        else:
            for i in range(len(row_neighbours)):
                button = Button(frame, bg="white", text="-", fg="white")
                button.grid(row=row_copy + row_neighbours[i],
                            column=column_copy + column_neighbours[i] + 30)
                button.config(height=3, width=6)

                cell = ButtonForRepo(button, row_copy + row_neighbours[i],
                                     column_copy + column_neighbours[i],
                                     "blue", "AI")
                #cell.hit_plane()
                if i != 0:
                    event = partial(self.HitPlane, [cell, frame])
                else:
                    event = partial(self.HitCockpit, [cell, frame])
                #button.unbind("<Button-1>")
                button.bind("<Button-1>", event)