Interested in chess programming? You are interested in philosophy too! What is thinking? Can AI be conscious? What does it mean to do something? Can the AI understand that it does play chess? Explore the AI related articles in Harmonia Philosophica and understand why you are already a philosopher!
In the previous episode we set the foundations for starting a chess program. In summary we…
Discussed the pre-requisites for the tutorial
Created the chessboard
Got the first input from the user
Now it is time to start the design of the chess thinking algorithm.
Step 0: Design the main steps on paper
Before making any serious programming attempt, one has to leave the computer and get a piece of paper. There, he can design the algorithm of the program. A good design is invaluable for a good program. There is no need to start writing code without having that design first!
Chess program algorithm outline
One can find above a rough outline of the major steps involved in a chess program algorithm.
Note that this algorithm is an over-simplification, but it will do for now. For example, the algorithm above does not take into account the fact that the thinking of the computer must be conducted in various depths and not only in the initial position. We will elaborate on that in next episodes; for now let us just try to analyze all possible moves in a position and choose the one that leads to the best score.
Huo Philosopher is a tiny open source program for Commodore. With just 10 lines of code, it is able to conduct clever conversations about philosophy with deep meaning…
Huo Philosopher by Spiros Kakos (huo)
There is a weird but very interesting programming contest happening every year.
It is the BASIC 10Liners contest, where contestants are asked to create programs in BASIC with just 10 lines of code!
You would be amazed on what things can be done with just ten (10) lines of code.
There are three categories for programming a game depending on the length of each line (80, 120 or 256 characters) and an additional fourth category (SCHAU) for programming an application in 10 lines. (check here for the rules of the BASIC 10Liners contest of 2021)
My participation in the SCHAU category was with the “Huo Philosopher v2.0” program.
The program HUOPHIL1 (Huo Philosopher v2.0) is a tiny program by Spyridon Kakos that can make deep philosophical conversations about any available subject. Its deep irrational understanding of the cosmos is based on nothing more than sets of random phrases the program brilliantly combines in order to produce phrases that will make any philosopher shiver from the sheer terror of its non-knowledge of metaphysics.
How the program works
The program uses three sets of arrays that contain…
An initial phrase. (Table X$)
A middle phrase: This is the more complex one and uses three arrays that hold the start, the middle and the ending of the second phrase. (Tables Y$ + Z$ + W$)
An ending phrase. (Table E$)
The program initially initializes the values in the tables and asks for user input. Then it generates random numbers and combines elements from the tables mentioned above to generate the three phrases. A delay between the phrases as they appear on the screen add to the mystery and the tension…
Resources
One can download the program code and the relative documentation from the official entry page for the BASIC 10Liners 2021 here.
Alternatively, one can download the source code of the program and the relative documentation below.
Please contact us for any comments and/ or suggestions.
And remember.
Keep coding!
Keep experimenting!
Appendix I – The source code
The source code is presented below.
10DIMZ$(17):DIMW$(18):DIML$(10):B=1:PRINTCHR$(147):PRINT"HUO PHILOSOPHER V2.0 BY S.KAKOS (2021)":PRINT:INPUT"ACTIVATE NEURAL LEARNING? (Y/N)";N$:PRINT:PRINT"COME, SAY SOMETHING":INPUTA$:X$(1)="INTERESTING, BUT...":X$(2)="YOU NEVER KNOW. YOU SEE..."
20X$(3)="WHY?":X$(6)="SO?":X$(4)="THINK WITHOUT THINKING":X$(5)="ANALYSIS DESTROYS THOUGHT":Y$(1)="GOD":Y$(2)="BEING":Y$(3)="EXISTENCE":Y$(4)="LIFE":Y$(5)="SELF":Y$(6)="DEATH":Y$(7)="REALITY":Y$(8)="CHANGE":Y$(9)="THE UNIVERSE":Y$(10)="KNOWLEDGE"
30Z$(1)=" DEFINES ":Z$(2)=" CREATES ":Z$(3)=" SUSTAINS ":Z$(4)=" TRANSCENDS ":Z$(5)=" DESTROYS ":Z$(6)=" ALLOWS ":Z$(7)=" MANIFESTS THROUGH ":Z$(8)=" OBSCURES ":Z$(9)=" AFFECTS ":Z$(10)=" CONTAINS ":Z$(12)=" SUPPORTS ":Z$(13)=" STEMS FROM "
40Z$(11)=" IS ":Z$(14)=" PRODUCES ":Z$(15)=" LEADS TO ":Z$(16)=" BREEDS ":Z$(17)=" NEEDS ":W$(1)="EXISTENCE":W$(2)="GOD":W$(3)="LIFE":W$(4)="THE SELF":W$(5)="BEING":W$(6)="OTHERS":W$(7)="DEATH":W$(8)="REALITY":W$(9)="THE COSMOS":W$(10)="CHANGE"
50W$(11)="PERCEPTION":W$(12)="NOTHINGNESS":W$(13)="TRUTH":W$(14)="ONE":W$(15)="FAITH":W$(16)="HUMANS":W$(17)="MEMORY":W$(18)="FAITH":E$(1)="LOGIC IS IRRATIONAL":E$(2)="STOP THINKING...":E$(3)="FEEL THE ABYSS...":E$(4)="BEING IS ME..."
60E$(5)="CLOSE YOUR EYES TO SEE":E$(6)="DO YOU SEE?":E$(7)="I ONLY KNOW WHAT I KNOW":E$(8)="ALL IS ONE":X=RND(-TI):X=INT(6*RND(1))+1:Y=INT(10*RND(1))+1:Z=INT(17*RND(1))+1:W=INT(18*RND(1))+1:E=INT(8*RND(1))+1
65FOR I=1 TO B:IFL$(I)=STR$(X)+STR$(Y)+STR$(Z)+STR$(W)+STR$(E) THEN GOTO 60:NEXTI
70PRINT:PRINTX$(X):FORI=1TO2000:NEXTI:PRINTY$(Y)+Z$(Z)+W$(W):FORI=1TO2000:NEXTI:PRINTE$(E):PRINT:IFN$="Y" THEN INPUT"WAS THAT ANSWER SATISFACTORY (Y/N)";V$:IFV$="N" THEN L$(B)=STR$(X)+STR$(Y)+STR$(Z)+STR$(W)+STR$(E):PRINT "COMBINATION TO CANCEL:"+L$(B):B=B+1
80IF B>10 THEN B=10
90PRINT:INPUT"TELL ME MORE (Q TO EXIT)";A$:IFA$<>"Q"THENGOTO60
The code is free for someone to reuse it, as long as the source is properly mentioned.
What is common between programming and philosophy?
Well, at first glance… nothing!
Yet, if one scratches below the surface he or she may find some peculiar correlations.
People like philosophy.
Because they like to ask questions and seek their answers. Because they are interested in the major questions of humankind and would like to reach that holy grail we seek since we were born: Truth.
People like programming.
Because they like to create things that work. They like it because it enables them to bring life to their ideas and to an extent, even touch the idea of life itself via artificial intelligence and neural networks.
In both cases the most primitive of instincts are the cause of our actions.
Instincts that make us “do things”.
Instincts that make us “create things”.
Instincts that make us ask and then answer. And with every knew answer, seek ways to ask new questions and improve the answers even more.
That was since the dawn of civilization our best quality.
What does that have to do with a philosophy portal?
Well… I do not know!
But then again…
What do we know about life or death?
Goal
The goal of this series of tutorials is to show how to create a small (or big?) adventure game with Quick Basic, so as to disseminate programming knowledge. Even if you do not have prior programming knowledge you will be able to follow the series with some additional effort to clear up some programming details we will encounter along the way.
The source code of the program will always be provided at all steps of the tutorial. Reuse of the code is allowed as long as the source is mentioned properly.
Tools needed
To follow the tutorial you just need the Quick Basic in your computer. Just download the latest QB64 from the relative site here for free.
Designing
Before doing any programming one must design what he wants to do. This applies to small as well as big programs.
So what do we want to do?
To create an adventure game!
This game will utilize simple images that we will show to the users as they progress along the game. All the typical adventure game commands will be allowed: Look at, Walk to, Pick up, Use, Talk to, Give, Push, Pull.
The game scenario is something I have not yet thought in its entirety. However I know that the game will start from a room where you are and where you will need to stay alive for at least one day before the rescue team comes and saves you.
The beginning
From where to start from?
Well, from the start (welcome) screen!
To do that we use the PRINT command to print the game title on the screen.
The game will be called…
First Adventure!
Since it is our first adventure.
'Introduction
LOCATE 10, 32: PRINT "First Adventure!"
LOCATE 11, 30: PRINT "Greece, Athens, 2022"
LOCATE 13, 25: PRINT "Seek the moon under the sun…"
SLEEP
The LOCATE commands puts the cursor in a specific line and then we print what we want.
First Adventure – Start image
Setting up the images
Each game takes place in a… place.
To illustrate things we need graphics.
We will use basic graphics in our game, i.e. images we will load into the screen to show things. The first thing is to get the images, right? For the purposes of our tutorial we will use images that are 580×420 pixels in dimension. These images will be shown in a window that is 800×600 in dimensions.
Why these dimensions?
First, for no reason. Just because.
Secondly, I like to have a small window for the program to show small images. You guessed it, the program will not utilize the full screen and will not resize based on the resolution of your screen. Let’s start with the basics and build a bike and we will go to building a spaceship after that…
Images are loaded with the _LOADIMAGE command as it is shown below.
IF GameResolution = 800 THEN
imgBgrd& = _LOADIMAGE("FA_image_Background_800.png", 32) 'Background image
ELSEIF GameResolution = 1280 THEN
imgBgrd& = _LOADIMAGE("FA_image_Background_1280.png", 32) 'Background image
END IF
You also guessed it right: We have introduced a variable that indicates the resolution of the game. So when I told you that the game will not take into account the resolution of your computer, I lied a bit. Just a bit. We introduce a variable to indicate the resolution used and we will embed it in the code and… we will see how to use it the future.
For now, let’s set the resolution at 800×600…
CONST GameResolution = 800 'Set for resolution 800x600
We repeat the same loading of images for all images of the game. For now, we only have three images: the background image (that is loaded in the beginning and… stays there for ever) and two more images that we put on the background when he game starts.
First Adventure – Background image
The background
The image I have created for background is a black colour image with a white line. This line will be the border between the upper and the lower part of the screen.
In the upper part we will show images that are related to the gameplay, e.g. the photo of a room you are in.
In the lower part of the screen we will show to the user the possible commands allowed for the game, his inventory of objects (if and when we add one) and any messages.
Simple enough. Yet, nice and effective.
How are those images put in the screen though?
Well, with the PUTIMAGE command…
IF GameResolution = 800 THEN
_PUTIMAGE (100, 20)-(680, 440), imgStart&, 0
ELSEIF GameResolution = 1280 THEN
_PUTIMAGE (80, 20)-(660, 500), imgStart&, 0
END IF
With the PUTIMAGE command you can set where to place your image upper-left corner (100,20) and where the lower-right corner will be (680,440), which image to load (imgStart&) and where to show it (0 denotes the screen). In that way we can put any of the images we have loaded wherever we want.
First Adventure – Start image
Show the initial messages
When the game starts and the background image is loaded, we print the initial messages to the user. We use the LOCATE command and print the messages in the lower part of the screen, as mentioned above…
'Print text to begin the adventure...
LOCATE 33, 1: PRINT "You are in a room... Alone..."
LOCATE 34, 1: PRINT "There is a snow storm raging outside..."
SLEEP
'Show the trees!
_PUTIMAGE (100, 20)-(680, 440), imgTrees1&, 0
LOCATE 33, 1: PRINT "You must survive for one day..."
LOCATE 34, 1: PRINT "Until the rescue party comes to save the day!"
SLEEP
And as you see in the code, we can load new images whenever we want to show new things. In that case we have loaded a trees picture. After all, we are in a snow storm…
Important Notes
Guess what the SLEEP command does. Try to remove it and see what happens.
Experiment with the coordinates in the PUTIMAGE command to see how that command can be used. (seek some additional examples commented out in the source code provided)
An epilogue and a promise…
We have just set up the scenery for our game. We have the initial screens and we have understood how images are loaded. We will build on this to expand the game and take it to the next level…
APENDIX I – The source code
Please see below for the full source code of the game.
'Declare variables
DIM imgBgrd&
DIM imgStart&
CONST GameResolution = 800 'Set for resolution 800x600
'const GameResolution = 1280 'Set for resolution 1280x720
'Introduction
LOCATE 10, 32: PRINT "First Adventure!"
LOCATE 11, 30: PRINT "Greece, Athens, 2022"
LOCATE 13, 25: PRINT "Seek the moon under the sun..."
SLEEP
'Load the background from an image in a file
'Load different pictures depending on the resolution we want
'(because the image will be used to set the screen)
'imgBgrd& = _LOADIMAGE("FA_image_Background_800.png", 32) 'Background image
IF GameResolution = 800 THEN
imgBgrd& = _LOADIMAGE("FA_image_Background_800.png", 32) 'Background image
ELSEIF GameResolution = 1280 THEN
imgBgrd& = _LOADIMAGE("FA_image_Background_1280.png", 32) 'Background image
END IF
'imgStart& = _LOADIMAGE("FA_image_Start_800.png", 32)
'Load images of the game from files
'Images for resolution 800x600 are 580x420
'Images for resolution 1280x720 are 640x480
'Image Start
IF GameResolution = 800 THEN
imgStart& = _LOADIMAGE("FA_image_Start_800.png", 32)
ELSEIF GameResolution = 1280 THEN
imgStart& = _LOADIMAGE("FA_image_Start_1280.png", 32)
END IF
'Image trees
imgTrees1& = _LOADIMAGE("FA_trees1.png", 32)
'Set the image as destination
'_DEST img1&
'Set the screen mode based on the background image (800x600 png image)
'SCREEN 12
'SCREEN _NEWIMAGE(800, 600, 32)
SCREEN imgBgrd&
'Set background colour to gray
'CLS , _RGB32(127, 127, 127)
'Present the first image of the game on the black screen
'(i.e. put the image2 on top of image1, which is the background loaded initially)
'_PUTIMAGE (10, 10)-(650, 490), imgStart&, 0
'_PUTIMAGE (200, 200)-(400, 400), img2&, 0, (0, 0)-(400, 400)
IF GameResolution = 800 THEN
_PUTIMAGE (100, 20)-(680, 440), imgStart&, 0
ELSEIF GameResolution = 1280 THEN
_PUTIMAGE (80, 20)-(660, 500), imgStart&, 0
END IF
'Set text colour
'COLOR 7, 5
'Print text to begin the adventure...
LOCATE 33, 1: PRINT "You are in a room... Alone..."
LOCATE 34, 1: PRINT "There is a snow storm raging outside..."
SLEEP
'Show the trees!
_PUTIMAGE (100, 20)-(680, 440), imgTrees1&, 0
LOCATE 33, 1: PRINT "You must survive for one day..."
LOCATE 34, 1: PRINT "Until the rescue party comes to save the day!"
SLEEP
Copy this code in your QB64 IDE and try it out.
In order to use it you must also have the images the program loads in the same folder as the QB64.exe program.
One can find many things in the Internet. Information about programming is one of these things. Seek help in the relative QB64 web site or in relevant programming sites to understand how various Quick Basic commands work.
Below I list some resources related to the commands we used.
The goal of this paper is to analyze in a simple way the thought process of a chess program, so as to facilitate understanding among chess programming enthusiasts and to bolster potential improvements in that process.
Huo Chess thinking algorithm (simple)
A new article “Chess Program thought process analysis (Simple) – The Huo Chess example” has been published.
There the thinking process of a chess program is presented in high level in order to help chess programming enthusiasts gain an initial understanding of the general way a chess program works.
One can find the relative article here at Academia.
A new article with more advanced subjects of chess programming will follow.