user17465: 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.

Game Loop (start move)
is in checkmate?

yes


lose 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





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

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.

17465: <p>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).</p>

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

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

<p>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.</p>

<p><a href="https://i.stack.imgur.com/NejIH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NejIH.png" alt="flowchart/pictorial of chess game loop"></a></p>

<h1>Game Loop (start move)</h1>

<h3>is in checkmate?</h3>

<ul>
<li>yes

<ul>
<li>lose game</li>
</ul></li>
<li>no

<ul>
<li>threefold repetition?

<ul>
<li>yes

<ul>
<li>stalemate</li>
</ul></li>
<li>no

<ul>
<li>pawn moved in past 75 moves?

<ul>
<li>yes

<ul>
<li>stalemate</li>
</ul></li>
<li>no

<ul>
<li>has captured in past 75 moves?

<ul>
<li>yes

<ul>
<li>stalemate</li>
</ul></li>
<li>no

<ul>
<li>Get all available pieces for current player

<ul>
<li>iterate all pieces

<ul>
<li>check pawn moves</li>
<li>check knight moves</li>
<li>check bishop moves</li>
<li>check castle moves</li>
<li>check queen moves</li>
<li>check king moves

<ul>
<li>change player</li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
</ul>

<h1>Chess Moves</h1>

<h2>Pawn Move</h2>

<h3>was last move a pawn?</h3>

<ul>
<li>yes

<ul>
<li>was it a double move?

<ul>
<li>yes

<ul>
<li>choice

<ul>
<li>capture en passant</li>
<li>other choice</li>
</ul></li>
</ul></li>
<li>no

<ul>
<li>other choice</li>
</ul></li>
</ul></li>
</ul></li>
<li>no

<ul>
<li>is first move?

<ul>
<li>yes

<ul>
<li>check diagonals for enemy?

<ul>
<li>yes

<ul>
<li>choice

<ul>
<li>would kill enemy put in check?

<ul>
<li>yes

<ul>
<li>check other pieces to move</li>
</ul></li>
<li>no

<ul>
<li>kill enemy

<ul>
<li>is on opposite side of board?

<ul>
<li>yes

<ul>
<li>promote</li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
<li>other choice</li>
</ul></li>
</ul></li>
<li>no

<ul>
<li>forward 1 empty?

<ul>
<li>yes

<ul>
<li>forward 2 empty?

<ul>
<li>yes

<ul>
<li>choice

<ul>
<li>would forward 1 put in check?

<ul>
<li>no

<ul>
<li>forward 1

<ul>
<li>is on opposite side of board?

<ul>
<li>yes

<ul>
<li>promote</li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
<li>yes

<ul>
<li>other move</li>
</ul></li>
</ul></li>
<li>would forward 2 put in check?

<ul>
<li>no

<ul>
<li>forward 2

<ul>
<li>is on opposite side of board?

<ul>
<li>yes

<ul>
<li>promote</li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
<li>yes

<ul>
<li>other move</li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
<li>no

<ul>
<li>would forward 1 put in check?

<ul>
<li>forward 1

<ul>
<li>is on opposite side of board?

<ul>
<li>yes

<ul>
<li>promote</li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
<li>no

<ul>
<li>check other pieces to move</li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
<li>no

<ul>
<li>check diagonals for enemy?

<ul>
<li>yes

<ul>
<li>choice

<ul>
<li>would kill put in check?

<ul>
<li>yes

<ul>
<li>other choice</li>
</ul></li>
<li>no

<ul>
<li>kill enemy

<ul>
<li>is on opposite side of board?

<ul>
<li>yes

<ul>
<li>promote</li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
<li>other choice</li>
</ul></li>
</ul></li>
<li>no

<ul>
<li>forward 1 empty?

<ul>
<li>yes

<ul>
<li>forward 1

<ul>
<li>is on opposite side of board?

<ul>
<li>yes

<ul>
<li>promote</li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
<li>no

<ul>
<li>check other pieces to move

<h2>Knight Move</h2></li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
</ul>

<h3>forward 2 left  1 empty?</h3>

