Microsoft Small Basic

Program Listing: MKR454
'Mastermind game
'Gerrit van den Bosch
'copyright 2014 ;)
' 1=rood 2=blauw 3 = groen 4=geel 5=paars 6=oranje

'variabelen
'MasterMindCode
'beurt
'zet
'GoedeKleuren
'Goedeplaats
'GeselcteerdeKleur

'SpeelbordVakken


'Gebeurtenissen
GraphicsWindow.MouseDown = OnMouseDown

'maten van bord
GraphicsWindow.Height = 570
GraphicsWindow.Width = 329

'Bepaal de pinkleuren
PinKleur[1] = "#FF0000" ' "Red"
PinKleur[2] = "#0000FF" ' "Blue"
PinKleur[3] = "#008000" ' "Green"
PinKleur[4] = "#FFFF00" ' "Yellow"
PinKleur[5] = "#800080" ' "Purple"
PinKleur[6] = "#FFA500" ' "Orange"

Start()

'Gebeurtenis Afandelingen
Sub OnMouseDown
x = GraphicsWindow.MouseX
y = GraphicsWindow.MouseY

'Start het spel opnieuw
If x > 229 And x < 320 And y > 510 And y < 550 Then
Start()
EndIf

If beurt < 11 then

'kleurKiezen
If x >= 279 And x <= 330 And y >= 0 And y <= 510 then
KiesKleur()
ZetKleurMarker()
EndIf

'kleur plaatsen 25,0,204,510
If x >= 25 And x <= 229 And y >= (-51 + beurt * 51) And y <= (beurt * 51) then
'kleurKiezen
PlaatsPin()
EndIf
Else
GraphicsWindow.ShowMessage("Het spel is afgelopen", "Mastermind")
EndIf


EndSub

'plaats een pin
Sub PlaatsPin
if array.ContainsValue(aantalPinsGezet, GeselecteerdeKleur) Then
GraphicsWindow.ShowMessage("Deze kleur heb je al gebruikt", "mastermind")
else
'bepaal in welke kolom is geklikt
If GraphicsWindow.MouseX > 25 Then
kolom = 1
EndIf

If GraphicsWindow.MouseX > 76 Then
kolom = 2
EndIf

If GraphicsWindow.MouseX > 127 Then
kolom = 3
EndIf

If GraphicsWindow.MouseX > 178 Then
kolom = 4
EndIf

x = 25 + 5 + (kolom *51) - 51
y = 5 + beurt * 51 - 51
GraphicsWindow.BrushColor = GeselecteerdeKleur
GraphicsWindow.FillEllipse(x, y, 41,41)


aantalPinsGezet[kolom] = GeselecteerdeKleur

If Array.GetItemCount(aantalPinsGezet) = 4 Then
GraphicsWindow.Title = ""

'Controleer gezette pins
BerekenScore()
'Plaats resultaat van de beurt

For i=1 To 4
GraphicsWindow.Title = GraphicsWindow.Title + aantalPinsGezet[i] + " "
EndFor
beurt = beurt + 1
ZetBeurMarker()
aantalPinsGezet = ""
EndIf

If beurt = 11 then
GraphicsWindow.ShowMessage("ëinde van het spel", "Einde")
endif
EndIf
EndSub

'Kies een kleur
Sub KiesKleur
If Array.ContainsValue(PinKleur, GraphicsWindow.GetPixel(x,y)) then
GeselecteerdeKleur = GraphicsWindow.GetPixel(x,y)
GraphicsWindow.Title = GeselecteerdeKleur
EndIf
EndSub

'Plaats kleurMarker
Sub ZetKleurMarker
TekenVoorbeeldKleuren()
For i=1 To 6
If PinKleur[i] = GeselecteerdeKleur Then
x = 279
y = i * 51
GraphicsWindow.PenColor = "white"
GraphicsWindow.PenWidth = 4
GraphicsWindow.DrawEllipse(x+4, y+4-51, 43, 43)

EndIf
EndFor
EndSub

'Plaats het driehoek om aan te geven welke beurt
Sub ZetBeurMarker
TekenBeurtAanduiding()
x = 0
y = (51 * beurt) -51
'GraphicsWindow.Title = "y= " + y
GraphicsWindow.BrushColor = "White"
GraphicsWindow.FillTriangle(x, y, x, y + 51, x + 25, y + 26)
EndSub

'Bereken de score van de beurt
Sub BerekenScore

For i=1 To 4
If aantalPinsGezet[i] = PinKleur[MasterMindCode[i]] Then
aantalKleurenGoedePlaats = aantalKleurenGoedePlaats + 1
Else
If Array.ContainsValue(aantalPinsGezet, PinKleur[MasterMindCode[i]] ) then
aantalKleurenGoed = aantalKleurenGoed + 1
EndIf
EndIf
EndFor
GraphicsWindow.Title = "kleurGoed:" + aantalKleurenGoed + " PlekGoed:" + aantalKleurenGoedePlaats
PlaatsScorePins()

If aantalKleurenGoedePlaats =4 Then
GraphicsWindow.ShowMessage("Gefeliciteerd! Je hebt de code geraden.", "Mastermind")

'Toon de geheime Mastermind code
TekenMastermindCode()
beurt = 11
EndIf

