The Moosader Community

Visit the IRC at irc.freenode.net #Moosader! Community dedicated to programming and game development! Moosader.com
It is currently Thu Nov 14, 2019 3:54 am

All times are UTC - 6 hours [ DST ]




Post new topic Reply to topic  [ 30 posts ]  Go to page Previous  1, 2, 3  Next
Author Message
PostPosted: Thu May 03, 2012 4:06 pm 
Offline
Koopa
Koopa

Joined: Tue Apr 10, 2012 6:07 am
Posts: 41
Hey,

Is it ok to start posting code for some of these on here? Or should I start a new thread?


Top
 Profile  
 
PostPosted: Thu May 03, 2012 5:49 pm 
Offline
Site Admin
User avatar

Joined: Wed May 14, 2008 4:43 am
Posts: 2328
Location: Kansas City
zer0c00l wrote:
Hey,

Is it ok to start posting code for some of these on here? Or should I start a new thread?


Posting links in this thread is fine. Just make sure to throw everything in pastebin, since it'll be easier for us to check that way.

_________________
Android apps by Moosader! - Open Source projects - Moosader.com


Top
 Profile  
 
PostPosted: Thu May 03, 2012 7:33 pm 
Offline
Koopa
Koopa

Joined: Tue Apr 10, 2012 6:07 am
Posts: 41
[url][/url]Ok,

Thanks for clarifying,

Will add to this post as I go then.

Part I: Input, Output, and Variables
Test 1: Short story - http://pastebin.com/x7Tw4cxh
Test 2: How much you got? http://pastebin.com/WTLL6ERe

Part II: Conditional Statements
Test 1: How old are you - http://pastebin.com/bRD14CJy
Test 2: Fraction math - http://pastebin.com/m7PaahKr
Test 3: Choose your own adventure - http://pastebin.com/BnDrSzyf

Part III: Arrays
Test 1: Party Creation - http://pastebin.com/xRteThyy
Test 2: Map Selector - http://pastebin.com/Ah3kTURM
Test 3: Map Navigator - http://pastebin.com/4PQ5Fcwr

Part IV: Loops
Test 1: RPG Battle Part I - http://pastebin.com/htyKneeD
Test 2: Weapon Shop - http://pastebin.com/50g3tz1f
Test 3: ASCII PIckin' Sticks - http://pastebin.com/SeFU0P7A

Part V: Functions
Test 1: RPG Battle Part II - http://pastebin.com/BnDrSzyf
Test 2: Where Am I? - http://pastebin.com/zLzgUi86

Part VI: Classes
Test 1: Map Navigator Part II
+Map.h - http://pastebin.com/GDvWBNpC
+Map.cpp - http://pastebin.com/7m7zhCPB
+main.cpp - http://pastebin.com/sk0zqeFx
Test 2: RPG Battle Part III
+ Stats.h http://pastebin.com/WMB5jYUg
+ Player.h http://pastebin.com/cgfZ4qRx
+ Enemy.h http://pastebin.com/3ZUtwptF
+ Stats.cpp http://pastebin.com/wQr6GpRJ
+ Player.cpp http://pastebin.com/eguMKEPw
+ Enemy.cpp http://pastebin.com/3ZUtwptF
+ main.cpp http://pastebin.com/GMEba0qj

Part VII - Inheritance
Test 1: RPG Battle Part IV
+ Stats.h http://pastebin.com/J0b4uB2W
+ Player.h http://pastebin.com/my8hSTPv
+ Character.h http://pastebin.com/mCzPgBFy
+ Enemy.h http://pastebin.com/KLYgZi5H
+ Stats.cpp http://pastebin.com/f4NEDAWS
+ Player.cpp http://pastebin.com/QVfURwes
+ Character.cpp http://pastebin.com/KrhgVQ3x
+ Enemy.cpp http://pastebin.com/QwBRacsG
+ main.cpp http://pastebin.com/UFWRLsvm

Test 2: ASCII Pickin Sticks Part II
+ Character.h http://pastebin.com/fLwHjkKm
+ Entity.h http://pastebin.com/WGckmaZk
+ Grass.h http://pastebin.com/1JmsGhYb
+ Item.h http://pastebin.com/x3tnaRwx
+ Npc.h http://pastebin.com/gTLvYBcN
+ Player.h http://pastebin.com/FqXJTbbc
+ Rock.h http://pastebin.com/SNwNH92Q
+ Stick.h http://pastebin.com/gqu3p4cH
+ Wall.h http://pastebin.com/9LAHVCpa
+ Character.cpp http://pastebin.com/H5htW0rY
+ Entity.cpp http://pastebin.com/kLxTTZYW
+ Grass.cpp http://pastebin.com/MGTn2P8z
+ Item.cpp http://pastebin.com/FXxDRWjE
+ MainLoop.cpp http://pastebin.com/gdMbkH7d
+ Npc.cpp http://pastebin.com/LU09V130
+ Player.cpp http://pastebin.com/L2ydibDg
+ Rock.cpp http://pastebin.com/G48esNig
+ Stick.cpp http://pastebin.com/vLzVjyh8
+ Wall.cpp http://pastebin.com/Q5sSa9KG

