# AI Draw Game

## Customizing AI draw game logic&#x20;

<figure><img src="https://2079102914-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FA7OKfIba5xvQF1lhv60z%2Fuploads%2FOGrag7PxOlW9vHPniYHz%2Fdevenv_btYHpo1x4Z.png?alt=media&#x26;token=2a1ad5a9-729a-4c96-9cb4-cdf3cc2612f3" alt=""><figcaption></figcaption></figure>

There are three ascending levels of draw game functions in ChessGameAI class that are used by AI for decisions such as making draw offer and accepting draw offers. `RequestByFirstToleranceLevel`, `RequestBySecondToleranceLevel` and `RequestByThirdToleranceLevel` functions, all have the common variables as shown on the image above, which can be customized for different draw behaviour. All of the values of these variables are a benchmark to position score.&#x20;

-tooEarly -> When total moves made since the beginning of a chess game never reach beyond this threshold, draw offer will be rejected with [*Too Early Comment*](https://freedom-developer.gitbook.io/the-chess/scenes-details/chessai).

-margin -> The position score from within range 0 to +-margin at which draw offer will directly being rejected. For example, if margin is 100, when AI is white and position score is within 100 or -100 for black, they will never accept draw offer. It will send *Reject Comment* text in this case.

-optimism -> The confidence threshold AI had while the game position score was favoring their opponent by the optimism amount. This is related to *The Optimistic Comment.*

*-*&#x73;urrenderMargin -> If AI' opponent has a leading position score beyond the surrenderMargin, AI will request for a resign offer.

-For *Still Want To Play Comment*,  it is dependent on the pieces count on the board during early game. All three functions has the same condition for this particular case which is no more than 14 pieces in total and no less than 5 pieces for AI color.

<figure><img src="https://2079102914-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FA7OKfIba5xvQF1lhv60z%2Fuploads%2Fjfled6eoJ8X4YX30Qd6G%2Fdevenv_IYAGjdRtNi.png?alt=media&#x26;token=aa02777f-bad6-40d5-8a51-6c671c445254" alt=""><figcaption></figcaption></figure>

`RequestBySecondToleranceLevel` and `RequestByThirdToleranceLevel` functions however, have surrender variable which the value is the position score for endgame when AI side has **no major pieces left** including rook and queen. When the condition is met and the position score is beyond the surrender value for AI' opponent, AI will resign.

## AI level to Their Draw Tolerence Level

<figure><img src="https://2079102914-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FA7OKfIba5xvQF1lhv60z%2Fuploads%2FpYIwd4fynBWti2q0Dks0%2Fdevenv_rs4PBVcld5.png?alt=media&#x26;token=b1ea4b7b-4dc0-4fca-bc83-5a5e8ebf0bf9" alt=""><figcaption></figcaption></figure>

There are already enum prepared to easily assign which functions to call for which AI level. Check out ChessAITypeIIMechanism class and ChessAITypeIMechanism class for their getter `DrawToleranceLevel`. The enum value One indicate `RequestByFirstToleranceLevel` will be used for the AI level, Two being`RequestBySecondToleranceLevel` and Three for `RequestByThirdToleranceLevel`.&#x20;
