1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
#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')
{
up(playerX, playerY);
}
else if(UserMove == 'D')
{
down(playerX, playerY);
}
else if(UserMove == 'L')
{
left(playerX, playerY);
}
else if(UserMove == 'R')
{
right(playerX, playerY);
}
else
{
cout << "You entered a invailed character, try again" << endl;
system("pause");
}
// 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(int &playerX, int &playerY) // 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 == #
int prevplayerX = playerX;
int prevplayerY = playerY;
if(gameBoard [playerX][playerY - 1] == '#')
{
playerY--;
//cout << playerX << "," << playerY << endl;
gameBoard [prevplayerX][prevplayerY] = '#';
}
// 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(int &playerX, int &playerY) // 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 == #
/*
Just testing stuff here
bool isValid = false;
cout << "playerY = " << playerY;
system("pause");
if(playerY - 1 == '#' && playerY > 0 || playerY - 1 == 'O' && playerY > 0)
{
isValid = true;
}
else
{
cout << "Illegal Move" << endl;
}
*/
// 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(int &playerX, int &playerY) // 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(int &playerX, int &playerY) // 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()
}
|