2

I am trying to program a chess game as a challenge to myself, however, I am unsure if I have all the moves correct to play a legal game (not any advanced moves, that would be a challenge for another day).

I have created a flowchart/pictorial and text version to depict the flow of the game and the available moves.

The area with a blue background is the main game loop. The area in dark purple are all the possible chess moves.

if there is any part that doesn't make sense or needs clarification please don't hesitate to ask and I will try my best to reword it.

flowchart/pictorial of chess game loop

Game Loop (start move)

is in checkmate?

  • yes
    • loose game
  • no
    • threefold repetition?
      • yes
        • stalemate
      • no
        • pawn moved in past 75 moves?
          • yes
            • stalemate
          • no
            • has captured in past 75 moves?
              • yes
                • stalemate
              • no
                • Get all available pieces for current player
                  • iterate all pieces
                    • check pawn moves
                    • check knight moves
                    • check bishop moves
                    • check castle moves
                    • check queen moves
                    • check king moves
                      • change player

Chess Moves

Pawn Move

was last move a pawn?

  • yes
    • was it a double move?
      • yes
        • choice
          • capture en passant
          • other choice
      • no
        • other choice
  • no
    • is first move?
      • yes
        • check diagonals for enemy?
          • yes
            • choice
              • would kill enemy put in check?
                • yes
                  • check other pieces to move
                • no
                  • kill enemy
                    • is on opposite side of board?
                      • yes
                        • promote
              • other choice
          • no
            • forward 1 empty?
              • yes
                • forward 2 empty?
                  • yes
                    • choice
                      • would forward 1 put in check?
                        • no
                          • forward 1
                            • is on opposite side of board?
                              • yes
                                • promote
                        • yes
                          • other move
                      • would forward 2 put in check?
                        • no
                          • forward 2
                            • is on opposite side of board?
                              • yes
                                • promote
                        • yes
                          • other move
                  • no
                    • would forward 1 put in check?
                      • forward 1
                        • is on opposite side of board?
                          • yes
                            • promote
              • no
                • check other pieces to move
      • no
        • check diagonals for enemy?
          • yes
            • choice
              • would kill put in check?
                • yes
                  • other choice
                • no
                  • kill enemy
                    • is on opposite side of board?
                      • yes
                        • promote
              • other choice
          • no
            • forward 1 empty?
              • yes
                • forward 1
                  • is on opposite side of board?
                    • yes
                      • promote
              • no
                • check other pieces to move

                  Knight Move

forward 2 left 1 empty?

forward 2 right 1 empty?

right 2 up 1 empty?

right 2 down 1 empty?

  • would move/kill put in check?
    • yes
      • if checked all moves check next piece
    • no
      • move to available spot

        back 2 left 1 empty?

back 2 right 1 empty?

left 2 up down 1 empty?

left 2 down 1 empty?

Bishop Move

check all diagonal spaces stopping if there is a piece in its way

  • does move put in check?
    • yes
      • check other moves else check other pieces
    • no
      • move to available spot

        Queen Move

same as bishop except all cardinal and intercardinal directions.

Castle Move

choice

  • check all cardinal spaces stopping if there is a piece in its way
    • does move put in check?
      • yes
        • check other moves else check other choice
      • no
        • move to available spot
  • has moved?
    • yes
      • check other pieces
    • no
      • has king moved?
        • yes
          • check other pieces
        • no
          • is queenside castle?
            • yes
              • is in check?
                • yes
                  • check other pieces
                • no
                  • king 1 to queenside castle in check?
                    • yes
                      • check other pieces
                    • no
                      • king 2 to queenside castle in check?
                        • yes
                          • check other pieces
                        • no
                          • is adjacent spot towards king free?
                            • yes
                              • would put in check?
                                • yes
                                  • check other pieces
                                • no
                                  • move king 2 towards queenside castle, jump castle over king
                            • no
                              • check other pieces
            • no
              • is in check?
                • yes
                  • check other pieces
                • no
                  • king 1 to kingside castle in check?
                    • yes
                      • check other pieces
                    • no
                      • king 2 to kingsidecastle in check?
                        • yes
                          • check other pieces
                        • no
                          • move king 2 towards kingside castle, jump castle over king

                            King Move

choice

  • has moved?
    • yes
      • can castle? (Note: for the sake of non duplication see arrow)
    • no
      • check other pieces
  • check all 1 move in cardinal and intercardinal spaces
    • does move put in check?
      • yes
        • check other moves else check other choice
      • no
        • move to available spot
  • 3
    Can't you present this in text form and edit it into the question? This picture is not exactly user friendly (and on many StackExchange sites unwanted) – user4378 Oct 1 '18 at 11:25
  • 1
    @JanDoggen Sorry about that, I will attempt to put this in text form, however some links may be missing due to relationships which is difficult to show in text form. – Jordan Oct 2 '18 at 19:33
2

I don't think it's going to work. Consider this fragment:

enter image description here

You don't "kill enemy" just because your pawn can do the capture. What if your pawn capture exposes your queen behind? What if your pawn capture leads to back-rank mate?

Furthermore, you're not making yourself clear how to choose between moves. Your flowchart implies your program would be making legal pawn moves before considering anything else. This won't work.

You'll have to do searching, something like alpha-beta/nega-max. Your flowchart is way too simple for a reasonable chess engine, if that's your goal.

There won't be any "challenge" from your flowchart. I estimate the program would play no better than a random player. However, if your aim is just to make something that can play a legal chess game; your flowchart is reasonable. It's also a nice mini-project for practising programming.

  • 1
    Thanks, initially I want am just creating a chess game that is non-AI just to make sure it works correctly. I believe the AI would be much more difficult due to all the different multi turn moves, but that is a problem for another day :) – Jordan Oct 2 '18 at 19:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.