Microsoft Small Basic

Program Listing:
Embed this in your website
' CodeBreaker Copyright June 2011 Neil Kendall
' A game of logic, based on 'Cows and Bulls' and 'Mastermind'
' This is my first Small Basic program, and the code is rather messy in places!
'
'

CodeSize=4
Logo = ImageList.LoadImage("http://www.logan-5.co.uk/mastermind/images/logo.png")

NewGame()
while Round<=10 And Round>=1

endwhile


Sub DrawGameBoard

  For Row=1 To 10
    For Column=1 To 4
      GraphicsWindow.PenColor="black"
      GraphicsWindow.DrawEllipse((Column-1)*25+5+XOffset,(Row-1)*25+5+YOffset,15,15)
      If GameBoard[row][column]=0 then
        GraphicsWindow.BrushColor="Sienna"
      endif
      If GameBoard[row][column]=1 then
        GraphicsWindow.BrushColor="Red"
      endif
      If GameBoard[row][column]=2 then
        GraphicsWindow.BrushColor="Blue"
      endif
      If GameBoard[row][column]=3 then
        GraphicsWindow.BrushColor="Green"
      endif
      If GameBoard[row][column]=4 then
        GraphicsWindow.BrushColor="Yellow"
      endif
      If GameBoard[row][column]=5 then
        GraphicsWindow.BrushColor="Purple"
      endif
      If GameBoard[row][column]=6 then
        GraphicsWindow.BrushColor="White"
      endif
      GraphicsWindow.FillEllipse((Column-1)*25+5+XOffset,(Row-1)*25+5+YOffset,15,15)
    EndFor
  EndFor

  For Row=1 to 10
    GraphicsWindow.BrushColor="Black"
    GraphicsWindow.DrawRectangle((5-1)*25+5+XOffset,(row-1)*25+5+YOffset,15,15)
    GraphicsWindow.BrushColor="Gray"
    GraphicsWindow.FillRectangle((5-1)*25+5+XOffset,(row-1)*25+5+YOffset,15,15)
  endfor


EndSub

Sub InitialiseGfxWin
  GraphicsWindow.backgroundcolor="Sienna"
  GraphicsWindow.Title="CodeBreaker"
  XOffset=0
  YOffset=50
  GraphicsWindow.Height=380
  GraphicsWindow.Width=200


EndSub

Sub InitialiseBoard
  GraphicsWindow.clear()
  GraphicsWindow.DrawImage(Logo,10,10)
  GraphicsWindow.BrushColor="Black"
  NewGameButton=Controls.AddButton("New Game",5,340)
  InstructionsButton=Controls.AddButton("Instructions",90,340)
  For Row=1 To 10
    For Column=1 To 4
      GameBoard[Row][Column]=0
    EndFor
  EndFor
EndSub

Sub ComputeScore
  For x=1 To 4
    TargetNumber[x]=GameBoard[Try][x]
  EndFor
  Bulls=0
  Cows=0
  For x=1 to 4
    CopyHiddenNumber[x]=HiddenNumber[x]
  endfor

  For i=1 To 4
    If (TargetNumber[i]=CopyHiddenNumber[i]) and TargetNumber[i]<>0 Then
      Bulls=Bulls+1
      CopyHiddenNumber[i]=0
      TargetNumber[i]=0
    EndIf
  EndFor

  for i=1 to 4
    CowFound=0
    For j=1 to 4
      If (TargetNumber[i]=CopyHiddenNumber[j]) And TargetNumber[i]<>0 then
        CowFound=1
        TargetNumber[i]=0
        CopyHiddenNumber[j]=0
      endif
    endfor
    If CowFound=1 Then
      Cows=Cows+1
    endif
  endfor
  GraphicsWindow.BrushColor="Green"
  GraphicsWindow.FillRectangle((6-1)*25+5+XOffset,(Try-1)*25+5+YOffset,15,15)
  GraphicsWindow.BrushColor="Red"
  GraphicsWindow.FillRectangle((7-1)*25+5+XOffset,(Try-1)*25+5+YOffset,15,15)
  GraphicsWindow.BrushColor="Black"
  GraphicsWindow.DrawBoundText((6-1)*25+8+XOffset,(Try-1)*25+5+YOffset,50,Bulls)
  GraphicsWindow.DrawBoundText((7-1)*25+8+XOffset,(Try-1)*25+5+YOffset,50,Cows)
  GraphicsWindow.BrushColor="Sienna"

  If Bulls=4 then
    try=10
    GraphicsWindow.BrushColor="Black"
    GraphicsWindow.DrawBoundText(10,302,150,"Congratulations :-)")
    GraphicsWindow.DrawBoundText(30,314,150,"You Won!")
  endif

  If Try=10 and Bulls<4 then
    GraphicsWindow.BrushColor="Black"
    GraphicsWindow.DrawBoundText(10,302,150,"Commiserations :-(")
    GraphicsWindow.DrawBoundText(30,314,150,"You Lost!")
  endif


EndSub

Sub NewRound
  InitialiseBoard()
  DrawGameBoard()
  Try=1
EndSub

Sub NewGame
  InitialiseGfxWin()
  graphicswindow.Mousedown=MouseDownSub
  Controls.ButtonClicked=ButtonClickSub
  For x=1 To CodeSize
    HiddenNumber[x]=Math.GetRandomNumber(6)
  EndFor
  GameScore=100
  Round=1
  NewRound()
EndSub

Sub MouseDownSub
  RowClicked=Math.floor((GraphicsWindow.MouseY-YOffset)/25)+1
  ColumnClicked=Math.Floor((graphicswindow.mousex-XOffset)/25)+1
  If RowClicked=Try then
    NewColour=GameBoard[RowClicked][ColumnClicked]+1
    If NewColour=7 Then
      NewColour=1
    EndIf
    GameBoard[RowClicked][ColumnClicked]=NewColour
    If RowClicked=Try And ColumnClicked = 5 Then
      TryComplete=1
      For a =1 To 4
        If GameBoard[Try][a]=0 then
          TryComplete=0
        endif
      EndFor
      if TryComplete=1 Then
        ComputeScore()
        Try=Try+1
      EndIf
    EndIf
    DrawGameBoard()
  endif
EndSub

Sub ButtonClickSub
  If Controls.LastClickedButton=NewGameButton then
    NewGame()
  EndIf
  If Controls.LastClickedButton=InstructionsButton Then
    GraphicsWindow.ShowMessage("The computer chooses a secret code consisting of four coloured pegs. The user has 10 attempts to guess this code. Click the round 'holes' to change the colour of the pegs in your guess and when you have entered all 4 click the grey square to see if you are correct. For each attempt you will be given two scores - a green score telling you how many colours are correct and in the correct position, and a red score telling you how many colours are correct but in the wrong position. Good Luck!","CodeBreaker Instructions - By Neil Kendall June 2011.")
  EndIf
EndSub


Copyright (c) Microsoft Corporation. All rights reserved.