<h3>forward 2 right 1 empty?</h3>

<h3>right 2 up 1 empty?</h3>

<h3>right 2 down 1 empty?</h3>

<ul>
<li>would move/kill put in check?

<ul>
<li>yes

<ul>
<li>if checked all moves check next piece</li>
</ul></li>
<li>no

<ul>
<li>move to available spot

<h3>back 2 left 1 empty?</h3></li>
</ul></li>
</ul></li>
</ul>

<h3>back 2 right 1 empty?</h3>

<h3>left 2 up down 1 empty?</h3>

<h3>left 2 down 1 empty?</h3>

<h2>Bishop Move</h2>

<h3>check all diagonal spaces stopping if there is a piece in its way</h3>

<ul>
<li>does move put in check?

<ul>
<li>yes

<ul>
<li>check other moves else check other pieces</li>
</ul></li>
<li>no

<ul>
<li>move to available spot

<h2>Queen Move</h2></li>
</ul></li>
</ul></li>
</ul>

<p>same as bishop except all cardinal and intercardinal directions.</p>

<h2>Castle Move</h2>

<h3>choice</h3>

<ul>
<li>check all cardinal spaces stopping if there is a piece in its way

<ul>
<li>does move put in check?

<ul>
<li>yes

<ul>
<li>check other moves else check other choice</li>
</ul></li>
<li>no

<ul>
<li>move to available spot</li>
</ul></li>
</ul></li>
</ul></li>
<li>has moved?

<ul>
<li>yes

<ul>
<li>check other pieces</li>
</ul></li>
<li>no

<ul>
<li>has king moved?

<ul>
<li>yes

<ul>
<li>check other pieces</li>
</ul></li>
<li>no

<ul>
<li>is queenside castle?

<ul>
<li>yes

<ul>
<li>is in check?

<ul>
<li>yes

<ul>
<li>check other pieces</li>
</ul></li>
<li>no

<ul>
<li>king 1 to queenside castle in check?

<ul>
<li>yes

<ul>
<li>check other pieces</li>
</ul></li>
<li>no

<ul>
<li>king 2 to queenside castle in check?

<ul>
<li>yes

<ul>
<li>check other pieces</li>
</ul></li>
<li>no

<ul>
<li>is adjacent spot towards king free?

<ul>
<li>yes

<ul>
<li>would put in check?

<ul>
<li>yes

<ul>
<li>check other pieces</li>
</ul></li>
<li>no

<ul>
<li>move king 2 towards  queenside castle,  jump castle over king</li>
</ul></li>
</ul></li>
</ul></li>
<li>no

<ul>
<li>check other pieces</li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
<li>no

<ul>
<li>is in check?

<ul>
<li>yes

<ul>
<li>check other pieces</li>
</ul></li>
<li>no

<ul>
<li>king 1 to kingside castle in check?

<ul>
<li>yes

<ul>
<li>check other pieces</li>
</ul></li>
<li>no

<ul>
<li>king 2 to kingsidecastle in check?

<ul>
<li>yes

<ul>
<li>check other pieces</li>
</ul></li>
<li>no

<ul>
<li>move king 2 towards kingside castle,  jump castle over king

<h2>King Move</h2></li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
</ul></li>
</ul>

<h3>choice</h3>

<ul>
<li>has moved?

<ul>
<li>yes

<ul>
<li>can castle? (Note: for the sake of non duplication see arrow)</li>
</ul></li>
<li>no

<ul>
<li>check other pieces</li>
</ul></li>
</ul></li>
<li>check all 1 move in cardinal and intercardinal spaces

<ul>
<li>does move put in check?

<ul>
<li>yes

<ul>
<li>check other moves else check other choice</li>
</ul></li>
<li>no

<ul>
<li>move to available spot</li>
</ul></li>
</ul></li>
</ul></li>
</ul>
 825: <p>I don't think it's going to work. Consider this fragment:</p>

<p><a href="https://i.stack.imgur.com/q5125.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/q5125.png" alt="enter image description here"></a></p>

<p>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?</p>

<p>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.</p>

<p>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.</p>

<p>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.</p>
