GithubHelp home page GithubHelp logo

marcusbuffett / command-line-chess Goto Github PK

View Code? Open in Web Editor NEW
487.0 487.0 155.0 221 KB

A python program to play chess against an AI in the terminal.

License: MIT License

Python 100.00%
chess hacktoberfest python

command-line-chess's People

Contributors

aaaandrzej avatar aitorres avatar clasherkasten avatar ddugovic avatar djhenderson avatar gauthambellamkonda avatar grbavacigla avatar innesgarmany avatar jeromew21 avatar johndoknjas avatar marcusbuffett avatar mohebmithani avatar pawelszulczewski avatar programmin1 avatar scientia-ac-labore avatar ste-t avatar stjet avatar timgates42 avatar wobot1340 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

command-line-chess's Issues

Multiplayer capabilities

I think that it would be really good if this Chess app had multiplayer capabilities so that one could play with someone else who has this app, or it could even just be people on the same network with the app installed, but whatever it is, some sort of multiplayer capabilities would be good.

Error Drawing Pieces

I just installed the game for the first time (5/5/23), and when I start a new game, it never draws the pieces in row 8, i.e. black's non-pawn pieces. It also never draws those pieces as the game continues. Note, I am on Windows 11 currently.
image_2023-05-05_155738231

Replace termcolor

In the process of adding the new CLI, @GauthamBellamkonda noticed that termcolor has a very limited color set which doesn't provide as much flexibility as we want to provide.
To fix this, we need to find another library which provides the following abilities:

  • colores the output (foreground and background)
  • accepts hex colors as input
  • (eventually shoud have a core set of named colors (E.g. "white", "black", "blue", ...))

Make `--unicode`behaviour the standard

Right now the CLI provides an option named --unicode or -u which uses unicode characters to display the pieces.
If the option isn't used, then it falls back to "normal", non-unicode characters.
In the process of switching out termcolor to colored We noticed that it is really hard to display a colored board with non-unicode characters.

To fix this, I decided to remove the non-unicode character support and make the --unicode behaviour standard.
ToDo:

  • remove --unicode option as it is the default
  • remove all the code that don't need to exist anymore (E.g. the makeStringRep method)

Git Clone Problem?

When I run it like !pip install cl-chess it works fine but when instead I use:
git clone https://github.com/marcusbuffett/command-line-chess
like you suggested (so it has the most recent version), it shows a different graphical version of the chess board.
This is how it looks with pip install
Screenshot 2023-04-18 at 07-57-10 Google Colaboratory
and this is git clone:
Screenshot 2023-04-18 at 07-56-53 Google Colaboratory

I only play chess IRL, not online, so maybe I am not used to how it works, but the git clone way only shows me what the AI is doing, not my own moves.

I should note that I am using Google Colab, so maybe somehow that is changing it. In case you want to try it, the Google Colab code is:
!git clone https://github.com/marcusbuffett/command-line-chess
%cd /content/command-line-chess
!pip install .
!chess

Cleanup or something

This is more of a collecting tank for some "little" improvements.

Some of these are:

  • Fix flake8 errors
  • removing dead code
  • eventually we find some slow code and can fix it? (AI is a separate topic)
  • change things to follow PEP8
  • Making Coordinate and abstract class

Seeing move list?

Is it possible to print the list of moves already made while playing the game?

Google Colab Problem?

I know you don't officially support Google Colab, but I am not sure if this is a problem only with Colab or not (I don't run Python locally so I have not tried it without Colab). In Colab when I install via git clone like:
!pip install git+https://github.com/marcusbuffett/command-line-chess
and run it like:
!chess --white="red"
it does not correctly show the position of the pieces that were moved. It looks like this:
Screenshot 2023-04-18 at 08-43-53 Google Colaboratory
If I instead use your old version by installing it like:
!pip install cl-chess
it works fine, see below:
Screenshot 2023-04-18 at 08-47-31 Google Colaboratory

Castling maybe not working

Just as an example, here is the board, player is white. As you can see, castling is not in the list of possible moves. There is a Board.getShortNotationOfMove() that should return '0-0-0' and '0-0', but move.kingsideCastle and move.queensideCastle are not getting recognized at that point.

It's your move. Type '?' for options. ? 0-0
command: 0-0
move None
Couldn't parse input, enter a valid command or move.
   a b c d e f g h   

8  R N B Q K B N R  8
7  x p x p p p x p  7
6  p x x x x x x x  6
5  x x p x x x p x  5
4  x x B x p x x x  4
3  x x x x x N x x  3
2  p p p p x p p p  2
1  R N B Q K x x R  1

   a b c d e f g h

Currently, the point difference is : 0
It's your move. Type '?' for options. ? o-o
command: o-o
move None
Couldn't parse input, enter a valid command or move.
   a b c d e f g h   

8  R N B Q K B N R  8
7  x p x p p p x p  7
6  p x x x x x x x  6
5  x x p x x x p x  5
4  x x B x p x x x  4
3  x x x x x N x x  3
2  p p p p x p p p  2
1  R N B Q K x x R  1

   a b c d e f g h

Currently, the point difference is : 0
It's your move. Type '?' for options. ? O-O
command: o-o
move None
Couldn't parse input, enter a valid command or move.
   a b c d e f g h   

8  R N B Q K B N R  8
7  x p x p p p x p  7
6  p x x x x x x x  6
5  x x p x x x p x  5
4  x x B x p x x x  4
3  x x x x x N x x  3
2  p p p p x p p p  2
1  R N B Q K x x R  1

   a b c d e f g h

Currently, the point difference is : 0
It's your move. Type '?' for options. ? 0-0-0
command: 0-0-0
move None
Couldn't parse input, enter a valid command or move.
   a b c d e f g h   

8  R N B Q K B N R  8
7  x p x p p p x p  7
6  p x x x x x x x  6
5  x x p x x x p x  5
4  x x B x p x x x  4
3  x x x x x N x x  3
2  p p p p x p p p  2
1  R N B Q K x x R  1

   a b c d e f g h

Currently, the point difference is : 0
It's your move. Type '?' for options. ? o-o-o
command: o-o-o
move None
Couldn't parse input, enter a valid command or move.
   a b c d e f g h   

8  R N B Q K B N R  8
7  x p x p p p x p  7
6  p x x x x x x x  6
5  x x p x x x p x  5
4  x x B x p x x x  4
3  x x x x x N x x  3
2  p p p p x p p p  2
1  R N B Q K x x R  1

   a b c d e f g h

Currently, the point difference is : 0
It's your move. Type '?' for options. ? O-O-O
command: o-o-o
move None
Couldn't parse input, enter a valid command or move.
   a b c d e f g h   

8  R N B Q K B N R  8
7  x p x p p p x p  7
6  p x x x x x x x  6
5  x x p x x x p x  5
4  x x B x p x x x  4
3  x x x x x N x x  3
2  p p p p x p p p  2
1  R N B Q K x x R  1

   a b c d e f g h

Currently, the point difference is : 0
It's your move. Type '?' for options. ? l
command: l
a3
a4
b3
b4
c3
d3
d4
e5
g3
g4
h3
h4
Nc3
Na3
Qe2
Ke2
Kf1
Bd5
Be6
Bxf7
Bd3
Be2
Bf1
Bb5
Bxa6
Bb3
Nh4
Nd4
Nxg5
Ng1
Ne5
Rg1
Rf1
   a b c d e f g h   

8  R N B Q K B N R  8
7  x p x p p p x p  7
6  p x x x x x x x  6
5  x x p x x x p x  5
4  x x B x p x x x  4
3  x x x x x N x x  3
2  p p p p x p p p  2
1  R N B Q K x x R  1

   a b c d e f g h

Currently, the point difference is : 0
It's your move. Type '?' for options. ?

Faster AI possible?

For the information how the AI works right now, please look at this article.

It would be kinda interesting to research if there are other (faster, more efficient) ways.
And if there are such ways, then we could implement them.

For question or answers to this task, just leave a comment or create a new issue. If there are any other questions or so, please open an issue.

'NoneType' object has no attribute 'board'

Currently, the point difference is : 3
It's your move, what do you want to do? Type '?' for options. dxe7

Making move : dxe7
R N x K Q B x R
p p x B p p p p
x x p x x x x x
x x x x x x x x
x x x p x x x x
x x x x x p x x
p p p x x x p R
R N B K Q B N x

AI thinking...
Traceback (most recent call last):
File "main.py", line 122, in
startGame(board, playerSide, opponentAI)
File "main.py", line 113, in startGame
move = ai.getBestMove()
File "/tmp/command-line-chess-master/AI.py", line 123, in getBestMove
moveTree = self.generateMoveTree()
File "/tmp/command-line-chess-master/AI.py", line 78, in generateMoveTree
self.populateNodeChildren(node)
File "/tmp/command-line-chess-master/AI.py", line 91, in populateNodeChildren
legalMoves = self.board.getAllMovesLegal(side)
File "/tmp/command-line-chess-master/Board.py", line 397, in getAllMovesLegal
if self.moveIsLegal(move) :
File "/tmp/command-line-chess-master/Board.py", line 388, in moveIsLegal
self.undoLastMove()
File "/tmp/command-line-chess-master/Board.py", line 89, in undoLastMove
self.pieces.remove(promotedPiece)
File "/tmp/command-line-chess-master/Piece.py", line 37, in eq
if self.board == other.board and self.side == other.side and self.position == other.position and self.class == other.class:
AttributeError: 'NoneType' object has no attribute 'board'

Configure `mypy` and type annotate the code

The code is untyped, what doesnn't cause problems, but type code would make it easier for people to work with the code.
(This is an feature what would be kinda cool if we provide something like an API in the future to make it more hackable)

Tasks which have to be done to accomplish this would be:

  • setup mypy
  • add type annotations to almost everything

Integrate pre-commit into the worflow

Tasks:

  • Add .pre-commit-config.yaml with the following hooks:
    • end-of-file-fixer
    • trailing-whitespace
    • double-quote-string-fixer
    • debug-statements
    • check-ast
    • mypy
    • flake8
    • autopep8
    • add-trailing-comma
    • pyupgrade
    • reorder-python-imports
  • Run pre-commit run --all-files once
  • Eventually notice it in CONTRIBUTING.md under Pull Request Process

Hooks that are listed can be discussed. When claiming this issue and you want to add additional hooks , please first comment these hooks here so they also can be discussed.

Some ideas

Was looking at the code i'm on a jupyter notebook though.
Could it be converted to it? ,
I've still not cracked the looking ahead for moves..

Though for your code.

  • Maybe an idea, what if all the potential moves was just selected from list(board.legal_moves) ?
  • Bishop/Knight values, how about adjusting it during the game a single bishop is less then a pair
    its not just comparing number off bishops vs knights, their value change slightly. (i'd even prefer 2 knights end games).
  • Not sure if you allready do this
    If you check several moves ahead, one doesnt need to recalculate if a specific move is indeed played, top results can be cashed
    and if so one can expand on previous results, which might even then get a bonus 1 level deeper.
  • Not sure if you take it in the calculation, diagonal pawn walls, might get a higher peace value.

Adding unittests

There are not really tests in this repository which makes it a bit hard to ensure there are no bugs and/or unintentional behavior.

To fix this, unittests should be written.
When this is done, integration tests can be done too.

Right now there is only one (unnecessary) test.
All new tests should be created in a new directory called tests/ and because it is already in use, pytest should be used as test-framework.

problems installing

Hello, i am having problems installing this.

i tried pip3 install cl-chess

and i get the answer:

Could not find any downloads that satisfy the requirement cl-chess
Cleaning up...
No distributions at all found for cl-chess

do you know a solution to this?

thank you :-)

When player chooses *Black*, AI should make the first move as *White*

Bug

$ chess
What side would you like to play as [wB]? B
You will play as black

How deep should the AI look for moves?
Warning : values above 3 will be very slow. [2]? 3

8  R N B Q K B N R
7  ▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲
6  · · · · · · · ·
5  · · · · · · · ·
4  · · · · · · · ·
3  · · · · · · · ·
2  ▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲
1  R N B Q K B N R

   a b c d e f g h

It's your move. Type '?' for options. ?

Here, I chose B (Black) for player's side. The AI should have made the first move as White. But it told me (the player) to make the first move - which is not possible, because the first move is always made by White.

New CLI

Right now the CLI looks like this:

try:
    if len(sys.argv) >= 2 and sys.argv[1] == "--two":
        twoPlayerGame(board)
    else:
        playerSide = askForPlayerSide()
        board.currentSide = playerSide
        print()
        aiDepth = askForDepthOfAI()
        opponentAI = AI(board, not playerSide, aiDepth)
        printBoard(board)
        startGame(board, playerSide, opponentAI)
except KeyboardInterrupt:
    sys.exit()

I think it would be a cool idea to switch to argparse.ArgumentParser which would make it more readable and easier to add new features to it.
A few feature proposals:

  • An option to enable unicode representation
  • Options to set colors (Fixed set of colors / completely variable)

If you want to propose other new features you would like to see, please leave a comment or create an issue.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.