[Re-usable Code] Simple Interactive Chess Engine in Ren'Py

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Post Reply
Message
Author
RLinZ
Newbie
Posts: 17
Joined: Wed Jul 18, 2018 9:34 pm
Contact:

[Re-usable Code] Simple Interactive Chess Engine in Ren'Py

#1 Post by RLinZ »

Updates as of 09/16/2018
Updated a bug fix for building Android distribution, thanks to @Imperf3kt and @PyTom.

Updates as of 07/21/2018

A huge thank-you to those who discovered the problem and please accept my apologies!

Updated attachment:

Fatal bug fixes for Player vs. Computer mode. Minor modifications were made in script.rpy and screens.rpy for a smoother exit from the chess game.

The attached code files and files in my github repo have been updated accordingly, and updates to the instructions have been addressed. Please do download this latest version. My apologies for any inconvenience caused!

------

My first post not counting the guestbook!

So I implemented a simple Chess Game in Ren'Py using Screen Language and creator-defined Displayables. I don't know if anyone has done similar things before but if so, please take this as an alternative version! You are more than welcome to incorporate this into any project of yours!

Image

Gameplay and Operations
Click on a piece and all of its available moves will be highlighted. Click on any of the available destination squares to make a move. Press <- on the keyboard to undo moves.

The default window size is 1280 * 720. The Chess Game supports a choice between Player vs. Self and Player vs. Computer.The current version implements the basic chess rules except for castling, en passant and promotion. In Player vs. Computer, the Player plays White by default and the opponent is a minimally-implemented Chess AI.

Feel free to use the code in any project of your choice, free or commercial. Instructions are as follows:

List of core files for the Chess Game:
  • images/bg chessboard.png - the chess board image
  • images/pieces_image - the chess pieces images
  • chesslogic.py - the rules and logic behind chess
  • chessai.py - the auto-player that evaluates and selects move
  • chessgui.rpy - the Renpy Displayable class and methods
  • screens.rpy - the mini-game screen holding the Displayable
  • script.rpy - the game script that calls the mini-game screen
Copy the image files, chesslogic.py, chessai.py and chessgui.rpy into your game/ directory.
Paste the following code into specified .rpy files.

In screens.rpy
Note that ai_mode is a Boolean, for Player vs. Self, call screen minigame(False) and for Player vs. Computer, call screen minigame(True)

Code: Select all

## This screen is used for the chess game.
screen minigame(ai_mode):
    default chess = ChessDisplayable(chess_ai=ai_mode)
    add "bg chessboard"
    add chess
    if chess.winner:
        timer 6.0 action Return(chess.winner)

In script.rpy (or any script file in which the chess game should occur)
Note the way screen minigame() is called with the variable ai_mode

Code: Select all

label start:
    $ ai_mode = False
    $ winner = None

    "Welcome to the Ren'Py Chess Game!"

    label opponent_selection:
        menu:
            "Please select the game mode."

            "Player vs. Self":
                $ ai_mode = False
            "Player vs. Computer":
                $ ai_mode = True
                # Player plays White by default
                $ player_color = 'White'
                $ computer_color = 'Black'

    window hide

    # Start chess game
    call screen minigame(ai_mode)
    # End chess game
    $ winner = _return

    if ai_mode:
        if winner == player_color:
            "Congratulations! You won!"
        elif winner == computer_color:
            "The computer defeated you. Better luck next time!"
        elif winner == 'draw':
            "The game ended in a draw. See if you can win the next time!"
    else:
        if winner != 'draw':
            "The winner is [winner]! Congratulations!"
        else:
            "The game ended in a draw."
I have included in-text code snippets as well as downloadables at the bottom. You can also find the source code and detailed instructions in my github repo.

And that's it for my first post in Lemma Soft and hope you enjoy it! Thanks for reading this!
Attachments
renpy-chessgame0916.zip
(771.09 KiB) Downloaded 245 times
Last edited by RLinZ on Sat Sep 15, 2018 10:27 pm, edited 7 times in total.

User avatar
Andredron
Miko-Class Veteran
Posts: 700
Joined: Thu Dec 28, 2017 2:37 pm
Location: Russia
Contact:

