Maze Game

Nov 21, 2016 at 11:34pm
I want to add some randomness to this game. Does anyone know how I could have the players character be different each game or possibly show up on different parts of the board? The top half of my code is below. Any help would be great thanks!

#include <iostream>
using std::endl;
using std::cout;
using std::cin;
#include <string>
using std::string;

#include<Windows.h>
#include<conio.h>

void welcome();
char getKeyPress();
void printLevel(int);
void setMe(int);
bool isExit(int, int, int);
bool isWall(int, int, int);
int getPos(int, int&);
int getX(int, int &);
void update(int, int, int);
void makeSpace(int, int, int);

const char space = ' ';
const char me = '@';
char lvl1[15][15] = { { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ 'X', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', ' ', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', '#', ' ', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', '#', '#', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', '#', '#', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', '#', '#', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', '#', '#', ' ', '#', '#', '#', '#', '#', ' ', ' ', ' ', ' ', ' ', 'O' },
{ '#', '#', '#', ' ', '#', '#', '#', '#', '#', ' ', '#', '#', '#', '#', '#' },
{ '#', '#', '#', ' ', ' ', ' ', '#', '#', '#', ' ', '#', '#', '#', '#', '#' },
{ '#', '#', '#', '#', '#', ' ', '#', '#', '#', ' ', '#', '#', '#', '#', '#' },
{ '#', '#', '#', '#', '#', ' ', '#', '#', '#', ' ', '#', '#', '#', '#', '#' },
{ '#', '#', '#', '#', '#', ' ', ' ', ' ', ' ', ' ', '#', '#', '#', '#', '#' },
{ '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' } };

char lvl2[15][15] = { { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', ' ', ' ', ' ', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', ' ', '#', '#', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', ' ', '#', '#', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', ' ', ' ', '#', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', '#', ' ', '#', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', '#', ' ', '#', ' ', '#', '#', '#', '#', '#', '#', '#', '#', ' ', 'O' },
{ '#', '#', ' ', '#', ' ', '#', '#', '#', '#', '#', '#', '#', '#', ' ', '#' },
{ '#', '#', ' ', '#', ' ', '#', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
{ '#', '#', ' ', '#', ' ', '#', ' ', ' ', '#', '#', '#', '#', '#', '#', '#' },
{ 'X', ' ', ' ', '#', ' ', ' ', ' ', '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' } };


Nov 22, 2016 at 1:35am
Have you written your function main() yet?
Nov 22, 2016 at 1:48am
yea I just put the top half of the code because I just want to have the const char me ='a random character every level'
Nov 22, 2016 at 1:49am
There is no way to make the maze random. You have to create the maze yourself.
Nov 22, 2016 at 1:57am
I just want to make the players character different after each level not the entire game board
Nov 22, 2016 at 2:19am
Huawei wrote:
There is no way to make the maze random

No, it's entirely possible to generate a random maze; there's plenty of ways to do that. If you're interested I can provide an example program that does this.

OP, you can use code like this:
1
2
3
4
5
6
7
8
# include <string>
# include <random>
/* List of possible characters: */
static const std::string chset =
  "0123456789!@#$%^&*ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
auto r_char = [=, r_gen{std::mt19937{std::random_device{}()}}] () mutable {
  return chset[std::uniform_int_distribution<>{0, chset.size() - 1}(r_gen)];
}


And then you can call r_char() to get a random (draw with replacement) character from the character set: const char me{r_char()};

If you want a strictly different character (draw with no replacement) each time, I like to use this code to generate grab-bags:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# include <random>
# include <functional>
# include <vector>
# include <iterator> // for iterator_traits
# include <algorithm> // for shuffle
namespace {
  template <typename Iter>
  using IValue = typename std::iterator_traits<Iter>::value_type;
}

template <typename Iter>
std::function <IValue<Iter>()>
make_grab_bag(Iter begin, Iter end) {
  std::random_device r_dev;
  std::mt19937       r_gen(r_dev());

  std::vector <IValue<Iter>> bag(begin, end);
  std::shuffle(bag.begin(),
               bag.end(), /* <-- Obligatory Tolkien reference here */
               r_gen);
  
  return [bag{std::move(bag)}] () mutable {
    if (bag.empty())
      throw std::runtime_error("no more elements in bag");

    auto const ret = bag.back();
    bag.pop_back();
    return ret;
  };
}
...
auto charset = std::string{"123456789"}
auto charbag = make_grab_bag(charset.begin(), charset.end());

for (int i = 0; i < std::size(charset); ++i) std::cout << charbag() << "\n";

Last edited on Nov 22, 2016 at 2:33am
Nov 22, 2016 at 2:30am
mbozzi wrote:
No, it's entirely possible to generate a random maze; there's plenty of ways to do that. If you're interested I can provide an example program that does this.

Yeah, I would be glad to. I wonder how people like you can actually make this entirely possible.
Topic archived. No new replies allowed.