Pointers & References
Test 2- Quiz Game
+ Answer.h http://pastebin.com/tspnru2K
+ Question.h http://pastebin.com/vMnNtLWW
+ Answer.cpp http://pastebin.com/adKWUvhQ
+ Question.cpp http://pastebin.com/Ykqqa7fd
+ main.cpp http://pastebin.com/MD72autR


Last edited by zer0c00l on Wed May 23, 2012 10:23 pm, edited 17 times in total.

Top
 Profile  
 
PostPosted: Fri May 04, 2012 1:19 am 
Offline
Koopa
Koopa
User avatar

Joined: Sun Jun 19, 2011 2:52 am
Posts: 35
Location: Bethany, OK
Hmm. This sound interesting, I might do it on my free time while I'm taking a break from the current game I'm working on...


Top
 Profile  
 
PostPosted: Fri May 04, 2012 10:28 pm 
Offline
Site Admin
User avatar

Joined: Wed May 14, 2008 4:43 am
Posts: 2328
Location: Kansas City
zer0c00l wrote:
Ok,

Thanks for clarifying,

Will add to this post as I go then.

Part I: Input, Output, and Variables
Test 1: Short story - http://pastebin.com/x7Tw4cxh
Test 2: How much you got? http://pastebin.com/WTLL6ERe

Part II: Conditional Statements
Test 1: How old are you - http://pastebin.com/bRD14CJy
Test 2: Fraction math - http://pastebin.com/m7PaahKr
Test 3: Choose your own adventure - http://pastebin.com/Ln13vN3T


Alright, let's see what we have here.

Part 1 - Test 1 -
Is it just a stylistic choice to write your end lines like this?
Code:
"Oh hi, sorry about that, I suppose you came here to hear a story did you?" << "\n\n"

You could just have "...did you?\n\n"; and that would work.

Interesting story. I like the writing. :)

The only other thing that I notice is the system("PAUSE") . The only thing wrong with it is that it isn't cross-platform. You can use System() to call anything that you can call in DOS, or whatever terminal you're in.

Here's the code I used in K.C. Noire to wait for the user to hit enter:
Code:
void WaitForEnter()
{
    while ( 1 ) { if ( '\n' == getchar() ) { break; } }
}


Part 1 - Test 2 -
The way you're getting input has kind of a bad code smell- it's a lot longer than it has to be, and it's probably not a great idea to just "loop while true" and wait for a break. It kind of defers the responsibility down to some random break call - When does the loop stop? Who knows. Have to hunt for that break. Could be really iffy in a larger code-base.
Code:
string name = "";
while ( name == "" )
   cin >> name;

Or however you prefer to use your cins.

_________________
Android apps by Moosader! - Open Source projects - Moosader.com


Top
 Profile  
 
PostPosted: Fri May 04, 2012 10:49 pm 
Offline
Koopa
Koopa

Joined: Tue Apr 10, 2012 6:07 am
Posts: 41
Moosader,

Thank you for the feedback, sounds like I went overboard on the user input. My thought was to to be able to tell if the user entered the wrong input and keep asking until they entered the correct input. So for example in test 2, the users age should be an integer value, whereas if they enter a string, like "twelve" then it asks them to try again. Is there a cleaner way to do that? Or am I just approaching the problem wrong in the first place.

That being said, I now see what you are saying about how it is difficult to follow and hence bad code, just to warn you, I used this same technique for all the subsequent tests. Looks like I should go back and find a cleaner more readable way to do that.

Thank you for the tip on System("Pause"), you can probably tell I have never programmed for anything other than a Windows machine, also this is my first time using C++. I am new to programming, although I have been learning some java (sorry i know how unpopular that is here, lol.)

Lastly, regarding the newline characters being separated out from the rest of the text... I don't know why I did that, and if you look at some of my other code I am not consistent, is there a rule to follow in this case? Either way, I should at least be consistent!

EDIT: Just have to say that these "tests" are very well written and a good guide for anyone who wants to learn C++ but doesn't know where to start. For example just doing the first few of these I have learned a lot about C++ including

-forward declaration
-what .h files are and how they alleviate the tedium of forward declaration
-basic standard input/output in C++
-i am sure there is more, and will probably write them as i think of them...


Top
 Profile  
 
PostPosted: Sat May 05, 2012 1:44 pm 
Offline
Site Admin
User avatar

Joined: Wed May 14, 2008 4:43 am
Posts: 2328
Location: Kansas City
zer0c00l wrote:
Moosader,

Thank you for the feedback, sounds like I went overboard on the user input. My thought was to to be able to tell if the user entered the wrong input and keep asking until they entered the correct input. So for example in test 2, the users age should be an integer value, whereas if they enter a string, like "twelve" then it asks them to try again. Is there a cleaner way to do that? Or am I just approaching the problem wrong in the first place.

Yes, that was my point. It could be done in fewer lines like:
Code:
int age = -1;
while ( age < 0 )
{
     cout << "enter your age " << endl;
     cin >> age;
}

or
Code:
int age;
cout << "Enter your age: ";
cin >> age;
while ( age < 0 || age > 200 )
{
     cout << "Invalid age. Try again: ";
     cin >> age;
}


