How does your Pseudocode look?

Started by
22 comments, last by 100GPing100 10 years, 1 month ago

Great variation of approaches. Looks like I am going to stick to the PDL design, which seems to be the common factor of the posts here, for most of my projects. I am sure there are times when I will do something like you NightCreature, especially if I happen to be doing some type of program for a robot or something. Thanks again! I am going to go and practice PDL to see how quickly I can get something done using that method.

They call me the Tutorial Doctor.

Advertisement

Oh Yeah! This is going to do just fine.


--while a key is pressed, start the character walking. When the key is released, stop walking. 
-- walking is playing an animation while translating the character. "Stop walking" should not only stop the animation, but set the animation to it's first frame one the walk animation has finished.
--Use two booleans to turn walking on and off. If walking is on, thhe walking function plays. If walking is off, the walking function stops. 

walking = false
idling = true

function walk(character,speed) --character and speed not added yet.
	if onKeyDown(key) and idling then
		walking = true
		idling = false
		playAnimation(walk_animation)
	else if onKeyUP(key) and walking then
		idling = true
		walking = false
		playAnimation(idling_animation)
	end
end



--Game Loop
function onSceneUpdate()
	walk(joe,2)-- joe walks at 2 units per second/frame.
end


--Turn a light on when a button is pressed:
 
--if a key is pressed down, turn on a light and play a sound. When it is released, the light should be on still, and another sound should play. 
 
 
if isKeyPressed(key) then
    turnOnLight(light) --sets a light variable to true and turns a gameObject light brighteness to 100%
    playSound(sound) --plays a sound
end
 
if isKeyReleased(key)
    playSound(sound2) --plays a sound
end

They call me the Tutorial Doctor.

Oh Yeah! This is going to do just fine.

--while a key is pressed, start the character walking. When the key is released, stop walking. 
-- walking is playing an animation while translating the character. "Stop walking" should not only stop the animation, but set the animation to it's first frame one the walk animation has finished.
--Use two booleans to turn walking on and off. If walking is on, thhe walking function plays. If walking is off, the walking function stops. 

walking = false
idling = true

function walk(character,speed) --character and speed not added yet.
	if onKeyDown(key) and idling then
		walking = true
		idling = false
		playAnimation(walk_animation)
	else if onKeyUP(key) and walking then
		idling = true
		walking = false
		playAnimation(idling_animation)
	end
end



--Game Loop
function onSceneUpdate()
	walk(joe,2)-- joe walks at 2 units per second/frame.
end

--Turn a light on when a button is pressed:
 
--if a key is pressed down, turn on a light and play a sound. When it is released, the light should be on still, and another sound should play. 
 
 
if isKeyPressed(key) then
    turnOnLight(light) --sets a light variable to true and turns a gameObject light brighteness to 100%
    playSound(sound) --plays a sound
end
 
if isKeyReleased(key)
    playSound(sound2) --plays a sound
end
Wow. When did you start learning C++?

UNREAL ENGINE 4:
Total LOC: ~3M Lines
Total Languages: ~32

--
GREAT QUOTES:
I can do ALL things through Christ - Jesus Christ
--
Logic will get you from A-Z, imagination gets you everywhere - Albert Einstein
--
The problems of the world cannot be solved by skeptics or cynics whose horizons are limited by the obvious realities. - John F. Kennedy


Wow. When did you start learning C++?

Assuming the first part of each snippet is the pseudo-code, followed by the proper implementation, the stuff you quoted is not C++:

- Comments in C++ do not start with "--", they start with "//" (or "/*" ended with "*/").

- Expressions in C++ end with a ";".

- When declaring variables, you need to include the data type (in this case, most likely "bool" before "walking" and "idling").

- Function definitions in C++ do not start with the word "function". They start with the return type of the function (e.g. "void", "int", "float"). "function" could be a badly named data type, but the function doesn't return anything, so it isn't.

- Function definitions in C++ include the data type before the parameter.

- Functions and if statements in C++ do not end with "end". Scope is indicated by "{" and "}".

- If statements with multiple conditionals do not use "and" as a keyword. In this case, "&&" would be the correct operator.

- If statements do not have a "then" in them to signify start of block.

- If statements with multiple expressions in C++ are scoped using the "{" and "}". Failure to do so can lead to stuff like the "goto fail" Apple bug that was recently discovered.

There are potential other issues as well, but for the sake of this reply I'm assuming those things were done correctly outside of the code snippet in a C++ scenario.

