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 = 20;
- const int DIMY = 20;
- int ALIVE = 75;
- const bool DEBUG = true;
- // Tools Vars
- int glX = 0;
- int glY = 0;
- int turn = 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;
- 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)
- {
- table[glX][glY].nearAlive = 0;
- 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].nearAlive++;
- }
- }
- }
- if (table[glX][glY].isAlive) table[glX][glY].nearAlive--;
- }
- // 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])
- {
- //system("cls");
- 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].nearAlive;
- 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);
- print(150, WINY - 32, "Step:");
- print(230, WINY - 32, turn);
- color(0, 0, 0);
- line(0, 20, 20, 20);
- line(20, 0, 20, 20);
- print(3, -4, "R");
- }
- // Display Cells
- void discover(mCase table[DIMX][DIMY], int x, int y)
- {
- if (!table[x][y].isAlive) return;
- int wX = x + SIZE * x;
- int wY = 30 + -y + SIZE * (DIMY - 2 - y)+8;
- color(250, 0, 0);
- print(wX, wY, "O");
- }
- // Reset Void
- void reset(mCase table[DIMX][DIMY])
- {
- system("cls");
- init(table);
- startTime = clock();
- turn = 0;
- }
- // Finish Detector 3000
- bool isFinished(mCase table[DIMX][DIMY], mCase previous[2][DIMX][DIMY])
- {
- if (table == previous[0] || table == previous[1] || table == previous[2]) return true;
- else
- {
- cout << "P0";
- mPrint(previous[0]);
- cout << "P1";
- mPrint(previous[1]);
- cout << "P2";
- mPrint(previous[2]);
- memcpy(previous[2], previous[1], DIMX*DIMY);
- memcpy(previous[1], previous[0], DIMX*DIMY);
- memcpy(previous[0], table, DIMX*DIMY);
- return false;
- }
- }
- // 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);
- }
- // Update Void
- void update(mCase table[DIMX][DIMY], mCase previous[2][DIMX][DIMY])
- {
- delay(150);
- system("cls");
- int x, y;
- mousePos(x, y);
- if (x < 20 && y < 20)
- {
- reset(table);
- return;
- }
- turn++;
- for (int x = 1; x < DIMX - 1; x++)
- {
- for (int y = 1; y < DIMY - 1; y++)
- {
- checkNear(table, x, y);
- }
- }
- for (int x = 1; x < DIMX - 1; x++)
- {
- for (int y = 1; y < DIMY - 1; y++)
- {
- // if (table[x][y].isAlive && (table[x][y].nearAlive == 2 || table[x][y].nearAlive == 3)) // Stance
- if (table[x][y].isAlive && table[x][y].nearAlive >= 4) table[x][y].isAlive = false; // Mort
- else if (table[x][y].isAlive && table[x][y].nearAlive <= 1) table[x][y].isAlive = false;
- else if (!table[x][y].isAlive && table[x][y].nearAlive == 3) table[x][y].isAlive = true;
- }
- }
- for (int x = 1; x < DIMX - 1; x++)
- {
- for (int y = 1; y < DIMY - 1; y++)
- {
- checkNear(table, x, y);
- }
- }
- mPrint(table);
- if (isFinished(table, previous)) cout << "Finished";
- }
- int main(int, char**)
- {
- srand(time(NULL));
- mCase table[DIMX][DIMY];
- mCase previous[2][DIMX][DIMY];
- init(table);
- mPrint(table);
- winInit(title, WINX, WINY);
- setKeyRepeatMode(true);
- grapicInit();
- while (!stop)
- {
- winClear();
- draw(table);
- if (isMousePressed(SDL_BUTTON_LEFT)) update(table, previous);
- stop = winDisplay();
- }
- winQuit();
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement