P.S. - I'm doing this in Visual studios 2010 so it might help to copy/paste code into there.
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;
//declare array for game board and fill it
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 &);
int main()
{
int playerX = 1; // variable to hold players x coord remember higher numbers move right
int playerY = 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
system("cls"); //clear screen
for(int i=0; i<20; i++) // start display map
{
for(int j=0; j<20; j++)
{
cout << gameBoard[i][j];
}
cout << endl;
} // end display map
// You need to create a main game loop, anything that your game needs to do needs to take place
// inside this loop
// hint: You can use a do while loop, and it needs to include your display of the board,
// a prompt to the user, a function call and a check to see if the game is won
do
{
// inside the loop clear the screen and display the map again to start
system("cls");
for(int i=0; i<20; i++)
{
for(int j=0; j<20; j++)
{
cout << gameBoard[i][j];
}
cout << endl;
}
// After the board is displayed tell the player their current location and ask which
// way they want to go and get their input
cout << "Your current location is (" << playerX << "," << playerY << ")" << endl;
cout << "Which way to you want to go? (Up = U, Down = D, Left = L, Right = R)" << endl;
char UserMove;
cin >> UserMove;
// Based off the input the user enters call one of the functions to move their player
// in the desired direction
UserMove = toupper(UserMove);
if(UserMove == 'U')
{
void up();
}
else if(UserMove == 'D')
{
void down();
}
else if(UserMove == 'L')
{
void left();
}
else if(UserMove == 'R')
{
void right();
}
else
{
cout << "You entered a invailed character, try again" << endl;
//cin >> UserMove;
}
// the last thing to do before the loop starts over is check if the player has won
// ak the player position is the same as the end xy(1,4) or array[4][1]
if(playerX == 1 && playerY == 4)
{
game = true;
}
} // end do while loop
while(game == false);
// outside the loop make sure to tell the player they won
system("cls");
cout << "YOU WIN!!!" << endl;
system("pause");
return 0;
} // end main
void up() // function that moves the player in the up direction
{
// the first part of this function needs to check if the move is valid
// make sure the space to be moved into is a '#' or the end 'O' not a wall
// hint: use an If and check the next space == #
// If the move is vaild you need to replace the current space with a # and
// place the players peice into the new square and change their coords
// hint: vertical movement is controled in the first array
// If the move is bad tell the user
} // end up()
void down() // function that moves the player in the down direction
{
// the first part of this function needs to check if the move is valid
// make sure the space to be moved into is a '#' not a wall
// hint: use an If and check the next space == #
// If the move is vaild you need to replace the current space with a # and
// place the players peice into the new square
// hint: vertical movement is controled in the first array
// If the move is bad tell the user
} // end down()
void left() // function that moves the player in the left direction
{
// the first part of this function needs to check if the move is valid
// make sure the space to be moved into is a '#' not a wall
// hint: use an If and check the next space == #
// If the move is vaild you need to replace the current space with a # and
// place the players peice into the new square
// hint: horizontal movement is controled in the second array
// If the move is bad tell the user
} // end left()
void right() // function that moves the player in the right direction
{
// the first part of this function needs to check if the move is valid
// make sure the space to be moved into is a '#' not a wall
// hint: use an If and check the next space == #
// If the move is vaild you need to replace the current space with a # and
// place the players peice into the new square
// hint: horizontal movement is controled in the second array
// If the move is bad tell the user
} // end right()

New Topic/Question
Reply




MultiQuote





|