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' );
}

Advertisements

Tic Tac Toe

Using:

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

This is an example source code for Tic Tac Toe against a smart computer player. This took about a week to make in total. I had help from forums on cplusplus.com. Free to copy.


#include <iostream>
#include <vector>

using namespace std;

char square[10] = {'o', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

void board();
int checkwin();
int cppmove();
int cppmovesmart();

int main()
{
 int win = 0;
 int tries = 0;
 int userinput;
 int player = 1;

 while (win == 0 && tries < 9)
 {
 board();

 if(player == 1){
 cout << "\tPlayer " << player << " choose a number: ";
 cin >> userinput;
 }
 else
 {
 userinput = cppmovesmart();
 cout << "\tPlayer cpp chooses number " << userinput << "...\n\n";
 system("PAUSE");
 }




 //Invalid move, doesn't use up tries.
 if(square[userinput] == 'X' || square[userinput] == '0')
 {
 cout << "INVALID MOVE";
 }
 else
 {
 square[userinput] = (player ==1) ? 'X' : 'O';

 //Check win
 win = checkwin();

 //Change player
 player = (player == 1) ? 2 : 1;

 //Add 1 to tries
 tries++;
 }
 }
 //Undo the changed player to output winner.
 player = (player == 1) ? 2 : 1;

 //Output result.
 if(win == 1){
 board();
 cout << "\n\n\tPlayer " << player << " WINS!!!\n\n";
 }
 else{
 cout << "\n\n\tDRAW\n\n";
 }
}
/* ########################################FUNCTIONS###############################################
###################################################################################################*/

//COMPUTER MOVIES SMART
int cppmovesmart()
{
 int s;

 if(square[1] == '1' && (square[2] == square[3] || square [4] == square[7] || square[5] == square[9]))
 {

 s = 1;
 }
 else if(square[2] == '2' && (square[1] == square[3] || square [5] == square[8]))
 {
 s = 2;
 }
 else if(square[3] == '3' && (square[1] == square[2] || square [6] == square[9] || square [5] == square[7]))
 {
 s = 3;
 }
 else if(square[4] == '4' && (square[5] == square[6] || square [1] == square[7]))
 {
 s = 4;
 }
 else if(square[5] == '5' && (square[4] == square[6] || square [2] == square[8] || square [1] == square[9] || square[7] == square[3]))
 {
 s = 5;
 }
 else if(square[6] == '6' && (square[4] == square[5] || square [3] == square[9]))
 {
 s = 6;
 }
 else if(square[7] == '7' && (square[8] == square[9] || square [1] == square[4] || square [5] == square[3]))
 {
 s = 7;
 }
 else if(square[8] == '8' && (square[7] == square[9] || square [2] == square[5]))
 {
 s = 8;
 }
 else if(square[9] == '9' && (square[7] == square[8] || square [3] == square[6] || square [1] == square[5]))
 {
 s = 9;
 }
 else
 {
 s = cppmove();
 }
 return s;
}

//COMPUTER MOVES RANDOMLY
int cppmove()
{
 int r;
 vector<int> newvector(0);

 for(int i=1; i<10; i++)
 {
 if(square[i] != 'X' && square[i] != 'O')
 {
 newvector.push_back(i);
 }
 }

 srand(time(0));
 r = newvector.at(rand() % newvector.size());

 return r;
}

&nbsp;

//BUILD BOARD
void board()
{
 system("cls"); //Clears screen.
 cout << "\n\n\tTic Tac Toe\n\n";

 //builds a 3*3 board with numbers 123,456,789.
 for (int i = 1; i < 10; i = i + 3)
 {
 cout << "\t" << square[i] << " " << square[i+1] << " " << square[i+2] << endl << endl;
 }
}

int checkwin()
{
 int win = 0;

 //Horizontal Lines
 for (int i = 1; i < 10; i = i + 3)
 {
 if(square[i] == square[i+1] && square[i+1] == square[i+2])
 {
 win = 1;
 }
 }
 //Vertical Lines
 for (int i=1; i<4; i++){
 if(square[i] == square[i+3] && square[i] == square[i+6]){
 win = 1;
 }
 }
 //Diagonal Lines
 if((square[1] == square[5] && square[1] == square[9])||
 (square[3] == square[5] && square[3] == square[7])){
 win = 1;
 }
 //Return 1 if win, else return 0.
 if(win == 1){
 return 1;
 }
 else{
 return 0;
 }
}