Advertisement Jump to content
  • Advertisement


  • Content Count

  • Joined

  • Last visited

Community Reputation

137 Neutral

About YyYo

  • Rank
  1. Ok Thanks..   BTW, user input(such as touch event), are stored in Queue, instead of handling them immediately. In every iteration of game loop i 'poll' event from the queue and handle it.
  2. Hi...   I am newbie to game programming, so excuse me if I ask silly question. I start to implement my first game. The game is like a turn-base board game.(implemented using Java for Android) Each player have 14 soldiers. each soldier placed/located in default position. Before starting game, there is an 'INIT' stage, where each player can change the default location of each of his soldiers. After each player set their soldiers, the game can start.   My game loop(pseudo code) while(true){ processUserEvents(); // if its player's TURN, will update the event(else will drop it) draw(); // redraw } My question is: should the stage of 'INIT' need to be inside game loop? or it should be in a different place, and when 'INIT' finished, only then execute game loop ?   any byte of advice will help.  
  3. First thank @hplus0603   I take your idea and Factory pattern and it fits exactly as I need. public class ActionFactory { public static Action createAction(JSONObject object){ try{ String username = object.getString("USERNAME"); String type = object.getString("TYPE"); String src= object.getString("SRC"); String dest = object.getString("DEST"); if(type == "MOVE"){ return new ActionMove(username,src,dest); } else if(type == "KILL"){ return new ActionKill(username,src,dest); } else if(type == "DIED"){ return new ActionDied(username, src, dest); } else if(type == "TIE"){ } } catch(JSONException e){ e.printStackTrace(); } return null; } }
  4. you mention:   Should I deserialize the received json into an object ??? e.g: { TYPE: 'MOVE' ...}   => class MoveAction    currently I parse the received json and using String comparison to figure out what is the action.   What is more appropriate ??   Thx
  5. Hi...   I an designing my first game, like checkers, and decided that messages between client and server will be text based(json).   In the game, each player in its turn, can:  - move to next position  - kill opponent's soldier -  eaten by opponent soldier   option 1: use a single message for all action, and let the server decide if the action is move || kill || eaten e.g: {TYPE: "MOVE" , SRC_POS: <POSITION> DEST_POS: <POSITION> }   option 2: use a dedicated message for each type of action e.g:  {TYPE: "MOVE" ....} , {TYPE: "KILL" ....} , {TYPE: "EATEN"  ....}   I prefer option-2, since its eliminate so many if-else in server code, to figure out what kind of action is.   What is better, and which will cause failures/bugs in future ???    
  6. Ok... lets take following scenario. There 2 player: playerA & playerB Current Player: playerA Lets say both players are cheating and sends the following message order: [ B4 , A3 , A2 , B3 , A1 , B2 , B1] -> pollFirst() If I decide to discard message before insert them to quaue, the following will happend in main loop: 1th iteration: B1 will be discarded and updateBoard wont do nothing(since queue is empty) 2th iteration: B2 will be discarded and updateBoard wont do nothing 3th iteration: A1 will be accepted and updateBoard will make the move and change the turn to playerB 4th iteration: B3 will be accepted and updateBoard will make the move and change the turn to playerA 5th iteration: A2 will be accepted and updateBoard will make the move and change the turn to playerB 6th iteration: A3 will be discarded and updateBoard wont do nothing. 7th iteration: B4 will be accepted and updateBoard will make the move and change the turn to playerA   is this what you ment to discard message before insert them to queue?   Another thing raises while writing the scenario above(discarding message before adding to queue...) It seems that the first valid message is processed immidiatly by updateGame(), and when finish updating, it pass the turn to next player. It seems that the queue always have single valid message in queue at any time, so I guess I don't ready need the queue. Am I wright or I'm starting to get a little confused ???
  7. BTW: isn't it better to CLEAN gameMessages queue after I make a move for the current player??   Since it is a turn-base game, which each player can make a single move, isn't it better to empty gameMessage queue? It can prevent for cheater which try to 'attack' the game, by sending multiple messages to game when its not their turn. I will adopt your idea which each player can't    but then the gameMessage queue will still contain "garbage" messages   am I right ??
  8. First thanks for the fast response.   didn't understand what doesn't make sense   what I do in updateGame is to loop all "movement" messages in queue, and ignore messages not belonging to current player. Can you please explain again?
  9. HI...   A few weeks ago I started to implement my first multiplayer game(Java/Android) The game is turn-base game similar to tic-tac-toe and players can Chat during the game. The server always knows who is the current player. since I need to support chatting no matter who's turn it is, I collect ALL messages from both players. Chat messages are stored in a dedicated queue and broadcast to players. Game messages are also stored in a dedicate queue.   Now comes the problematic part: Lets say one of the player is cheating, and manage to send a "move" message to server even though its not is turn, how can I "recover" from this situation. I have a solution for it, by start polling messages from game queue. if the message is not belong to current player i ignore it. if the message is belong to current player, I update game and switch turn to next user. Is this solution is the correct one or there is something more simpler/elegant ???   I added a snapshot of pseudo code: package ServerGame; import java.util.Iterator; import java.util.Queue; class GameManager extends Thread{ Player currentPlayer; Queue<Player> players; Queue<String> chatMessages; Queue<String> gameMessages; public void run(){ String message; while(true){ for(Player player : players){ if(player.isReadAvailable()){ // check if there is a message sent from client message = player.doRead(); // read message content router(message); // route the message according to its type: game | chat } } broadcastChatMessages(); // send chat messages to all players updateGame(); // update game } } private void updateGame(){ for(Iterator<String> iterator = gameMessages.iterator() ; iterator.hasNext() ; ){ String message =; if(currentPlayer.isYours(message)){ // handle message if(message.type ==”move”){ updateBoard(); // process the move and do a switch turn to opponent } switchTurn(); } else{ // ignore messages that are not belong to current player(avoid cheating) iterator.remove(); } } } private void broadcastChatMessages(){ for(String message : chatMessages){ for(Player player : players){ player.doWrite(message); } } } private void router(String message){ if(message == null){ return; } if(message.proprietary == “CHAT”){ chatMessages.add(message); } else if(message.proprietary == “GAME”){ gameMessages.add(message); } else{ // ignore message - maybe log it } } }
  10. Ok I will use AsyncTask. I currently stuck in a critical decision point, and I really need your help! I already said its my first game implementation and I don't know if my architecture is correct. Note: I am using Java/Android.   In my Activity, I have 2 objects: 1) Game - which is the game's board and all the game rules including the game loop.    the player plays according to GameState. 2) ConnectionThread with 2 mainly methods: 2.1) onRead() runs in endless loop and wait for server response.        When a response received it notify both Game object and Activity.       A response is used to notify player if he should WAIT/TURN, or to update opponent's soldiers after moving.         2.2) onWrite(String Message) just sends a message to server, and exits   Game & Activity assign a callback to Connection's onRead() method so they could be notified if new message received And Game also has ConnectionThread reference, so it could use its onWrite() method to sends about his moves   Is this technique is the right way to implement, or should I do something else?   Thx
  11. I was thinking about using AsyncTask but, according to Docs: AsyncTasks should ideally be used for short operations (a few seconds at the most.) and my client app can wait, in worst case if opponent not make a move, for 15 seconds before turn switch back to him.   More over, when the Activity is destroyed, the AsyncTask will still runs, and if you do stuff in onPostExecute() and try to refer to views of the destroyed Activity, you will receive an Exception.
  12. No I am not using a new game library. Its a multiplayer game, something like Checkers, implemented in Java. The client application runs on Android. The server is just a simple TCP server(decided not to use UDP since its not realtime game) which sends/received messages using Json.   The Client responsible for player movements, and sends to Server the move for authentication(client received an ACK). The server sends to opponent the other player movement.   BTW, there is no select() in Java(there is the option to use NIO or MINA) I decided that each client will have 2 threads: one for reading and one for writing, to mimic non-blocking I/O   Please feel free to advice since its my first time implementing a game.   Thx
  13. I am using TCP, but even if i use UDP with non-blocking socket, how can you implement a state where the client need to wait until a response from server without blocking? You will implement a loop(true) until some text can be recv() ?
  14. Hi...   I newbie to the forum and to multiplayer game. I started to implement a simple multiplayer game, using Java, and I have a question. When 2 clients login to server, a new game is establish for them. each player, in his turn, make a move and inform the server(which authenticate and send to opponent).    when one player is playing, the other is waiting for the server to inform him that now its his turn(server send 'TURN' message) The problem is that the player is waiting for the server by readLine() method which block the main thread. I am planning to implement a Chat so the players could communicate each other, and if the main thread is block I wont be able to do so.   Should I need to implement 2 thread for each player: one for reading and one for writing, so the application wont block?   Any advice will appreciated.   Thx.  
  15. OK, now i understand. My game also has a Chat option, so BLOCKING until recv() is not an option. I am writing the game with Java, and look for equivalent for select() method with no luck.   Is there anything close to select() method in java? or should I try to implement something using a thread   Thx
  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!