//wipe the room, expand it if needed, and reset locations void newRoom() { int x, y; //the dungeon has a max size of 20 if (level > 20) { theMap.blankRoom(20); } else { theMap.blankRoom(level); } //setup player theMap.insertElement('P', 0, 0); thePlayer.setPlayerLocation(0, 0); //setup exit theMap.insertElement('X', theMap.getMax(), theMap.getMax()); //setup NORMAL_TRAPS for (int i = 0; i != level; i++) { x = rand() % theMap.getMax(); y = rand() % theMap.getMax(); while (theMap.getElement(x, y) != ' ') { x = rand() % theMap.getMax();; y = rand() % theMap.getMax();; } theEnemies.addEnemy(pTheMap, "NORMAL_TRAP", x, y); } //setup GRUNTS for (int i = 0; i != level / 2; i++) { x = rand() % theMap.getMax(); y = rand() % theMap.getMax(); while (theMap.getElement(x, y) != ' ') { x = rand() % theMap.getMax(); y = rand() % theMap.getMax(); } theEnemies.addEnemy(pTheMap, "GRUNT", x, y); } //setup STALKERS for (int i = 0; i != level / 5 ; i++) { x = rand() % theMap.getMax(); y = rand() % theMap.getMax(); while (theMap.getElement(x, y) != ' ') { x = rand() % theMap.getMax(); y = rand() % theMap.getMax(); } theEnemies.addEnemy(pTheMap, "STALKER", x, y); } //setup ITEM if (int r = rand() % 10 + 1 == 1) { x = rand() % theMap.getMax(); y = rand() % theMap.getMax(); while (theMap.getElement(x, y) != ' ') { x = rand() % theMap.getMax(); y = rand() % theMap.getMax(); } theMap.insertElement('*', x, y); } }
//the main game loop void gameLoop() { hasMoved = false; //draw screen theMap.drawMap(); cout << "Level: " << level << " - 'h' for help " << endl; if (thePlayer.getItem() != "NONE") { cout << "Type 'f' to use '" << thePlayer.getItem() << "'." << endl; } //player turn while (!hasMoved) { moveInput = _getch(); //player move if (moveInput == 'w' || moveInput == 'a' || moveInput == 's' || moveInput == 'd') { hasMoved = thePlayer.playerMove(pTheMap, pTheEnemies, pTheItems, moveInput); } else if (moveInput == 'h') { help(); //redraw screen theMap.drawMap(); cout << "Level: " << level << endl; } else if (moveInput == 'f') { //use item theItems.useItem(pTheMap, thePlayer.getPlayerLocation(), thePlayer.getItem()); thePlayer.removeItem(); //redraw screen without the 'type f to use' etc theMap.drawMap(); cout << "Level: " << level << " - 'h' for help " << endl; } } //death check if (thePlayer.isDead()) { isPlaying = false; } // victory check if (victoryCheck()) { newRoom(); return; } //enemies act for (int x = 0; x < theMap.getMax(); x++) { for (int y = 0; y != theMap.getMax(); y++) { if (theEnemies.isEnemy(pTheMap, x, y)) { if (theMap.getElement(x, y) == 'm') { theEnemies.moveEnemy(pTheMap, thePlayer.getPlayerLocation(), "GRUNT", x, y); } else if (theMap.getElement(x, y) == 'M') { theEnemies.moveEnemy(pTheMap, thePlayer.getPlayerLocation(), "STALKER", x, y); } } } } //Change new enemy placements to valid enemy placements for (int i = 0; i < theMap.getMax(); i++) { for (int a = 0; a < theMap.getMax(); a++) { if (theMap.getElement(i, a) == 'n') { theMap.insertElement('m', i, a); } if (theMap.getElement(i, a) == 'N') { theMap.insertElement('M', i, a); } } } // - victory check }