Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include <ctime>
- #include <cstring>
- #include <iostream>
- #include <Grapic.h>
- #define CHMAX 255
- using namespace std;
- using namespace grapic;
- // Global Vars (EDITABLE)
- const int DIMX = 10;
- const int DIMY = 10;
- int ALIVE = 25;
- const bool DEBUG = true;
- // Tools Vars
- int glX = 0;
- int glY = 0;
- int randInt(int min, int max) { return min + (rand() % (int)(max - min + 1)); }
- clock_t startTime = clock();
- // Grapic Vars
- const char title[CHMAX] = "Jeu de la Vie";
- const int SIZE = 32;
- const int WINX = 20 * 2 + (DIMX - 2) * SIZE + DIMX;
- const int WINY = 80 + (DIMY - 2) * SIZE + DIMY;
- bool stop = false;
- struct mCase
- {
- int x;
- int y;
- int nearAlive = 0;
- bool isAlive = false;
- bool isVisible = true;
- bool isDisplayed = false;
- void init()
- {
- x = glX;
- y = glY;
- nearAlive = 0;
- if (x == 0 || y == 0 || x == DIMX || y == DIMY) isVisible = false;
- isAlive = false;
- }
- };
- void checkNear(mCase table[DIMX][DIMY], int glX, int glY)
- {
- for (int x = glX - 1; x <= glX + 1; x++)
- {
- for (int y = glY - 1; y <= glY + 1; y++)
- {
- if (table[x][y].isAlive == true)
- {
- table[glX][glY].isAlive++;
- }
- }
- }
- }
- // Init a double array of mCase
- void init(mCase table[DIMX][DIMY])
- {
- // Just calling init
- for (glX = 0; glX < DIMX; glX++)
- {
- for (glY = 0; glY < DIMY; glY++)
- {
- table[glX][glY].init();
- }
- }
- // Placing cells
- for (int i = 0; i < ALIVE; i++)
- {
- int x = randInt(1, DIMX - 2);
- int y = randInt(1, DIMY - 2);
- if (!table[x][y].isAlive && table[x][y].isVisible) table[x][y].isAlive = true;
- else i--;
- }
- // Count near bombs
- for (int x = 1; x < DIMX - 1; x++)
- {
- for (int y = 1; y < DIMY - 1; y++)
- {
- checkNear(table, x, y);
- }
- }
- }
- // Print in Console
- void mPrint(mCase table[DIMX][DIMY])
- {
- std::cout << "To disable cheat, set DEBUG to false in Source Code" << std::endl << std::endl;
- for (glX = 1; glX < DIMX - 1; glX++)
- {
- for (glY = 1; glY < DIMY - 1; glY++)
- {
- if (table[glX][glY].isAlive) std::cout << '*';
- else std::cout << table[glX][glY].isAlive;
- std::cout << " ";
- }
- std::cout << std::endl;
- }
- }
- // Grapic Init Void
- void grapicInit()
- {
- backgroundColor(127, 127, 127);
- }
- // Draw black grid
- void gridDrawer()
- {
- for (int vl = 20; vl < WINX - 20; vl += SIZE + 1)
- {
- color(0, 0, 0);
- line(vl, 20, vl, WINY - 62);
- }
- for (int hl = 20; hl < WINY - 60; hl += SIZE + 1)
- {
- color(0, 0, 0);
- line(20, hl, WINX - 22, hl);
- }
- // Coord Tester
- if (DEBUG)
- {
- int x, y;
- mousePos(x, y);
- color(0);
- if ((x - 20) % (SIZE + 1) == 0 || (y - 20) % (SIZE + 1) == 0) color(255);
- if (x < 20 || x > WINX - 20 || y < 20 || y > WINY - 60) color(255);
- print(WINX - 35, WINY - 24, x);
- print(WINX - 35, WINY - 40, y);
- }
- }
- // Draw HUD at top
- int mRemaining(mCase table[DIMX][DIMY])
- {
- int remaining = 0;
- for (int x = 1; x < DIMX - 1; x++)
- {
- for (int y = 1; y < DIMY - 1; y++)
- {
- if (table[x][y].isAlive) remaining++;
- }
- }
- return remaining;
- }
- // Draw HUD at top
- void hud(mCase table[DIMX][DIMY])
- {
- int remaining = mRemaining(table);
- double elapsedTime = double(clock() - startTime) / CLOCKS_PER_SEC;
- color(255, 255, 255);
- print(20, WINY - 32, "Time:");
- print(80, WINY - 32, int(elapsedTime));
- print(20, WINY - 55, "Cells Alive:");
- print(180, WINY - 55, remaining);
- }
- // Draw Void
- void draw(mCase table[DIMX][DIMY])
- {
- gridDrawer();
- for (glX = 1; glX < DIMX - 1; glX++)
- {
- for (glY = 1; glY < DIMY - 1; glY++)
- {
- //discover(table, glX, glY);
- }
- }
- hud(table);
- }
- int main(int, char**)
- {
- srand(time(NULL));
- mCase table[DIMX][DIMY];
- init(table);
- mPrint(table);
- winInit(title, WINX, WINY);
- setKeyRepeatMode(true);
- grapicInit();
- while (!stop)
- {
- winClear();
- draw(table);
- stop = winDisplay();
- }
- winQuit();
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement