Exemplo n.º 1
0
def main():
    game_server = socket.socket(socket.AF_INET,
                                socket.SOCK_STREAM)  # Create a socket object
    if const.GAME_SERVER_ADDR == '':
        host = socket.gethostname()  # Get local machine name
        game_server.connect((host, const.GAME_SERVER_PORT))
    else:
        game_server.connect((const.GAME_SERVER_ADDR, const.GAME_SERVER_PORT))

    status = game_server.recv(
        1024)  # used for client/server synchronisation purpose
    game_server.send(
        const.ACKNOWLEDGED)  # used for client/server synchronisation purpose
    print status

    if status == "connected":
        while True:
            request = game_server.recv(1024)
            print "dummy side rqt:", request, "|"

            regex_match = re.match("\((.+), (.+)\)", request)
            if not regex_match:
                # Request from server is malformed
                print("[ERROR] Could not parse request from server: " +
                      request)
                game_server.send(const.ACKNOWLEDGED)
                break
            else:
                command = regex_match.group(1).strip("'")
                data = regex_match.group(2).strip("'")

                print("Received command: " + command)
                print("Received data: " + data)

            if command == "getName":
                player.myPlayerNumber = int(data)
                print "I am player " + str(player.myPlayerNumber)
                game_server.send(str(player.playerName))

            elif command == "chooseMove":
                game_server.send(str(player.chooseMove(data)))

            elif command == "newGame":
                player.newGame()
                game_server.send(const.ACKNOWLEDGED)

            elif command == "gameOver":
                print "I won!" if data == "Win" else "I lost!"
                break

            else:
                # unknown commmand so must be either end of game or a fatal error. Exit the game loop
                print("[ERROR] Unknown command received in request: " +
                      request)
                game_server.send(const.ACKNOWLEDGED)
                break

    game_server.close  # Close the socket when done
Exemplo n.º 2
0
def main():
    game_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Create a socket object
    if const.GAME_SERVER_ADDR == '':        
        host = socket.gethostname() # Get local machine name
	print host
        game_server.connect((host, const.GAME_SERVER_PORT))
    else:
        game_server.connect(('127.0.0.1', const.GAME_SERVER_PORT))
        
    status = game_server.recv(1024)                     # used for client/server synchronisation purpose
    game_server.send(const.ACKNOWLEDGED)    # used for client/server synchronisation purpose    
    print status
    
    if status == "connected":
        while True:
            request = game_server.recv(1024)
            print "dummy side rqt:", request, "|"
            request = eval(request)
            
            if request[0] == "deployFleet":
                print 'deploying fleet'
                player_board = player.deployFleet()
                game_server.send(str(player_board))

            elif request[0] == "getName":
                game_server.send(str(player.playerName))
                
            elif request[0] == "newPlayer":
                player.newPlayer()
                game_server.send(const.ACKNOWLEDGED)    # used for client/server synchronisation purpose
                
            elif request[0] == "newRound":
                player.newRound()
                game_server.send(const.ACKNOWLEDGED)    # used for client/server synchronisation purpose
                
            elif request[0] == "getDescription":
                game_server.send(str(player.playerDescription))
                
            elif request[0] == "chooseMove":
                game_server.send(str(player.chooseMove()))
                
            elif request[0] == "setOutcome":
                player.setOutcome(request[1], request[2], request[3])
                game_server.send(const.ACKNOWLEDGED)    # used for client/server synchronisation purpose
                
            elif request[0] == "getOpponentMove":
                player.getOpponentMove(request[1], request[2])                    
                game_server.send(const.ACKNOWLEDGED)    # used for client/server synchronisation purpose
                
            else:
                # unknown request so must be either end of game or a fatal error. Exit the game loop
                print request
                game_server.send(const.ACKNOWLEDGED)    # used for client/server synchronisation purpose
                break
            

    game_server.close # Close the socket when done
Exemplo n.º 3
0
def main():
    game_server = socket.socket(socket.AF_INET,
                                socket.SOCK_STREAM)  # Create a socket object
    if const.GAME_SERVER_ADDR == '':
        host = socket.gethostname()  # Get local machine name
        game_server.connect((host, const.GAME_SERVER_PORT))
    else:
        game_server.connect((const.GAME_SERVER_ADDR, const.GAME_SERVER_PORT))

    status = game_server.recv(
        1024)  # used for client/server synchronisation purpose
    game_server.send(
        const.ACKNOWLEDGED)  # used for client/server synchronisation purpose
    print status

    if status == "connected":
        while True:
            request = game_server.recv(1024)
            print "dummy side rqt:", request, "|"
            request = eval(request)

            if request[0] == "getName":
                player.myPlayerNumber = int(request[1])
                print "I am player " + str(player.myPlayerNumber)
                game_server.send(str(player.playerName))

            elif request[0] == "chooseMove":
                game_server.send(str(player.chooseMove(request[1])))

            elif request[0] == "newGame":
                player.newGame()
                game_server.send(const.ACKNOWLEDGED)

            elif request[0] == "gameOver":
                print "I won!" if request[1] == "Win" else "I lost!"
                break

            else:
                # unknown request so must be either end of game or a fatal error. Exit the game loop
                print request
                game_server.send(
                    const.ACKNOWLEDGED
                )  # used for client/server synchronisation purpose
                break

    game_server.close  # Close the socket when done
