Sunday, January 6, 2013

Small Basic: Craps Game Program With Comments

Click on photo to view it
'This is the starting point of the craps program
Main_Program:
Initialize_Craps()

Sub Initialize_Craps
  'Generate and size the graphics window
  GraphicsWindow.Show()
  GraphicsWindow.Height = 620
  GraphicsWindow.Width = 800
  GraphicsWindow.BackgroundColor = "Cyan"
GraphicsWindow.Title = "Craps Game"
'
'Draw the die outline for die 1
GraphicsWindow.DrawRectangle(100,100,180,180)
GraphicsWindow.BrushColor = "Yellow"
GraphicsWindow.FillRectangle(100,100,180,180)
'
'Draw the die outline for die 2
GraphicsWindow.DrawRectangle(500,100,180,180)
GraphicsWindow.BrushColor = "Yellow"
GraphicsWindow.FillRectangle(500,100,180,180)
'
'Initialize all variables for the graphics program
Initialize_Data()
'
'Draw nine  buttons in each die.
Draw_Buttons()
'Create text boxes and instructions
GraphicsWindow.BrushColor = "Black"

GraphicsWindow.DrawText(260,300,"Press the left mouse button to roll the dice")
'
GraphicsWindow.DrawText(280,340,"Press the right mouse button exit")
'
'The Instruction Box displays the messages
'New Game
'and
'Roll em again
InstructionBox = Controls.AddTextBox(340,50)
Controls.SetSize(InstructionBox,100,30)

GeneralMessageBox = Controls.AddTextBox(290,25)
Controls.SetSize(GeneralMessageBox,200,30)
Controls.SetTextBoxText(GeneralMessageBox,"   Let's Play A Game Of Craps")
GraphicsWindow.DrawText(20,400,"Rules of Craps Game")
GraphicsWindow.DrawText(20,430,"First Roll of Dice")
GraphicsWindow.DrawText(20,460,"7 or 11 wins")
GraphicsWindow.DrawText(20,490,"2, 3 or 12 loses")
GraphicsWindow.DrawText(20,520,"4, 5, 6, 7, 9 or 10 okay")
GraphicsWindow.DrawText(20,550,"Matching first roll wins")
GraphicsWindow.DrawText(20,580,"Rolling a 7 loses")
'
'The following instruction causes the execution of
'the subroutine RollTheDice every time a mouse button
'is pressed.
'
GraphicsWindow.MouseDown = RollTheDice
EndSub

'This sub is executed every time a mouse button is pressed
Sub RollTheDice
  'If the right mouse button is pressed, end the program
  If Mouse.IsRightButtonDown Then
    Program.End()
  EndIf
  'MouseEvent is used to prevent the program from responding to
  'the mouse button being pressed again before this sub is completely executed.
  If MouseEvent = 0 then
    MouseEvent = 1
    Controls.SetTextBoxText(InstructionBox," ")
    'Create two small die
  GraphicsWindow.BrushColor = "Yellow"
  MyDie1=Shapes.AddRectangle(50,50)
  Shapes.HideShape(MyDie1)
  Shapes.Move(MyDie1,340,170)
  Shapes.ShowShape(MyDie1)
  MyDie2=Shapes.AddRectangle(50,50)
  Shapes.HideShape(MyDie2)
  Shapes.Move(MyDie2,400,170)
  Shapes.ShowShape(MyDie2)
  talpha = 50
  ' this for loop rotates the dice
  For ndexa = 1 To 20
    Shapes.Rotate(MyDie1,talpha)
    Shapes.ShowShape(MyDie1)
    Program.Delay(30)
    talpha = talpha+10
    Shapes.Rotate(MyDie2,talpha)
    Shapes.ShowShape(MyDie2)
    Program.Delay(30)
    talpha = talpha+10
  EndFor
  'Fetch randon value for each die
   Die1_number = Math.GetRandomNumber(6)
   Die2_number = Math.GetRandomNumber(6)
   'Hide the dice
   Shapes.HideShape(MyDie1)
   Shapes.HideShape(MyDie2)
   GraphicsWindow.BrushColor = "Blue"
   'Display value on each die via coloring the appropriate buttons
   Display_Die_One()
   Display_Die_two()
   'Display results of first roll of dice
   InfoBoxes()
   MouseEvent = 0
 Endif
 EndSub
 '          Each die contains nine ellipses
 '          Here are the ellipse numbers
 '         In this program ellipses are also referred
 '         to as buttons
 '        
 '          1         4       7
 '          2        5       8
 '          3        6       9
 '
 Sub Display_Die_One
   'Ensure that the die only displays the results
   'of the current roll.
    Reset_Die1()
    '
    If Die1_number = 1 Then
      'sub call
      'ellipse number five is the center ellipse and
      'it's the only ellipse that is colored if the die1_number is
      'a one.
      el_num = 5 'Select the center ellipse
      ColorAnEllipse()
       '
    ElseIf Die1_number = 2 then
      'sub call
      'die number = 2
      'ellipse number 1 and ellipse number 9 are colored
            el_num = 1
            ColorAnEllipse()
            el_num = 9
            ColorAnEllipse()
         
            '
    Elseif Die1_number = 3 then
      'sub call
      'die number = 3
      'ellipse numbers 1, 5 and 9 are colored
            el_num = 1
            ColorAnEllipse()
            el_num = 5
            ColorAnEllipse()
            el_num = 9
            ColorAnEllipse()
      '
    Elseif Die1_number = 4 then
      'sub call
      'Ellipse number 1, 3, 7, and 9 are          
            el_num = 1
            ColorAnEllipse()
            el_num = 3
            ColorAnEllipse()
            el_num = 7
            ColorAnEllipse()
            el_num = 9
            ColorAnEllipse()
   

    Elseif die1_number = 5 then
  'Ellipse number 1, 3, 5, 7 and 9 are colored
            el_num = 1
            ColorAnEllipse()
            el_num = 3
            ColorAnEllipse()
            el_num = 5
            ColorAnEllipse()
            el_num = 7
            ColorAnEllipse()
            el_num = 9
            ColorAnEllipse()    

      'subcall
    Elseif Die1_number = 6 then
      'subcall
      'Ellipse number 1, 2, 3, 7, 8 and 9 are colored
            el_num = 1
            ColorAnEllipse()
            el_num = 2
            ColorAnEllipse()
            el_num = 3
            ColorAnEllipse()
            el_num = 7
            ColorAnEllipse()
            el_num = 8
            ColorAnEllipse()
            el_num = 9
            ColorAnEllipse()
    EndIf
  EndSub
  '
  Sub Display_Die_Two
   'Ensure that the die only displays the results
   'of the current roll.
    Reset_Die2()
    '
    If Die2_number = 1 Then
      'sub call
      'ellipse number five is the center ellipse and
      'it's the only ellipse that is colored if the die2_number is
      'a one.
      el_num = 5 'Select the center ellipse
      ColorAnEllipse2()
       '
    ElseIf Die2_number = 2 then
      'sub call
      'die number = 2
      'ellipse number 1 and ellipse number 9 are colored
            el_num = 1
            ColorAnEllipse2()
            el_num = 9
            ColorAnEllipse2()
         
            '
    Elseif Die2_number = 3 then
      'sub call
      'die number = 3
      'ellipse numbers 1, 5 and 9 are colored
            el_num = 1
            ColorAnEllipse2()
            el_num = 5
            ColorAnEllipse2()
            el_num = 9
            ColorAnEllipse2()
      '
    Elseif Die2_number = 4 then
      'sub call
      'Ellipse number 1, 3, 7, and 9 are          
            el_num = 1
            ColorAnEllipse2()
            el_num = 3
            ColorAnEllipse2()
            el_num = 7
            ColorAnEllipse2()
            el_num = 9
            ColorAnEllipse2()
   

    Elseif die2_number = 5 then
  'Ellipse number 1, 3, 5, 7 and 9 are colored
            el_num = 1
            ColorAnEllipse2()
            el_num = 3
            ColorAnEllipse2()
            el_num = 5
            ColorAnEllipse2()
            el_num = 7
            ColorAnEllipse2()
            el_num = 9
            ColorAnEllipse2()    

      'subcall
    Elseif Die2_number = 6 then
      'subcall
      'Ellipse number 1, 2, 3, 7, 8 and 9 are colored
            el_num = 1
            ColorAnEllipse2()
            el_num = 2
            ColorAnEllipse2()
            el_num = 3
            ColorAnEllipse2()
            el_num = 7
            ColorAnEllipse2()
            el_num = 8
            ColorAnEllipse2()
            el_num = 9
            ColorAnEllipse2()
          EndIf
  EndSub
  '
  Sub ColorAnEllipse
    'Color an ellipse on die 1
      GraphicsWindow.BrushColor = "Black"
      ButtonNumber[el_num] = Shapes.AddEllipse(10,10)
      'Hide the button
      Shapes.HideShape(ButtonNumber[el_num])
      'Move the button
      Shapes.Move(ButtonNumber[el_num],die1_btnpos_x[el_num],die1_btnpos_y[el_num])
      'Show the button
      Shapes.ShowShape(ButtonNumber[el_num])
    EndSub
 
    Sub ColorAnEllipse2
    'Color an ellipse on die 2
      GraphicsWindow.BrushColor = "Black"
      ButtonNumber[el_num] = Shapes.AddEllipse(10,10)
      'Hide the button
      Shapes.HideShape(ButtonNumber[el_num])
      'Move the button
      Shapes.Move(ButtonNumber[el_num],die2_btnpos_x[el_num],die2_btnpos_y[el_num])
      'Show the button
      Shapes.ShowShape(ButtonNumber[el_num])
    EndSub
 
 
  Sub Reset_Die1
            el_num = 1
            UncolorAnEllipse()
            el_num = 2
            UncolorAnEllipse()
            el_num = 3
            UncolorAnEllipse()
            el_num = 4
            UncolorAnEllipse()
            el_num = 5
            UncolorAnEllipse()
            el_num = 6
            UncolorAnEllipse()
            el_num = 7
            UncolorAnEllipse()
            el_num = 8
            UncolorAnEllipse()
            el_num = 9
            UncolorAnEllipse()
  EndSub
       
  Sub Reset_Die2
            el_num = 1
            UncolorAnEllipse2()
            el_num = 2
            UncolorAnEllipse2()
            el_num = 3
            UncolorAnEllipse2()
            el_num = 4
            UncolorAnEllipse2()
            el_num = 5
            UncolorAnEllipse2()
            el_num = 6
            UncolorAnEllipse2()
            el_num = 7
            UncolorAnEllipse2()
            el_num = 8
            UncolorAnEllipse2()
            el_num = 9
            UncolorAnEllipse2()
  EndSub

  Sub UncolorAnEllipse
      GraphicsWindow.BrushColor = "White"
      ButtonNumber[el_num] = Shapes.AddEllipse(10,10)
      'Hide the button
      Shapes.HideShape(ButtonNumber[el_num])
      'Move the button
      Shapes.Move(ButtonNumber[el_num],die1_btnpos_x[el_num],die1_btnpos_y[el_num])
      'Show the button
      Shapes.ShowShape(ButtonNumber[el_num])
 EndSub

   Sub UncolorAnEllipse2
      GraphicsWindow.BrushColor = "White"
      ButtonNumber[el_num] = Shapes.AddEllipse(10,10)
      'Hide the button
      Shapes.HideShape(ButtonNumber[el_num])
      'Move the button
      Shapes.Move(ButtonNumber[el_num],die2_btnpos_x[el_num],die2_btnpos_y[el_num])
      'Show the button
      Shapes.ShowShape(ButtonNumber[el_num])
    EndSub

 
   Sub Initialize_Data