Re: Simple Chess Engine in Ren'Py

#2 Post by Andredron »

cool done! Thank you for sharing this link.

User avatar
Alex
Lemma-Class Veteran
Posts: 3090
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Simple Chess Engine in Ren'Py

#3 Post by Alex »

Great! Thanks for this...))

RLinZ
Newbie
Posts: 17
Joined: Wed Jul 18, 2018 9:34 pm
Contact:

Re: [Re-usable Code] Simple Interactive Chess Engine in Ren'Py

#4 Post by RLinZ »

Thanks for the replies! I would like to know if people will be interested if I make a tutorial on creator-defined Displayables in Ren'Py based on this project? Any thought is appreciated!

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3785
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: [Re-usable Code] Simple Interactive Chess Engine in Ren'Py

#5 Post by Imperf3kt »

I downloaded the zip file and ran it.
After selecting player vs self, I was greeted to this:
screenshot0018.png
It seems that many moves have already been made?


Actually, it seems to happen every time I try to start a game in either mode, and also when I roll back.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

RLinZ
Newbie
Posts: 17
Joined: Wed Jul 18, 2018 9:34 pm
Contact:

Re: [Re-usable Code] Simple Interactive Chess Engine in Ren'Py

#6 Post by RLinZ »

My sincere apologies and thank you so much for pointing this out! I left some code for testing purpose but forgot to take them out... The code has been properly fixed and please do give it another try if you wish so! Sorry for any inconvenience caused!

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3785
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: [Re-usable Code] Simple Interactive Chess Engine in Ren'Py

#7 Post by Imperf3kt »

Found an issue with how you are using images. (Or rather, PyTom found it)
There is an unnecessary period in lines 215 and 219 of chessgui.rpy which causes issues when building distributions (specifically Android versions)

This should fix it, replace lines 215 - 219 with the following

Code: Select all

                    image_path = 'images/pieces_image/' + SHORT_HAND[black_piece].lower() + '_black.png'
                    image_obj = Image(image_path)
                    self.pieces_image[black_piece] = image_obj
                for white_piece in WHITE_PIECES:
                    image_path = 'images/pieces_image/' + SHORT_HAND[white_piece.upper()].lower() + '_white.png'
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

RLinZ
Newbie
Posts: 17
Joined: Wed Jul 18, 2018 9:34 pm
Contact:

Re: [Re-usable Code] Simple Interactive Chess Engine in Ren'Py

#8 Post by RLinZ »

As always, thanks for the insight! The code has been properly fixed now.
Imperf3kt wrote: Thu Aug 30, 2018 6:57 am Found an issue with how you are using images. (Or rather, PyTom found it)
There is an unnecessary period in lines 215 and 219 of chessgui.rpy which causes issues when building distributions (specifically Android versions)

This should fix it, replace lines 215 - 219 with the following

Code: Select all

                    image_path = 'images/pieces_image/' + SHORT_HAND[black_piece].lower() + '_black.png'
                    image_obj = Image(image_path)
                    self.pieces_image[black_piece] = image_obj
                for white_piece in WHITE_PIECES:
                    image_path = 'images/pieces_image/' + SHORT_HAND[white_piece.upper()].lower() + '_white.png'

User avatar
bonnie_641
Regular
Posts: 133
Joined: Sat Jan 13, 2018 10:57 pm
Projects: Código C.O.C.I.N.A.
Deviantart: rubymoonlily
Contact:

Re: [Re-usable Code] Simple Interactive Chess Engine in Ren'Py

#9 Post by bonnie_641 »

Thank you very much. :D :D :D
I speak and write in Spanish. I use an English-Spanish translator to express myself in this forum. If I make any mistakes, please forgive me.
I try my best to give an answer according to your question. :wink:

total911
Newbie
Posts: 4
Joined: Wed Apr 20, 2016 2:54 am

Re: [Re-usable Code] Simple Interactive Chess Engine in Ren'Py

#10 Post by total911 »

Thank you so much for the game.
I have a question. How can I add sounds?
I need the sounds of the shape selection, the sound of clicking on the board anywhere, the sound when we remove the shape.

Post Reply

Who is online

Users browsing this forum: No registered users