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 AppGameKit Corner / [Score/Levels/High score] - Padding with 0's

Author
Message
damothegreat
User Banned
Posted: 13th Oct 2016 20:09
Hello

Does anyone like something to pad 0's to a current score/level/highscore etc etc

Here is a function called "integer2stringwithpadding" I created to do the trick

for example
gamescore=1392
gamescorestring=integer2stringwithpadding(str(gamescore),"0",8) "
will return "00001392"

gamescore=13922
gamescorestring=integer2stringwithpadding(str(gamescore),"0",8) "
will return "00013922"

gamescore=13
gamescorestring=integer2stringwithpadding(str(gamescore),"0",4) "
will return "0013"

gamescore=832
gamescorestring=integer2stringwithpadding(str(gamescore),"0",6) "
will return "000832"

etc
etc




-------------------------------------------------------------------------------------------------------------------------------
score as integer
gamescore as integer
gamescorestring as string

createtext (score, "Score = " + gamescore)
do
gamescore=gamescore+5
gamescorestring=integer2stringwithpadding(str(gamescore),"0",8)

SetTextString(score,"Score = " + gamescorestring)
sync()
loop

function integer2stringwithpadding(stringx as string, pad as string, strlen as integer)
returnnewstr as String
for a=1 to strlen-len(stringx)
returnnewstr=returnnewstr+pad
next

returnnewstr=returnnewstr+stringx

endfunction returnnewstr



Hope it helps some of you

Damo
Kevin Picone
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 14th Oct 2016 00:25
Just add a string of zeros to the string of the value and then grab the right string of that.



So the score will be padded to 8 characters.

PlayBASIC To HTML5/WEB - Convert PlayBASIC To Machine Code
Scraggle
Moderator
20
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 14th Oct 2016 08:21
Here are my pad functions:

Pad Left:


Pad Right:


To call it:

That would print 00001392
AGK V2 user - Tier 1 (mostly)
Scraggle
Moderator
20
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 14th Oct 2016 12:03
I just realised that the pad functions call another function insertString() which in turn goes on to call two of my other functions.
So it's probably easier if I give you the whole file.
Just #include it in any of your projects and call the functions as normal.

AGK V2 user - Tier 1 (mostly)

Attachments

Login to view attachments
damothegreat
User Banned
Posted: 14th Oct 2016 12:28
Thanks guys

Damo
DavidAGK
AGK Developer
10
Years of Service
User Offline
Joined: 1st Jan 2014
Location:
Posted: 15th Oct 2016 20:24
I asked about this but with the addition of commas to separate thousands a while back. Search on score and number format and you should find it. I got some great advice and code.
Using Tier 1 AppGameKit V2
Started coding with AMOS (Thanks Francois Lionet)
DavidAGK
AGK Developer
10
Years of Service
User Offline
Joined: 1st Jan 2014
Location:
Posted: 15th Oct 2016 20:28

https://forum.thegamecreators.com/thread/214418#msg2559668
Using Tier 1 AppGameKit V2
Started coding with AMOS (Thanks Francois Lionet)
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 16th Oct 2016 21:55
Years back, several of us wrote snippets to convert to and from roman numerals, if you were interested in doing something fancier. I really only mention this cause I won that challenge

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
damothegreat
User Banned
Posted: 17th Oct 2016 10:28
Wow - Roman Numerals conversion seems to be a good challenge - I shall have a go with it next few days and then pop my code on
here to see if my programming skills does meet up to the level of everyone here


Damo
Using Tier 1 AppGameKit V2
Started coding with AMOS
damothegreat
User Banned
Posted: 17th Oct 2016 16:41
My code


//--------------------------------------------------------------------------------------------------
// Project: Roman by Damo
// Created: 2016-10-17
//--------------------------------------------------------------------------------------------------


// set window properties
SetWindowTitle( "Roman" )
SetWindowSize( 1024, 768, 0 )

// set display properties
SetVirtualResolution( 1024, 768 )
SetOrientationAllowed( 1, 1, 1, 1 )
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery


// Min and Max = 1 to 4999
start=2000
startvalue=Start
do
repeat
print ("Calculating " + str(startvalue))

//Do a calculation
//Check for thousands
result=start/1000
if result>0
thousands=result
start=start-(result*1000)
for a=1 to result
romanvalue=romanvalue+"M"
next


endif

result=start/100
if result>0
hundreds = result
start=start-(result * 100)
if hundreds=1 then romanvalue=romanvalue+"C"
if hundreds=2 then romanvalue=romanvalue+"CC"
if hundreds=3 then romanvalue=romanvalue+"CCC"
if hundreds=4 then romanvalue=romanvalue+"CD"
if hundreds=5 then romanvalue=romanvalue+"D"
if hundreds=6 then romanvalue=romanvalue+"DC"
if hundreds=7 then romanvalue=romanvalue+"DCC"
if hundreds=8 then romanvalue=romanvalue+"DCCC"
if hundreds=9 then romanvalue=romanvalue+"CM"

endif

result=start/10
if result>0
tens=result

start=start-(result * 10)

if tens=1 then romanvalue=romanvalue+"X"
if tens=2 then romanvalue=romanvalue+"XX"
if tens=3 then romanvalue=romanvalue+"XXX"
if tens=4 then romanvalue=romanvalue+"XL"
if tens=5 then romanvalue=romanvalue+"L"
if tens=6 then romanvalue=romanvalue+"LX"
if tens=7 then romanvalue=romanvalue+"LXX"
if tens=8 then romanvalue=romanvalue+"LXXX"
if tens=9 then romanvalue=romanvalue+"XC"


endif

if start=1 then romanvalue=romanvalue+"I"
if start=2 then romanvalue=romanvalue+"II"
if start=3 then romanvalue=romanvalue+"III"
if start=4 then romanvalue=romanvalue+"IV"
if start=5 then romanvalue=romanvalue+"V"
if start=6 then romanvalue=romanvalue+"VI"
if start=7 then romanvalue=romanvalue+"VII"
if start=8 then romanvalue=romanvalue+"VIII"
if start=9 then romanvalue=romanvalue+"IX"



print ("Result = There are ")
print(str(thousands) + " Thousands")
print(str(hundreds) + " Hundreds")
print(str(tens) + " Tens")
print(str(start) + " Units in " + str(startvalue))
print(" Roman Value = " + romanvalue)

romanvalue=""
start=startvalue
print ("Press Space Mouse button for next random number")

sync()
until GetRawKeyPressed(32)=1


start=random(1,4999)
startvalue=Start
hundreds=0
thousands=0
tens=0
romanvalue=""

loop


//--------------------------------------------------------------------------------------------------
// End code
//--------------------------------------------------------------------------------------------------
Using Tier 1 AppGameKit V2
Started coding with AMOS
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 17th Oct 2016 21:15 Edited at: 17th Oct 2016 21:27
If you'd like to look at all the previous entries:
http://dbcc.zimnox.com/?page=view&challenge=Roman%20to%20Arabic&lang=


Straying a little off topic, but if you're interested in checking out code challenges we used to do, here's the thread for DBP. They're all on my site above as well. I think.
https://forum.thegamecreators.com/thread/48011

I forgot we had one for DBC too, which is actually where the roman conversion challenge was.
https://forum.thegamecreators.com/thread/97545?page=69

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
Ortu
DBPro Master
16
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 19th Oct 2016 20:57
interesting challenge, thought I'd give it a go before I look at the other entries, here's what I came up with (dbp, but easy enough to convert):



A single player RPG featuring a branching, player driven storyline of meaningful choices and multiple endings alongside challenging active combat and intelligent AI.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 20th Oct 2016 17:00
If it worked through all the tests, I'd say you would've had a shot at winning

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds

Login to post a reply

Server time is: 2024-04-20 01:13:39
Your offset time is: 2024-04-20 01:13:39