def main():
    game_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Create a socket object
    if const.GAME_SERVER_ADDR == '':        
        host = socket.gethostname() # Get local machine name
        game_server.connect((host, const.GAME_SERVER_PORT))
    else:
        game_server.connect((const.GAME_SERVER_ADDR, const.GAME_SERVER_PORT))
        
    status = game_server.recv(1024)                     # used for client/server synchronisation purpose
    game_server.send(const.ACKNOWLEDGED)    # used for client/server synchronisation purpose    
    print status
    
    if status == "connected":
        while True:
            request = game_server.recv(1024)
            print "dummy side rqt:", request, "|"
            request = eval(request)
            
            if request[0] == "getName":
                player.myPlayerNumber = int(request[1])
                print "I am player "+str(player.myPlayerNumber)
                game_server.send(str(player.playerName))

            elif request[0] == "chooseMove":
                game_server.send(str(player.chooseMove(request[1])))
            
            elif request[0] == "newGame":
                player.newGame()
                game_server.send(const.ACKNOWLEDGED)
            
            elif request[0] == "gameOver":
                print "I won!" if request[1]=="Win" else "I lost!"
                break
                    
            else:
                # unknown request so must be either end of game or a fatal error. Exit the game loop
                print request
                game_server.send(const.ACKNOWLEDGED)    # used for client/server synchronisation purpose
                break
            

    game_server.close # Close the socket when done
Exemplo n.º 5
0
def main(gui):
    game_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Create a socket object
    if const.GAME_SERVER_ADDR == '':        
        host = socket.gethostname() # Get local machine name
	print host
        game_server.connect((host, const.GAME_SERVER_PORT))
    else:
        game_server.connect((const.GAME_SERVER_ADDR, const.GAME_SERVER_PORT))
        
    status = game_server.recv(1024)
    game_server.send(const.ACKNOWLEDGED)    # used for client/server synchronisation purpose
    print status
    
    if status == "connected":
        while True:
            request = game_server.recv(1024)
            print "template rqt:", request, "|"
            request = eval(request)
            
            if request[0] == "deployFleet":
                player_board = player.deployFleet()
                for row in range(len(player_board)):
                    for col in range(len(player_board[row])):
                        if player_board[row][col] == const.OCCUPIED:
                            gui.drawBoat('left', row, col)
                game_server.send(str(player_board))

            elif request[0] == "getName":
                game_server.send(str(player.playerName))
                
            elif request[0] == "newPlayer":
                player.newPlayer()
                game_server.send(const.ACKNOWLEDGED)    # used for client/server synchronisation purpose
                
            elif request[0] == "newRound":
                gui.turtle.clear()
                gui.drawBoards()
                gui.drawPlayer(player.playerName, '', 'left')
                gui.drawPlayer('Opponent', '', 'right')
                player.newRound()
                game_server.send(const.ACKNOWLEDGED)    # used for client/server synchronisation purpose
                
            elif request[0] == "getDescription":
                game_server.send(str(player.playerDescription))
                
            elif request[0] == "chooseMove":
                game_server.send(str(player.chooseMove()))
                
            elif request[0] == "setOutcome":
                player.setOutcome(request[1], request[2], request[3])
                if request[1] == const.HIT:
                    gui.drawHit('right', request[2], request[3])
                else:
                    gui.drawMiss('right', request[2], request[3])
                game_server.send(const.ACKNOWLEDGED)    # used for client/server synchronisation purpose
                
            elif request[0] == "getOpponentMove":
                result = player.getOpponentMove(request[1], request[2])
                if result == const.HIT:
                    gui.drawHit('left', request[1], request[2])
                else:
                    gui.drawMiss('left', request[1], request[2])
                                        
                game_server.send(const.ACKNOWLEDGED)    # used for client/server synchronisation purpose
                
            else:
                # unknown request so must be either end of game or a fatal error. Exit the game loop
                print request
                game_server.send(const.ACKNOWLEDGED)    # used for client/server synchronisation purpose
                break
            

    game_server.close # Close the socket when done