Permanent socket for playing chess for board games (Android)

I just learned Android and Java programming (very noob inside) and I would like to ask some questions about Android and Socket Server programming.

I got the task to create a simple chess application (AI excluded), the pawn position will be received from TCP socket in:

Server: xinuc.org

Port: 7387

I was told to use Socket Persistent because the server updates the pawn position every second, the pawn position will be sent in this format

[Pawn Code] [Horizontal Position] [Vertical Position] [Space],

Lot code:

K: White king

Q: The White Queen

B: White bishop

N: White Knight

R: White rook

k: Black king

q: Black queen

b: Black bishop

n: Black Knight

r: Black rook

For example: Ka1 Qg3 Be6 etc.

Then my application has to adjust the resulting position and move the pawns accordingly.

I have read several tutorials on Android Socket programming but still am confused but used AsyncTask instead of Thread because I read that AsyncTask would be the best option in this case.

And after reading and learning a bit about it, this is how I get data using Socket (in doInBackground):

try {
    clientSocket = new Socket(SERVERADD, SERVERPORT);
    InputStreamReader inputStream = new InputStreamReader(clientSocket.getInputStream());
    BufferedReader reader = new BufferedReader(inputStream);
    String latestPosition = reader.readLine();
    storedPosition=latestPosition;
} catch (UnknownHostException e) {
    Log.d("Error Unknown Host", String.valueOf(e));
} catch (IOException e) {
    Log.d("Error IOException", String.valueOf(e));
}

      

I don't think the code I put above for fetching data from Socket is best practice, CMIIW.

So this is a (rather long and boring) background to support my question below.I have two main questions:

  • I am a bit confused about what I am doing here, would it provide the code I provided above to read data from the Client?

  • And after receiving the data, I have to move the pawn position accordingly. And I still don't know how this should be done (about how I create the board and move the position of the pawns). Can you tell me in a more understandable way how this should be done?

I have read: Android Chess Game Example Android Source Code - Chess .

But I think they are too hard for me

Thanks in Advance

+3


source to share


1 answer


Your question is too broad. You can specify which part you need help with. Anyway, I'll try to give some general ideas. Hope this helps.

Model

  • First, create data models for chess pieces, players, and a chess board.

Net

  • Then make sure you are receiving data correctly over the TCP network. This is where you should master AsyncTask. After you understand the AsyncTask workflow, you can use this example to start developing your own AsyncTask.


GUI

Finally, you must implement the graphics. Based on your project's application, AFAIK there are three alternative approaches to borrowing.

  • You can use ImageButton on your chess pieces and move them using Android animation. Google GridView too. I found two versions of Grid based chess on Android: this and what .

  • If you are a web developer or, by chance, know the Android Canvas API, this is the best way to give you more freedom and precision.

  • Another more time consuming but professional approach to the user interface for the game is possibly using OpenGL ES for Android. You can read white papers , watch online tutorials, or follow a master book on a topic. Definitely use OpenGL if you're going 3D.


All that said, there are popular game engines that could help you become a professional game developer efficiently and quickly. Cocos2D is one of my favorites, it approaches the chess game and taught Todd Perkins comprehensively   on Linda.

+1


source







All Articles