Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Total Noob

Author
Message
mikewhl
14
Years of Service
User Offline
Joined: 19th Jul 2010
Location: Columbus,Ohio
Posted: 19th Jul 2010 18:13 Edited at: 20th Jul 2010 01:40
Hi All,
Just received my DGS in the mail. Note I have never programed anything before, so don't laugh at my code.

Anyways, I tried to write a simple program. It will compile and run, but wont do what it is suppose to do. All the stuff I've been reading, it looks like it ought to run. As you can see, I don't even know what I don't know. So could you guys look this over and educate me a little? I think I am lacking some basic understanding of what going on! I am having at it, and really enjoying learning to write code!
Thanks in advanced for any help.

Mike

SET DISPLAY MODE 640,480,16
REM Set the background color to grey
CLS RGB(120,120,120)
REM Set the font color to white on blue
INK RGB(255,255,255), RGB(0,0,255)
Randomize Timer()
Print rem put this here for spacing
Print "This is a firing simulator for the US Army's 75mmL38 Tank Gun."
Print
InPut "What is the range to target in meters? ";Range
Print
InPut "What is the gunners rating? (Scale 1-10) ";Rating
Print
InPut "How many Rounds will you fire? (1-1000) ";Shots
ToHit= 100
Dice=1
RoundCount=0
Hits=0
Start:
Do
If (rating+Dice+Rnd(99))-(range/100) >= ToHit
Inc Hits
Inc RoundCount
Ratio#= Hits/RoundCount
If RoundCount <= Shots
GoTo Start:
Else
Print "You fired ";RoundCount;" rounds."
Print
Print "You hit the target ";Hits;" times."
Print
Print "For accuracy percentage of ";ratio#;" ."
Print
Print "Hit any key to Exit"
Wait Key
End
EndIf
EndIf
Loop
End



some cool stuff someone else said here
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 19th Jul 2010 22:15
First off, welcome to the forums. I hope you like it here.

Secondly, try to frame your question / problem in such a way that it gives others a clue as to what you need. Total noob may be accurate, but it doesn't inspire many to help you.

Thirdly, when you post code, click the CODE button, paste your code and click the CODE button again. It will put it in a nice snippet box.

Next, I made some edits to your code. This way, you can play again and again if you want.



Lastly, I would encourage you to research the tutorials available. They are a tremendous help.

So many games to code.......so little time.
ghostkrashers
14
Years of Service
User Offline
Joined: 19th Jul 2010
Location:
Posted: 20th Jul 2010 01:26
That actually is pretty cool. Nice work (yes, I'm a noob too)
mikewhl
14
Years of Service
User Offline
Joined: 19th Jul 2010
Location: Columbus,Ohio
Posted: 20th Jul 2010 01:38
LFBN
I realized that my heading was "not correct", but since it was my first post, I couldn't edit it. I wanted to change it. Sorry.
Didn't know I had to put the code thing at the end too, good to know. Thanks.
FYI, I have read a lot of the tutorials and purchased a book on dark basic Pro. That is literally the first code I've ever tried to write on my own.

The changes you made, while nice, was more than I wanted. I wanted to know where in my original code did I go wrong.
It would do nothing after imputing the shots var.

Thanks again LFBN.

some cool stuff someone else said here
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 20th Jul 2010 02:06
Quote: "The changes you made, while nice, was more than I wanted. I wanted to know where in my original code did I go wrong.
It would do nothing after imputing the shots var."


Okay, sorry about that. Your code actually does work on my computer. It takes a second or two to do the calcs, but it does not freeze my computer or simply hang up. The reason it takes so long to calculate is that you are printing data to the screen every time a shot is calculated. It does return how many hits (usually all of them register as hits and the percentage is 1 instead of 100). I was hoping that you could take the edited version of code and compare it to the original to determine how things could work better. Obtaining the hits was a bit of trial and error on my part. The edited code runs the total shots before printing. (While I'm at it, you might want to use the TEXT command instead of the PRINT command).

I have told two other coders this week about the potential difficulties using GOTO will bring you. I would suggest you learn to code using GOSUBs or FUNCTIONs. I hope you will put GOTO away for good.

The variable range / 100 is always going to return 0 if you enter any value less than 100. Dividing it by 50 seems to work better.

You'll notice that I put in a WHILE / ENDWHILE loop. This is because we want the code to execute before we test to see if RoundCount = shots. (Since RoundCount starts at 0, this is correct). In your code, it comes out 1 higher than it should. This also provides a way to calculate all of the shots quickly and efficiently.

Anyway, hope this helps. Best of luck to you.

So many games to code.......so little time.
mikewhl
14
Years of Service
User Offline
Joined: 19th Jul 2010
Location: Columbus,Ohio
Posted: 20th Jul 2010 02:51
Thanks again LFBN!

I see what you mean about printing data to screen (never occurred to me that was what I was asking it to do).
This little program was written as a self imposed pop quiz. The actual hit or miss part is just to make it seem more gamey. The formula was something I just made up.
Agreed on the GoTo. All the books and tut's I've read repeat your warning over and again. I used it here because of the limited scope of the code.
Great help with the While, EndWhile loop. Now that is info a noob can use!

Thanks again LFBN!

some cool stuff someone else said here
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 20th Jul 2010 16:06
Quote: "Agreed on the GoTo."
Good to hear. You will not regret it, especially if you write larger programs. Having good program structure is extremely important, especially when you have bugs or have difficulties with part of your code.

It is also good that you are working some tuts and learning. Programming can be very rewarding and enjoyable (albeit there are times it is aggravating too ), but it takes some effort to get where you need to be.

In addition to WHILE / ENDWHILE, I use REPEAT / UNTIL a lot. I seldom use DO / LOOP anymore, because you have to add an exit condition within it and REPEAT / UNTIL gives you one less thing to do.

Quote: "Thanks again LFBN!"


You are very welcome.

So many games to code.......so little time.

Login to post a reply

Server time is: 2024-09-28 20:32:38
Your offset time is: 2024-09-28 20:32:38