Ejemplo n.º 1
0
    def get_target(self, next_player):
        x = y = -1
        while True:
            # Grab the input from the player and separate the words into an array.
            arg_in = input("Enter a target [Column] [Row] (Eg: D 7) >> ").split()
            if len(arg_in) == 2:
                # Check first argument validity.
                try:
                    x = board.col_lookup(arg_in[0])
                except ValueError:
                    print("Please enter a valid column!")
                    continue

                # Check second argument validity.
                try:
                    y = int(arg_in[1])
                    if not 0 <= y <= 9:
                        raise ValueError
                except ValueError:
                    print("Please enter a valid row!")
                    continue

                # Check if this position has been shot before.
                if self.tracking_board.coord[x][y] != 0:
                    print("You have shot this spot before!")
                else:
                    break
            else:
                print("Please use the format '[Column] [Row]'!")

        # Check if the shot results in a hit or not.
        for s in next_player.ships:
            if s.intercept(x, y):
                # We got a hit with ship s.
                print("Hit!")
                # Mark it on the tracking board.
                self.tracking_board.coord[x][y] = \
                    next_player.own_board.coord[x][y] = 2
                # Add damage to the ship.
                if s.add_damage():
                    # If the ship got sunk, mark it's perimeter.
                    self.mark_perimeter(s)
                return
        # If we fall through to here, we got a miss.
        print("Miss!")
        # Mark the miss on the tracking board.
        self.tracking_board.coord[x][y] = \
            next_player.own_board.coord[x][y] = 3
Ejemplo n.º 2
0
    def place_ships(self):
        print("================================================")
        print("=================== Player", self.player_id, "===================")
        print("================================================")

        print("\nShip placement:")

        for s in self.ships:
            x = y = o = -1
            while True:
                print("")
                print(" |A|B|C|D|E|F|G|H|I|J|")
                for i in range(10):
                    print(i, "|", sep="", end="")
                    for j in range(10):
                        placeholder = " "
                        if self.own_board.coord[j][i] == 1:
                            placeholder = "0"
                        print(placeholder, "|", sep="", end="")
                    print("")
                print("")

                print("Where do you want to place your ", s.name, "?", sep="")
                raw_in = input("Example: A 8 horizontal  >> ")
                arg_in = raw_in.split()
                if len(arg_in) == 3:
                    # Check first argument validity.
                    try:
                        x = board.col_lookup(arg_in[0])
                    except ValueError:
                        print("Please enter a valid column!")
                        continue

                    # Check second argument validity.
                    try:
                        y = int(arg_in[1])
                        if not 0 <= y <= 9:
                            raise ValueError
                    except ValueError:
                        print("Please enter a valid row!")
                        continue

                    # Check third argument validity.
                    if arg_in[2] == "horizontal":
                        o = 0
                    elif arg_in[2] == "vertical":
                        o = 1
                    else:
                        print("Please enter 'horizontal' or 'vertical' as an orientation!")
                        continue

                    # Check if the chosen placement is valid.
                    if self.check_placement(s, x, y, o):
                        break
                    else:
                        print("Invalid placement!")
                else:
                    print("Please use the format '[Column] [Row] [Orientation]'!")

            # Finally place the ship.
            s.place(x, y, o)
            # Mark the corresponding board tiles as occupied.
            for i in range(s.size):
                if o == 0:
                    # Ship horizontal.
                    self.own_board.coord[x + i][y] = 1
                elif o == 1:
                    # Ship vertical.
                    self.own_board.coord[x][y + i] = 1
                else:
                    print("Fatal error: 0x0001",
                          file=sys.stderr)
                    exit()

        print("\nPlacement completed.\n")