#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <time.h> //Both of these are needed for Code::Blocks to use rand
#include <stdlib.h> //Both of these are needed for Code::Blocks to use rand
#include <chrono>
#include <random>
#include <conio.h>
#define HEIGHT 12
#define WIDTH 10
#define TRAPS 15
#define ENEMIES 5
/*
Name: zntr
date: 16.03.17
programm: dungeoncrawler 1
*/
using namespace std;
void createTraps();
void createEnemies(); // Create POS for Enemies and Traps once
void placePlayer(); // place Player based on User Input
void placeTraps(); // place Traps on Board
void placeEnemies(); // place Enemies on Board
void placeGoal(); // place Goal on Board
void fillBoard(); // Fill Board with ENV and Player
void drawBoard(); // Draw Board
bool gameWon(); // Check if Player reached end
bool gameOver(); // Check if Player still exists
bool gamewon = false;
bool gameover = false;
int i, j = 0;
char turn = '0';
const char player = 'P';
const char trap = '*';
const char enemy = 'E';
const char goal = '_';
const char boarddot = '.';
// initialize board array
char board[HEIGHT][WIDTH] = {0};
//Player Start-POS
int rowPos = 0;
int colPos = 0;
//initialize Environment POS Array
int trapPOS[TRAPS][2]; //Trap location array
int enemyPOS[ENEMIES][2]; //Enemy location array
int main(){
// create boards first, so we can create Traps, Enemies (dependend on '.')
for (i = 0; i < HEIGHT; i++){
for (j = 0; j < WIDTH; j++){
board[i][j] = boarddot;
}
}
createTraps();
createEnemies();
fillBoard();
do{
drawBoard();
cout << "\t\t >> ";
cin >> turn;
switch(turn){
case 'W':
case 'w':
if(rowPos > 0){
rowPos = rowPos - 1;
fillBoard();
drawBoard();
gameWon();
if(!gamewon){
gameOver();
}
}
else{
drawBoard();
cout << "This does not work!" << endl;
}
break;
case 'A':
case 'a':
if(colPos > 0){
colPos = colPos - 1;
fillBoard();
drawBoard();
gameWon();
if(!gamewon){
gameOver();
}
}
else{
drawBoard();
cout << "This does not work!" << endl;
}
break;
case 'S':
case 's':
if(rowPos < HEIGHT-1){
rowPos = rowPos + 1;
fillBoard();
drawBoard();
gameWon();
if(!gamewon){
gameOver();
}
}
else{
drawBoard();
cout << "This does not work!" << endl;
}
break;
case 'D':
case 'd':
if(colPos < WIDTH-1){
colPos = colPos + 1;
fillBoard();
drawBoard();
gameWon();
if(!gamewon){
gameOver();
}
}
else{
drawBoard();
cout << "This does not work!" << endl;
}
break;
default:
drawBoard();
cout << "Please use W, A, S, D to move your Player!";
break;
}
}
while(!gameover && !gamewon );
return 0;
}
// Fill board with dots and then overwrite with Env and Player
void fillBoard(){
for (i = 0; i < HEIGHT; i++){
for (j = 0; j < WIDTH; j++){
board[i][j] = boarddot;
}
}
placePlayer();
placeTraps();
placeEnemies();
board[HEIGHT-1][WIDTH-1] = goal;
}
// draw current board
void drawBoard(){
system("cls");
cout << "\t\t\tDNGNCRWL 1-\n\t\t\t(W, A, S, D)\n\n";
for (i = 0; i < HEIGHT; i++){
cout << "\t\t\t";
for (j = 0; j < WIDTH; j++){
cout << board[i][j];
}
cout << endl;
}
}
// create trap-positions
void createTraps(){
auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937 mt(seed);
uniform_int_distribution<int>distCols(0,WIDTH-1);
uniform_int_distribution<int>distRows(1,HEIGHT-1);
// Create POS for Traps
for (i = 0; i <= TRAPS; i++){
trapPOS[i][0] = distRows(mt);
trapPOS[i][1] = distCols(mt);
// Check if Coords were used for traps already so we dont place 2 Traps on one POS
for (j = 0; j <= TRAPS; j++){
while(trapPOS[i][0] == trapPOS[j][0] && trapPOS[i][1] == trapPOS[j][1] && i != j){
trapPOS[i][0] = distRows(mt);
trapPOS[i][1] = distCols(mt);
}
}
// Check if POS is empty on that place, else rerun current loop
if(board[trapPOS[i][0]][trapPOS[i][1]] != boarddot){
i--;
}
}
}
// create enemy-positions
void createEnemies(){
// change seed so enemies do not overwrite Traps
auto seed = ((chrono::high_resolution_clock::now().time_since_epoch().count())/3)*4;
mt19937 mt(seed);
uniform_int_distribution<int>distCols(0,WIDTH-1);
uniform_int_distribution<int>distRows(2,HEIGHT-1);
// Create Y/X POS for enemies
for (i = 0; i <= ENEMIES; i++){
enemyPOS[i][0] = distRows(mt);
enemyPOS[i][1] = distCols(mt);
// Check if Coords were used for traps already so we don't place 2 Enemies on one POS
for (j = 0; j <= ENEMIES; j++){
while(enemyPOS[i][0] == enemyPOS[j][0] && enemyPOS[i][1] == enemyPOS[j][1] && i != j){
enemyPOS[i][0] = distRows(mt);
enemyPOS[i][1] = distCols(mt);
}
}
// Check if Coords are empty on that place, if not loop again
if(board[enemyPOS[i][0]][enemyPOS[i][1]] != '.'){
i--;
}
}
}
// place player on board
void placePlayer(){
board[rowPos][colPos] = player;
}
// place traps on board
void placeTraps(){
for (i = 0; i < TRAPS; i++){
board[trapPOS[i][0]][trapPOS[i][1]] = trap;
}
}
// currently only places enemies on board
void placeEnemies(){
for (i = 0; i < ENEMIES; i++){
board[enemyPOS[i][0]][enemyPOS[i][1]] = enemy;
}
}
// Check if Player lost! I know, bool is not necessary
bool gameWon(){
if(board[HEIGHT-1][WIDTH-1] == board[rowPos][colPos]){
gamewon = true;
cout << "\n\t\t\tYou won!\n";
getch();
}
}
// Check if Player lost! I know, bool is not necessary
bool gameOver(){
if(board[rowPos][colPos] != player){
gameover = true;
cout << "\n\t\t\tYou lost!\n";
getch();
}
}
my first dungeoncrawler program.
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.