'Initialize all variables used in the program
'
'Generate buttom positions on die 1
die1_btnpos_x[1] = 120
die1_btnpos_x[2] = 120
die1_btnpos_x[3] = 120
die1_btnpos_x[4] = 185
die1_btnpos_x[5] = 185
die1_btnpos_x[6] = 185
die1_btnpos_x[7] = 250
die1_btnpos_x[8] = 250
die1_btnpos_x[9] = 250
'
die1_btnpos_y[1] = 120
die1_btnpos_y[2] = 185
die1_btnpos_y[3] = 250
die1_btnpos_y[4] = 120
die1_btnpos_y[5] = 185
die1_btnpos_y[6] = 250
die1_btnpos_y[7] = 120
die1_btnpos_y[8] = 185
die1_btnpos_y[9] = 250
'
'Generate die 2 button positions
displacement = 400
For ndex = 1 to 9
die2_btnpos_x[ndex] = die1_btnpos_x[ndex] + displacement
die2_btnpos_y[ndex] = die1_btnpos_y[ndex]
EndFor

ButtonSize_x = 10
ButtonSize_y = 10

' This flag indicates the first roll of
'the dice in a game of craps
First_Dice_Roll = 1
First_Die1_Roll_no = 0
First_Die2_Roll_no = 0
' MouseEvent prevents a mouse down event from
'being serviced while a previous mouse down event
'is being serviced.
'Intialize MouseEvent to zero to indicate that
'no mouse event is being serviced
MouseEvent = 0
InitializeMsgData()
EndSub

