Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String Command
#7
(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


Attached Files
.n7   Mastermind_simplified.n7 (Size: 9.21 KB / Downloads: 5)
Reply


Messages In This Thread
String Command - by johnno56 - 02-02-2024, 09:40 PM
RE: String Command - by Marcus - 02-03-2024, 12:08 PM
RE: String Command - by Marcus - 02-03-2024, 01:24 PM
RE: String Command - by aurel - 02-03-2024, 01:43 PM
RE: String Command - by Marcus - 02-03-2024, 01:49 PM
RE: String Command - by johnno56 - 02-03-2024, 07:27 PM
RE: String Command - by 1micha.elok - 02-11-2024, 03:02 PM
RE: String Command - by johnno56 - 02-11-2024, 08:29 PM

Forum Jump:


Users browsing this thread: 3 Guest(s)