How do I make a chess game?

Not favoritedFavorited Favorited 0 favourites
  • 5 posts
From the Asset Store
Beautiful Black & White Chess Pieces for Chess Game.
  • Hey gang,

    I'm working on making a chess game in construct 3, after which I plan to make some 'alternate rules' chess games. I also want to put a chess program as a 'game within a game' feature in an RPG I want to make. I'm a litte stuck right now on how to write up the event sheet, with movement logic, piece captures, piece selection and turn progression. Can anyone help out with this?

  • Programming chess with events would be a pretty big task - there are a lot of rules. Especially if you’re planning to take the classic array-based approach.

    I'd actually suggest avoiding arrays altogether and instead building everything with sprites, tilemaps, collision checks, image points, raycasting, etc. - it should be easier.

    If you hadn't mentioned 'alternative rules', I would've just recommended using a JavaScript library. Here's an example:

    howtoconstructdemos.com/chess-game

  • Thanks for the link, but could you give me some pointers on how to write that up in the event sheet?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Here is an example of knight movement:

    dropbox.com/scl/fi/if73d7fsl1hfrxrkirp1m/Chess_example_knight.c3p

    You can create similar patterns for the other pieces. For pieces like rooks and bishops, you'll also need to use LOS to check for obstacles in their path.

    That should cover the basic movement rules, but you'll still need to handle the rest of the mechanics: capturing, castling, check and checkmate detection etc.

    And if you plan to add AI, the difficulty goes up dramatically. I don't think that's realistically achievable with pure events in C3.

  • I mean chess is kind of like any other game. It’s made up of many simpler steps which can be broken up into even simpler steps.

    Generally I think you’d start with how you want to move the pieces. I find moving them with drag and drop sounds pretty intuitive. When you drop you’d snap it to the nearest square. To make it abide by rules you can instead mark all possible moves of that piece and only let you drop on those spots. Just need to be able to look at squares around the piece and see if they are empty or occupied.

    Consider a pawn. One move would be moving up one if the space is free, or move up two if there two spaces ahead are empty. Then for attacks it can go diagonal left or right if there’s an enemy in those squares. It’s pretty straightforward, you just break it down for each piece’s moves.

    The way you implement that can vary a lot. Since it’s a 8x8 grid an array works well but you’d also need to maintain a visual version of that. Using a tilemap mixes array with a visual but you are limited to positions at grid positions. Finally just using sprites for everything gives more position options but you’d have to deal with collisions or picking to inspect squares. Plus you will have to deal with the case of picking two instances of the same type at once I suppose. Anyways, you have options and there are pros and cons of each.

    To see if the king is in check one thing you could do is loop over all the enemy pieces and mark the squares they can attack. If your king is on any of those it’s in check. You can use that to limit where the king moves to so it’s not in check too. But to check if moving another piece will put the king in check you’d just have run the test after the move. And undo it if need be.

    For ai it’s useful to be able to make copies of the game board and try out different moves. So that can add another level of complexity. And as far as how you’d make the ai work it could be as simple as picking random pieces and moves or something more clever and involved. It’s a broad topic and seems involved to make one that isn’t easy to beat.

    Could be educational to try to implement tic-tac-toe, checkers or fox and geese first to get the feel of making a game like chess.

    Beginners tend to write logic like that in a long way. It can be correct but can be tedious to write and refactor. The more you do the more you can use shortcuts like loops, functions and such to reuse code so things aren’t so repetitive.

    Anyways. Interesting idea if you want to make alternate rules. But since it’s intended as a sub game I’d try to find ways to cut as many corners as possible to reduce the time you’d spend on it.

    EDIT:

    Every approach will be a bit different but here is one wip idea how to go about it with a few pieces implemented.

    dropbox.com/scl/fi/260ne0mq2ponctl8rmm6g/chess_wip.capx

    Here's another old one from 2014. It's always interesting to see different approaches. Here it looks like I used arrays more and got it to detect if the kings were in check, but I don't find it very readable.

    dropbox.com/scl/fi/wzw1iy1ng75ijdl26pfl3/chess.capx

Jump to:
Active Users
There are 0 visitors browsing this topic (0 users and 0 guests)