Advertisements

C++ Programming Maze

This is a final project of mine from my C++ class.  Its currently a working program but feel free to make changes if you must and enjoy!  Oh, if you decide to copy the code you have to like the page.

rant away

 

#include <iostream> //programmer rant4u
#include <string>  //
#include <iomanip> //
#include <cmath>

using namespace std;

//declare array for game board and fill it    // gameboard
char gameBoard[20][20] =
{ {‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘, ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘}
, {‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’, ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘|’}
, {‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘, ‘-‘ , ‘-‘ , ‘#’ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘|’}
, {‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘#’, ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘|’}
, {‘|’ , ‘O’ , ‘|’ , ‘#’ , ‘|’ , ‘-‘ , ‘-‘ , ‘#’ , ‘|’ , ‘-‘ , ‘|’, ‘#’ , ‘|’ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘#’ , ‘|’}
, {‘|’ , ‘-‘ , ‘-‘ , ‘-‘ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’, ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘|’}
, {‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘|’, ‘#’ , ‘|’ , ‘#’ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘|’}
, {‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘-‘ , ‘#’ , ‘|’ , ‘-‘, ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘|’}
, {‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘|’ , ‘#’, ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘-‘ , ‘-‘ , ‘#’ , ‘|’}
, {‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘-‘ , ‘|’ , ‘#’, ‘#’ , ‘|’ , ‘-‘ , ‘-‘ , ‘-‘ , ‘|’ , ‘|’ , ‘#’ , ‘|’}
, {‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘|’ , ‘#’, ‘#’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘|’ , ‘#’ , ‘|’}
, {‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘-‘ , ‘-‘ , ‘-‘ , ‘#’ , ‘|’ , ‘#’, ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘-‘ , ‘-‘ , ‘|’ , ‘#’ , ‘|’}
, {‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘|’ , ‘#’, ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘|’}
, {‘|’ , ‘#’ , ‘-‘ , ‘-‘ , ‘|’ , ‘#’ , ‘|’ , ‘-‘ , ‘#’ , ‘|’ , ‘#’, ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘-‘ , ‘-‘ , ‘#’ , ‘-‘ , ‘|’}
, {‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘|’ , ‘#’, ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘|’}
, {‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘|’ , ‘#’, ‘|’ , ‘#’ , ‘|’ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘#’ , ‘|’}
, {‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘|’ , ‘#’, ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘|’}
, {‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘|’ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘#’, ‘|’ , ‘#’ , ‘|’ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘#’ , ‘|’}
, {‘|’ , ‘X’ , ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’, ‘|’ , ‘#’ , ‘|’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘#’ , ‘|’}
, {‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘, ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘ , ‘-‘}};

void up(int &, int &); // pass the players position to all
void down(int &, int &); // functions by reference
void left(int &, int &);
void right(int &, int &);   //declaring functions

int main()
{
char ch1;                    // variables
int playerCOL = 1; // variable to hold players x coord remember higher numbers move right
int playerROWS= 18; // variable to hold players y coord remember higher numbers move down
bool game = false; // bool to hold tell when the game is over

// at start of program clear the screen and output the map
do            // starting the do while loop
{
system(“cls”);             //clearing the screen
for(int i = 0; i < 20; i++)           //refreshing the output screen
{
for(int j = 0; j < 20; j++)
{
cout << gameBoard[i][j] << ” “;
}
cout <<endl;
}
cout <<“Your currently at location ” << playerROWS << ” ” << playerCOL <<endl;   //asking the user for input
cout << “Which direction would you like to move:  WSAD ?” <<endl;
cin >> ch1;      //input

if (ch1 == ‘w’)                   //series of if checks, then calls the function of movement up
{
up(playerCOL, playerROWS);
}
else if (ch1 == ‘a’)
{
left(playerCOL, playerROWS);
}
else if (ch1 == ‘s’)
{
down(playerCOL, playerROWS);
}
else if (ch1 == ‘d’)
{
right(playerCOL, playerROWS);      // end of directional checks
}
else
{
cout <<“error”<<endl;            //if the user enters wrong letter for direction
}
if(playerCOL == 1 && playerROWS == 4)   //if the player reaches the ‘O’ and wins
{
game = true;
}
}
while(game == false);          //triggers end of game

cout << ” Congrats you have won the game”<<endl;         //display when you win
system(“pause”);
return 0;
} // end main
void up(int & COL, int & ROWS) // function that moves the player in the up direction
{
if(gameBoard[ROWS-1][COL] == ‘#’)           //moves your marker up
{
ROWS–;
gameBoard[ROWS][COL] = ‘X’;
gameBoard[ROWS+1][COL] = ‘#’;
}
else
{
cout <<“Bad Move” <<endl;            //displaying your bad move if you cant move there
}

} // end up()
void down(int & COL, int & ROWS) // function that moves the player in the down direction
{

if(gameBoard[ROWS+1][COL] == ‘#’ ||gameBoard[ROWS+1][COL] ==’O’)   //moves your marker down
{
ROWS++;
gameBoard[ROWS][COL] = ‘X’;
gameBoard[ROWS-1][COL] = ‘#’;
}
else
{
cout <<“Bad Move” <<endl;           //displaying your bad move if you cant move there
}

} // end down()

void left(int & COL, int & ROWS) // function that moves the player in the left direction
{

if(gameBoard[ROWS][COL-1] == ‘#’)   //moves your marker left
{
COL–;
gameBoard[ROWS][COL] = ‘X’;
gameBoard[ROWS][COL+1] = ‘#’;

}
else
{
cout <<“Bad Move” <<endl;           //displaying your bad move if you cant move there
}

} // end left()

void right(int & COL, int & ROWS) // function that moves the player in the right direction
{
if(gameBoard[ROWS][COL+1] == ‘#’)          //moves your marker right
{
COL++;
gameBoard[ROWS][COL] = ‘X’;
gameBoard[ROWS][COL-1] = ‘#’;

}
else
{
cout <<“Bad Move” <<endl;    //displaying your bad move if you cant move there
}

} // end right()

Advertisements

About Rant4u

A Revolution in Social Networking

Posted on May 9, 2012, in #c++, #C++ programming and tagged , , , , , . Bookmark the permalink. 2 Comments.

  1. Hmm it appears like your blog ate my first comment (it was super long) so I guess I’ll just sum it up what I submitted and say, I’m thoroughly
    enjoying your blog. I too am an aspiring blog writer but I’m still new to everything. Do you have any points for novice blog writers? I’d
    definitely appreciate it.

    Like

  2. The Internet has made meeting people, much easier than ever
    before. The only thing you can do in this situation is to wait
    for her reply and at the same time move on and message someone else.
    You should realize that ladies are typically hard to study
    even for a smart guy but the difference between a smart, confident guy and a shy guy is that the former makes an approach while the latter never makes a move.

    Like

Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: