Answers.Body: <p>You can do this (the space is, after all, finite) but it ends up being tedious, and is probably inferior to just looking it up in a tablebase. </p>
  
  <p>Let's start with one of the most basic concepts in king and pawn endgames: the pawn square. White generally wins if Black is too far away to enter the square. We just need to convert this rule into simpler rules, and then convert those into your mathematical scheme.</p>
  
  <p>If, with White to move, the White pawn is farther advanced than the Black king, White wins. If, with Black to move, the White pawn is 2 ranks farther advanced than the Black king, White wins.<br>
  So, if <code>floor(y/8) + w &lt; floor(z/8)</code> then <code>f(x,y,z,w} = 1</code>.  </p>
  
  <p>If, with White to move, there are more files between the Black king and the pawn than there are ranks between the White pawn and the 8th rank, White wins. If, with Black to move, there are 2 more files between the Black king and the pawn than there are ranks between the White pawn and the 8th rank, White wins.<br>
  So, if <code>abs((y mod 8)-(z mod 8)) + w &lt; (8-floor(z/8))</code> then <code>f(x,y,z,w} = 1</code>.</p>
  
  <p>But we aren't done yet. If the pawn is on the second rank, it can go an extra square on its first move. But if Black has the move and is next to the pawn, he might be able to capture the pawn right away before it can move those 2 squares.<br>
  So, if <code>z</code> is in {9...16}, and <code>w = 0</code> or <code>(abs(z/8 - y/8 &gt; 1)</code> or <code>abs((z mod 8) - (y mod 8) &gt; 1)</code>, then if  <code>((y mod 8)-(z mod 8)) + w+1 &lt; (8-floor(z/8))</code> or <code>floor(y/8)+w+1 &lt; floor(z/8)</code> then <code>f(x,y,z,w} = 1</code>.</p>
  
  <p>But we still aren't done. There's an exception to the pawn square rule. Here, Black is outside the square and cannot enter it, but the game is drawn because of stalemate:</p>
  
  <pre><code>[FEN "K7/P7/3k4/8/8/8/8/8 b - - 0 1"]  
  
  1... Kc7
  </code></pre>
  
  <p>So now, to be complete, we'd have to exclude the situation where the White king is in the corner, blocking his own pawn, and Black can trap him in that corner. This function is getting very convoluted already, and that's just one of the most basic concepts.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-07-21T11:26:04.267Z
Answers.Text: This is  a great answer!! Thank you so much for your time and effort!
Answers.UserDisplayName: Zuriel
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-07-21T02:21:09.107Z
Answers.LastActivityDate: 2019-07-21T02:21:09.107Z
Answers.UserDisplayName: D M
Body: <p>I am aware of questions such as <a href="https://chess.stackexchange.com/questions/6048/is-there-an-easy-way-to-detect-a-draw-in-a-king-pawn-vs-king-endgame">this one</a> which discussed a method of determining if a king pawn vs king endgame is a draw or not. The accepted answer gave detailed guidelines but is not mathematically strict. I am asking the question again, but from a different angle.</p>
  
  <p>If we label all the squares of the board as 1, 2, ..., 64 and we will use three numbers to denote the position of white king, black king, and white pawn, respectively. Let f be a function whose domain is a subset of {1,...,64}×{1,...,64}×{9,...,56}×{0,1} and whose range is {1/2,1}. Here × denotes the Cartesian product and the third set is {9,...,56} instead of {1,...,64} as it denotes the position of the white pawn. The fourth set {0,1} denotes whose turn it is; 0 for white and 1 for black. For the range, 1/2 means draw and 1 means win for white (assuming both sides only play the best moves possible). The actually domain is a subset of {1,...,64}×{1,...,64}×{9,...,56}×{0,1} instead of the entire set since no two or more pieces can occupy the same square and two kings cannot be adjacent to each other. </p>
  
  <p>With the above definition, f is a function whose domain contains fewer than 64×64×48×2=393216 elements and whose range contains 2 elements. So it is possible (though may be difficult) to express f explicitly. </p>
  
  <p>My question is: How to mathematically determine the output of the function f, given any input and therefore determine if a king and pawn vs king endgame is a draw or not?</p>
  
  <p>In other words, I am looking for a way to find the value of f(x,y,z,w), where x denotes the position of the white king, y for the black king, z for the white pawn, w for whose turn it is and the output of f can only be 1/2 and 1.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-07-21T09:22:11.047Z
Comments.Text: Many chess engines implement such a function in their evaluation, but as a precomputed bitbase (only a few KB, even less after exploiting board symmetries) rather than doing so mathematically.
Comments.UserDisplayName: konsolas
Tags: <endgame><mathematics><pawn-endgame>
Title: How to mathematically determine if king and pawn versus king endgame is a draw or a win?
ThreadText: # How to mathematically determine if king and pawn versus king endgame is a draw or a win?
  
  (Asked by: username_0 on 2019-07-20)
  
  I am aware of questions such as [this one](https://chess.stackexchange.com/questions/6048/is-there-an-easy-way-to-detect-a-draw-in-a-king-pawn-vs-king-endgame) which discussed a method of determining if a king pawn vs king endgame is a draw or not. The accepted answer gave detailed guidelines but is not mathematically strict. I am asking the question again, but from a different angle.
  
  If we label all the squares of the board as 1, 2, ..., 64 and we will use three numbers to denote the position of white king, black king, and white pawn, respectively. Let f be a function whose domain is a subset of {1,...,64}×{1,...,64}×{9,...,56}×{0,1} and whose range is {1/2,1}. Here × denotes the Cartesian product and the third set is {9,...,56} instead of {1,...,64} as it denotes the position of the white pawn. The fourth set {0,1} denotes whose turn it is; 0 for white and 1 for black. For the range, 1/2 means draw and 1 means win for white (assuming both sides only play the best moves possible). The actually domain is a subset of {1,...,64}×{1,...,64}×{9,...,56}×{0,1} instead of the entire set since no two or more pieces can occupy the same square and two kings cannot be adjacent to each other.
  
  With the above definition, f is a function whose domain contains fewer than 64×64×48×2=393216 elements and whose range contains 2 elements. So it is possible (though may be difficult) to express f explicitly.
  
  My question is: How to mathematically determine the output of the function f, given any input and therefore determine if a king and pawn vs king endgame is a draw or not?
  
  In other words, I am looking for a way to find the value of f(x,y,z,w), where x denotes the position of the white king, y for the black king, z for the white pawn, w for whose turn it is and the output of f can only be 1/2 and 1.
  
  **Tags:** endgame, mathematics, pawn-endgame
  
  > **Comments:**
  > **username_1**: Many chess engines implement such a function in their evaluation, but as a precomputed bitbase (only a few KB, even less after exploiting board symmetries) rather than doing so mathematically.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2019-07-21. Score: 5
  
  You can do this (the space is, after all, finite) but it ends up being tedious, and is probably inferior to just looking it up in a tablebase.
  
  Let's start with one of the most basic concepts in king and pawn endgames: the pawn square. White generally wins if Black is too far away to enter the square. We just need to convert this rule into simpler rules, and then convert those into your mathematical scheme.
  
  If, with White to move, the White pawn is farther advanced than the Black king, White wins. If, with Black to move, the White pawn is 2 ranks farther advanced than the Black king, White wins.  
  So, if `floor(y/8) + w < floor(z/8)` then `f(x,y,z,w} = 1`.
  
  If, with White to move, there are more files between the Black king and the pawn than there are ranks between the White pawn and the 8th rank, White wins. If, with Black to move, there are 2 more files between the Black king and the pawn than there are ranks between the White pawn and the 8th rank, White wins.  
  So, if `abs((y mod 8)-(z mod 8)) + w < (8-floor(z/8))` then `f(x,y,z,w} = 1`.
  
  But we aren't done yet. If the pawn is on the second rank, it can go an extra square on its first move. But if Black has the move and is next to the pawn, he might be able to capture the pawn right away before it can move those 2 squares.  
  So, if `z` is in {9...16}, and `w = 0` or `(abs(z/8 - y/8 > 1)` or `abs((z mod 8) - (y mod 8) > 1)`, then if `((y mod 8)-(z mod 8)) + w+1 < (8-floor(z/8))` or `floor(y/8)+w+1 < floor(z/8)` then `f(x,y,z,w} = 1`.
  
  But we still aren't done. There's an exception to the pawn square rule. Here, Black is outside the square and cannot enter it, but the game is drawn because of stalemate:
  
  ```
  [FEN "K7/P7/3k4/8/8/8/8/8 b - - 0 1"]  
  
  1... Kc7
  
  ```
  
  So now, to be complete, we'd have to exclude the situation where the White king is in the corner, blocking his own pawn, and Black can trap him in that corner. This function is getting very convoluted already, and that's just one of the most basic concepts.
  
  > **Comments:**
  > **username_0**: This is  a great answer!! Thank you so much for your time and effort!  
  
  ---
  
  