Sub InitializeMsgData
  'Initializes the data for mytextbox1 and mytextbox2
  'which are used to display the results
  'of the first dice roll
yposText = 500
xposText1 = 250
xposText2 = 450
xposDie1 = 320
xposDie2 = 520
yposDie = 500
EndSub

Sub InfoBoxes
  'This sub displays the first roll of the dice
  If First_Dice_Roll = 1 Then
GraphicsWindow.DrawText(xposText1+100,yposText-30, "First Roll of Dice")
GraphicsWindow.DrawText(xposText1,yposText,"Die One ")
mytextbox1 = Controls.AddTextBox(xposDie1,yposDie)
Controls.SetSize(mytextbox1,40,20)
Controls.SetTextBoxText(mytextbox1,Die1_Number)
GraphicsWindow.DrawText(xposText2,yposText,"Die Two ")
mytextbox2 = Controls.AddTextBox(xposDie2,yposDie)
Controls.SetSize(mytextbox2,40,20)
Controls.SetTextBoxText(mytextbox2,Die2_Number)
First_Die1_Roll_no = Die1_number
First_Die2_Roll_no = Die2_number
Die1_number = 0
Die2_number = 0
First_Dice_Roll = 0
EndIf
'Controls.SetTextBoxText(mytextbox,myvalue) 'overwrites previous value
DetermineWinOrLoss()
EndSub

'
Sub Draw_Buttons
  '-------------------------------------------
'Software to draw nine buttons on die_1
' and nine buttons on die-2
'Left column Button one Top
'
'Create the buttons
For i = 1 to 9
GraphicsWindow.BrushColor = "White"
ButtonNumber[i] = Shapes.AddEllipse(10,10)
'Hide the button
Shapes.HideShape(ButtonNumber[i])
'Move the button
Shapes.Move(ButtonNumber[i],die1_btnpos_x[i],die1_btnpos_y[i])
'Show the button
Shapes.ShowShape(ButtonNumber[i])
GraphicsWindow.BrushColor = "White"
ButtonNumber[i] = Shapes.AddEllipse(10,10)
'Hide the button
Shapes.HideShape(ButtonNumber[i])
'Move the button
Shapes.Move(ButtonNumber[i],die2_btnpos_x[i],die2_btnpos_y[i])
'Show the button
Shapes.ShowShape(ButtonNumber[i])
EndFor
EndSub
'
Sub DetermineWinOrLoss
First_Dice_Total = First_Die1_Roll_no + First_Die2_Roll_no
  Current_Dice_Total = Die1_number + Die2_number
If First_Dice_Total = 7 Then
  YouWin()
ElseIf First_Dice_Total = 11 Then
  YouWin()
ElseIf First_Dice_Total = 2 Then
  YouLose()
ElseIf First_Dice_Total = 3 Then
  YouLose()
ElseIf First_Dice_Total = 12 Then
  YouLose()
ElseIf Current_Dice_Total = First_Dice_Total Then
  YouWin()
Elseif Current_Dice_Total = 7 then
  YouLose()
Else
Controls.SetTextBoxText(InstructionBox,"Roll 'em again!")
Endif
EndSub

Sub YouWin
  Controls.SetTextBoxText(InstructionBox," ")
  Sound.PlayChime()
  GraphicsWindow.ShowMessage("You Win!","Craps")
  Controls.SetTextBoxText(mytextbox1," ")
  Controls.SetTextBoxText(mytextbox2," ")
  First_Dice_Roll = 1
  First_Die1_Roll_no = 0
  First_Die2_Roll_no = 0
  Controls.SetTextBoxText(InstructionBox,"  New game!")
EndSub

Sub YouLose
  Controls.SetTextBoxText(InstructionBox," ")
  Sound.PlayBellRing()
  GraphicsWindow.ShowMessage("Dang it! You lose","Craps")
  Controls.SetTextBoxText(mytextbox1," ")
  Controls.SetTextBoxText(mytextbox2," ")
  First_Dice_Roll = 1
  First_Die1_Roll_no = 0
  First_Die2_Roll_no = 0
  Controls.SetTextBoxText(InstructionBox,"  New game!")
EndSub

No comments: