A set of cross-platform Gobang online games development Experience (i)

Source: Internet
Author: User
Tags addchild epoll

Free to do nothing, because they want to in the server development of more in-depth learning, accumulate more rich experience. decided to write a set of network game C/S.

Because the main purpose is the development of the server, so the game I choose the rules are relatively simple, the picture effect does not require the turn-based game: Gobang. I used to do this game under the console when I was in touch with programming, and I didn't even win him at the time. Sure is the production of Gobang, but also to meet the characteristics of cross-platform, after all, the mobile internet era, the end of the world. The game is made into a full platform to better gather all kinds of players together. Cross-platform? b/S is usually the first thought of the cross-platform approach, indeed there are many on the market based on B/S page tour, most of the use of Flash as a game engine. But few people on the phone use the browser to play games. (actually does not flash,html also rotten very much, once to others to do the data management website with PHP does not have the decent interface) then chose the C + + cross-platform game engine COCOS2DX, this engine is easy to use, and because is C + + as the game logic, the transplant is very convenient, I've used this engine (a game) before. The final version chosen is Cocos2d-x 3.4.

Since is the network game server, then must be efficient, and is under the Linux, therefore I choose Epoll Model carries on the service side development, the Epoll part writes in this article: the Epoll model understanding and the encapsulation realization, uses the Linux system for cent OS 6.4, the kernel is linux2.6.


thinking about the game development steps:

Due to the lack of experience in the game, according to their previous habits of the routine, communication and protocol design should be in the first place, then the server, then to the client (no art).

And I used to play a lot of stand-alone games, updated version, the game will increase the network game function. This seems to show that many games and network protocols are independent of each other. Even the network protocol is designed according to the actual game logic, not the game according to the Protocol to design its own logic.

Finally decided to make a stand-alone version of the first. The following development process was developed:

1. Game algorithm and data structure design and implementation

2. Game Interaction Design and implementation

3, single-machine game implementation

4, Game Communication protocol design

5, the server implementation (can not ignore the focus, the purpose of their own writing games)

6, Network game function realization

7. Platform porting


1, the game algorithm and data structure design and implementation:

Gobang This game is a two-dimensional game on the plane, we will consider the chessboard as an array, each of the state of the lattice is divided into two: no chess and chess, there are different players and the difference (unlimited number, can be directly as a multiplayer multiplayer game base class)

Code:

chess.h#ifndef  _chess_h_#define  _chess_h_#include  "Cocos2d.h" USING_NS_CC;// Chess coordinate State structure body struct chesspos{int x,y;int player;//The number of steps the player Chesspos () {}; Chesspos (int px,int py,int pp) {x=px;y=py;player=pp;}}; class chessway{chesspos *way;//path Array int totallen;//Total length int len;//The current number of steps Public:chessway (int  totalnum); ~chessway (void); Void setempty (); Bool addway (Int x,int y,int player); /Add Step number Int getstep (); Chesspos getnow ();}; Class chess{public:chess (int width,int heigh,int winlen=5,int playernum=2); ~Chess ( void); Int **board;int w,h;int pnum; //palyer numint wlen; //how number  can winChessway *way;int playercnt;//player start at 1bool isgameend; Bool init (int width,int heigh,int winlen=5,int playernum=2); Void exit (); void  restart (); Bool nextstep (chesspos&nbsp, NP);//chess, automatic judgment player Bool nextstep (int x,int y); Int getstatus (int x,int y);// Get the game state Bool checklen (int x,int y); Int checkwin ();//Determine if the game is over and return to the winning player}; #endif  //_chess_h_

The logic to detect victory is simple: find a place to play chess, check the position under, right, bottom left, and right down whether there are consecutive equal 5 chess, that is the game victory. Once the game is won, it is not possible to continue playing chess, so there will only be one player to win. The following is a code for judging:

