Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
b+ Beginners Corner
#1
I hope to post fun little bits of code and then try to comment them as fully as I can in attempts to get people new to QB64 up and running faster. Feel free to ask questions to get clarification, I may have screwed up.

This is something I posted at Friends after messing around with a dice roll thing at Aurels place:
BTW that's here: https://friends-of-basic.freeforums.net
and here: http://basic4us.epizy.com/forum/index.php

Here is original post:
Code: (Select All)
_Title "Playing for Charity: 100 dice" ' b+ 2023-05-20
' A game that no one loses!
Randomize Timer
Dim As Long roll, Charity, me, r
For roll = 1 To 100
    r = Int(Rnd * 6) + 1
    Print "  Roll number:"; roll; "is"; r
    If r < 5 Then Charity = Charity + r Else me = me + r
    Print: Print "         I get 5's and 6's:"; me
    Print " Charity gets 1, 2, 3, 4's:"; Charity
    Print: Print "     zzz... press any to continue"
    Print
    Sleep
Next
Print: Print " Treat ";
If me > Charity Then Print "yourself"; Else Print "Others";
Print " specially well today."

Then I decided if I really wanted to do something Friendly I should comment more fully for Beginners.
So here is same resulting output with lots of comments:
Code: (Select All)
' The following not in original code but a very good habit to get into, it will save you
' from your typos. 3P = Proper Programming Practice :)
Option _Explicit ' This forces you to Dim (or ReDim) your variables before you use them.
' !!! EXCEPTION !!! should you do something like DEFINT A-Z or DEFLNG A-Z then everything
' is automatically. :O

' If the following is in the first lines of your program and you go to save it, QB64 IDE
' will suggest your _Title string for your .bas file name when starting from File > New.
_Title "Playing for Charity: 100 dice" ' b+ 2023-05-20   < 3P author and date with _Title.
' A game that no one loses! < 3P a quick summary or comments. I usually list versions here.

Randomize Timer ' this is here so I don't play the same game over on each run.

' Dim As Long roll, Charity, me, r  < 3P originally I had this, but really strong 3P is
' declaring all your variables first 2nd part of Option _Explicit team.
Dim As Long roll '            this tracks the For loop index eg roll number
Dim As Long r '               this is for random to be used to store the dice roll value.
Dim As Long Charity '         variable to store running total points for Charity
Dim As Long me '              variable to store running total points for me.
' I am using Long because I want Integers and Long is easier to type and doesn't take more
' time than Integer Type. So I get a range of over 32,000 with 0 cost.

For roll = 1 To 100 '         the main loop does 100 rolls

    r = Int(Rnd * 6) + 1 '    Rnd = 0 to .9999 Multiply by 6 and have 0 to 5.9999
    '                         Take Int(0 to 5.99999) get 0,1,2,3,4,5
    '                         Add 1 for 1 to 6
    '
    ' Print something; < with semi colon ending keeps the print head right where it stops.
    ' Print something, < with comma tabs the print head right to set column widths
    ' Print something  < nothing after literals or variables moves print head to next line.
    ' Print will automatically start on next line if can't finish print job on current one.
    ' Print:           < finishes last print line or starts new one by inserting blank line.
    Print "  Roll number:"; roll; "is"; r ' this first line reporting roll number and roll.

    If r < 5 Then Charity = Charity + r Else me = me + r ' this decides if me or Charity
    ' gets the rolled points

    ' This reports the running scores and tells Sleep is activated so we have to press a key
    ' to continue game.
    Print: Print "         I get 5's and 6's:"; me
    Print " Charity gets 1, 2, 3, 4's:"; Charity
    Print: Print "     zzz... press any to continue"
    Print
    Sleep
Next ' end of 100 rolls loop onto summary result.

Print: Print " Treat ";
If me > Charity Then Print "yourself"; Else Print "Others";
Print " specially well today."
' the End  < 3P if you had GoSubs after this End put an End statement here.

I decided to post the 2nd one here first because A) this is my favorite forum and B) it is likely to be seen more here.
C) I thought it an excellent first post for this thread.
b = b + ...
Reply


