How to make a chess game?

Hello,

I m searching about the way to do a chess game with cocos2d-x.
Do you think use tilemap is a good idea ? Or maybe just with sprites with a 2 dimensions Array 8x8. Do you know where i can find some source code ?

In advance thanks,

I’d make a vector of 64 sprites, black and white. Calculating their position would be easy. You can also store their index in tag. Later you can retrieve x = index % 8 and = index / 8. Now you have to make sprites for all figures and place them at starting points according to the rules of chess. So moving figures by drag and drop would be easy. The real pain would be to check if move is valid chess move :slight_smile: And ofc computer AI. Tilemap is not needed here, but you can try it as well.

1 Like

I think if you search you’ll find another discussion or two about it. You can use a tile map, or just a single image (various resolutions for device sizes), or a DrawNode (draw the squares and board using rectangles), or 2d array of sprites for the board, or even a Sprite3D model for the board. The board itself doesn’t really change. The pieces move so you’ll have individual sprites for each, or Sprite3D, which are displayed on top of the board.

Google/StackOverflow will probably be better for the overall ā€œhow to create a chess gameā€ and come here for specific cocos2d-x specific questions, but of course feel free to ask whatever here.

1 Like

Thanks a lot for yours answers, look like use tile map for that its like use a bulldozer for make a niche, i think i ll go on make 64 Sprites how is a representation of a arrays in 2 dimensions, like myArray[1][1] = ā€œQā€ for queen, myArray[1][2] = ā€œKā€ for King etc… and 64 other sprites for the board black and white, and calculate the size of the square with the visibleSize.heigth and visibleSize.width availables or maybe draw the boards or using a picture.

Thanks, :smile:

I have a chess game for iOS, so can share a few things that I learnt. (It was made with the Cocos2d-iPhone, but much of the challenges are the same)

The most important thing that I realised was that the best approach was to create a strict separation between the UI and the logic. More like the architecture of a non-game application. When a move has to be processed, the engine that drives the game processes everything, and on completion the board gets updated to reflect any new state.

So the only extra information that my sprite subclass I was using for the game pieces contained was an id, so that I could link it to a piece representation that I was using in my chess engine.

This is especially helpful when creating the AI (if that’s what you plan to do). I found that I was copying the data structure for the board many times in the course of evaluating many possible moves. This was made much more manageable by having a lightweight structure that existed only within the chess ā€˜engine’, and didn’t contain UI elements.

It also contains any bugs in the logic to within the logic, which helps keep everything under control. If you interlink the two, then you could end up in a situation where the output of your process is correct, but your sprites could end up in the wrong screen position, or disappear, due to some side effect of an operation in the middle of the process, which could be tricky to debug.

Within the data structure that contains the state of the board, all of your piece positions should be stored just as the index of the square on the board. You can go for a (1, 3) type coordinate system if you like, but I found a single integer between 0-63 to be easiest to work with. This way you can leave calculating the screen position of the pieces to the UI portion of the game, which will just be updated at the end.

Writing a chess logic (especially with an AI) is a pretty big task, and once it’s written you might want to reuse that logic down the line. I initially wrote mine in Objective-C for iOS several years ago. A few months back I ported it to java for android. It was really simple to do, because the logic was encapsulated and didn’t rely on any framework specifics, so I could just port the logic almost line for line, and rewrite the UI using the android sdk.

Quick plug (couldn’t resist): http://www.stevebarnegren.com/pocket-chess.html

1 Like

Thanks a lot for your answer and advices, :smile: Very nice apps pocket chess !