C++ Learning Community Forum
September 09, 2010, 07:54:45 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Hello. Smiley
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: New Style Of Pesudocode( Shonoby Style hehe )  (Read 604 times)
Shonoby
Programmer
Dr. of C++ology
****
Posts: 659


Pixel Artist


View Profile
« on: August 02, 2007, 06:00:45 AM »

Well, since i had to get back into programming i thought the best way to do this was make a hangman program. The reason i chose to do a hangman program was because i had tried to do it before and failed( quit miserably too ). Now while i was on "vacation" i might not have programmed but i sure did learn and develop a few new things. Since i have always had trouble organizing my codes, creating classes( i.e. knowing what to put in a class ), while i was on the "vacation" i developed a type of pesudocode( that i used during many other pesudocodes, but this time i created it into a template thing ). This pesudocode is split into two phases:
In phase 1, you write out the program as it is going to be in the console window, BUT you add a little bit of coding in it. Such as, if there were a line in phase 1 that use a variable, then you would make it stand out on the paper( i.e. make a key as in, variables are red, class variables are blue, also you can have switch strucs etc ). This helps you plan out your code in the sense of output, but the next phase will do the other.
In phase 2, you create a list of variables, classes, functions, etc; that you will need in your code. You can figure this out by looking at your phase 1 section. Which should have variables, class variables etc; in it. This way you can figure out almost all the essential parts to your code, as far as variables, and functions go. Then the rest is up to you, you have to string everything together, do the implemention of the functions, variables etc. But this style of pesudocode is meant to make you create a template ( on your code ) in the general, so you might have to change/add/remove things as you progress. Besides that your code should come out looking something like your phase 1.
Well if anyone follows this kind of pesudocode, feel free to add anything new, like a new phase or a new section etc. I really tend to make things that are not set, as in you can change and mold them into whatever you need. So you don't have to follow my instructions to the bone, just as long as you can follow it.
Since i developed this form of pesudocode in my "vacation" i did not have time to test it out until today, so here is what i made( along with my hangman code ).
*NOTE* This is exactly how my pesudocode was( maybe not as neat( i.e. handwriting sucks ) ).
Quote
                                                                         HANGMAN
PHASE 1:
Main
    Hangman:
*  { if empty, display nothing }
*  else { display body }

    Pevious Guesses:
*  { if none, display nothing }
*   else { display guesses }

    Guess: *var$string Guess

*  { if *Guess != *var$string Current_Word }
    ( *var$int Incorrect++
        { if Incorrect >= 7 }
         ( Display x hangedman, Display x guesses,
            Display *Current_Word, ask to replay - )
         else { continue game }
     )
-    else { "congratulation, you guess right, play again, Y/N" )
            { if *var$char replie != 'Y' }
            ( end game )
             else { New game -- )
--   Choose New current_word, reset var$

PHASE 2:
CLASS
      Game_Int
public:
-Get vector list = *List
-If incorrect <= 7 return true
-set current word
-get current word
-set guess
-get guess
-add guess to vector of guesses
-get the guesses from vector
-print hangman
private:
*vector of strings - list
*string - Current_Word
*vector of strings - Guesses
*int - incorrect
*string Guess

*NOTE* After you enter a guess, press enter, than enter again. If you press enter once then type a guess it will be like if you guessed again. But if u have 6 incorrect guesses, than when you type a guess in( the screen that tells you if u were right or not ), it will skip the displacement and make you play again!

Game Intellegence.h
Code:
// Hangman
// Game Intellegence.h
// By: Darwin Aguirre
#ifndef GAME_INTELLEGENCE_H
#define GAME_INTELLEGENCE_H
#include <vector>

using std::vector;

#include <string>

using std::string;

class Game_Int {
public:
Game_Int( vector < string > & );

bool End() const;

void setCurrentWord();
string getCurrentWord() const;
void PrintWord() const;

bool setGuess( string & );
string getGuess() const;

void AddGuesses( string & );
void PrintGuesses() const;

bool PrintHangman() const;

private:
vector < string > wordList;
string Current_Word;

vector < string > Guesses;
string Guess;

int Incorrect;
};

#endif

Game Intellegence.cpp
Code:
// Hangman
// Game Intellegence.cpp
// By: Darwin Aguirre
#include <iostream>

using std::cout;
using std::endl;

#include <cstdlib>
#include <ctime>

#include "Game Intellegence.h"

Game_Int::Game_Int( vector < string > &copyList )
: Incorrect( 0 )
{
wordList = copyList;

Guesses.resize( 0 );
srand( time( NULL ) );
}

bool Game_Int::End() const
{
return Incorrect == 7;
}

void Game_Int::setCurrentWord()
{
int random = rand() % ( wordList.size() - 1 );

Current_Word = wordList.at( random );
Incorrect = 0;
Guesses.clear();
Guesses.resize( 0 );
}

string Game_Int::getCurrentWord() const
{
return Current_Word;
}

void Game_Int::PrintWord() const
{
for ( size_t p = 0; p < Current_Word.size(); p++ )
cout << "X";
cout << endl;
}

bool Game_Int::setGuess( string &copyGuess )
{
Guess = copyGuess;

if ( Guess != Current_Word ) {
Incorrect++;
return false;
}
else
return true;
}

string Game_Int::getGuess() const
{
return Guess;
}

void Game_Int::AddGuesses( string &newGuess )
{

Guesses.resize( Guesses.size() + 1 );
Guesses.at( Guesses.size() - 1 ) = newGuess;
}

void Game_Int::PrintGuesses() const
{
for ( size_t i = 0; i < Guesses.size(); i++ )
cout << Guesses.at( i ) << endl;
}

bool Game_Int::PrintHangman() const
{
if ( !End() ) {
cout << ( 7 - Incorrect ) << " limbs to go!" << endl;
return false;
}
else {
cout << "  O  " << endl;
cout << " /|\\ " << endl;
cout << "  |  " << endl;
cout << " / \\ " << endl;
cout << "You have been hung!\n";
return true;
}
}

Hangman.cpp
Code:
// Hangman
// Hangman.cpp
// By: Darwin Aguirre
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

#include <limits>
#include <ios>

using std::numeric_limits;
using std::streamsize;
using std::max;

#include "Game Intellegence.h"

vector < string > MakeList();
bool Replay();

int main()
{
Game_Int Hangman( MakeList() );

string Guess;

Hangman.setCurrentWord();

while ( 1 ) {
cout << "Hangman:" << endl;
if ( Hangman.PrintHangman() == true ) {
cout << "Guesses:" << endl;
Hangman.PrintGuesses();
cout << "The word was: " << Hangman.getCurrentWord() << endl;
if ( Replay() == true ) {
Hangman.setCurrentWord();
    cout << "Hangman:" << endl;
Hangman.PrintHangman();
}
else
return 0;
}

cout << "Guesses:" << endl;
Hangman.PrintGuesses();

cout << "\nWord:  ";
Hangman.PrintWord();
cout << "Guess: ";
cin >> Guess;

system( "CLS" );
cin.get();
if ( Hangman.setGuess( Guess ) == true ) {
cout << "You guessed the word!\n";
if ( Replay() == true )
Hangman.setCurrentWord();
else
return 0;
}
else {
Hangman.AddGuesses( Guess );
cout << "You guessed the word wrong!\n";
}
cin.get();
system( "CLS" );
}

system( "PAUSE" );
return 0;
}

vector < string > MakeList()
{
vector < string > List( 26 );

List.at( 0 ) = "Horse";
List.at( 1 ) = "Ignorance";
List.at( 2 ) = "Diversity";
List.at( 3 ) = "War";
List.at( 4 ) = "Nation";
List.at( 5 ) = "Communication";
List.at( 6 ) = "Broadcast";
List.at( 7 ) = "Newspaper";
List.at( 8 ) = "Desktop";
List.at( 9 ) = "Television";
List.at( 10 ) = "Witch";
List.at( 11 ) = "Sex";
List.at( 12 ) = "Price";
List.at( 13 ) = "Elephant";
List.at( 14 ) = "Fuck";
List.at( 15 ) = "Bitch";
List.at( 16 ) = "Mage";
List.at( 17 ) = "Warrior";
List.at( 18 ) = "Theif";
List.at( 19 ) = "House";
List.at( 20 ) = "Fat";
List.at( 21 ) = "Battle";
List.at( 22 ) = "Cage";
List.at( 23 ) = "Bat";
List.at( 24 ) = "Kill";
List.at( 25 ) = "Monkey";

return List;
}

bool Replay()
{
char Replie;

cout << "Do you wish to play again; Y/N" << endl;
while ( !( cin >> Replie ) && Replie != 'Y' && 'y' && 'n' && 'N' ) {
cin.clear();
cin.ignore( numeric_limits< streamsize >::max(), '\n' );
cout << "That is not a valid choice, please choose again: " << endl;
}
switch ( Replie ) {
case 'Y':
cout << "O.K. Let play again!" << endl;
return true;
break;

case 'y':
cout << "O.K. Let play again!" << endl;
return true;
break;

case 'N':
cout << "O.K. see you later" << endl;
return false;
break;

case 'n':
cout << "O.K. see you later" << endl;
return false;
break;
}

return true;
}
In the codes you can see some similarities but it is not the same( like i said before ). The reason because was as i programmed more into the code i realized i needed other things. I say that this type of pesudocode( unnamed at the time ), is pretty cool, and is gonna help me out in the future a lot. I don't think you should you this type of pesudocode in small projects, this is more for big - huge program size. The hangman program might not be the best ever, but i think it is pretty fun( you can look at the word list during the game LOL, i do it ). I have to say, i acutally think this is one of the best things i have made, it runs perfectly( though there were somethings i could not fix ). I am not sure on how efficent i made it, but i think it is pretty cool, i also used some new tactics i had been developing over the "vacation".

*Fixed something in the code.
« Last Edit: August 02, 2007, 06:38:54 AM by Shonoby » Logged

Reality is what we make of it, so by definition we're all living in a fantasy.
- Shonoby
zaqufant
Farmer Brown
Dr. of C++ology
****
Posts: 963


Harder, better, faster, stronger.


View Profile
« Reply #1 on: August 03, 2007, 12:43:32 AM »

I don't really use a pseudo-code. I know what I want my program to achive eventually in the end, and I make little goals of what I want the program to do next and acheive that to reach my final goal. And from the time I start and to the time I end. I kinda outline the main things that I want my app to do, but other than that, I just program. The only kinda psedo code I use is just flow charts of what I want a certain function, or aspect to do.
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!