Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.01 KB | None | 0 0
  1. /*
  2. * Programmeringsteknik med C och Matlab
  3. * Fall 17
  4. * Assignment 3
  5.  
  6. * File: ou3.c
  7. * Description: A simple implementation of Conway's Game of Life.
  8. * Author: William Fransson
  9. * CS username: c17wfn
  10. * Date: 2017-10-16
  11. * Input: Choice of initial configuration and then instruction to step
  12. * or exit.
  13. * Output: Prints the game field in each step.
  14. * Limitations: No validation of input.
  15. */
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <time.h>
  20.  
  21. /* Constants, representation of states */
  22. #define ALIVE 'X'
  23. #define DEAD '.'
  24.  
  25. /* Declaration of data structure */
  26. typedef struct{
  27. char current;
  28. char next;
  29. } cell;
  30.  
  31. /* Declaration of functions */
  32. void initField(const int rows, const int cols, cell field[rows][cols]);
  33. void loadGlider(const int rows, const int cols, cell field[rows][cols]);
  34. void loadSemaphore(const int rows, const int cols, cell field[rows][cols]);
  35. void loadRandom(const int rows, const int cols, cell field[rows][cols]);
  36. void loadCustom(const int rows, const int cols, cell field[rows][cols]);
  37. void printField(const int rows, const int cols, cell field[rows][cols]);
  38. int optionSelect();
  39. void updateField(const int rows, const int cols, cell field[rows][cols]);
  40. void determineState(int r, int c, const int rows, const int cols, cell field[rows][cols]);
  41. int checkNrOfNeighbours(int r, int c, const int rows, const int cols, cell field[rows][cols]);
  42.  
  43.  
  44. /* Function: main
  45. * Description: Start and run games, interact with the user.
  46. * Input: About what initial structure and whether to step or exit.
  47. * Output: Information to the user, and the game field in each step.
  48. */
  49.  
  50. int main(void) {
  51.  
  52. const int rows = 20;
  53. const int cols = 20;
  54. int option = 1;
  55. cell field[rows][cols];
  56.  
  57. initField(rows, cols, field);
  58.  
  59. do{
  60. printField(rows, cols, field);
  61. updateField(rows, cols, field);
  62. option = optionSelect();
  63. }while(option);
  64. return 0;
  65. }
  66.  
  67.  
  68. /* Function: initField
  69. * Description: Initialize all the cells to dead, then asks the user about
  70. * which structure to load, and finally load the structure.
  71. * Input: The field array and its size.
  72. * Output: The field array is updated.
  73. */
  74.  
  75. void initField(const int rows, const int cols, cell field[rows][cols]) {
  76.  
  77. for (int r = 0 ; r < rows ; r++) {
  78. for (int c = 0 ; c < cols ; c++) {
  79. field[r][c].current = DEAD;
  80. }
  81. }
  82.  
  83. printf("Select field spec to load ([G]lider, [S]emaphore, [R]andom ");
  84. printf("or [C]ustom): ");
  85.  
  86. int ch = getchar();
  87. switch (ch) {
  88. case 'g':
  89. case 'G':
  90. loadGlider(rows, cols, field);
  91. break;
  92. case 's':
  93. case 'S':
  94. loadSemaphore(rows, cols, field);
  95. break;
  96. case 'r':
  97. case 'R':
  98. loadRandom(rows, cols, field);
  99. break;
  100. case 'c':
  101. case 'C':
  102. default:
  103. loadCustom(rows, cols, field);
  104. break;
  105. }
  106.  
  107. /* Ignore following newline */
  108. if (ch != '\n') {
  109. getchar();
  110. }
  111. }
  112.  
  113.  
  114. /* Function: loadGlider
  115. * Description: Inserts a glider into the field.
  116. * Input: The field array and its size.
  117. * Output: The field array is updated.
  118. */
  119.  
  120. void loadGlider(const int rows, const int cols, cell field[rows][cols]) {
  121.  
  122. field[0][1].current = ALIVE;
  123. field[1][2].current = ALIVE;
  124. field[2][0].current = ALIVE;
  125. field[2][1].current = ALIVE;
  126. field[2][2].current = ALIVE;
  127. }
  128.  
  129.  
  130. /* Function: loadSemaphore
  131. * Description: Inserts a semaphore into the field.
  132. * Input: The field array and its size.
  133. * Output: The field array is updated.
  134. */
  135.  
  136. void loadSemaphore(const int rows, const int cols, cell field[rows][cols]) {
  137.  
  138. field[8][1].current = ALIVE;
  139. field[8][2].current = ALIVE;
  140. field[8][3].current = ALIVE;
  141. }
  142.  
  143.  
  144. /* Function: loadRandom
  145. * Description: Inserts a random structure into the field.
  146. * Input: The field array and its size.
  147. * Output: The field array is updated. There is a 50 % chance that a cell
  148. * is alive.
  149. */
  150.  
  151. void loadRandom(const int rows, const int cols, cell field[rows][cols]) {
  152.  
  153. for(int r = 0; r < rows; r++){
  154. for(int c = 0; c < cols; c++){
  155.  
  156. int randomNumber = rand() % 2;
  157.  
  158. if(randomNumber == 1) {
  159. field[r][c].current = ALIVE;
  160. }
  161. else{
  162. field[r][c].current = DEAD;
  163. }
  164. }
  165. }
  166. }
  167.  
  168.  
  169. /* Function: loadCustom
  170. * Description: Lets the user specify a structure that then is inserted into
  171. * the field.
  172. * Input: The field array and its size.
  173. * Output: The field array is updated.
  174. */
  175.  
  176. void loadCustom(const int rows, const int cols, cell field[rows][cols]) {
  177.  
  178. printf("Give custom format string: ");
  179. do {
  180. int r, c;
  181. scanf("%d,%d", &r, &c);
  182. field[r][c].current = ALIVE;
  183. } while (getchar() != '\n');
  184. }
  185.  
  186.  
  187. /* Function: printField
  188. * Description: Prints all the cells as a 20 x 20 field.
  189. *
  190. * Input: The field array and its size.
  191. * Output: The field array is updated.
  192. */
  193.  
  194. void printField(const int rows, const int cols, cell field[rows][cols]) {
  195.  
  196. for(int r = 0; r < rows; r++){
  197. for(int c = 0; c < cols; c++){
  198. printf("%c ", field[r][c].current);
  199. }
  200. printf("\n");
  201. }
  202. }
  203.  
  204.  
  205. /* Function: optionSelect
  206. * Description: Lets user decide if the program will go to the next step or
  207. * exit.
  208. * Input: Variable for running the while-loop.
  209. * Output: Returns a new value for the run variable.
  210. */
  211.  
  212. int optionSelect() {
  213.  
  214. char ch;
  215.  
  216. printf("Select one of the following options:\n");
  217. printf("\t(enter) Step\n");
  218. printf("\t(any) Exit\n");
  219. scanf("%c", &ch);
  220.  
  221. if (ch == '\n') {
  222. return 1;
  223. }
  224. else{
  225. return 0;
  226. }
  227. }
  228.  
  229.  
  230. /* Function: updateField
  231. * Description: Updates the cells to their next values.
  232. *
  233. * Input: The field array and its size.
  234. * Output: The field array is updated.
  235. */
  236.  
  237. void updateField(const int rows, const int cols, cell field[rows][cols]) {
  238.  
  239. for(int r = 0; r < rows; r++) {
  240. for(int c = 0; c < cols; c++) {
  241. determineState(r, c, rows, cols, field);
  242. }
  243. }
  244.  
  245. for(int r = 0; r < rows; r++) {
  246. for(int c = 0; c < cols; c++) {
  247. field[r][c].current = field[r][c].next;
  248. }
  249. }
  250. }
  251.  
  252.  
  253. /* Function: determineState
  254. * Description: Determines state(Alive or Dead) of the next generation of cells.
  255. *
  256. * Input: An element of the Field Array.
  257. * Output: Determines the next state of a cell.
  258. */
  259.  
  260. void determineState(int r, int c, const int rows, const int cols, cell field[rows][cols]) {
  261.  
  262. int neighbours;
  263.  
  264. neighbours = checkNrOfNeighbours(r, c, rows, cols, field);
  265.  
  266. if(field[r][c].current == ALIVE && (neighbours == 2 || neighbours == 3)) {
  267.  
  268. field[r][c].next = ALIVE;
  269. }
  270. else if(field[r][c].current == DEAD && (neighbours == 3)) {
  271.  
  272. field[r][c].next = ALIVE;
  273. }
  274. else{
  275.  
  276. field[r][c].next = DEAD;
  277. }
  278. }
  279.  
  280.  
  281. /* Function: checkNrOfNeighbours
  282. * Description: Calculates the number of neighbours next to the cell.
  283. *
  284. * Input: An element of the Field Array.
  285. * Output: Returns the number of neighbours related to a certain element.
  286. */
  287.  
  288. int checkNrOfNeighbours(int r, int c, const int rows, const int cols, cell field[rows][cols]) {
  289.  
  290. int n = 0;
  291.  
  292. for(int i = r - 1; i <= r + 1 ; i++) {
  293. for(int j = c - 1 ; j <= c + 1 ; j++){
  294. if(i >= 0 && i < 20 && j >= 0 && j < 20){
  295. if(field[i][j].current == ALIVE){
  296.  
  297. n++;
  298. }
  299. }
  300. }
  301. }
  302. /* To prevent the cell to count itself as a neighbour*/
  303. if(field[r][c].current == ALIVE) {
  304.  
  305. n--;
  306. }
  307.  
  308. return n;
  309. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement