diff --git a/.gitignore b/.gitignore index ddc0a5b..9d1b952 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ -*.db +secret.nim +nim.cfg bin htmldocs -pgn -secret.nim +ressources diff --git a/README.md b/README.md index 145d1b6..f5bcdd5 100644 --- a/README.md +++ b/README.md @@ -4,17 +4,44 @@ ychess is a chess implementation and engine written in nim. -## Quick Setup +## Usage and contribution + +For the following subsections to work properly you need to have `nim.cfg` files +so you don't need to pass all compiler arguments manually. +To automatically create the config files run `nim c -r createCfg.nim` from the +root directory of the project. + +### Todos + +[TODO.md](./TODO.md) contains a list of features that are planned. + +### Command line To play chess in the commandline simply download the code (or clone the repository) and run `nim c -r game.nim`. You can either play the 1v1 hotseat mode or a single player mode vs the engine. -Additionally ychess uses the lichess api to make playing more convenient. +### Lichess + +ychess uses the lichess api to make playing more convenient. An instance of the engine occasionally plays on [lichess](https://lichess.org/@/tiyn-ychess). To get into the whitelist just write a ingame message to the account. +If you want to create an instance on lichess yourself you need to set a api +token. +This is done in `src/engine/secret.nim`. +It should have the following structure: + +```nim +let api_token* = "" +``` + +Following that you will want to set your username into the whitelist in +`src/engine/lichessBridge.nim`. +After that you can start the lichess bot by running +`nim c -r src/engine/lichessBridge.nim`. + ## Project Structure - `art` - contains pictures and arts not used in the code. @@ -22,6 +49,8 @@ To get into the whitelist just write a ingame message to the account. be created if you compile a program. - `htmldocs` - is not pushed to the git repository but contains all automatically generated documentation. +- `ressources` - is not pushed to the git repository but contains all +the data used in source code but not being source code itself (e.g. databases). - `src` - is the root folder for all programs except tests. - `tests` - contains all tests. @@ -32,11 +61,12 @@ For this reason it is not saved in this repository. To extract it into html (assuming you want the documentation for `game.nim`) run `nim doc --project --index:on --outdir:htmldocs game.nim`. -## General Design Choices +## Additional Documentation ### Moves -Moves are read from the commandline as [pure coordinate notation](https://www.chessprogramming.org/Algebraic_Chess_Notation#Pure_coordinate_notation). +Moves are read from the commandline as +[pure coordinate notation](https://www.chessprogramming.org/Algebraic_Chess_Notation#Pure_coordinate_notation). The inner program will convert this notation to a move-tuple. ### Board Representation @@ -53,31 +83,40 @@ For the evaluation function each piece has a corresponding value. Additionally [piece-square tables](https://www.chessprogramming.org/Piece-Square_Tables) are used. -## Contributing +### Code Style Guide -### Setup +Make sure to take a look at the +[official nim style guide](https://nim-lang.org/docs/nep1.html). +All conventions should be applied to this code. -To setup the project for development you need to create the file -`src/secret.nim`. -It should contain values for the following variables: +Additionally there are a bunch of steps to make the code more consistent. -```nim -let projectdir* = "" -let api_token* = "" -``` +#### Constants -### Code Style Guide +Constants should start with an uppercase letter but shouldn't be written in all +caps (`const FooBar = 2`). + +#### Whitespaces Basic arithmetic operations should be surrounded by spaces for example: `1 + 3`. This however is not true for negation of a single value (`-1`) or if the arithmetic operation is done inside array brackets or in iterators (`a+1..3`, `a[c+3]`). +#### Function calls + Determining the length of a string, array, etc should not be done via a function (`len(array)`) but by appending it like `array.len`. +In the same style function calls should be done (`chess.convertToFen()` instead +of `convertToFen(chess)`). +This however is not true if the function takes a first argument that is not an +abstract datatype like the joinPath function (all the parameters are strings). + +#### booleans and logic If statements should not contain outer brackets. In some cases (especially concatenations of `and` and `or`) inner brackets are useful to increase readability in complexer logic formulas. -When assigning booleans with logical formulas outer brackets are expected. +When assigning booleans with logical formulas outer brackets are expected +(`var boolVar = (1 == 1)`). diff --git a/TODO.md b/TODO.md index a54584b..afb2cbf 100644 --- a/TODO.md +++ b/TODO.md @@ -4,5 +4,4 @@ This is a list of features that are to be implemented. - Opening Book - Endgame Table -- Installation script for easy setup of the repo - Docker integration diff --git a/createCfg.nim b/createCfg.nim new file mode 100644 index 0000000..d486c5d --- /dev/null +++ b/createCfg.nim @@ -0,0 +1,13 @@ +import os + +import src/project + +let projDir = getProjRootDir() +var tmpPath: string + +for dir in ["src", "tests"]: + tmpPath = joinPath(projDir, dir) + var f = open(joinPath(tmpPath, "nim.cfg"), fmWrite) + f.writeLine("--outdir:\"" & joinPath(projDir, "bin", dir) & "\"") + f.writeLine("--path:\"" & joinPath(projDir, "src") & "\"") + f.close() diff --git a/src/engine/lichessBridge.nim b/src/engine/lichessBridge.nim index 689ad05..acec904 100644 --- a/src/engine/lichessBridge.nim +++ b/src/engine/lichessBridge.nim @@ -2,7 +2,7 @@ import nimpy import asyncnet, asyncdispatch import chess -import secret +import engine/secret import engine/engine let berserk = pyImport("berserk") diff --git a/src/engine/nim.cfg b/src/engine/nim.cfg deleted file mode 100644 index f944151..0000000 --- a/src/engine/nim.cfg +++ /dev/null @@ -1 +0,0 @@ ---outdir:"../../bin/src" diff --git a/src/engine/openingBook.nim b/src/engine/openingBook.nim index 5a39d91..5625e31 100644 --- a/src/engine/openingBook.nim +++ b/src/engine/openingBook.nim @@ -3,10 +3,12 @@ import sequtils import strutils import sugar import tables +import os include chess import secret +import project type BookMove* = object @@ -20,7 +22,7 @@ type rating*: int # `rating` is the average rating of the player to move. -let dbConn = projectdir & "src/engine/openings.db" +let dbConn = joinPath(getProjRootDir(), "ressources/openings.db") let dbUser = "" let dbPasswd = "" let dbName = "" diff --git a/src/nim.cfg b/src/nim.cfg deleted file mode 100644 index 5f03f93..0000000 --- a/src/nim.cfg +++ /dev/null @@ -1,2 +0,0 @@ ---outdir:"../bin/src" ---path:"." diff --git a/src/project.nim b/src/project.nim new file mode 100644 index 0000000..0f7a8dc --- /dev/null +++ b/src/project.nim @@ -0,0 +1,15 @@ +import os + +proc getProjRootDir*(): string = + ## Returns the root directory for the ychess project. + var maxDepth = 4 + var path = os.getCurrentDir() + var projectDir: string + while maxDepth > 0: + if dirExists(joinPath(path, "src")): + projectDir = path + break + else: + path = parentDir(path) + maxDepth -= 1 + return projectDir diff --git a/tests/nim.cfg b/tests/nim.cfg deleted file mode 100644 index f7f73d6..0000000 --- a/tests/nim.cfg +++ /dev/null @@ -1,2 +0,0 @@ ---path:"../src/" ---outdir:"../bin/tests/"