Beispiel #1
0
 def move(self, car, direction):
     """
     Move a car in the given direction.
     :param car: A Car object to be moved.
     :param direction: A Direction object representing desired direction
         to move car.
     :return: True if movement was possible and car was moved, False otherwise.
     """
     color, length, location, orientation = car
     new_location = location[:]
     if orientation == 0:
         if direction == Direction.UP:
             new_location[0] -= 1
         elif direction == Direction.DOWN:
             new_location[0] += 1
         else:
             gh.report(gh.ERROR_DIRECTION)
     if orientation == 1:
         if direction == Direction.LEFT:
             new_location[1] -= 1
         elif direction == Direction.RIGHT:
             new_location[1] += 1
         else:
             gh.report(gh.ERROR_DIRECTION)
     if Board.is_empty(self, new_location):
         location = new_location
     return location
Beispiel #2
0
    def __single_turn(self):
        """
        Note - this function is here to guide you and it is *not mandatory*
        to implement it. The logic defined by this function must be implemented
        but if you wish to do so in another function (or some other functions)
        it is ok.

        The function runs one round of the game :
            1. gh.report board to the screen
            2. Get user's input of: what color car to move, and what direction to
                move it.
            2.a. Check the the input is valid. If not, gh.report an error message and
                return to step 2.
            2. Move car according to user's input. If movement failed (trying
                to move out of board etc.), return to step 2. 
            3. Report to the user the result of current round ()
        """

        # Asking to choose a car and a direction until the user enter valid
        # data
        while True:
            color = input(MSG_CHOOSE_CAR)
            for car in self.__board.cars:
                if car.color == color:
                    direction = gh.get_direction()
                    while not self.__check_direction(car,
                                                     direction) or \
                            direction not in Direction.ALL_DIRECTIONS:
                        direction = gh.get_direction()
                    return self.__board.move(car, direction)
            else:  # If we still return nothing, that's mean that the car
                # doesn't exist, so print a message and asking again
                gh.report(MSG_NOT_A_CAR)
Beispiel #3
0
    def play(self):
        """
        The main driver of the Game. Manages the game until completion.
        :return: None
        """
        gh.report(MSG_WELCOME)

        # Putting the red car according to the position of the exit
        location, orientation = 0, 0
        if self.__board.exit_board[0] == self.__board.size:
            location = (0, self.__board.exit_board[1])
            orientation = Direction.VERTICAL
        elif self.__board.exit_board[1] == 0:
            location = (self.__board.exit_board[0],
                        self.__board.size - RED_CAR_SIZE)
            orientation = Direction.HORIZONTAL
        red_car = Car(RED_CAR_COLOR, RED_CAR_SIZE, location, orientation)
        self.__board.add_car(red_car)

        print(self.__board)  # Printing the board

        added_colors = [RED_CAR_COLOR]  # Creating a list of all cars that's
        # already on screen

        num_cars = gh.get_num_cars()  # Asking how much car the user
        # want to add

        for i in range(num_cars):
            # Create each car as wanted
            new_car = gh.get_car_input(self.__board.size)
            while new_car[0] in added_colors:
                # If we already chose this color, displaying a message and
                # asking again
                gh.report(MSG_CHOSEN)
                new_car = gh.get_car_input(self.__board.size)

            # Adding the new car to added list and adding it to the board
            added_colors.append(new_car[0])
            self.__board.add_car(
                Car(new_car[COLOR_INDEX], new_car[LENGTH_INDEX],
                    new_car[LOCATION_INDEX], new_car[ORIENTATION_INDEX]))
            # Displaying the board
            print(self.__board)

        while self.__check_exit(red_car):  # Running single turn and printing
            # the board until the victory
            self.__single_turn()
            print(self.__board)
        # Display end message
        gh.report_game_over()
Beispiel #4
0
 def move(self, car, direction):
     """
     Move a car in the given direction.
     :param car: A Car object to be moved.
     :param direction: A Direction object representing desired direction
         to move car.
     :return: True if movement was possible and car was moved, False otherwise.
     """
     location = car.location
     # For the given direction, checking that the place that the car
     # will take after moving is on the board and is empty
     if direction == Direction.UP:
         # To move up, we need to check the place at the top of the car
         if self.__is_empty(
             (location[0] - EDGE_SHIFT, location[1])) and self.__is_in(
                 (location[0] - EDGE_SHIFT, location[1])):
             car.location = (location[0] - EDGE_SHIFT, location[1])
             return True
         else:
             gh.report(MSG_NOT_AVAILABLE)
             return False
     elif direction == Direction.DOWN:
         # To move down, we need to check the place at the bottom of the car
         if self.__is_empty(
             (location[0] + car.length, location[1])) and self.__is_in(
                 (location[0] + car.length, location[1])):
             car.location = (location[0] + EDGE_SHIFT, location[1])
             return True
         else:
             gh.report(MSG_NOT_AVAILABLE)
             return False
     elif direction == Direction.LEFT:
         # To move left, we need to check the place at the left of the car
         if self.__is_empty(
             (location[0], location[1] - EDGE_SHIFT)) and self.__is_in(
                 (location[0], location[1] - EDGE_SHIFT)):
             car.location = (location[0], location[1] - EDGE_SHIFT)
             return True
         else:
             gh.report(MSG_NOT_AVAILABLE)
             return False
     elif direction == Direction.RIGHT:
         # To move right, we need to check the place at the right of the car
         if self.__is_empty(
             (location[0], location[1] + car.length)) and self.__is_in(
                 (location[0], location[1] + car.length)):
             car.location = (location[0], location[1] + EDGE_SHIFT)
             return True
         else:
             gh.report(MSG_NOT_AVAILABLE)
             return False
Beispiel #5
0
    def play(self):
        """
        The main driver of the Game. Manages the game until completion.
        :return: None
        """
        gh.report(MSG_WELCOME)  # print welcome message
        self.__add_red_car()  # add the red car
        gh.report(self.__board)  # print the empty board
        chosen_color = [RED_CAR_COLOR]
        num_car = gh.get_num_cars()  # ask how much car play with

        for i in range(num_car):
            car_tuple = gh.get_car_input(self.__board.size)
            while car_tuple[0] in chosen_color:
                gh.report(MSG_CHOSEN)
                car_tuple = gh.get_car_input(self.__board.size)

            chosen_color.append(car_tuple[0])
            car = Car(car_tuple[0], car_tuple[1], car_tuple[2], car_tuple[3])
            self.__board.add_car(car)
            print(self.__board)

        while self.win_game(red_car):
            self.__single_turn()
            print(self.__board)

        gh.report_game_over()
Beispiel #6
0
    def add_car(self, car):
        """
        Add a single car to the board.
        :param car: A car object
        :return: True if a car was successfully added, or False otherwise.
        """

        # Checking if all the locations that the new car want to take are
        # available
        available = True
        for location in car.list_location():
            if not self.__is_empty(location):
                available = False
            if not self.__is_in(location):
                available = False
        if available:
            self.cars.append(car)  # Adding the car
            if car.color != RED_CAR_SYMBOL:  # Do not display the message if
                # it's the red car
                gh.report(MSG_SUCCESS)
        else:
            gh.report(MSG_FAIL)
        return available
Beispiel #7
0
    def __play_one_round(self):
        """
        Note - this function is here to guide you and it is *not mandatory*
        to implement it. The logic defined by this function must be implemented
        but if you wish to do so in another function (or some other functions)
        it is ok.

        Te function runs one round of the game :
            1. Get user coordinate choice for bombing.
            2. Move all game's ships.
            3. Update all ships and bombs.
            4. Report to the user the result of current round (number of hits and
             terminated ships)
        :return:
            (some constant you may want implement which represents) Game status :
            GAME_STATUS_ONGOING if there are still ships on the board or
            GAME_STATUS_ENDED otherwise.
        """
        new_bomb = gh.get_target(self.__board_si



        ze)
        self.__bomb_on_board[new_bomb] = 4
        self.ship_mover()
        self.damaged_ships()
        self.bombs_life_time()
        hits = self.last_hit()
        self.remove_items(hits[0])
        self.not_damaged_pos()
        self.damaged_pos()
        terminations = self.terminated_ships()
        gh.report(gh.board_to_string(self.__board_size, hits[0],
                                     self.__bomb_on_board,
                                     self.__hit_ship_cells,
                                     self.__healthy_ships))
        gh.report_turn(hits[1], terminations)
Beispiel #8
0
 def add_car(self, car):
     """
     Add a single car to the board.
     :param car: A car object
     :return: True if a car was succesfuly added, or False otherwise.
     """
     color, length, location, orientation = car
     x, y = location
     board = self.exit_board
     if orientation == 0:
         if y + length <= len(board) - 1:
             counter = 0
             for i in range(length):
                 if not Board.is_empty(self, (x, y + i)):
                     counter = 0
                 else:
                     counter += 1
             if counter == length:
                 for i in range(length):
                     board[x][y + i] = color
         else:
             gh.report(gh.ERROR_COORDINATE_OUT_OF_BOUND)
     if orientation == 1:
         if x + length <= len(board) - 1:
             counter = 0
             for i in range(length):
                 if not Board.is_empty(self, (x + i, y)):
                     counter = 0
                 else:
                     counter += 1
             if counter == length:
                 for i in range(length):
                     board[x + i][y] = color
         else:
             gh.report(gh.ERROR_COORDINATE_OUT_OF_BOUND)
     return board