chess.cpp//Victory Detection code bool Chess::checklen (int x,int y) {for (int. i=1;i<wlen;i++) {if (x+i>=w) {break;} if (Board[x+i][y]!=board[x][y]) {break;} if (i==wlen-1) {return true;}} for (int i=1;i<wlen;i++) {if (y+i>=h) {break;} if (Board[x][y+i]!=board[x][y]) {break;} if (i==wlen-1) {return true;}} for (int i=1;i<wlen;i++) {if (x+i>=w| | Y+I>=H) {break;} if (Board[x+i][y+i]!=board[x][y]) {break;} if (i==wlen-1) {return true;}} for (int i=1;i<wlen;i++) {if (x-i<0| | Y+I>=H) {break;} if (Board[x-i][y+i]!=board[x][y]) {break;} if (i==wlen-1) {return true;}} return false;} int Chess::checkwin () {for (int. i=0;i<w;i++) {for (int j=0;j



2. Game Interaction design and implementation

When it comes to game interaction, it's going to be used in the game engine. First of all, some of the game's image resources are roughly done, here with a drawing of a few unsightly pictures of the resources: 650) this.width=650; "Src=" http://s3.51cto.com/wyfs02/M01/6E/6A/ Wkiom1v7l9uin4iwaahmhnnadys050.jpg "title=" Res1.png "alt=" wkiom1v7l9uin4iwaahmhnnadys050.jpg "/> Don't look at the ugliness of this painting, I use the mouse and window to bring out the drawing, when the game will look like there is no vainly disobey sense (the author of the primary school would draw H diffuse).

We're going to use COCOS2DX's stuff here. First for each chess lattice design a block of nodes, and then design the game main scene layer:

Class Chessnode:public Nodeclass chessmain:public Layer

As the game board, each lattice is the same shape, I just need to splice them into a matrix into a complete chessboard. So in the game set layer, I opened a vector chessnode, which was arranged in a compact sequence on the screen. In the initial game state, Chess_1.png, Chess_2.png is not displayed, (I directly use the off-the-shelf game):

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/66/wKioL1V7nOChCM7EAAM5FIcJZ1Q677.jpg "style=" float: none; "title=" Game1.png "alt=" Wkiol1v7nochcm7eaam5ficjz1q677.jpg "/>

Does such a chessboard look like a vainly disobey feeling?

When you play chess, you can display the corresponding chess chart:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/6A/wKiom1V7mzexs2LGAAPhmb7GBlc756.jpg "style=" float: none; "title=" Game2.png "alt=" Wkiom1v7mzexs2lgaaphmb7gblc756.jpg "/>

It turns out that a real chess game is next to the cross at the intersection.

The main considerations in this section are touch detection and board screen size. The position of playing chess can be calculated relative to the coordinates of the board setting layer by touching the words. The chessboard is based on a static value of 480px, which is scaled when it is called elsewhere.

 #ifndef   _chessmain_h_#define  _chessmain_h_#include   "Cocos2d.h" #include   "Chess.h" USING_NS_CC; #define  defaultwinsize 480.0#define  Chesspicsize 50.0static point winsize;class chessnode:public node{public:chessnode ( int playernum=2); vector<sprite *> chesspicarr; sprite * basepic;}; class chessmain:public layer{public:chess *chessdata; Vector<chessnode *> basenode;virtual bool init ();//virtual void onenter (); Void restart (); Void updateone (int x,int y); Void updateall (); Bool nextstep (int  x,int y); Int  checkwin (); Create_func (chessmain);}; #endif  //_chessmain_h_ 
#include   "ChessMain.h" Chessnode::chessnode (int playernum) {basepic=sprite::create ("chess_base_1.png") ; Basepic->setanchorpoint (CCP (0,0)); This->addchild (basepic); char addname[]= "Chess_1.png"; for (int  i=0;i<playernum;i++) {addname[6]= ' 0 ' +i+1;auto newsprite=sprite::create (addname); Chesspicarr.pushback (Newsprite); Chesspicarr.back ()->setanchorpoint (CCP (0,0)); This->addchild ( Chesspicarr.back ());}} Bool chessmain::init () {winsize=director::shareddirector ()->getwinsize ();//default Checkerboard chessdata=new  Chess (15,15); for (int i=0;i<chessdata->w;i++) {for (int j=0;j<chessdata->h;j++) { Basenode.pushback (New chessnode ()); Basenode.back ()->setscale ((defaultwinsize/chessdata->h)/ chesspicsize); Basenode.back ()->setposition (CCP (defaultwinsize/chessdata->w*i,defaultwinsize/chessdata- &GT;H*J)); Basenode.back ()->setanchorpoint (CCP (0,0)); This->addchild (Basenode.back ());}} Restart (); return true;} /*void chessmain::onenter () {;} */void chessmain::restart () {Chessdata->restart (); UpdateAll ();} Void chessmain::updateone (Int x,int y) {for (int i=0;i<chessdata->pnum;i++) {if ( Chessdata->getstatus (x, y) ==i+1) {basenode.at (x*chessdata->w+y)->chesspicarr.at (i)->setVisible (true) ;} else{basenode.at (x*chessdata->w+y)->chesspicarr.at (i)->setvisible (false);}} Void chessmain::updateall () {for (int i=0;i<chessdata->w;i++) {for (int j=0;j<chessdata- >h;j++) {updateone (I,J);}}} Bool chessmain::nextstep (int x,int y) {if (chessdata->isgameend) {return false;} if (!chessdata->nextstep (x, y)) {return false;} Updateone (x, y); Checkwin (); return true;} Int chessmain::checkwin () {Return chessdata->checkwin ();} /*bool chessmain::ontouchbegan (touch *touch, event *unused_event) {Point pos= Converttouchtonodespace (touch); if (pos.x>defaultwinsize| | pos.y>defaultwinsize) {return false;} Int x=chessdata->w* (pos.x/defaultwinsize); int y=chessdata->h* (pos.y/defaultwinsize); Return nextstep (x, y);} */

The touch function here is rewritten by the subclasses of the later Chessmain.

3, single-machine game implementation

Single-player game, just write good opponent's AI logic. Fortunately is Gobang not go, AI is very good to write, can quickly calculate the winning state. Because its main purpose is to write the network side. So I put the single-machine function after the implementation and did not write AI, the interface is left, only a random function, and so on later have leisure to add the AI logic.

In general this part is added to the pre-game menu as well as the option of a single game and the end of the game dialog box:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/66/wKioL1V7ov7hB1VTAAD5lHH5qd8186.jpg "title=" m1.png "alt=" Wkiol1v7ov7hb1vtaad5lhh5qd8186.jpg "/>650) this.width=650; src=" http://s3.51cto.com/wyfs02/M02/6E/66/ Wkiol1v7ow_xjcrxaardcvtwnak672.jpg "title=" Game3.png "alt=" Wkiol1v7ow_xjcrxaardcvtwnak672.jpg "/>

#ifndef _aigamemain_h_#define _aigamemain_h_#include "cocos2d.h" #include "ChessMain.h" USING_NS_CC; #define Defaulttoolwidth 200.0#define defaulttoolheight 100.0//game End Menu Class Aigameendtool:public Layer{public:aigameendtool ( int type), BOOL init (int type), void Gamerestart (ref* psender), void Menuclosecallback (ref* psender);};/ /ai game inherits from Chessmainclass Aigamemain:public chessmain{public:virtual bool Init (), virtual bool Ontouchbegan (Touch *touch, Event *unused_event); void Nextaistep (); bool Checkwin (); Create_func (aigamemain);}; #endif//_aigamemain_h_

Now a playable game has been completed, followed by the focus of the network section.

After the document has been written in here to give the Web part of the address of the article and source files, please look forward to.

This article is from the "Wchrt" blog, make sure to keep this source http://wchrt.blog.51cto.com/8472636/1661524

A set of cross-platform Gobang online games development Experience (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.