Results 1 to 10 of 10
  1. #1
    T-Prime is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default Networked Card Game.

    I've just finished writing all the game logic for an electronic version of a game a friend of mine and I used to play. The game logic is 100% complete and it does exactly what I want. The problem I have is that I have no idea where to start with implementing the networking aspect of the program. I've done a bunch of reading about sockets and client-server setups, but I can't wrap my head around how to make that do what I want. The specific problem I have is how to synchronize the GUI. I have a bunch of JToggleButtons that I used to build the basic playing field. There are 15 flippable cards, what I need to do is make sure that when 1 player flips a card on his screen, that same card flips on the other players screen. That's the main issue. I'm hoping figuring that out will help me in how to send and implement the other pieces of data I have (that include the player's hand, a simple chat, and cards revealed from the other player's hand), but one step at a time. If anyone could provide any insight or point me to useful information I'd greatly appreciate it.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,897
    Rep Power
    25

    Default

    I'd start with a simple client / server set of programs and see how they work. There are many examples here on the forum if you Search for say ServerSocket.
    Then design a protocol for how the players and the server will communicate.

  3. #3
    T-Prime is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default

    Thanks for the advice, Norm. I've managed to work out most of the basics. I just want to get confirmation on one thing. I think I will need to start anew thread to handle listening on the socket while the rest of the program goes about it's business, as the data I need to send is pushed from the source PC to the destination PC instead of requested, correct? Also can I send more than 1 type of data on a single open connection? This program sends a String and several custom objects with absolutely no pattern as to how and when. All the examples I've seen have been a client making a request and then getting a response of a single data type.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,897
    Rep Power
    25

    Default

    Yes, you'll want the communication stuff on its own thread.
    You can mix data types on an ad hoc basis by using a protocol of sending a short message saying what the next type is. Something like: NxtMsg=String String NxtMsg=int int ...
    Where NxtMsg=String is a representation and not the actual data sent. You could use something like an enum to define a bunch of single bytes that would tell the receiver how to read the following data. You'd need to use a class like ObjectOutputStream and ObjectInputStream that have different methods for reading different data types.
    Last edited by Norm; 07-21-2011 at 09:18 PM. Reason: Correct class name

  5. #5
    T-Prime is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default

    Thanks again Norm.

    Okay, now I'm looking at a situation like this:
    Server starts, initializes the game and listens for a client connection(works)
    Client starts, connects to the server (works)
    Server sends initial data to the client(fails)

    As best I can figure I'm doing something wrong with ObjectOutputStream.

    ObjectOutputStream out = null;
    ...
    out = new ObjectOutputStream(clntSkt.getOutputStream());
    out.writeObject(<A Deck Object>);
    out.writeObject(<A Hand Object>);
    out.writeObject(<A Hand Object>);

    Nothing gives me any issues until the out.writeObject call. Any idea where I'm going wrong.

    On the client side it does the following:

    try {
    clntSkt = new Socket("mypc", 3797);
    } catch (UnknownHostException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }

    try {
    in = new ObjectInputStream(clntSkt.getInputStream());
    } catch (IOException e) {
    System.out.println("Your input stream is pooched.");
    }

    //Draw game based on parameters passed from server
    try {
    d=(Deck)in.readObject();
    pyr=(Hand)in.readObject();
    p=(Hand)in.readObject();
    GUI.startGUI(d, pyr, p);
    } catch (IOException e) {

    } catch (ClassNotFoundException e) {

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,897
    Rep Power
    25

    Default

    Try flushing the output after writing

    Nothing gives me any issues until
    What does this mean? Errors with messages? Please post any error messages.

    Always have a printStackTrace in all of your catch blocks. You want to know exactly where and why there was an exception.

  7. #7
    T-Prime is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default

    Good call on the printStackTrace. Turns out one of my objects wasn't serializable, but I didn't notice it because it wasn't being used directly.

  8. #8
    T-Prime is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default

    I am on the cusp of completing this project. I'd like to thank you for all your assistance. I have just one last issue, that's simply an annoyance rather than a flaw in the program. I set up a simple chat embedded in the program. It works great except for one problem; The chat history doesn't auto scroll when it receives a message from the other computer. It auto scrolls just fine when it updates the messages from the local machine (assuming it's already at the very bottom of the scroll window). Anyone know how to fix this?

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,897
    Rep Power
    25

    Default

    chat history doesn't auto scroll when it receives a message from the other computer. It auto scrolls just fine when it updates the messages from the local machine
    That's strange. You are saying that the source of the data to be displayed determines if the history scrolls.
    No ideas.

  10. #10
    T-Prime is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default

    Yeah, it's a weird problem, but I found a workaround.

    textArea.setCaretPosition(textArea.getDocument().g etLength());

    Works fine now.

Similar Threads

  1. Creating a Card Game
    By Kidd91 in forum New To Java
    Replies: 1
    Last Post: 12-24-2010, 03:25 PM
  2. Card Game
    By abby0910 in forum New To Java
    Replies: 1
    Last Post: 07-24-2010, 01:38 AM
  3. please help me with this card game
    By noobinoo in forum New To Java
    Replies: 13
    Last Post: 03-28-2010, 03:07 PM
  4. card game Rummy
    By javafox in forum New To Java
    Replies: 4
    Last Post: 03-14-2009, 04:53 PM
  5. A Online Card Game
    By GonzaloP in forum Networking
    Replies: 0
    Last Post: 12-28-2008, 07:37 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •