This is a really fun little game
I'm posting my modifications for fun
the little red 0s are oil so that you can last a little longer
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#define CLS std::cout << "\033[2J";
#define ESC "\e["
#define BLACK ESC << "0;30m"
#define RED ESC << "0;31m"
#define GREEN ESC << "0;32m"
#define YELLOW ESC << "0;33m"
#define BLUE ESC << "0;34m"
#define MAGENTA ESC << "0;35m"
#define CYAN ESC << "0;36m"
#define WHITE ESC << "0;37m"
#define DEFAULT ESC << "0;39m"
#ifdef WIN32
#include <conio.h>
#else
#include <termios.h>
#include <unistd.h>
int getch( )
{
struct termios oldt, newt;
int ch;
tcgetattr( STDIN_FILENO, &oldt );
newt = oldt;
newt.c_lflag &= ~( ICANON | ECHO );
tcsetattr( STDIN_FILENO, TCSANOW, &newt );
ch = getchar();
tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
return ch;
}
#endif
#define FLOOR 0
#define WALL 1
#define COIN (1 << 1)
#define STAIRS_DOWN (1 << 2)
#define STAIRS_UP (1 << 3)
#define TORCH (1 << 4)
#define OIL (1 << 5)
#define MAPSIZE 15
int x, y;
int coins = 0, moves = 0, torch = 30, level = 1;
int lvl[MAPSIZE][MAPSIZE];
void gen(int seed) {
srand(seed);
for (int X = 0; X < MAPSIZE; X++)
{
for (int Y = 0; Y < MAPSIZE; Y++)
{
if (X == 0 || X == MAPSIZE-1 || Y == 0 || Y == MAPSIZE-1 || rand() % 10 == 0)
lvl[X][Y] = 1;
else if (rand() % 20 == 0) //add something to the map
{
if (rand() % 3 == 0)
lvl[X][Y] = OIL;
else
lvl[X][Y] = COIN;
}
else
lvl[X][Y] = 0;
}
}
#define randcoord (1+rand()%(MAPSIZE-2))
x = randcoord;
y = randcoord;
lvl[x][y] ^= STAIRS_DOWN;
lvl[randcoord][randcoord] = STAIRS_UP;
}
void draw() {
CLS;
std::cout << YELLOW << "Coins: " << WHITE << coins << std::endl;
std::cout << RED << "Torch: " << WHITE << torch << std::endl;
std::cout << MAGENTA << "Moves: " << WHITE << moves << std::endl;
std::cout << GREEN << "Level: " << WHITE << level << std::endl;
for (int Y = 0; Y < MAPSIZE; Y++)
{
for (int X = 0; X < MAPSIZE; X++)
{
if (X == x && Y == y)
std::cout << WHITE << "@";
// controls your view size
else if (pow(x-X,2) + pow(y-Y,2) > std::min(49,torch/2) )
std::cout << " ";
else if (lvl[X][Y] == 0)
std::cout << BLUE << ".";
else if (lvl[X][Y] & WALL)
std::cout << CYAN << "#";
else if (lvl[X][Y] & COIN)
std::cout << YELLOW << "$";
else if(lvl[X][Y] & OIL)
std::cout << RED << "0";
else if (lvl[X][Y] & STAIRS_DOWN)
std::cout << RED << "<";
else if (lvl[X][Y] & STAIRS_UP)
std::cout << GREEN << ">";
else if (lvl[X][Y] & TORCH)
std::cout << RED << "f";
}
std::cout << std::endl;
}
}
int main() {
gen(level);
std::cout << GREEN << "Welcome!" << std::endl;
while (true)
{
// Drawing
draw();
// Input
char k = getch();
int oldx = x, oldy = y;
if (k == 'a')
--x;
else if (k == 'd')
++x;
else if (k == 'w')
--y;
else if (k == 's')
++y;
else if (k == 'q')
break;
// Collisions
if (lvl[x][y] & WALL)
{
x = oldx;
y = oldy;
}
else if (lvl[x][y] & COIN)
{
coins++;
lvl[x][y] ^= COIN;
}
else if (lvl[x][y] & OIL)
{
torch+=7;
lvl[x][y] ^= OIL;
}
else if (lvl[x][y] & TORCH)
{
torch++;
lvl[x][y] ^= TORCH;
}
else if (lvl[x][y] & STAIRS_UP)
gen(++level);
// successful move means torch used up
if(oldy != y || oldx != x)
{
++moves;
--torch;
}
// Die
if (torch <= 0)
break;
}
std::cout << DEFAULT;
return 0;
}