Persistent Socket for Chess Board Games logic (Android)

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

I got an assignment to create a simple chess application (excluding the AI), the position of pawn will be retrieved from TCP socket in :

Server : xinuc.org

Port : 7387

I was told to use Socket Persistent because Server will update pawn's position in every second, the pawns position will be sent in this format

[Pawn's Code] [Horizontal Position] [Vertical Position] [space],

Pawn's Code :

K: White King

Q: 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 and so on.

Then my application have to adjust the retrieved position and move the pawns accordingly.

I've read some tutorial about Android Socket Programming, and still kinda confuse though, I used AsyncTask instead of Thread because I read that AsyncTask will be a better option in this case.

And after read and learn a bit about it, here is how I retrieve data with 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 to retrieve data from Socket is the best practice, CMIIW.

So that is the (quite long and boring) background to support my question below, I have two main questions here :

  • I am a little bit clueless about what I am doing here, will the code I provided above able to read the data from Client?

  • And after I retrieve the data, I should move the pawn's position accordingly. And I still have no single clue about how it should be done (About how I create the board, and move the pawns position). Can you tell me in a more understandable way how it should be done?

  • I've Read these : Android Chess Game Example Android Source Code - Chess,

    But I think they are too complicated for me,

    Thank you in Advance


    Your question is way too broad. You might want to specify on which part you need help. Anyway, I will try to give some general ideas. I hope it helps.

    Model

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

  • Then make sure that you are receiving data correctly over the TCP network. This is where you should master the AsyncTask. After you have understood the workflow of AsyncTask, you can use this example to begin developing your own AsyncTask.
  • GUI

    Finally you should implement the graphics. Based on the application of your project, AFAIK there are three alternative approaches to take.

  • You can use ImageButton for your chess pieces, and move them using Android Animations. Google GridView too. I found two Grid-based implementions of chess on Android: this and that.

  • If you are a web developer, or by any chance, you are familiar with Android Canvas API, this is a better way, providing you with more freedom and accuracy.

  • Another more time-consuming yet professional approach to UI for a game, might be using OpenGL ES for Android. You can read official documents, have a look at online tutorials, or master a good book on the topic. Definitely use OpenGL if you are going 3D.


  • All that said, there are popular game engines which could help you become a professional game developer efficiently and swiftly. Cocos2D is one of my favorites, it suits a chess game, and is comprehensively taught by Todd Perkins on Lynda.

    链接地址: http://www.djcxy.com/p/84666.html

    上一篇: 在国际象棋中获得敌人可能的移动到2D阵列

    下一篇: 棋盘游戏逻辑的持久化插槽(Android)