zer0c00l wrote:
Lastly, regarding the newline characters being separated out from the rest of the text... I don't know why I did that, and if you look at some of my other code I am not consistent, is there a rule to follow in this case? Either way, I should at least be consistent!

I've never really seen people separate the new lines from the rest of the cout like you did:
cout << "Hi!" << "\n\n";
Usually it's either:
cout << "Hi!\n\n";
or
cout << "Hi!" << endl << endl;

_________________
Android apps by Moosader! - Open Source projects - Moosader.com


Top
 Profile  
 
PostPosted: Sun May 06, 2012 11:39 pm 
Offline
Smushed Goomba
Smushed Goomba
User avatar

Joined: Sat Apr 14, 2012 8:11 pm
Posts: 8
Location: Austria
For the conditional input, consider the following solution:
Code:
#include <iostream>
#include <functional>

template <typename TYPE>
void input(TYPE& to, std::function<bool (TYPE const&)> validator)
{
   do
   {
      std::cin.sync();
      std::cin.clear();
      std::cin >> to;
   } while(std::cin.fail() || !validator(to));
}

which would replace all the while() loops with one-liners, depending on the verbosity of the validator. For example:
Code:
void Pause()
{
   std::cin.clear();
   std::cin.sync();
   while(std::cin.get() != '\n'){}
}

int main()
{
   short num = 0;

   std::cout << "Enter a number between -20 and 20: ";
   input<short>(num, [](short const& num) -> bool { return num >= -20 && num <= 20; });
   std::cout << "You entered: " << num;

   Pause();

   return 0;
}

input() would loop until the user enters a valid number, but it wouldn't print any error messages. If so desired, they could be added to the validator:
Code:
   input<short>(num, [](const short& num) -> bool
   {
      if(num < -20 || num > 20)
      {
         std::cout << "The valid range is -20 to +20, try again: ";
         return false;
      }
      return true;
   });

It doesn't get rid of the conditional statements, but it takes care of the repetitive loops and buffer synchronisation.

This utilises C++11 lambda functions (anonymous functions), GCC 4.5 and above and VC10 and above should compile it, older versions probably will not, though some with experimental C++0x support might under the tr1-namespace. It's just a proof of concept, there's room for refinement of course :)

I took the liberty of applying this to zer0c00l's fraction calculator, hope you don't mind ;)
Additionally, I fixed addition and subtraction (I think), added reduction to lowest terms (but still fractional, it will not form mixed numbers) and replaced the ifs with a switch. Also, the operation selection now accepts the appropriate symbols in addition to the option numbers.
It's pasted as a new version of the original, credit is given of course: http://pastebin.com/qGkk663u


Top
 Profile  
 
PostPosted: Mon May 07, 2012 1:03 am 
Offline
Koopa
Koopa
User avatar

Joined: Sun Jun 19, 2011 2:52 am
Posts: 35
Location: Bethany, OK
Here's my first round of test...

Assignment 1: Input, Output, and Variables.

Test 1: Short Story http://pastebin.com/T6YXqGMQ (Note: I ain't no Mark Twain lol)

Test 2: How much do you have? http://pastebin.com/yFfCD9DS

Assignment 2: Conditional Statements

Test 1: How old are you? http://pastebin.com/a4gyTR60

Test 2: Fraction Program http://pastebin.com/eLMkSbYR (Note: Beware its not pretty code :twisted: )!

Ex1: Fraction Program 2 http://pastebin.com/n8mwLy3V (Note: I rewrote my original version of the Fraction Program using a Fraction Object and Extending the standard stream library to accommodate for input/output operation on the object. Which in my opinion makes it a lot cleaner :D).

Well round 2 coming tomorrow or some time this week.


Top
 Profile  
 
PostPosted: Mon May 07, 2012 8:37 am 
Offline
Site Admin
User avatar

Joined: Wed May 14, 2008 4:43 am
Posts: 2328
Location: Kansas City
morpha wrote:
For the conditional input, consider the following solution:
Code:
#include <iostream>
#include <functional>

template <typename TYPE>
void input(TYPE& to, std::function<bool (TYPE const&)> validator)
{
   do
   {
      std::cin.sync();
      std::cin.clear();
      std::cin >> to;
   } while(std::cin.fail() || !validator(to));
}

which would replace all the while() loops with one-liners, depending on the verbosity of the validator. For example:
Code:
void Pause()
{
   std::cin.clear();
   std::cin.sync();
   while(std::cin.get() != '\n'){}
}

int main()
{
   short num = 0;

   std::cout << "Enter a number between -20 and 20: ";
   input<short>(num, [](short const& num) -> bool { return num >= -20 && num <= 20; });
   std::cout << "You entered: " << num;

   Pause();

   return 0;
}



I appreciate the effort that went into that, but this is a prerequisite test, and passing around functions really isn't part of what I'm checking for; I'm expecting the user to have a working knowledge of C++ basics.

_________________
Android apps by Moosader! - Open Source projects - Moosader.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 30 posts ]  Go to page Previous  1, 2, 3  Next

All times are UTC - 6 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: