Microsoft Small Basic

Program Listing:
Embed this in your website
' Mastermind
' Original by Justaname21 (2018)
' Modified by Nonki Takahashi (2019)

blackpeg = 0
whitepeg = 0
try = 1
'sets the starting menu for the game'
GraphicsWindow.BackgroundColor = "Lightgray"
GraphicsWindow.Height = 500
GraphicsWindow.Width = 500
GraphicsWindow.FontSize = 18
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.DrawText(190, 1, "Mastermind")
GraphicsWindow.FontSize = 12

NewGameButton = Controls.AddButton("New game" , 195, 160)
Controls.SetSize(NewGameButton, 100, 60)

ExitButton = Controls.AddButton("Exit game", 195, 240)
Controls.SetSize(ExitButton, 100, 60)


'generates the computer colours/code for the game'

For i = 1 To 4
  colourgenerator[i] = Math.GetRandomNumber(6)
  TextWindow.Write(colourgenerator[i])
EndFor
For i = 1 To 4
  backup[i] = colourgenerator[i]
EndFor


'clears the current and creates the main game textwindow when the new game button is clicked
Controls.ButtonClicked = ButtonClicked
Buttonclicked()

Sub Buttonclicked
  ButtonClicked = Controls.LastClickedButton

  If NewGameButton = ButtonClicked Then

    GraphicsWindow.Clear()
    GraphicsWindow.Width = 480
    GraphicsWindow.Height = 800
    GraphicsWindow.FontSize = 14

    'generates the circles for the colours to be filled in by the player'

    x = 120
    y = 10
    While y < 750
      While x < 350

        GraphicsWindow.DrawEllipse(x, y, 50, 50)
        x = x + 70
      EndWhile
      x = 120
      y = y + 70
    EndWhile
    'generate the smaller circles for the black and white pegs'
    x = 400
    y = 10
    While y < 750
      While x < 450
        GraphicsWindow.DrawEllipse(x, y, 20, 20)
        x = x + 30
      EndWhile
      x = 400
      y = y + 70
    EndWhile
    x = 400
    y = 40
    While y < 750
      While x < 450
        GraphicsWindow.DrawEllipse(x, y, 20, 20)
        x = x + 30
      EndWhile
      x = 400
      y = y + 70
    EndWhile
    x = 120

    y = 10
    index = 1
    'generates the buttons which the fills the circles with specified colours when clicked, then it stores the colour clicked into an array'
    GraphicsWindow.BrushColor = "Red"
    Red = Controls.AddButton("Red", 50, 80)
    GraphicsWindow.BrushColor = "Blue"
    blue = Controls.AddButton("Blue", 50, 140)
    GraphicsWindow.BrushColor = "Yellow"
    yellow = Controls.AddButton("Yellow", 50, 200)
    GraphicsWindow.BrushColor = "#30FA11"
    green = Controls.AddButton("Green", 50, 260)
    GraphicsWindow.BrushColor = "#FF9933"
    orange = Controls.AddButton("Orange", 50, 320)
    GraphicsWindow.BrushColor = "#A505F5"
    purple = Controls.AddButton("Purple", 50, 380)
  EndIf


  If buttonclicked = Red Then
    GraphicsWindow.BrushColor = "Red"
    GraphicsWindow.FillEllipse(x, y, 50, 50)
    counter = counter + 1
    If x < 320 Then
      x = x + 70
    ElseIf y < 700 Then
      x = 120
      y = y + 70
    EndIf
    If index < 5 Then
      input[index] = 1
      index = index + 1
    EndIf
  ElseIf buttonclicked = blue Then
    GraphicsWindow.BrushColor = "Blue"
    GraphicsWindow.FillEllipse(x, y, 50, 50)
    if x < 320 Then
      x = x + 70
    ElseIf y < 700 Then
      x = 120
      y = y + 70
    EndIf
    If index < 5 Then
      input[index] = 2
      index = index + 1
    EndIf
  ElseIf buttonclicked = yellow Then
    GraphicsWindow.BrushColor = "Yellow"
    GraphicsWindow.FillEllipse(x, y, 50, 50)
    if x < 320 Then
      x = x + 70
    ElseIf y < 700 Then
      x = 120
      y = y + 70
    EndIf
    If index < 5 Then
      input[index] = 3
      index = index + 1
    EndIf
  ElseIf buttonclicked = green Then
    GraphicsWindow.BrushColor = "#30FA11"
    GraphicsWindow.FillEllipse(x, y, 50, 50)
    if x < 320 Then
      x = x + 70
    ElseIf y < 700 Then
      x = 120
      y = y + 70
    EndIf
    If index < 5 Then
      input[index] = 4
      index = index + 1
    Endif
  ElseIf buttonclicked = orange Then
    GraphicsWindow.BrushColor = "#FF9933"
    GraphicsWindow.FillEllipse(x, y, 50, 50)
    If x < 320 Then
      x = x + 70
    ElseIf y < 700 Then
      x = 120
      y = y + 70
    EndIf
    If index < 5 Then
      input[index] = 5
      index = index + 1
    Endif
  ElseIf buttonclicked = purple Then
    GraphicsWindow.BrushColor = "#A505F5"
    GraphicsWindow.FillEllipse(x, y, 50, 50)
    If x < 320 Then
      x = x + 70
    ElseIf y < 700 Then
      x = 120
      y = y + 70
    EndIf
    If index < 5 Then
      input[index] = 6
      index = index + 1
    Endif
  EndIf
  For index1 = 1 To 4
    input1[index1] = input[index]
  EndFor

  'compares the four colours clicked with the computer genearted colour, black pegs is then added for every colour that is in the correct position and colour'

  If index = 5  Then

    TextWindow.Clear()
    For i = 1 To 4
      For index = 1 To 4
        If input[index] = colourgenerator[i] Then
          If index = i Then
            blackpeg = blackpeg + 1
            colourgenerator[i] = "null"
            input[index] = "null1"
          Endif
        Endif
      EndFor
    EndFor

    For i = 1 to 4
      For index = 1 to 4
        If input[index] = colourgenerator[i] Then
          whitepeg = whitepeg + 1
          colourgenerator[i] = "null"
          input[index] = "null1"
        EndIf
      EndFor
    EndFor

    If blackpeg < 4 Then
      DrawFeedback()
      try = try + 1
      TextWindow.WriteLine(whitepeg + " whitepegs | " + blackpeg + " blackpegs")
      blackpeg= 0
      whitepeg = 0

      switch = 1
      For i = 1 To 4
        colourgenerator[i] = backup[i]
      EndFor

      For index = i To 4
        input[index] = input1[index1]
      EndFor
      index = 1
    ElseIf blackpeg = 4 Then
      DrawFeedback()
      TextWindow.Clear()
      TextWindow.WriteLine("You Win!!! You did it in " + try + " guesses")
    EndIf
  EndIf
EndSub

Sub DrawFeedback
  Stack.PushValue("local", x)
  Stack.PushValue("local", y)
  Stack.PushValue("local", blackpeg)
  Stack.PushValue("local", whitepeg)
  x = 400
  y = y - 70
  GraphicsWindow.BrushColor = "Black"
  While 0 < blackpeg
    GraphicsWindow.FillEllipse(x, y, 20, 20)
    GraphicsWindow.DrawEllipse(x, y, 20, 20)
    x = x + 30
    If 430 < x Then
      x = 400
      y = y + 30
    EndIf
    blackpeg = blackpeg - 1
  EndWhile
  GraphicsWindow.BrushColor = "White"
  While 0 < whitepeg
    GraphicsWindow.FillEllipse(x, y, 20, 20)
    GraphicsWindow.DrawEllipse(x, y, 20, 20)
    x = x + 30
    If 430 < x Then
      x = 400
      y = y + 30
    EndIf
    whitepeg = whitepeg - 1
  EndWhile
  whitepeg = Stack.PopValue("local")
  blackpeg = Stack.PopValue("local")
  y = Stack.PopValue("local")
  x = Stack.PopValue("local")
EndSub
Copyright (c) Microsoft Corporation. All rights reserved.