Beispiel #1
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 #2
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()
    def play(self):
        """
        The main driver of the Game. Manages the game until completion.
        :return: None
        """
        print(Game.MESSAGE_HELLO)
        start, orientation, _ = \
            self.__board. \
                get_red_car_location_orientation_and_deviation(Game.LENGTH_OF_RED_CAR)

        red_car = Car(Game.COLOR_OF_RED_CAR, Game.LENGTH_OF_RED_CAR, start,
                      orientation)

        self.__board.add_car(red_car)

        print(self.__board)

        num_of_cars_to_add = gh.get_num_cars()  # assumes input is valid

        for _ in range(num_of_cars_to_add):
            car_data = gh.get_car_input(self.__board.get_size())
            new_car = Car(*car_data)
            self.__board.add_car(new_car)
            self.update_valid_colors()
            print(self.__board)

        while \
                self.__board.min_distance_between_car_exit(Game.COLOR_OF_RED_CAR) > 1:
            # while the red car has still not reached the exit
            self.single_turn()

        gh.report_game_over()
Beispiel #4
0
 def final_board(self):
     x = self.get_num_cars()
     board = self.exit_board
     car = [[] for k in range(x)]
     for i in range(x):
         car[i] = gh.get_car_input(self.size)
         board = self.add_car(car[i])
     return board
Beispiel #5
0
 def __init__(self, board):
     """
     Initialize a new Game object.
     :param board: An object of type board
     """
     self.board = board
     for i in range(gh.get_num_cars()):
         self.board.add_car(Car(*gh.get_car_input(board.size)))
Beispiel #6
0
    def play(self):
        """
        The main driver of the Game. Manages the game until completion.
        :return: None
        """
        # implement your code here (and then delete the next line - 'pass')

        system('cls')
        print(STARTING_MSG)
        x, y = self.board.exit_board
        # put the red car randomly according to the exit location
        if x == (board.size - 1):
            orientation_red = 0
            # length is the length of the red car + 1
            length = 3
            location_r = (randint(0, board.size - length), y)
            red_car = Car('R', length-1, location_r, orientation_red)
            self.board.add_car(red_car)
        elif y == (self.board.size - 1):
            orientation_red = 1
            # length is the length of the red car + 1
            length = 3
            location_r = (x, randint(0, board.size - length))
            red_car = Car('R', length-1, location_r, orientation_red)
            self.board.add_car(red_car)
        # print the board with the red car on it
        print(self.board)
        """asking the user for the number of car that he wants to add to the 
        board"""
        num_of_cars_to_put_on_board = gh.get_num_cars()
        #  than add the the cars to the board according to the user input
        for i in range(0, num_of_cars_to_put_on_board):
            color, len_of_car, location_on_board, orientation = \
                gh.get_car_input(self.board.size)
            new_car = Car(color, len_of_car, location_on_board, orientation)
            board.add_car(new_car)
        red_car_exit = True
        # keep running the game until the red car exit the board
        while red_car_exit is True:
            self.__single_turn()
            x_r, y_r = red_car.get_location()
            x_e, y_e = self.board.exit_board
            """according the the red car orientation and length check if the
            red car succeed to exit the board - got to the exit coordination"""
            if red_car.orientation == 0:
                for i in range(0, red_car.length):
                    if x_r+i == x_e and y_r == y_e:
                        red_car_exit = False
            if red_car.orientation == 1:
                for j in range(0, red_car.length):
                    if x_r == x_e and y_r+j == y_e:
                        red_car_exit = False
        # print a message the the red car got out of the board
        gh.report_game_over()
 def place_cars_on_board(self):
     """
     This method maps the car on the board in accordance to input
     parameters that will be given inside the method
     :return: False if car with given parameters can't be added
     True if car can be added
     """
     color, length, location, orientation = gh.get_car_input(self.__size)
     new_car = Car(color, length, location, orientation)
     self.__cars[color] = new_car
     if not self.add_car(new_car):
         return False
     else:
         self.__valid_colors.remove(color)  # extract the chosen color
         return True
Beispiel #8
0
    def play(self):
        """
        The main driver of the Game. Manages the game until completion.
        :return: None
        """
        # implement your code here (and then delete the next line - 'pass')

        system('cls')
        print(STARTING_MSG)
        x, y = self.board.exit_board
        if x == (board.size - 1):
            orientation_red = 0
            location_r = (randint(0, board.size - 3), y)
            red_car = Car('R', 2, location_r, orientation_red)
            self.board.add_car(red_car)
        elif y == (self.board.size - 1):
            orientation_red = 1
            location_r = (x, randint(0, board.size - 3))
            red_car = Car('R', 2, location_r, orientation_red)
            self.board.add_car(red_car)
        print(self.board)
        num_of_cars_to_put_on_board = gh.get_num_cars()
        for i in range(0, num_of_cars_to_put_on_board):
            color, len_of_car, location_on_board, orientation = \
                gh.get_car_input(self.board.size)
            new_car = Car(color, len_of_car, location_on_board, orientation)
            board.add_car(new_car)
        red_car_exit = True
        while red_car_exit is True:
            self.__single_turn()
            x_r, y_r = red_car.get_location()
            x_e, y_e = self.board.exit_board
            if red_car.orientation == 0:
                for i in range(0, red_car.length):
                    if x_r + i == x_e and y_r == y_e:
                        red_car_exit = False
            if red_car.orientation == 1:
                for j in range(0, red_car.length):
                    if x_r == x_e and y_r + j == y_e:
                        red_car_exit = False
        gh.report_game_over()