Messages In This Thread
b+ Beginners Corner - by bplus - 05-20-2023, 06:34 PM
RE: b+ Beginners Corner - by vince - 05-20-2023, 06:47 PM
RE: b+ Beginners Corner - by bplus - 05-20-2023, 07:11 PM
RE: b+ Beginners Corner - by PhilOfPerth - 05-26-2023, 12:12 AM
RE: b+ Beginners Corner - by bplus - 05-26-2023, 04:11 PM
RE: b+ Beginners Corner - by PhilOfPerth - 05-26-2023, 11:18 PM
RE: b+ Beginners Corner - by mnrvovrfc - 05-27-2023, 12:15 AM
RE: b+ Beginners Corner - by PhilOfPerth - 05-27-2023, 02:27 AM
RE: b+ Beginners Corner - by bplus - 05-29-2023, 12:07 AM
RE: b+ Beginners Corner - by bplus - 05-29-2023, 01:37 AM
RE: b+ Beginners Corner - by mnrvovrfc - 05-29-2023, 02:29 AM
RE: b+ Beginners Corner - by bplus - 05-30-2023, 04:17 PM
RE: b+ Beginners Corner - by bplus - 06-15-2023, 03:06 PM
RE: b+ Beginners Corner - by GareBear - 06-15-2023, 07:50 PM
RE: b+ Beginners Corner - by bplus - 06-15-2023, 10:42 PM
RE: b+ Beginners Corner - by bplus - 06-23-2023, 02:46 PM
RE: b+ Beginners Corner - by CharlieJV - 06-23-2023, 03:26 PM
RE: b+ Beginners Corner - by bplus - 06-23-2023, 08:28 PM
RE: b+ Beginners Corner - by TerryRitchie - 06-23-2023, 09:45 PM
RE: b+ Beginners Corner - by bplus - 06-23-2023, 09:56 PM
RE: b+ Beginners Corner - by TerryRitchie - 06-24-2023, 02:47 AM
RE: b+ Beginners Corner - by bplus - 06-23-2023, 10:02 PM
RE: b+ Beginners Corner - by Dimster - 06-24-2023, 02:35 PM
RE: b+ Beginners Corner - by bplus - 06-24-2023, 02:52 PM
RE: b+ Beginners Corner - by Dimster - 06-24-2023, 07:48 PM
RE: b+ Beginners Corner - by mnrvovrfc - 06-24-2023, 08:02 PM
RE: b+ Beginners Corner - by bplus - 06-24-2023, 08:40 PM
RE: b+ Beginners Corner - by TerryRitchie - 06-24-2023, 10:07 PM
RE: b+ Beginners Corner - by bplus - 06-24-2023, 09:08 PM
RE: b+ Beginners Corner - by Dimster - 06-24-2023, 09:12 PM
RE: b+ Beginners Corner - by mnrvovrfc - 06-24-2023, 11:44 PM
RE: b+ Beginners Corner - by bplus - 06-25-2023, 02:27 PM
RE: b+ Beginners Corner - by OldMoses - 06-25-2023, 05:49 PM
RE: b+ Beginners Corner - by bplus - 06-25-2023, 06:40 PM
RE: b+ Beginners Corner - by OldMoses - 06-25-2023, 08:03 PM
RE: b+ Beginners Corner - by bplus - 06-26-2023, 01:14 AM
RE: b+ Beginners Corner - by mnrvovrfc - 06-26-2023, 02:26 AM
RE: b+ Beginners Corner - by Ultraman - 06-26-2023, 11:29 AM
RE: b+ Beginners Corner - by bplus - 06-26-2023, 12:17 PM
RE: b+ Beginners Corner - by Ultraman - 06-26-2023, 12:21 PM
RE: b+ Beginners Corner - by Dimster - 06-26-2023, 02:38 PM
RE: b+ Beginners Corner - by bplus - 06-26-2023, 03:32 PM
RE: b+ Beginners Corner - by bplus - 06-26-2023, 04:48 PM
RE: b+ Beginners Corner - by bplus - 06-27-2023, 01:29 AM
RE: b+ Beginners Corner - by OldMoses - 06-27-2023, 11:49 AM
RE: b+ Beginners Corner - by bplus - 06-27-2023, 12:40 PM
RE: b+ Beginners Corner - by mnrvovrfc - 06-27-2023, 02:12 PM
RE: b+ Beginners Corner - by bplus - 06-27-2023, 03:22 PM
RE: b+ Beginners Corner - by mnrvovrfc - 06-27-2023, 05:21 PM
RE: b+ Beginners Corner - by bplus - 06-27-2023, 05:48 PM
RE: b+ Beginners Corner - by bplus - 06-28-2023, 03:20 AM
RE: b+ Beginners Corner - by bplus - 06-28-2023, 02:54 PM
RE: b+ Beginners Corner - by mnrvovrfc - 06-28-2023, 07:07 PM
RE: b+ Beginners Corner - by Dimster - 06-28-2023, 09:50 PM
RE: b+ Beginners Corner - by bplus - 06-28-2023, 10:27 PM
RE: b+ Beginners Corner - by bplus - 06-04-2024, 01:17 AM
RE: b+ Beginners Corner - by PhilOfPerth - 06-04-2024, 11:37 PM
RE: b+ Beginners Corner - by bplus - 06-05-2024, 12:42 AM
RE: b+ Beginners Corner - by gaslouk - 06-05-2024, 02:37 PM
RE: b+ Beginners Corner - by bplus - 06-30-2024, 07:38 PM
RE: b+ Beginners Corner - by bplus - 07-01-2024, 03:42 PM
RE: b+ Beginners Corner - by aurel - 07-01-2024, 06:16 PM
RE: b+ Beginners Corner - by bplus - 07-01-2024, 07:39 PM
RE: b+ Beginners Corner - by bplus - 07-07-2024, 06:42 PM



Users browsing this thread: 1 Guest(s)