Help with making my code smaller.
Author |
Message |
Arkhanno
|
Posted: Sat Jul 04, 2009 4:30 am Post subject: Help with making my code smaller. |
|
|
I'm a bit of a beginner when it comes to programming, but I'm trying to create a battle calculator. I've been trying to keep my code as neat and small as possible, but there are some points that I just don't know how to make smaller. Code is below.
Turing: |
setscreen ("text")
type Stats : record
Name : string
HP : int
Str : int
Def : int
Spd : int
Int : int
Arm : int
end record
var player, monster : array 1 .. 4 of Stats
var position : array 1 .. 8 of string
var currentp, currentm, playernum, monsternum, food, spell : int
put "How many players are there?"
get playernum
put ""
put "How many monsters are there?"
get monsternum
put ""
currentp := 0
for i : 1 .. upper (player )
currentp := currentp + 1
put "What is player ", currentp, "'s name and stats?"
get player (i ).Name, player (i ).HP, player (i ).Str, player (i ).Def, player (i ).Spd, player (i ).Int, player (i ).Arm
exit when currentp = playernum
end for
currentm := 0
for x : 1 .. upper (monster )
currentm := currentm + 1
put "What is monster ", currentm, "'s name?"
get monster (x ).Name, monster (x ).HP, monster (x ).Str, monster (x ).Def, monster (x ).Spd, monster (x ).Int, monster (x ).Arm
exit when currentm = monsternum
end for
if (playernum = 4) and (monsternum = 4) then
if (player (1).Spd > player (2).Spd ) and (player (1).Spd > player (3).Spd ) and (player (1).Spd > player (4).Spd ) and (player (1).Spd > monster (1).Spd ) and (player (1).Spd > monster (2).Spd ) and (player (1).Spd > monster (3).Spd ) and (player (1).Spd > monster (4).Spd ) then
position (1) := player (1).Name
end if
end if |
The reason the code is small is because I'm doing checks to see if it runs properly as I write it, so I don't have to go back and do major changes when it gets too big. Is there any way to shorten the speed check to set positions? I'd rather not go much further before fixing this. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
ecookman
|
Posted: Sat Jul 04, 2009 11:44 am Post subject: RE:Help with making my code smaller. |
|
|
i am not a turing master my self, but that looks like it can't be shrunk.
also when going back to look at code it is helpful to put in comments as you go along by putting % before a line
comments are lines of code that are within the code, but are ignored by the computer |
|
|
|
|
|
Zren
|
Posted: Sat Jul 04, 2009 2:10 pm Post subject: Re: Help with making my code smaller. |
|
|
Making it small and all is nice. However, people shouldn't have to look at the source in order to run it. Thus, Have some user output for the stats...
code: | var currentp, currentm, playernum, monsternum, food, spell : int := 0 |
thus killing the need for:
code: | currentm := 0
currentp := 0 |
Actually, you don't even need those variables...
code: | currentp := 0
for i : 1 .. upper (player)
currentp := currentp + 1
put "What is player ", currentp, "'s name and stats?"
get player (i).Name, player (i).HP, player (i).Str, player (i).Def, player (i).Spd, player (i).Int, player (i).Arm
exit when currentp = playernum
end for |
can become
code: | for i : 1 .. upper (player)
put "What is player ", i, "'s name and stats?"
get player (i).Name, player (i).HP, player (i).Str, player (i).Def, player (i).Spd, player (i).Int, player (i).Arm
exit when i = playernum
end for |
Why would you have two counters?
Efficient coding is good. However, unreadble and lazey coding that doesn't serve it's purpose, or the user doesn't understand what to do, is reaaaaaaally frowned upon, by me at least. |
|
|
|
|
|
ecookman
|
Posted: Sat Jul 04, 2009 2:33 pm Post subject: RE:Help with making my code smaller. |
|
|
hmm i never thought of that zren |
|
|
|
|
|
Arkhanno
|
Posted: Sat Jul 04, 2009 11:43 pm Post subject: Re: Help with making my code smaller. |
|
|
I've been improving my skills and looking around: I added a bubble sort for the speed check so I wouldn't have a giant set of if statements, which eliminated the counter. Again, the code is just in the beginning stages (And I'm still a poor programmer) and will be a lot bigger when it's finished. As for the comments, the reason I haven't been using them is because I'm the only one that's going to be looking at/changing the code around, at least for the most part. I didn't add any when posting it here because right now the code is straight forward and easily readable. If there was something that was more esoteric then I would have added a comment.
Thanks for the help; gonna go back to making the food and spell parts of the system. |
|
|
|
|
|
|
|