NaaLaa

Full Version: String Command
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Does N7/6 have the ability or command to perform Basic's string(var, text$) command?

J

In the meantime... I can use something like...

function String(var, txt)
    tmp = ""
    for z = 1 to var
        tmp = tmp + txt
    next
    return tmp
endfunc
Nope, sorry! But I can add it if you want. Is it useful?

I checked around some on the web. On some page 'str(string, repeat)' looks like the function you want. In naalaa 'str' is already used. Wouldn't it make more sense if the name of the function was 'repeat', which is not a reserved word in naalaa (in other languages repeat/until is a thing, but naalaa uses do/until, so ...).
Maybe

Code:
foo = "bar"*5

would be a sexy way of doing it. But, ugh, probably that would just be confusing? As it is, the * operator converts both sides to numbers. But I could add a rule that applies only if left side is a string and right side is a number. Maybe? No ... too confusing ... I think. Maybe. Would the other compiler creators laugh at me? I hate those guys, with their framed university diplomas and silly golf pants! No!
Quote:I hate those guys, with their framed university diplomas and silly golf pants! No!

I agree Marcus. Big Grin
Big Grin
Thanks Marcus. Is it useful? It is one of the string functions that I rarely use... so adding it to Naalaa would probably be more trouble than it is worth... lol

I was trying to convert an old Basic version of Mastermind when I came across the "string" command... The game may not be completed... Goto's! I really do not like goto's... some I can convert but some ... ugh!  That is a subject for another time....

I will probably go with the little "string" function for now... but, based on the number of times I use it, adding it Naalaa may not be necessary... But thank you for the offer anyway... much appreciated...

Now that you do not have to spend time on the "string" command... you could probably put that time towards the creation of the Neural Interface... right? Moo Ha Ha Ha....
(02-03-2024, 07:27 PM)johnno56 Wrote: [ -> ]I was trying to convert an old Basic version of Mastermind ...

I wrote a simplified version of Mastermind
It's not perfect, but it's playable  Big Grin

================================
              MASTERMIND simplified

 The board has 8 rows and 4 circles in each row
 - 8 rows      : eight chances to win the game
 - 4 circles  : guess what color

 There are 8 possible colors to choose.
 To help you win the game, watch the colored hints :
 - GREEN, correct in color and position
 - RED, color in wrong position

 Controls
 - Use Mouse and Left Click on the color
  Your mouse is only visible on 'Choose' Layout
 - ESC to quit
 - F5 is a secret key to preview solution

================================

Code:
#win32

'-----------------
' INITIALIZATION
'-----------------
set window "MASTERMIND",450,480,false
set redraw off

visible cBlack      = [0,0,0]        'black
visible cWhite      = [255,255,255]  'white
visible cRed        = [255,0,0]      'red
visible cGreen      = [0,255,0]      'green
visible cGray       = [192,192,192]  'light gray
visible cColor      = []             'hold some colors
visible colorPaint  = []             'hold random colors to choose from
visible chosenColor = 10             'color which is chosen by mouse's left click
visible c  = -1                      'just a counter
visible countGreen  = []             'count how many correct answers in one line

'Sprite Class Definition
Sprite =
[
    '---Properties---
    'position (x,y) and radius (r1,r2). code is color's code
    x:0,y:0,r1:10,r2:14,code:0,     
       
    '---Methods---
    'Pos      : set sprite's position
    'Create   : create circle on certain coordinate, with certain color, and certain size
    'ColorIt  : only color and certain size
    Pos       : function(posX,posY);this.x=posX;this.y=posY;endfunc,
    Create    : function(posX,posY,rgb,scale)
                    this.Pos(posX,posY)
                    set color cBlack;draw ellipse this.x,this.y,scale*this.r2,scale*this.r2,1
                    set color rgb;draw ellipse this.x,this.y,scale*this.r1,scale*this.r1,1
                endfunc,
    ColorIt   : function(rgb,scale)
                    set color cBlack;draw ellipse this.x,this.y,scale*this.r2,scale*this.r2,1
                    set color rgb;draw ellipse this.x,this.y,scale*this.r1,scale*this.r1,1
                endfunc                     
]

visible Guess       = fill(Sprite,32)  '32 guess objects
visible Hint        = fill(Sprite,32)  '32 hints objects
visible Paint       = fill(Sprite,8)   '8 possible colors
visible Solution    = fill(Sprite,4)   'Solution to Mastermind

InitialValue()
Layout()

'--------------
' MAIN PROGRAM
'--------------
do
    if keydown(KEY_F5) then ShowSolution("Solution") 'secret key F5 to show solution
    LeftClick()
    redraw
    wait 1
until keydown(KEY_ESCAPE)


'-----------
' FUNCTIONS
'-----------
function InitialValue()     
    '8 possible colors       
    cColor[0]   = [225,29,72]   'red
    cColor[1]   = [59,130,246]  'blue
    cColor[2]   = [34,197,94]   'green
    cColor[3]   = [249,115,22]  'orange
    cColor[4]   = [124,58,137]  'purple
    cColor[5]   = [146,64,14]   'brown
    cColor[6]   = [148,168,184] 'gray
    cColor[7]   = [251,191,36]  'yellow
    cColor[10]  = [255,255,255] 'white

    'choose random colors to be assigned to Paint objects
    randomize clock()'randomize seed
    for i = 0 to 3
        randomColor = rnd(1,8)
        select randomColor
            case 1;colorPaint[i]=cColor[0];colorPaint[i].code=0
            case 2;colorPaint[i]=cColor[1];colorPaint[i].code=1
            case 3;colorPaint[i]=cColor[2];colorPaint[i].code=2
            case 4;colorPaint[i]=cColor[3];colorPaint[i].code=3
            case 5;colorPaint[i]=cColor[4];colorPaint[i].code=4
            case 6;colorPaint[i]=cColor[5];colorPaint[i].code=5
            case 7;colorPaint[i]=cColor[6];colorPaint[i].code=6
            case 8;colorPaint[i]=cColor[7];colorPaint[i].code=7
        endsel
    next