---

EDIT: On-topic: I tend to do the same as Servant.

Hello to all my stalkers.

Pseudocode? What's that? XD

I have two approaches:

1) When explaining to others, I usually write short amounts of code, cutting out any unnecessarry stuff with "// snip" line (or replacing them with arbitrary methods), similar to what Servant does.

2) When doing it for myself, I write it mostly from head; when dealing with a complex problem, I usually take pencil and paper and sketch what it has to do, simulate algorithm behavior or some other things without writing any (pseudo)code, depending on the simulation. For example, if I need to simulate cannon fire, I'd sketch the path of cannon fire and write formulas for sling etc., then try to implement that in the code.

IMO it doesn't make sense to write psudocode in most cases. I might use a UML diagram to design objects and their responsibility, but for functions the approach is different. I just write out set by step, what the function does in actual function calls and control flow. Then for each function call I stub out the definition. Then you fill in each function so created by the same process, possibly adding parameters as needed.

So using Servant of the Lord's example, a first pass would look something like this
bool LoadConfigFile(std::vector<result> &results, const std::string &filepath){
     auto file = load_file(filepath);
     if(!file) {
           log_error("Could not open file");
           return false;
     }
     while (auto line = read_line(file) ){
           if(line_is_comment(line) ) 
               continue;
           auto var_name = get_var_name(line);
           auto var_value = get_var_value();            
           results.emplace_back({var_name, var_value});           
     }  
     return true;
}
Notice that that function a nice and short 13 lines. That's about as short as I try to make my functions. About 20 lines is the cutoff for when I'd think about splitting code out into another function. So if say, the while loop became too busy and long, I'd try to replace the while body with just a function or close to it.

Wow. When did you start learning C++?

Assuming the first part of each snippet is the pseudo-code, followed by the proper implementation, the stuff you quoted is not C++:
- Comments in C++ do not start with "--", they start with "//" (or "/*" ended with "*/").
- Expressions in C++ end with a ";".
- When declaring variables, you need to include the data type (in this case, most likely "bool" before "walking" and "idling").
- Function definitions in C++ do not start with the word "function". They start with the return type of the function (e.g. "void", "int", "float"). "function" could be a badly named data type, but the function doesn't return anything, so it isn't.
- Function definitions in C++ include the data type before the parameter.
- Functions and if statements in C++ do not end with "end". Scope is indicated by "{" and "}".
- If statements with multiple conditionals do not use "and" as a keyword. In this case, "&&" would be the correct operator.
- If statements do not have a "then" in them to signify start of block.
- If statements with multiple expressions in C++ are scoped using the "{" and "}". Failure to do so can lead to stuff like the "goto fail" Apple bug that was recently discovered.

There are potential other issues as well, but for the sake of this reply I'm assuming those things were done correctly outside of the code snippet in a C++ scenario.
---
EDIT: On-topic: I tend to do the same as Servant.
I knew it wasn't c++ but it was good pseudocode

UNREAL ENGINE 4:
Total LOC: ~3M Lines
Total Languages: ~32

--
GREAT QUOTES:
I can do ALL things through Christ - Jesus Christ
--
Logic will get you from A-Z, imagination gets you everywhere - Albert Einstein
--
The problems of the world cannot be solved by skeptics or cynics whose horizons are limited by the obvious realities. - John F. Kennedy

My pseudocode is always a fuzzy C++, with some inaccuracies to allow laziness in expressing thoughts.

To be honest, I hate pseudocode. When writing pseudocode, I find that I have to revise my ad-hoc grammar repeatedly to allow the rest of the pseudocode to make any sense at all. At the end, I've about made a new language, and it was no simpler. I may as well basically use an existing language as a basis; after all, my usual goal of pseudocode is not to pass through pseudolint, but to quickly prototype and explain ideas to others, where the reader can fill in blanks.

So, for me, I write in fuzzy, untested C++. In communicating with others, a fuzzy C. Who doesn't know C?

My pseudocode looks like Python, because Python looks like pseudocode anyway haha (I'm serious).

Creator and only composer at Poisone Wein and Übelkraft dark musical projects:

I'll do hand drawn flow charts if the program is very complex, but I usually don't do pseudocode. I just write it in a real programing language with some dummy methods for things that aren't clear.

SAMULIKO: My blog

[twitter]samurliko[/twitter]

BitBucket

GitHub

Itch.io

YouTube

This topic is closed to new replies.

Advertisement