Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Getting started online  (Read 2945 times)
0 Members and 1 Guest are viewing this topic.
Offline supermancc0386

Junior Newbie





« Posted 2009-12-09 01:51:15 »

So, I have been working on a card game and it has been going well. 
It is a 5 player game and it currently only runs on a single machine which isn't much fun.

My reason for writing it is that my friends and I used to have card night once a week.  Now, we're all off to college and we would like to play together once a week online.

Now I'm ready to take it online so that we can all play together.  The problem is, I'm not sure exactly how to go about this step.  I've done very little networking work with Java and have been looking on google for a simple multiplayer card game and haven't found a good tutorial.

So, what would be the best way to do something like this?
Can anyone point me in the direction of a good tutorial?  (The game doesn't need flashy graphics or anything.  Just simple cards)
Offline Xyle
« Reply #1 - Posted 2009-12-09 02:30:51 »

Well, Ive made a few client/ server attempts, got a few working but cant remember getting it going on my old web page.

I do remember starting here and learning about sockets.
Sockets Trail at Sun

Everything on that page will give you a good understanding of what your trying to do. Since its a turn based game your looking at, the standard java api's will suffice Im sure.

I used and chopped at the knockKnock server example which helped me understand networking basics.

I used 1 thread in my app to check for new connections and 1 thread in my app to handle the game logic. Basically everything a player does sends a message to the host, the host then parses the messages and reacts to it.

Its been a little bit since I messed with my networking apps, but currently Im rewriting my Hearts Single player card game and once I get that up and running on my web site, I will rework the multiplayer hearts and publish that too.

The biggest problem I had was the thread that waited for connections was a blocking thread, meaning that no other code would get passed until a new connection was found. Thats the reason for a multiple thread app. 1 to check for new connections and 1 to handle the game. Start with the link above then if anything you should be able to do better searches to help understand your direction.

Have fun!

Life is just a game, learn to play!
------------------------------------------
╬-YellzBellz Games!-╬ Cheesy
Offline Nate

JGO Kernel


Medals: 149
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #2 - Posted 2009-12-09 04:18:30 »

Shameless plug:
http://code.google.com/p/kryonet/

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Xyle
« Reply #3 - Posted 2009-12-09 04:56:21 »

Wow, nice looking stuff there. Once I get going with my networking code I'll take another gander at that.

Thanks for the share!

Life is just a game, learn to play!
------------------------------------------
╬-YellzBellz Games!-╬ Cheesy
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #4 - Posted 2009-12-09 17:53:51 »

You can take a look at Game Gardens, which has a lobby system and pretty much automatic network handling for you. It should work great for a card game or other simple online games.

See my work:
OTC Software
Offline supermancc0386

Junior Newbie





« Reply #5 - Posted 2009-12-10 02:05:41 »

Thanks,

I have a general idea of how I could do this with sockets now.  I have one more question.  Since it is turn based, will I need to make repeated connections to each client per turn? Or can I connect to each client once in the beginning and that is it?
I would like to include a simple chat also in the application.

My current idea:
1) I'm thinking each client connects to the server at the beginning.  It accepts chat lines from everyone and displays them to everyone.  And it only accepts play orders from the client who's turn it is.

Does this sound like the right idea?

So if input comes in from a client and it begins with a special chat character, display it in everyones chat window.  and if input comes in from the client who's turn it is and the input starts with a special play character, make the move and output the results to the clients. so it would ignore play requests from the other clients.



2) Or should I be making one connection from everyone to a chat, and a connection that connects only to the player who's turn it is?  So essentially everyone has one connection open, and only the person who's turn it is has the play connection open?
Offline ddyer
« Reply #6 - Posted 2009-12-10 02:44:31 »


You need to define a simple, language that you can parse and which will be extended as the
needs present themselves.  Each message should include the identity of the sender and
some sort of message-type token.  You should be able to parse the message stream without
understanding the particular content of any message.
Offline Xyle
« Reply #7 - Posted 2009-12-10 02:48:36 »

I made a model very similiar to #1. One connection per client, when client disconnects the connection is gone. I also preceded each message with a code that the server parsed and reacted accordingly.

It seems your on the right track as far as my experience went.

Game Gardens look like a nice solution too. One thing for me though is understanding other peoples code and implementation is a nightmare for myself, more often than not the code is too abstract or high level for me to understand. I'm sure its due to my coding techniques and unorthodox ways of putting stuff together. They would definitely make a veteran cringe in utter terror. Smiley

Life is just a game, learn to play!
------------------------------------------
╬-YellzBellz Games!-╬ Cheesy
Offline Nate

JGO Kernel


Medals: 149
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #8 - Posted 2009-12-10 19:51:49 »

You can do P2P or client/server. I recommend client/server. Each client connects to the server with a persistent connection.

You don't have to send strings and check if they start with a special character. You can use something like KryoNet and send whatever objects you want. So you would have a Chat object that has a field "String text". KryoNet already tells you what player the object came from. A client sends a Chat object to the server, the server then sends an object to each other client so they can add the text to their chat window. You might call this ChatUpdate and it would have "String text" and "int fromPlayerID" which is the KryoNet player ID of the player that sent the chat message.

KryoNet comes with a client/server chat example:
http://code.google.com/p/kryonet/source/browse/trunk/#trunk/kryonet/examples/com/esotericsoftware/kryonet/examples/chat

To structure your gameplay, the server keeps the state of the game. To start, it probably shuffles some cards, and gives each player some cards. After setting up the state, you probably want to send a StartGame object to each player, eg so the client knows what cards are in their hand.

You generally don't want to send information the client doesn't need to know. Eg, if the player has some face down cards and they aren't supposed to know what they are, don't send what the cards are until the player draws them. If you do, someone could hack the client to make those cards always visible. By not sending the hidden information, you prevent cheating. That said, if you don't care to worry about preventing cheating (eg you are playing with friends) then if it makes development easier to send hidden information, go ahead.

After sending StartGame, the server might send a StartTurn object to each player. It would have "int turnPlayerID" which is the ID of the player with the current turn. Each client would disable most of their UI so they can't play cards and highlight the current turn player. The client with the turnPlayerID would enable its UI and allow the user to play a card (or whatever). The client would then send a PlayCard object to the server with the necessary identifying information.

When the server receives a PlayCard object it should check to make sure it is from the current turn player. Then it determines any actions necessary when the card is played and sends objects to the clients so they can update their UIs. It then sends the next player a StartTurn message (assuming the first player is done).

Because your game is turn based, you could support persisting the state of the game between turns. If a connection is lost or all the players otherwise leave, the server could hold on to the game state. When the players come back, the StartGame object would restore the game to how it was before, rather than to a new game state.

You could take this even further to make your turn based game playable on mobile phones. The server is exactly the same except instead of having persistent connections, you could do the communication over something like HTTP. This way the clients are normally not connected and they poll for updates. I have a project for that too, also based on Kryo so you can send objects over HTTP, and it works on the desktop and Android:
http://code.google.com/p/legion/

You might want to structure your server to handle multiple games at once. Eg, you could have a lobby where players go when they first connect. They organize and start a game from there, are removed from the lobby, and then the game code runs just for those players. This way not all players are in the same game, and the server can be hosting multiple games. Building a lobby is a bit boring though, so you might want to add this after your game works. Then again, if you are just going to play with friends you may not care to host more than one game per server.

Here is an online multiplayer card game that uses KryoNet:
http://www.ttafo.com/afo.html

Offline supermancc0386

Junior Newbie





« Reply #9 - Posted 2009-12-11 18:02:58 »

Thank you much Nate!

I will definitely be looking into KryoNet.  Sounds pretty good.

It would be nice to have some of those additional features. Especially the lobby in time.

A couple more things:

So when going about this, which should I start with: the client or the server?  I'm thinking the server?

How will I test this? I don't happen to have 5-6 computers laying around.  I do have 2. Can I connect multiple clients from the same machine? (Something I would want to eliminate for distribution) Heck could I even run a client on the same computer as the server?

Also, I'm getting a bit ahead of myself, but what about having a user log in?  I mean for now, in the client, I can just have it ask for a name so that it can be displayed and people know who's who but in the event this becomes something more, how would KryoNet work with users logging in with a user name and password?  I guess I don't need to know how so much as if I go with KryoNet, is this still a possibilty?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Nate

JGO Kernel


Medals: 149
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #10 - Posted 2009-12-11 18:30:25 »

So when going about this, which should I start with: the client or the server?  I'm thinking the server?

I would build both at the same time.

Quote
How will I test this? I don't happen to have 5-6 computers laying around.  I do have 2. Can I connect multiple clients from the same machine? (Something I would want to eliminate for distribution) Heck could I even run a client on the same computer as the server?

The server runs on a specific port. The clients automatically choose an open port and connect to the server through this port. You can run multiple clients on the same machine as the server. To stop this you could prevent the server from accepting more than one connection from the same IP address. A better way, but more easily circumvented, might be to lock a file or open a socket on the client. The second client that runs would check this before allowing connection to the server.

Quote
Also, I'm getting a bit ahead of myself, but what about having a user log in?  I mean for now, in the client, I can just have it ask for a name so that it can be displayed and people know who's who but in the event this becomes something more, how would KryoNet work with users logging in with a user name and password?  I guess I don't need to know how so much as if I go with KryoNet, is this still a possibilty?

Logging in isn't much different than just registering a name. The client would ask for the name and password and send it to the server, which would either respond with a JoinLobby object (or whatever) or an InvalidPassword object.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (19 views)
2014-09-24 16:12:06

Grunnt (45 views)
2014-09-23 14:38:19

radar3301 (28 views)
2014-09-21 23:33:17

BurntPizza (63 views)
2014-09-21 02:42:18

BurntPizza (33 views)
2014-09-21 01:30:30

moogie (41 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!