endfunc

function Layout()
    set color cWhite;cls 'clear screen

    'Layout partition
    set color cBlack;draw line 0,40,width(primary),40 'horizontal line 
    set color cBlack;draw line 0,370,width(primary),370 'horizontal line
    set color cBlack;draw line 250,40,250,height(primary)'vertical line 
    set color cGray;draw rect 1,371,249,height(primary)-1,1 'area to choose color from             

    'Title for each part of layout   
    set color cBlack
    set caret 180,20;wln "MASTERMIND"
    set caret 50,50 ;wln "Guess"
    set caret 300,50;wln "Hints"
    set caret 50,380;wln "Choose"

    'Layout #1 (Circles for guess) and Layout #2 (Circle for hints)
    for j = 0 to 28 step 4      '8 rows
        for i = 0 to 3          'each row has 4 circles     
            Guess[i+j].Create(50+i*50,80+j*8,cColor[chosenColor],1)     'Layout #1: circles for guess
            'set color cRed; set caret 50+i*50,80+j*8;wln (i+j) 'for debugging
           
            Hint[i+j].Create(300+i*25,80+j*8,cColor[chosenColor],0.5)   'Layout #2: circles for hints
            'set color cRed; set caret 300+i*25,80+j*8;wln (i+j) 'for debugging
        next
    next
   
    'Layout #3 : Paint objects, 8 colors to choose from                                                               
    for j = 0 to 4 step 4
        for i = 0 to 3
            Paint[i+j].Create(50+i*50,410+j*8,cColor[i+j],1)
            'set color cWhite; set caret 50+i*50,410+j*8;wln (i+j) 'for debugging   
        next
    next
endfunc

function LeftClick()
        'Your mouse is only visible on 'Choose' Layout
        if mousey()>=370 and mousey()<height(primary) and mousex()<250 then
            if mousey()>=400 and mousey()<420 then

            if mousebutton(0,true) 'mouse left click
                if mousex()>=40 and mousex()<=60    then chosenColor = 0
                if mousex()>=90 and mousex()<110    then chosenColor = 1
                if mousex()>=140 and mousex()<160   then chosenColor = 2
                if mousex()>=190 and mousex()<210   then chosenColor = 3
                c = c + 1
                if c>=32 then                                             
                    set color cBlack;set caret 300,430
                    Box("Sorry, you lose")
                    ShowSolution("The solution is")
                    end
                else
                    Guess[c].code = chosenColor
                    Guess[c].ColorIt(cColor[chosenColor],1)
                    Compare(c)
                endif
            endif
           
            elseif mousey()>432 and mousey()<452 then 

            if mousebutton(0,true) 'mouse left click
                if mousex()>=40 and mousex()<=60    then chosenColor = 4
                if mousex()>=90 and mousex()<110    then chosenColor = 5
                if mousex()>=140 and mousex()<160   then chosenColor = 6
                if mousex()>=190 and mousex()<210   then chosenColor = 7
                c = c + 1
                if c>=32 then                                             
                    set color cBlack;set caret 300,430
                    Box("Sorry, you lose")
                    ShowSolution("The solution is")
                    end
                else
                    Guess[c].code = chosenColor
                    Guess[c].ColorIt(cColor[chosenColor],1)
                    Compare(c)
                endif
            endif
                       
            endif

        else
            set mouse 50,410 'set mouse position back to red circle
        endif
       
        redraw
endfunc

function ShowSolution(myText)
    set color cWhite; draw rect 251,371,width(primary)-1,height(primary)-1,1 'clear
    for i = 0 to 3
        set color cBlack; set caret 300,380;wln myText
        Solution[i].Create(300+i*35,430,colorPaint[i],1)
    next
    redraw
    wait 2000
    set color cWhite; draw rect 251,371,width(primary)-1,height(primary)-1,1 'clear
endfunc

function Compare(c)
    'Identify in whichColumn and in whichLine   
    for i = 0 to 3; if (c-i)%4=0 then whichColumn = i;next
    for i = 0 to 28 step 4
        if c>=i and c<=(i+3) then whichLine = (i/4)   
    next
       
    'RED, color in wrong position
    'GREEN, color in right position   
    for i = 0 to 3
        if i = whichColumn and Guess[c].code = colorPaint[i].code then
            Hint[c].ColorIt(cGreen,0.5)
            countGreen[whichLine] = countGreen[whichLine]+1
            if countGreen[whichLine] = 4 then
                Box("Congratulation")
                ShowSolution("You Win")
                wait 2000
                end
            endif
            break 'the same color may appear in some circles
        elseif i<>whichColumn and Guess[c].code = colorPaint[i].code then
            Hint[c].ColorIt(cRed,0.5)
        endif
    next
                                           
endfunc

function Box(myText)
    set color 200,200,200,100;draw rect 110,90,250,180,1    'shadow
    set color cGray;draw rect 100,80,250,180,1              'filled box
    set color cBlack;draw rect 100,80,250,180               'box frame
    set caret 150,130;wln myText                            'write myText
    redraw
    wait 3000
endfunc
Very nice! Very challenging. Eight guesses with eight colours. I like it! Well done!