Adapting MINIMAX under FreeBasic

User projects written in or related to FreeBASIC.
VANYA
Posts: 1834
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Adapting MINIMAX under FreeBasic

Post by VANYA »

I tried to adapt by MiniMax FreeBasic. It seems to work, but still need testing.

@BasicCoder2 and @Roland Chastain

Maybe try MINIMAX engine in their chess programs after testing?

I would like to put here the code but the message could not be more than 60 000 characters :(

So the reference:

Download

--------------------

Converted Minimax to a XBoard/WinBoard engine from Roland Chastain: viewtopic.php?p=277044#p277044

--------------------
Last edited by VANYA on Oct 20, 2020 6:38, edited 5 times in total.
Roland Chastain
Posts: 1002
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: Adapting MINIMAX under FreeBasic

Post by Roland Chastain »

It seems to work fine with me. I put it in my code examples and will try and study it. Thank you very much for this contribution !
Roland Chastain
Posts: 1002
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: Adapting MINIMAX under FreeBasic

Post by Roland Chastain »

While you are working on this FB adaptation, it would be good to give even a quick improvement to the console display. It is almost impossible to play a whole chess game on such an unreadable board. Do you intend to work again on it ?
VANYA
Posts: 1834
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: Adapting MINIMAX under FreeBasic

Post by VANYA »

Roland Chastain wrote:While you are working on this FB adaptation, it would be good to give even a quick improvement to the console display. It is almost impossible to play a whole chess game on such an unreadable board. Do you intend to work again on it ?
Roland!

I have read as BasicCoder2 expressed (so it seemed to me) the complexity of the translation code for QB FreeBasic. So I decided to try it. And it is your chess program and not have a good artificial intelligence. Connect this engine to your program I think will not make difficulties. I do not think that I will do my chess, but If I have a desire to make chess, the more likely it will be a simple GUI version with using my GUI library for Windows.
Roland Chastain
Posts: 1002
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: Adapting MINIMAX under FreeBasic

Post by Roland Chastain »

VANYA wrote:I have read as BasicCoder2 expressed (so it seemed to me) the complexity of the translation code for QB FreeBasic. So I decided to try it. And it is your chess program and not have a good artificial intelligence. Connect this engine to your program I think will not make difficulties. I do not think that I will do my chess, but If I have a desire to make chess, the more likely it will be a simple GUI version with using my GUI library for Windows.
Indeed I often looked for a FB chess engine that I could connect to my GUI. You made it ! Since you say that you don't intend to do something with that code, I take it as it is and I will probably do something with. Maybe I will connect it to my GUI, maybe I will just improve the console display... In all cases I will say to you.
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Adapting MINIMAX under FreeBasic

Post by BasicCoder2 »

Roland Chastain wrote:
VANYA wrote:I have read as BasicCoder2 expressed (so it seemed to me) the complexity of the translation code for QB FreeBasic.
I didn't mean a one to one translation without understanding although you did a very good job at that. I meant translate the program into an understanding, a high level description, of how the minimax works to enable you to write your own version in any language.

My intention was not so much a FB version but rather to get an understanding of how such a chess program works, an understanding which would be demonstrated by writing my own FB chess program. This would not be achieved by simply replacing the display and input sections of the MiniMax qb progam with a GUI although that would be a worthwhile project in itself.
Roland Chastain
Posts: 1002
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: Adapting MINIMAX under FreeBasic

Post by Roland Chastain »

I began to read MINIMAX. I had it since a long time in my computer, but it's more pleasant to study a program when you can run it (I am not used to QB). The code is very clear and well written, so you can understand easily enough what it does. I believe I begin to see what "minimax" is. You were right, BasicCoder2, when you said to me that it is not so hard to understand.
As an exercise, and a way to study the code, I would like to work on it and make my own adaptation. If I manage to understand how it works, and if I see that it really plays well, maybe I will connect it to my GUI. It should be easy enough, as VANYA said, because all the names are different.
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Adapting MINIMAX under FreeBasic

Post by BasicCoder2 »

This is an initial attempt to give Vanya's translation of the qb minimax program a friendlier user interface.

At the beginning I inserted two new lines,

Code: Select all

'  Calledby:  None
'********************************************************************

screenres 640,480,32
dim shared as integer exitFlag,resetFlag,flipFlag,buttonFlag
At the end of the BPAssessment () I added these two functions and modified the CommandLoop which follows by calling Commands = getMove() which uses the mouse and buttons instead of the keyboard. Although I have only used 3 buttons so far I think the method is clear and I have run out of time tonight to finish it.

Code: Select all

function getHalfMove() as string
    dim as integer mx,my,ox,oy,btn,x,y
    dim as integer p,q
    dim as string flagString
    'clear button flags
    exitFlag = 0
    resetFlag = 0
    flipFlag = 0
    buttonFlag = 0
    
    
   'pixel position of board's top/left coordinates
    p = 32
    q = 32  
    getmouse mx,my,,btn
    ox = mx
    oy = my
    while btn<>1
        getmouse mx,my,,btn
        if mx<>ox or my<>oy then
            DisplayBoard(False)
            ox = mx
            oy = my
            x = int((mx-12)/24)
            y = int((my-12)/24)
            if x>0 and x<9 and y>0 and y<9 then
                line ((x-1)*24+p,(y-1)*24+q)-((x-1)*24+24+p,(y-1)*24+24+q),rgb(255,255,0),b
            end if
          
            'over Game Over button?
            if mx>249 and my>34 and mx<340 and my<54 then
                line (249,34)-(340,54),rgb(255,255,255),b
            end if
                
            'over NEW GAME button
            if mx>249 and my>65 and mx<328 and my<84 then
                line (249,65)-(340,84),rgb(255,255,255),b
            end if
                
            'over FLIP BOARD button?
            if mx>249 and my>95 and mx<328 and my<114 then
                line (249,95)-(340,114),rgb(255,255,255),b
            end if

        end if
    wend
    
    if mx>249 and my>34 and mx<340 and my<54  then
        exitFlag =1
        flagString = "EN"
    end if
    
    if mx>249 and my>65 and mx<328 and my<84 then
        resetFlag = 1
        flagString = "NG"
    end if
    
    if mx>249 and my>95 and mx<328 and my<114 then
        flipFlag = 1
        flagString = "FB"
    end if
    
    buttonFlag = exitFlag or resetFlag or flipFlag
    
    
    'wait for button release
    while btn=1
        getmouse mx,my,,btn
    wend
    
    if buttonFlag = 0 then
        return chr(x+96)+str(y)
    else
        return flagString
    end if
    
end function

function getMove() as string
    
    dim as string move,move1,move2

        move1 = getHalfMove()
        if buttonFlag <> 1 then
            move2 = getHalfMove()
            if buttonFlag <> 1 then
                move = move1+move2
            else
                move = move2
            end if
        else
            move = move1
        end if
        
        
    return move
    
end function

'--------------------------------------------------------------------
'  CommandLoop:
'  Reads the player's commands in a loop and calls
'  the appropriate functions. The loop is terminated by
'  the "EN" command.
'  If the input is not a command it is interpreted as a
'  move (on the form "e2e4" (from-field,to-field).
'  and ignored as a command.     See also: PrintLogo
'  Calls:    Gameover; Initialize; Displayboard; InputPosition;
'            ComputerMove; Flipboard; MoveList; MoveBack;
'            ComputingDepth; InitGameTree; AssessPosition;
'  Calledby: Main
'--------------------------------------------------------------------
SUB CommandLoop
    Dim As Short Ends
    Dim As String Commands
    
    DO
        DisplayBoard(False) ' my new line

        Commands = getMove()  ' <--   THIS IS THE CHANGE MADE

      Commands = UCASE(Commands)    ' Change to upper case
      SELECT CASE Commands
         CASE "EN"
            Ends = True
            GameOver
         CASE "NG"
            PRINT " New Game"
            Initialize
         CASE "DB"
            DisplayBoard(False)
         CASE "CP"
            InputPosition
         CASE "PL"
            ComputerMove
         CASE "FB"
            FlipBoard
         CASE "PR"
            PRINT " Printing ";
            IF Printing = False THEN
               Printing = True
               PRINT "on"
            ELSE
               Printing = False
               PRINT "off"
            END IF
         CASE "MM"
            PRINT " Player-Player ";
            IF PlayerPlayer = False THEN
               PlayerPlayer = True
               PRINT "on"
            ELSE
               PlayerPlayer = False
               PRINT "off"
            END IF
         CASE "ML"
            MoveList
         CASE "TB"
            MoveBack
         CASE "SD"
            ComputingDepth
         CASE "DA"
            InitGameTree
            PRINT " Assessment= "; AssessPosition(-MateValue, MateValue, Colour)
         CASE ELSE
            IF InputMove(Commands) = False THEN
               PRINT " Illegal move or unknown Command"
            ELSEIF PlayerPlayer = False THEN
               ComputerMove
            END IF
      END SELECT

    LOOP WHILE Ends = False
    
END SUB
I also rewrote the first half of DisplayBoard() for mouse usage.

You could replace the

if abs(Board(i))=1 then draw string (x*24+8+p,y*24+8+q), "P"

with sprites something like this,

if abs(Board(x,y))=1 then put (i*24+p,j*24+q), wPawn,trans

Or even maybe use a font with the extended asc code for chess piece images.

Code: Select all

SUB DisplayBoard ( BoardOnly As short)
dim as integer x,y,p,q
   'pixel position of board's top/left coordinates
    p = 32
    q = 32 
     ' Display board
    cls

    for i as integer = 0 to BoardDim
        y = (i\10)
        x = (i-y*10)
        y = y - 2
        x = x - 1
    
        if x>=0 and x<8 and y<8 and y>=0 then
    
            if (int(x/2)<>x/2 and int(y/2)=y/2) or (int(x/2)=x/2 and int(y/2)<>y/2) then
                line (x*24+p,y*24+q)-(x*24+24+p,y*24+24+q),rgb(&H60,&H90,&H58),bf
            else
                line (x*24+p,y*24+q)-(x*24+24+p,y*24+24+q),rgb(&HB8,&HB0,&H50),bf
            end if
            'locate y+1,x+1

            color rgb(255,255,255)
            if sgn(Board(i))<0 then color rgb(20,20,20)
            if sgn(Board(i))>0 then color rgb(250,250,250)
            if abs(Board(i))=1 then draw string (x*24+8+p,y*24+8+q), "P"
            if abs(Board(i))=2 then draw string (x*24+8+p,y*24+8+q), "R"
            if abs(Board(i))=4 then draw string (x*24+8+p,y*24+8+q), "N"
            if abs(Board(i))=3 then draw string (x*24+8+p,y*24+8+q), "B"
            if abs(Board(i))=5 then draw string (x*24+8+p,y*24+8+q), "Q"
            if abs(Board(i))=6 then draw string (x*24+8+p,y*24+8+q), "K"
            if abs(Board(i))=0 then draw string (x*24+8+p,y*24+8+q), " "
            
        end if
        
        color rgb(255,255,255)
        'draw coordinates
        for z as integer = 0 to 7
            draw string(z*24+8+p,q-16),chr(z+97)
            draw string(p-16,z*24+8+q),str(z+1)
        next z
        'draw border
        line (p,q)-(p+192,q+192),rgb(255,255,255),b
    next i
    
    'draw buttons
    color rgb(0,0,0)
    line (249,34)-(340,54),rgb(255,0,0),bf
    draw string (256,40),"GAME OVER"
    line (249,65)-(340,84),rgb(255,0,0),bf
    draw string (260,71),"NEW GAME"
    line (249,95)-(340,114),rgb(255,0,0),bf
    draw string (256,101),"FLIP BOARD"
    color rgb(255,255,255)
'----------------------------------
   IF BoardOnly THEN EXIT SUB

      ' Remaining board/game state
  locate 35,10
  
   IF Colour = White THEN
      PRINT " White";
   ELSE
      PRINT " Black";
   END IF
   PRINT " is to make a move"

   PRINT " Material balance = "; MaterialBalance(Depth)
   PRINT " En Passant Field = "; Fieldnotation$(EpField(Depth))

      ' Castling is in principle possible if the king and appropriate
      ' rook have not moved.
  
   PRINT " Castling state black = ";
   IF MoveControl(E8) + MoveControl(H8) = 0 THEN PRINT "0-0  ";
   IF MoveControl(E8) + MoveControl(A8) = 0 THEN PRINT "0-0-0";
   PRINT
   PRINT " Castling State white    = ";
   IF MoveControl(E1) + MoveControl(H1) = 0 THEN PRINT "0-0  ";
   IF MoveControl(E1) + MoveControl(A1) = 0 THEN PRINT "0-0-0";
   PRINT
END SUB
Last edited by BasicCoder2 on Jan 23, 2012 10:13, edited 2 times in total.
Roland Chastain
Posts: 1002
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: Adapting MINIMAX under FreeBasic

Post by Roland Chastain »

@BasicCoder2

Thank you for your examples. I have not yet tried it. I continue to study the code. It's a lesson of clarity in writing. Good for me !
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Adapting MINIMAX under FreeBasic

Post by BasicCoder2 »

Roland Chastain wrote:@BasicCoder2
Thank you for your examples. I have not yet tried it. I continue to study the code. It's a lesson of clarity in writing. Good for me !
I have just edited the post with some improvements. All you have to do is cut and paste into Vanya's minimax translation.
Roland Chastain
Posts: 1002
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: Adapting MINIMAX under FreeBasic

Post by Roland Chastain »

@BasicCoder2

I tried your modifications but it didn't work, I don't know why. It would be easier if you could give the whole code. Are you still working on it, or did only want to give an example of a quick display improvement ?
I began to make my own adaptation of the program. I have already remade the console display : so I won't use your code. (But don't worry : I had a look at it ! I didn't imagine that it was so simple to make a sprite.) I don't intend to add mouse or anything else, but to remove as things as possible and to keep only the chess engine, to study it and why not connect it to my GUI for a special release.
BasicCoder2 wrote:Or even maybe use a font with the extended asc code for chess piece images.
If you know how one can do that, I would be glad to know.

I haven't yet played a whole game against MiniMAX. Did you ? How strong is he really ? I noticed that it plays instantly : so I wonder what its level can be.
VANYA
Posts: 1834
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: Adapting MINIMAX under FreeBasic

Post by VANYA »

Roland Chastain wrote: I haven't yet played a whole game against MiniMAX. Did you ? How strong is he really ? I noticed that it plays instantly : so I wonder what its level can be.
Roland!

I tried to play with him, of course it was uncomfortable to play, but it seemed to me that the program plays at 2-3 chess-level. That is, the engine program is very well suited
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Adapting MINIMAX under FreeBasic

Post by BasicCoder2 »

Roland Chastain wrote:
I tried your modifications but it didn't work, I don't know why. It would be easier if you could give the whole code.
Happy to do so will send it to you.

When I try and post it to the forum I get,

Your message contains 108018 characters. The maximum number of allowed characters is 60000

I guess I could break it into two posts but I am not sure if that would be ok?
Are you still working on it, or did only want to give an example of a quick display improvement ?
It was just a quick example. I am more interested in writing my own full program rather than using someone elses code as it is understanding it well enough to write my own version that interests me. Using someone elses engine without understanding would defeat the reason I wanted to write my own chess playing program in the first place.
BasicCoder2 wrote:Or even maybe use a font with the extended asc code for chess piece images.
If you know how one can do that, I would be glad to know.
http://www.freebasic.net/wiki/wikka.php ... comments=1

Essentially a fixed font is a list of images where the asc code is the number on the list. You could write your own subroutine which would take a string as input, and put the appropriate images on the screen in the same order using the asc code to select which image to use.
I haven't yet played a whole game against MiniMAX. Did you ? How strong is he really ? I noticed that it plays instantly : so I wonder what its level can be.
No I haven't played a full game as I spend my spare time writing one not playing it :) But mainly because I hate looking for the coordinates needed and typing them in when a simple mouse selection is easier and faster.
Last edited by BasicCoder2 on Jan 23, 2012 20:54, edited 1 time in total.
Roland Chastain
Posts: 1002
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: Adapting MINIMAX under FreeBasic

Post by Roland Chastain »

Hello VANYA!

I see that the link to your MINIMAX adaptation is broken. Could you give a new link?

Best regards.

Roland
VANYA
Posts: 1834
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: Adapting MINIMAX under FreeBasic

Post by VANYA »

Hi Roland!

I updated the link.

Best regards.
Post Reply