Skip to content

Instantly share code, notes, and snippets.

@devyn
Forked from anonymous/gist:7518312
Last active December 28, 2015 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devyn/7520392 to your computer and use it in GitHub Desktop.
Save devyn/7520392 to your computer and use it in GitHub Desktop.
// -------------------------------------------------------------------------------------------------------------
// simpleStarTrekSK - The program skeleton for a simple star trek game. The student must complete the game
// according to the lab instructions. The bulk of the game must be defined in the function
// starTrek(), to be defined by the student including any necessary other functions. As students
// implement the required code, they should add their attributions to the change log below,
// as well as add comment headers explaining each function they write and include attributions
// for any new code and functions they add.
//
// Written: E. Basiachvili, Langara College, 2011 used with permission by below
// Changes: P. Baker, Langara College, October 2013, comments added, minor modifications, starTrek() function
// GamePlay: C. Coulson Langara College, November 2013, game play implementation with starTrek() etc.
// Note: Each student should add an attribution here as they implement the game play code in starTrek()
// -------------------------------------------------------------------------------------------------------------
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstdio>
using namespace std;
// -------------------------------------------------------------------------------------------------------------
// isUnique - Determines if there is a collision between any of the entities in the galaxy, that is they
// have a unique position in the X, Y coordinate space of the playing field.
// Inputs: Accepts the x, y coordinates of 7 unique entities - the player, the two starts, the two wormholes,
// and the three blackholes.
// Returns: true - if all entities have unique positions, and there are no collisions
// false - if there is a collision between the entities, as the player is the only one that
// moves (except during intial position determination, false means the collision between
// the player and one of the other entities.
// -------------------------------------------------------------------------------------------------------------
bool isUnique(int pX, int pY, int sX, int sY, int w1X, int w1Y, int w2X, int w2Y,
int b1X, int b1Y, int b2X, int b2Y, int b3X, int b3Y);
// -------------------------------------------------------------------------------------------------------------
// starTrek - This function is called to implement the bulk of the game play after the game field and entity
// initialization in main(). The student must complete this function and any other functions necessary
// to implement the game according to the Lab requirements. The student may alter the function
// header, defintion, and prototype as necessary to implement the passage of the parameters and
// return values necessary for their implementation of the game. The student should alter this function
// comment header to reflect their implementation, and place an attribution to themselves for
// all functions they write.
// Inputs: Parameters may be altered to suit the student's implementation.
// Returns: Return values may be altered to suit the student's implementation.
// -------------------------------------------------------------------------------------------------------------
void starTrek(int pX, int pY, int sX, int sY, int w1X, int w1Y, int w2X, int w2Y,
int b1X, int b1Y, int b2X, int b2Y, int b3X, int b3Y, int fieldSizeX, int fieldSizeY);
void refreshDisplay(int pX, int pY, int sX, int sY, int w1X, int w1Y, int w2X, int w2Y,
int b1X, int b1Y, int b2X, int b2Y, int b3X, int b3Y, int fieldSizeX, int fieldSizeY);
void drawRow(int pX, int pY, int sX, int sY, int w1X, int w1Y, int w2X, int w2Y,
int b1X, int b1Y, int b2X, int b2Y, int b3X, int b3Y, int fieldSizeY, int row);
void endGame(int input);
void programInfo();
int moveLeft(int& pX);
int moveRight(int& pX);
int moveUp(int& pY);
int moveDown(int& pY);
// -------------------------------------------------------------------------------------------------------------
// main - Main function for the Simple Star Trek Game. This program simply prompts the user to set the size of
// the galaxy playing field, and then determines the unique random starting positions of the player and a
// range of galaxy entities, stars, wormholes, and blackholes. The function starTrek() is called to
// implement the actual game play.
// Inputs: Prompts for all inputs, uses time to set the random seed for initial positions
// Returns: 0 - on game completion with no errors
// -------------------------------------------------------------------------------------------------------------
int main()
{
// X, Y Coordinates of the User Player's current position on the Playing Field or Galaxy
int playerX = 0, playerY = 0;
// X, Y Coordinates of a single Star
int starX = 0, starY = 0;
// X, Y Coordinates of 2 Worm Holes
int worm1X = 0, worm1Y = 0;
int worm2X = 0, worm2Y = 0;
// X,Y Coordinates of 3 Black Holes
int blackhole1X = 0, blackhole1Y = 0;
int blackhole2X = 0, blackhole2Y = 0;
int blackhole3X = 0, blackhole3Y = 0;
// Ask user to Set Playing Field or Galaxy size to within reasonable bounds ....
int fieldSizeX, fieldSizeY;
do
{
cout<<"Please enter the galaxy width (10-70):";
cin>>fieldSizeX;
cout<<"Please enter the galaxy height (10-70):";
cin>>fieldSizeY;
}while((fieldSizeX < 10) || (fieldSizeX > 70) || (fieldSizeY < 10) || (fieldSizeY > 70));
// Set the random number generator (cstdlib) seed based on the time (ctime) of the start of the game
srand((unsigned)time(NULL));
// Set starting coordinates for the player and all galaxy entities to UNIQUE random locations within the
// bounds of the galaxy field.
while(!isUnique(playerX, playerY, starX, starY, worm1X, worm1Y, worm2X, worm2Y, blackhole1X, blackhole1Y,
blackhole2X, blackhole2Y, blackhole3X, blackhole3Y))
{
playerX = rand()%fieldSizeX;
playerY = rand()%fieldSizeY;
starX = rand()%fieldSizeX;
starY = rand()%fieldSizeY;
worm1X = rand()%fieldSizeX;
worm1Y = rand()%fieldSizeY;
worm2X = rand()%fieldSizeX;
worm2Y = rand()%fieldSizeY;
blackhole1X = rand()%fieldSizeX;
blackhole1Y = rand()%fieldSizeY;
blackhole2X = rand()%fieldSizeX;
blackhole2Y = rand()%fieldSizeY;
blackhole3X = rand()%fieldSizeX;
blackhole3Y = rand()%fieldSizeY;
} // End while
// Make these the galaxy entities CONSTANT for the rest of the game
// The Star ...
const int STAR_X = starX;
const int STAR_Y = starY;
// ... The Two Worm Holes ...
const int WORM_1_X = worm1X;
const int WORM_1_Y = worm1Y;
const int WORM_2_X = worm2X;
const int WORM_2_Y = worm2Y;
// ... and The Three Black Holes
const int BLACKHOLE_1_X = blackhole1X;
const int BLACKHOLE_1_Y = blackhole1Y;
const int BLACKHOLE_2_X = blackhole2X;
const int BLACKHOLE_2_Y = blackhole2Y;
const int BLACKHOLE_3_X = blackhole3X;
const int BLACKHOLE_3_Y = blackhole3Y;
// Display the inital variable positions of the player and the intial random CONSTANT positions
// of the other galaxy entities (stars, wormholes, and blackholes) for the user
cout << "Player x = " << playerX << " , y = " << playerY << '\n'
<< "Star x = " << STAR_X << " , y = " << STAR_Y << '\n'
<< "Wormhole 1 x = " << WORM_1_X << " , y = " << WORM_1_Y << '\n'
<< "Wormhole 2 x = " << WORM_2_X << " , y = " << WORM_2_Y << '\n'
<< "Blackhole 1 x = " << BLACKHOLE_1_X << " , y = " << BLACKHOLE_1_Y << '\n'
<< "Blackhole 2 x = " << BLACKHOLE_2_X << " , y = " << BLACKHOLE_2_Y << '\n'
<< "Blackhole 3 x = " << BLACKHOLE_3_X << " , y = " << BLACKHOLE_3_Y << '\n';
// NOW you can pass all these constants to the starTrek function (as necessary) that you define to
// implement the actual game play according to the lab requirements. You may alter the function header
// and function prototype as necessary to suit your implementation. Don't forget to alter the
// comment headers as appropriate to describe your functions. Be sure and leave your attribution
// in the comment headers of each function your write. Have fun ! =]
starTrek(playerX, playerY, STAR_X, STAR_Y, WORM_1_X, WORM_1_Y, WORM_2_X, WORM_2_Y, BLACKHOLE_1_X, BLACKHOLE_1_Y,
BLACKHOLE_2_X, BLACKHOLE_2_Y, BLACKHOLE_3_X, BLACKHOLE_3_Y, fieldSizeX, fieldSizeY);
}
// -------------------------------------------------------------------------------------------------------------
// isUnique - Determines if there is a collision between any of the entities in the galaxy, that is they
// have a unique position in the X, Y coordinate space of the playing field.
// Inputs: Accepts the x, y coordinates of 7 unique entities - the player, the two starts, the two wormholes,
// and the three blackholes.
// Returns: true - if all entities have unique positions, and there are no collisions
// false - if there is a collision between the entities, as the player is the only one that
// moves (except during intial position determination, false means the collision between
// the player and one of the other entities.
// -------------------------------------------------------------------------------------------------------------
bool isUnique(int pX, int pY, int sX, int sY, int w1X, int w1Y, int w2X, int w2Y, int b1X, int b1Y,
int b2X, int b2Y, int b3X, int b3Y)
{
// Assume no collisions at start
bool unique = true;
// Does the player position collide with anything?
if((pX == sX && pY == sY) ||
(pX == w1X && pY == w1Y) || (pX == w2X && pY == w2Y) ||
(pX == b1X && pY == b1Y) || (pX == b2X && pY == b2Y) || (pX == b3X && pY == b3Y))
{
// A collision was detected with the player and another galaxy entity
unique = false;
}
// Does the star position collide with any of the remaining entities?
if((sX == w1X && sY == w1Y) || (sX == w2X && sY == w2Y) ||
(sX == b1X && sY == b1Y) || (sX == b2X && sY == b2Y) || (sX == b3X && sY==b3Y))
{
// A collision was detected with the player and another galaxy entity
unique = false;
}
// Does the wormhole 1 position collide with any of the remaining entities?
if((w1X == w2X && w1Y == w2Y) ||
(w1X == b1X && w1Y == b1Y) || (w1X == b2X && w1Y == b2Y) || (w1X == b3X && w1Y == b3Y))
{
// A collision was detected with wormhole 1 and the remaining galaxy entities
unique = false;
}
// Does the wormhole 2 position collide with any of the remaining entities?
if((w2X == b1X && w2Y == b1Y ) || ( w2X == b2X && w2Y == b2Y) || (w2X == b3X && w2Y == b3Y))
{
// A collision was detected with wormhole 2 and the remaining galaxy entities
unique = false;
}
// Does the blackhole 1 position collide with any of the remaining entities?
if((b1X == b2X && b1Y == b2Y) || (b1X == b3X && b1Y == b3Y))
{
// A collision was detected with blackhole 1 and the remaining galaxy entities
unique = false;
}
// Does the blackhole 2 position collide with the remaining blackhole 3 entity?
if((b2X==b3X&&b2Y==b3Y))
{
// A collision was detected with blackhole 2 and the last galaxy entity
unique = false;
}
// return unique - false if there was a collision, true if all entities have unique positions
return unique;
} // End function isUnique
// -------------------------------------------------------------------------------------------------------------
// starTrek - This function is called to implement the bulk of the game play after the game field and entity
// initialization in main(). The student must complete this function and any other functions necessary
// to implement the game according to the Lab requirements. The student may alter the function
// header, defintion, and prototype as necessary to implement the passage of the parameters and
// return values necessary for their implementation of the game. The student should alter this function
// comment header to reflect their implementation, and place an attribution to themselves for
// all functions they write.
// Inputs: Parameters may be altered to suit the student's implementation.
// Returns: Return values may be altered to suit the student's implementation.
// -------------------------------------------------------------------------------------------------------------
void starTrek(int pX, int pY, int sX, int sY, int w1X, int w1Y, int w2X, int w2Y,
int b1X, int b1Y, int b2X, int b2Y, int b3X, int b3Y, int fieldSizeX, int fieldSizeY)
{
programInfo();
while(true) {
refreshDisplay(pX, pY, sX, sY, w1X, w1Y, w2X, w2Y, b1X, b1Y, b2X, b2Y, b3X, b3Y, fieldSizeX, fieldSizeY);
char command;
cin >> command;
cout << endl;
command = getchar();
switch(command) {
case 'h':
case 'H':
programInfo();
break;
case 'a':
case 'A':
case '4':
moveLeft(pX);
break;
case 'd':
case 'D':
case '6':
moveRight(pX);
break;
case 's':
case 'S':
case '2':
moveDown(pY);
break;
case 'w':
case 'W':
case '8':
moveUp(pY);
break;
default:
cout << "ERROR, Captain. Please try again." << endl;
}
}
} // End function starTrek
void refreshDisplay(int pX, int pY, int sX, int sY, int w1X, int w1Y, int w2X, int w2Y,
int b1X, int b1Y, int b2X, int b2Y, int b3X, int b3Y, int fieldSizeX, int fieldSizeY)
{
for(int row = 0; row < fieldSizeX; row++){
drawRow (pX, pY, sX, sY, w1X, w1Y, w2X, w2Y, b1X, b1Y, b2X, b2Y, b3X, b3Y, fieldSizeY, row);
}
}
void drawRow(int pX, int pY, int sX, int sY, int w1X, int w1Y, int w2X, int w2Y,
int b1X, int b1Y, int b2X, int b2Y, int b3X, int b3Y, int fieldSizeY, int row)
{
for(int column = 0; column < fieldSizeY; column++){
if(row == pX && column == pY) {
cout << "P";
} else if(row == sX && column == sY) {
cout << "*";
} else if((row == w1X && column == w1Y)||(row == w2X && column == w2Y)) {
cout << "@";
} else if((row == b1X && column == b1Y)||(row == b2X && column == b2Y)||(row == b3X && column == b3Y)) {
cout << "X";
} else {
cout << ".";
}
}
cout << endl;
}
//void endGame(int input);
//{
//}
void programInfo()
{
cout << "Welcome, Captain! A glorious mission among the stars awaits you! Your mission is simple: find the star!" << endl;
cout << "You must simply use your keys to move your starship - A for left, D for right, S for down and W for up." << endl;
cout << "Once you have found the star, the game will end! Don't forget to press H if you need any help!" << endl;
}
int moveLeft(int& pX)
{
pX++;
return pX;
}
int moveRight(int& pX)
{
pX--;
return pX;
}
int moveUp(int& pY)
{
pY++;
return pY;
}
int moveDown(int& pY)
{
pY--;
return pY;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment