コード例 #1
0
def check_player_map(location):
    lose = 0
    sunk = ''
    if " " != playerBoard[boardPosToIndex(location)]:
        playerBoard[boardPosToIndex(location)] = "X"
        for i in range(len(ships)):
            if ships[i].pos[0] % 10 == boardPosToIndex(location) % 10 or int(
                    ships[i].pos[0] / 10) == int(
                        boardPosToIndex(location) / 10):
                for j in range(len(ships[i].pos)):
                    if ships[i].pos[j] == boardPosToIndex(location):
                        ships[i].pos.remove(ships[i].pos[j])
                        if len(ships[i].pos) == 0:
                            sunk = ships[i].name
                            net.send(messages[8].format(ships[i].name))
                            ships.remove(ships[i])
                            if len(ships) == 0:
                                net.send(messages[16])
                                lose = 1
                        else:
                            net.send(messages[15])
                        return messages[6], lose, sunk

        net.send(messages[15])
        return messages[6], lose, sunk
    else:
        playerBoard[boardPosToIndex(location)] = "O"
        net.send(messages[15])
        return messages[7], lose, sunk
コード例 #2
0
def play():
    global turns, start, messages

    if start:
        print(messages[4])
    else:
        print(messages[5])

    while 1:
        if start == 0 and turns == 0:
            turns += 1
            try:
                msg = net.rec(3)
                msg2, lcheck, sunk = check_player_map(msg)
                net.send(msg2)
            except ValueError:
                msg = net.rec(3)
                msg2, lcheck, sunk = check_player_map(msg)
                net.send(msg2)

            print(messages[13].format(msg2, msg))
            printBoard(hitmap)
            printBoard(playerBoard)

        #---------------------------------------------------------

        player_move = input(messages[12])
        while not check_validity(player_move):
            print(messages[20])
            player_move = input(messages[12])
        net.send(player_move)
        msg2 = net.rec(len(messages[8]) + 16).upper()
        msg = net.rec(5)
        #        print(msg,player_move)
        update_hitmap(msg, player_move)

        if msg == messages[16][0:5]:
            print(messages[13].format(messages[6], player_move))
        else:
            print(messages[13].format(msg, player_move))
        if msg2 != messages[15]:
            print(msg2)
        printBoard(hitmap)
        printBoard(playerBoard)
        if msg == messages[16][0:5]:
            print(messages[17])
            return

        #---------------------------------------------------------

        try:
            msg = net.rec(3)
            msg2, lcheck, sunk = check_player_map(msg)
            net.send(msg2)
        except ValueError:
            msg = net.rec(3)
            msg2, lcheck, sunk = check_player_map(msg)
            net.send(msg2)
        print(messages[13].format(msg2, msg))
        if sunk:
            print(messages[19].format(sunk.upper()))
        printBoard(hitmap)
        printBoard(playerBoard)

        if lcheck == 1:
            print(messages[18])
            return
コード例 #3
0
def join_setup():
    global isHost, start
    isHost = 0
    net.set_target(input(messages[3]))
    net.send(join_request)
    start = (int(net.rec(1)) + isHost) % 2
コード例 #4
0
def place_ships():
    global playerBoard, ships, isHost
    if isHost:
        try:
            new_ship("Aircraft Carrier", 5)
            net.send(str(1))
        except ValueError:
            new_ship("Aircraft Carrier", 5)
            net.send(str(1))

        print(messages[9])
        net.rec(1)

        try:
            new_ship("Battleship", 4)
            net.send(str(1))
        except ValueError:
            new_ship("Battleship", 4)
            net.send(str(1))

        print(messages[9])
        net.rec(1)
        try:
            new_ship("Destroyer", 3)
            net.send(str(1))
        except ValueError:
            new_ship("Destroyer", 3)
            net.send(str(1))

        print(messages[9])
        net.rec(1)
        try:
            new_ship("Submarine", 3)
            net.send(str(1))
        except ValueError:
            new_ship("Submarine", 3)
            net.send(str(1))

        print(messages[9])
        net.rec(1)
        try:
            new_ship("Patrol Boat", 2)
            net.send(str(1))
        except ValueError:
            new_ship("Patrol Boat", 2)
            net.send(str(1))

        print(messages[9])
        net.rec(1)

    else:
        print(messages[9])
        net.rec(1)
        try:
            new_ship("Aircraft Carrier", 5)
            net.send(str(1))
        except ValueError:
            new_ship("Aircraft Carrier", 5)
            net.send(str(1))

        print(messages[9])
        net.rec(1)
        try:
            new_ship("Battleship", 4)
            net.send(str(1))
        except ValueError:
            new_ship("Battleship", 4)
            net.send(str(1))

        print(messages[9])
        net.rec(1)
        try:
            new_ship("Destroyer", 3)
            net.send(str(1))
        except ValueError:
            new_ship("Destroyer", 3)
            net.send(str(1))

        print(messages[9])
        net.rec(1)
        try:
            new_ship("Submarine", 3)
            net.send(str(1))
        except ValueError:
            new_ship("Submarine", 3)
            net.send(str(1))

        print(messages[9])
        net.rec(1)
        try:
            new_ship("Patrol Boat", 2)
            net.send(str(1))
        except ValueError:
            new_ship("Patrol Boat", 2)
            net.send(str(1))
コード例 #5
0
def final_setup():
    global start, isHost
    start = random.randint(0, 1)
    net.send(str(start))
    print(net.reciever_ip)
    start = (start + isHost) % 2