In a 4 player network big 2 card game. how do I track a player turn?

so in my program, my server will wait for 4 clients to login.
my server will spawn a new thread each time a client is connected
after 4 clients have log in, the server will send the game view to all the clients respectively.
I know how to connect the players together. but I just have no idea to keep track of a players turn.Not asking for codes but just some advice on how to kick start. please help me. thanks.

Recommended Answers

All 5 Replies

How about a Game class that, among other things, has apublic joinGame(Player p) method that stores each player in a private 4 element array. That class is then a place where you keep track of the Players and the state of the game. Rather than join players together directly it would probably also be better to do that via the Game instance

so you mean that after my server verify the player information, it will then send the player names to the joinGame(Player p) method in Game class?

Yes, but not the player names, the actual instances of the Player class, which should have all the player-related info, including their name, and a ref to the socket/thread/connection where they can be found.

So assuming that I have all these information passed tothe public joinGame(Player p) and what must I do next in order to tell the program "hey player 1 is your turn" "hey player 2,3 and 4 is not your turn yet. Wait for player 1 to make a move first

That's the kind of logic the belongs in the Game class. It should know whose turn it is next and instruct the appropriate Player - maybe call a method in the Player class like player.getNextMove().

Get some sheets of paper. Label one "Game" and the others Player1 and Player2.
On each sheet write down what info belongs where. Guess what each needs to do, and write method names for each of those things. Now step through a bit of a game, tracing who class which methods, and whether there is enough info on each sheet of paper to do what is needed. When you hit a snag, fix it and try again. When you get that all making sense then you have a documented class structure, instance variables, and method specifications. Then, and only then, start coding!

(This is a technique called Class Responsibility Collaborator (CRC) Modelling. It works really well, trust me. Here's a good write-up.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.