'Reset score tellers
aantalKleurenGoed = 0
aantalKleurenGoedePlaats = 0

EndSub

'Bepaal de geheime kleurcode
Sub GenereerKleurCode
AantalGevondenKleuren = 0

While Array.GetItemCount(MasterMindCode) < 4
kleur = Math.GetRandomNumber(6)
If Array.ContainsValue(MasterMindCode, kleur) then
'Doe niks
Else
'Voeg de kleur toe aan de code
AantalGevondenKleuren = AantalGevondenKleuren + 1
MasterMindCode[AantalGevondenKleuren] = kleur
EndIf
EndWhile
'GraphicsWindow.Title = MasterMindCode[1] + "+" + MasterMindCode[2] + "+" + MasterMindCode[3] + "+" + MasterMindCode[4]
EndSub

'teken de indeling van het bord
Sub TekenHetBord
'Beurtaanduiding
TekenBeurtAanduiding()

'SpeelbordPins
GraphicsWindow.BrushColor = "Gray"
GraphicsWindow.FillRectangle(25,0,204,510)

'SpeelbordScore
GraphicsWindow.BrushColor = "DarkGray"
GraphicsWindow.FillRectangle(229,0,50,510)

'SpeelbordAntwoord
GraphicsWindow.BrushColor = "DimGray"
GraphicsWindow.FillRectangle(25,510,204,60)

'Teken de pinnetjes
x = 25
y= 0

For i1 = 1 To 10 'teken de rijen
For i2 = 1 To 4 'teken de pinnetjes
GraphicsWindow.PenColor = "White"
GraphicsWindow.DrawEllipse(x+4, y+4, 43,43)
x = x + 51
EndFor
x = 25
y = y +51
EndFor

'Teken de voorbeeldkleuren
TekenVoorbeeldKleuren()

'Maak speel opnieuw knop
GraphicsWindow.BrushColor = "Yellow"
GraphicsWindow.FillRectangle(229,510,100,60)
GraphicsWindow.BrushColor = "White"
GraphicsWindow.FontSize = 18
GraphicsWindow.DrawBoundText(240,520,90," Speel Opnieuw")

EndSub

'Teken de voorbeeldkleuren
Sub TekenVoorbeeldKleuren
'Voorbeeldkleuren
GraphicsWindow.BrushColor = "DarkGray"
GraphicsWindow.FillRectangle(279,0,50,510)

x = 279
y = 0
For i = 1 To 6
GraphicsWindow.BrushColor = PinKleur[i]
GraphicsWindow.fillEllipse(x+4, y+4, 43,43)
y = y + 51
EndFor
EndSub

Sub TekenBeurtAanduiding
'BeurtAanduiding
GraphicsWindow.BrushColor = "Silver"
GraphicsWindow.FillRectangle(0,0,25,570)
EndSub

Sub TekenMastermindCode
x = 25
y = 510
For i = 1 To 4 'teken de pinnetjes
GraphicsWindow.brushColor = PinKleur[MasterMindCode[i]]
GraphicsWindow.FillEllipse(x+4, y+8, 43,43)
x = x + 51
EndFor
EndSub

Sub PlaatsScorePins
x = 229
y = (beurt * 51) - 51

TotaalAantalPins = aantalKleurenGoedePlaats + aantalKleurenGoed

'pin 1
x = x + 7
y = y + 7
If aantalKleurenGoedePlaats > 0 Then
GraphicsWindow.BrushColor = "black"
GraphicsWindow.FillEllipse(x, y, 15, 15)
Else
GraphicsWindow.BrushColor = "white"
GraphicsWindow.FillEllipse(x, y, 15, 15)
EndIf

'pin 2
x = x + 25
If aantalKleurenGoedePlaats > 1 Then
GraphicsWindow.BrushColor = "black"
GraphicsWindow.FillEllipse(x, y, 15, 15)
Else
GraphicsWindow.BrushColor = "white"
GraphicsWindow.FillEllipse(x, y, 15, 15)
EndIf

'pin 3
If TotaalAantalPins > 2 then
x = x - 25
y = y + 25
If aantalKleurenGoedePlaats > 2 Then
GraphicsWindow.BrushColor = "black"
GraphicsWindow.FillEllipse(x, y, 15, 15)
Else
GraphicsWindow.BrushColor = "white"
GraphicsWindow.FillEllipse(x, y, 15, 15)
EndIf
EndIf

'pin 4
If TotaalAantalPins > 3 then
x = x + 25
If aantalKleurenGoedePlaats > 3 Then
GraphicsWindow.BrushColor = "black"
GraphicsWindow.FillEllipse(x, y, 15, 15)
Else
GraphicsWindow.BrushColor = "white"
GraphicsWindow.FillEllipse(x, y, 15, 15)
EndIf
EndIf
EndSub

Sub Start
beurt = 1
aantalPinsGezet = ""
aantalKleurenGoed = 0
aantalKleurenGoedePlaats = 0
GraphicsWindow.Clear()
GraphicsWindow.PenWidth = 2

'Start van het spel
GenereerKleurCode()
TekenHetBord()
ZetBeurMarker()
GeselecteerdeKleur = PinKleur[1]
'TekenMastermindCode()

EndSub


Einde: