Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <fstream>
  5. #include <string>
  6. #include <sstream>
  7.  
  8. using namespace std;
  9.  
  10. const int gridSize = 25;
  11. void printGrid(bool gridOne[gridSize + 1][gridSize + 1]);
  12. void determineState(bool gridOne[gridSize + 1][gridSize + 1]);
  13. void clearScreen(void);
  14.  
  15. int main() {
  16.  
  17. bool gridOne[gridSize + 1][gridSize + 1] = {};
  18. int x, y, n;
  19. string nc;
  20. string start;
  21. string filename;
  22. cout << "Enter the number of cells";
  23. cin >> nc;
  24. cout << endl;
  25.  
  26. for (int i = 0; i < stoi(nc); i++)
  27. {
  28. cout << stoi(nc) << " Enter the coordinates of cell " << i + 1 << " : ";
  29. cin >> x >> y;
  30. gridOne[x][y] = true;
  31. printGrid(gridOne);
  32. }
  33.  
  34. cout << "Grid setup is done. Start the game ? (y/n)" << endl;
  35. printGrid(gridOne);
  36. cin >> start;
  37. if (start == "y" || start == "Y")
  38. {
  39. while (true)
  40. {
  41. printGrid(gridOne);
  42. determineState(gridOne);
  43. // clearScreen();
  44. }
  45. }
  46. else
  47. {
  48. return 0;
  49. }
  50. }
  51.  
  52. void clearScreen(void) {
  53. // Tested and working on Ubuntu and Cygwin
  54. #if defined(_WIN32) || defined(WIN32) || defined(__MINGW32__) || defined(__BORLANDC__)
  55. #define OS_WIN
  56. #endif
  57. #ifdef OS_WIN
  58. system("CLS");
  59. #endif
  60.  
  61. #if defined(linux) || defined(__CYGWIN__)
  62. system("clear");
  63. #endif
  64. }
  65.  
  66. void printGrid(bool gridOne[gridSize + 1][gridSize + 1]) {
  67. for (int a = 1; a < gridSize; a++)
  68. {
  69. for (int b = 1; b < gridSize; b++)
  70. {
  71. if (gridOne[a][b] == true)
  72. {
  73. cout << " O ";
  74. }
  75. else
  76. {
  77. cout << " - ";
  78. }
  79. if (b == gridSize - 1)
  80. {
  81. cout << endl;
  82. }
  83. }
  84. }
  85. }
  86.  
  87. void compareGrid(bool gridOne[gridSize + 1][gridSize + 1], bool gridTwo[gridSize + 1][gridSize + 1]) {
  88. for (int a = 0; a < gridSize; a++)
  89. {
  90. for (int b = 0; b < gridSize; b++)
  91. {
  92. gridTwo[a][b] = gridOne[a][b];
  93. }
  94. }
  95. }
  96.  
  97. void determineState(bool gridOne[gridSize + 1][gridSize + 1]) {
  98. bool gridTwo[gridSize + 1][gridSize + 1] = {};
  99. compareGrid(gridOne, gridTwo);
  100.  
  101. for (int a = 1; a < gridSize; a++)
  102. {
  103. for (int b = 1; b < gridSize; b++)
  104. {
  105. int alive = 0;
  106. for (int c = -1; c < 2; c++)
  107. {
  108. for (int d = -1; d < 2; d++)
  109. {
  110. if (!(c == 0 && d == 0))
  111. {
  112. if (gridTwo[a + c][b + d])
  113. {
  114. ++alive;
  115. }
  116. }
  117. }
  118. }
  119. if (alive < 2)
  120. {
  121. gridOne[a][b] = false;
  122. }
  123. else if (alive == 3)
  124. {
  125. gridOne[a][b] = true;
  126. }
  127. else if (alive > 3)
  128. {
  129. gridOne[a][b] = false;
  130. }
  131. }
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement