コード例 #1
0

# Lists for the map, and tracking shots
x_axis = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I']
y_axis = ['1', '2', '3', '4', '5', '6', '7', '8', '9']


# prints the map, with the letter/number label for rows/columns
def print_map():
    print ' ',  ' '.join(y_axis)
    print_count = 0
    for i in map_grid:
        print x_axis[print_count], ' '.join(i)
        print_count += 1

fleet_builder.build_fleet()


enemy_carrier = fleet_builder.enemy_fleet[0]
enemy_battleship = fleet_builder.enemy_fleet[1]
enemy_destroyer = fleet_builder.enemy_fleet[2]
enemy_submarine = fleet_builder.enemy_fleet[3]
enemy_patrol = fleet_builder.enemy_fleet[4]

enemy_fleet = [
    enemy_carrier,
    enemy_battleship,
    enemy_destroyer,
    enemy_submarine,
    enemy_patrol]
コード例 #2
0
# Lists for the map, and tracking shots
x_axis = ["A", "B", "C", "D", "E", "F", "G", "H", "I"]
y_axis = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]


# prints the map, with the letter/number label for rows/columns
def print_map():
    print " ", " ".join(y_axis)
    print_count = 0
    for i in map_grid:
        print x_axis[print_count], " ".join(i)
        print_count += 1


enemy_fleet = fleet_builder.build_fleet()


# Debug Purposes
def show_enemies():
    for ship in enemy_fleet:
        for spot in ship:
            map_grid[int(spot[0])][int(spot[1])] = "#"


# When the shot happens, this decides what the computer does
def shoot(x, y):

    global pass_turn

    if map_grid[x][y] in ["O", "X"]:
コード例 #3
0
def restart():
    while True:

        import fleet_builder
        fleet_builder.build_fleet()

        # This builds a 9x9 grid
        make_grid = []
        map_grid = []
        for i in range(9):
            for i in range(9):
                make_grid.append('-')
            map_grid.append(make_grid)
            make_grid = []

        # Lists for the map, and tracking shots
        x_axis = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I']
        y_axis = ['1', '2', '3', '4', '5', '6', '7', '8', '9']

        info = ['Game Start']
        def print_info():
            for i in info:
                print "-" + i

        def end_game():
            if len(fleet_builder.enemy_fleet) == 0:
                print "You won! Good game. Would you like to play again? (Y/N)"
                again = raw_input('> ').lower()
            else:
                print "You lost! Good game. Would you like to play again? (Y/N)"
                again = raw_input('> ').lower()
            while True:
                if again[0] == 'y' or again[0] == 'n':
                    if again[0] == 'y':
                        print "Restarting..."
                        restart()

                        break
                    if again[0] == 'n':
                        exit(1)
                        break
                else:
                    print "I didn't catch that. Yes or No?"
                    again = raw_input('> ').lower()

        # prints the map, with the letter/number label for rows/columns
        def print_map():
            print '\n'
            print ' ',  ' '.join(y_axis)
            print_count = 0
            for i in map_grid:
                print x_axis[print_count], ' '.join(i)
                print_count += 1

        # Debug Purposes
        def show_enemies():
            for ship in fleet_builder.enemy_fleet:
                for spots in ship.lay:
                    map_grid[int(spots[0])][int(spots[1])] = '#'

        def sink_checker():
            for ship in fleet_builder.enemy_fleet:
                if len(ship.lay) == 0:
                    info.append(str("You sunk their " + ship.name + '!'))
                    fleet_builder.enemy_fleet.remove(ship)

        def fleet_status():
            enemy_fleet_live = []
            enemy_fleet_status = []

        # When the shot happens, this decides what the computer does
        def shoot(x, y):
            global pass_turn

            if map_grid[x][y] in ['O', 'X']:
                print "You've already shot there, ya dingus."
                pass_turn = False
                return

            for ship in fleet_builder.enemy_fleet:
                for spots in ship.lay:
                    if spots == [x, y]:
                        map_grid[x][y] = 'X'
                        ship.lay.remove(spots)
                        info.append(str("You hit their " + ship.name + '!'))
                        pass_turn = True
                        return

            else:
                map_grid[x][y] = 'O'
                info.append(str("You missed."))
                pass_turn = True
                return

        def your_turn():

            global pass_turn
            pass_turn = False

            # Checks to see if input is legal, then sets it to be read if it's only a little off.
            while not pass_turn:
                shot = raw_input("> ").upper()
                shot = list(shot)
                shot.sort()
                shot.reverse()
                if len(shot) == 2 \
                    and shot[0] in x_axis \
                        and shot[1] in y_axis:
                            # # Debug Purposes
                            # print x_axis.index(shot[0]), y_axis.index(shot[1])
                            shoot(x_axis.index(shot[0]), y_axis.index(shot[1]))
                else:
                    print "Try again"
            sink_checker()
            if len(fleet_builder.enemy_fleet) == 0:
                print_map()
                print_info()
                end_game()
            info.append("Next Turn")

        # Here starts the game

        # Debugging Purposes
        show_enemies()

        while True:

            print_map()
            print_info()
            info = []
            your_turn()