Simple Dungeon Crawl

This is my second project using c++, Dungeon Crawl. The aim of the game is to reach the treasure ‘X’ without touching a trap. I have diy-ed the game a little and increased the number of traps with each turn.

Using:

  • variables, data types, and numerical operators
  • basic input/output
  • logic (if statements, switch statements)
  • loops (for, while, do-while)
  • arrays
  • functions

#include <iostream>
#include <string>
//GLOBAL VARIABLES
char board[8][10];
//FUNCTIONS
void buildboard();
void initialize();
int moveplayer(char userinput);
bool checkwin(int row, int col);
void settrap();
bool checklose(int row, int col);
void ClearScreen();
int main()
{
 //INITIALIZE
 char userinput;
 initialize();
 settrap();
 bool stop = false;

 //DO WHILE NO WINNER OR LOSER
 while(stop == false){

 buildboard();
 settrap();

 std::cout << "\n Player move up, down, left, right (w,s,a,d)?\n\n\n\n";
 std:: cout << " ";
 std::cin >> userinput;
 bool quit = false;
 while (!quit){

 switch(userinput){
 case 'w':
 case 's':
 case 'a':
 case 'd':
 quit = true;
 break;
 default:
 std::cout << "\n Invalid move, try again(w,s,a,d)\n";
 std:: cout << " ";
 std::cin >> userinput;
 }
 }

 stop = moveplayer(userinput);
 userinput = '.'; //RESET
 }
}

void initialize()
{
 int col = 0,row = 0;
 //GIVES ALL SPACES NULL VALUE
 for(int i=0; i<8; i++)
 {
 for(int j=0; j<10; j++)
 {
 board[i][j] = '.';
 }
 }

 //TREASURE RANDOM
 while(col+row == 0){
 srand(time(0));
 col = rand() % 10;
 row = rand() % 8;
 board[row][col] = 'X';
 }

 //PLAYER AT 0,0.
 board[0][0] = 'G';

}

void buildboard()
{
 ClearScreen();
 std::cout << "\n DUNGEON CRAWL \n";
 std::cout << "\n Hurry they're multiplying... \n\n";

 for(int row=0; row<8; row++)
 {
 std::cout << "\t ";
 for(int col=0; col<10; col++)
 {
 std::cout << board[row][col];
 }
 std::cout << "\n";
 }
}

int moveplayer(char userinput)
{
 int col,row;
 //need to set m,n to player's position
 for(int i=0; i<10; i++){
 for(int j=0; j<8; j++){
 if(board[j][i] == 'G'){
 col = i;
 row = j;
 break;
 }
 }
 }

 //W UP, S DOWN, A LEFT, D RIGHT;
 //DOES NOT LET PLAYER MOVE OFF BOARD
 board[row][col] = '.';
 if(userinput =='w' && row!=0){
 row--;
 }
 else if(userinput =='s' && row!=7){
 row++;
 }
 else if(userinput =='a' && col!=0){
 col--;
 }
 else if(userinput =='d' && col!=9){
 col++;
 }
 //before reassigning G check for treasure or trap!!******
 //if win
 if (checkwin(row,col) == true){
 std::cout << "\n WINNER!\n\n\n";
 return true;
 }
 //if lose
 else if(checklose(row,col) == true){
 std::cout << "\n Sorry you LOSE!\n\n\n";
 return true;
 }
 else{
 board[row][col] ='G';
 return false;
 }
}

void settrap()
{
 //TRAP RANDOM
 int col, row;
 bool ok = false;
 while(ok == false){
 col = rand() % 10;
 row = rand() % 8;
 if(board[row][col]=='.'){
 board[row][col] = 'T';
 ok=true;
 }
 }
}

bool checkwin(int row, int col)
{
 //IF PLAYER G FINDS TREASURE X
 if(board[row][col] == 'X'){
 return true;
 buildboard();
 }
 else
 {
 return false;
 }
}
bool checklose(int row, int col)
{
 //IF PLAYER G FINDS TRAP
 if(board[row][col] == 'T'){
 return true;
 buildboard();
 }
 else
 {
 return false;
 }
}
void ClearScreen()
{
 std::cout << std::string( 100, '\n' );
}

While User Guilible

This is another simple project using while loops and if statements.

The program continues to ask the user to input a number other than the number equal to the number of tries they’re had. If the user does, they lose. If they are patient for ten tries, they win.


#include <iostream>

using namespace std;

int main() {
 int i = 0;
 int x;

while (i<10 && x!=i-1){
 cout << "Please enter a number!\nAny number...\nNOT "<< i << "!!!\n";
 cin >> x;
 i += 1;
 }

 if (i==10){
 cout << "Wow, you're more patient then I am, you win.";
 }
 else{
 cout << "Hey! you weren't supposed to enter " << i-1 << "!";
 }


 }

Pancake Glutton

This is a simple project that uses logic (if statements) and loops (for).

The user inputs the number of pancakes ten people have eaten. It sorts the list and outputs in order.

#include <iostream>

using namespace std;

int main() {
 int numberOfPancakes[10];

 int person[10] = {1,2,3,4,5,6,7,8,9,10};
 int minPancakes = 100000;

 cout << "How many pancakes did you eat this morning?\n";

 //iterates from 0-9
 for (int i=0; i<10; i++){

 cout << "Person " << i+1 << "?";
 cin >> numberOfPancakes[i];

 }

 //SORTING USING BUBBLE SORT
 for (int i=0; i<10; i++){
 for(int j=0; j<9; j++){
 if(numberOfPancakes[j]>numberOfPancakes[j+1]){

 int temp = numberOfPancakes[j+1];
 int temp2 = person[j+1];

 numberOfPancakes[j+1] = numberOfPancakes[j];
 person[j+1] = person[j];

 numberOfPancakes[j] = temp;
 person[j] = temp2;
 }
 }
 }

 for(int i=0; i<10; i++){
 cout << "Person " << person[i];
 cout << " ate " << numberOfPancakes[i] << " pancakes." << endl;
 }

}

While Loops

While Loops can be used like For Loops, however, they have some differences.

Here’s the syntax:


while (condition){

// code

}

or


do{

} while(condition);

To be truthful, I’ve never used the second example.

While Loops can be used in a similar way to For Loops; as an example, while x < 10, do some thing, then add one to x each loop.

However, the difference is that you don’t have to use it for iterating.

How about this: you have 10 cups, and there’s a marble under one of them, the computer has to find the marble. A For Loop would look under all 10 cups before finishing, whereas, a while loop would stop the loop when it found the marble. This doesn’t make much difference with such small number, but imagine the difference over a thousand cups.

This example below is a program that generates a random number between 0-100. The user than has to guess the number, as program tells the user higher or lower. It’s called a Bracketing Search.


#include <iostream>

using namespace std;

int main()
{

int guess = 0; // users guess.
int t = 0; // number of tries.

srand(time(0)); // creates a seed based on the ever changing time.
int r = 1+ rand() % 100; // generates a rand # between 1-100.

// while guess is not the random number
//and number of tries is less than 10.
while (guess != r && t < 10)
{
 cout << "Guess a number between 1 and 100." << endl;
 cin >> guess;

if (guess > 0 && guess < 100)
 {
 if (guess == r)
 {
 cout << "You win! You guessed " << r << "!!!" << endl;
 }
 else if (r > guess)
 {
 cout << "HIGHER" << endl;
 }
 else
 {
 cout << "LOWER" << endl;
 }
 t ++;
 }
 else
 {
 cout << "Invalid Input" << endl;
 }
}

cout << "Number of tries: " << t << endl << endl;

if (t == 10)
{
 cout << "Sorry no more chances!" << endl;
}

}