Advertisement
Guest User

asdf

a guest
Jun 28th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1.  
  2. #include "Life.h"
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. Matrix newMatrix(unsigned int length){
  7. Matrix res;
  8. res.length=length;
  9. res.data = malloc(sizeof(bool*) * length);
  10. int i;
  11. for (i = 0; i < length ; i ++){
  12. res.data[i] = malloc(sizeof(bool*) * length);
  13. }
  14. int j;
  15. for(i=0; i<length; i++){
  16. for(j=0; j<length; j++){
  17. res.data[i][j]=false;
  18. }
  19. }
  20. return res;
  21. }
  22.  
  23. void deleteMatrix(Matrix m){
  24. int i=0;
  25. int length=m.length;
  26. for (i;i<length;i++){
  27. free(m.data[i]);
  28. }
  29. free(m.data);
  30. }
  31.  
  32. char* showMatrix(Matrix m){
  33. char* res;
  34. res = malloc(((m.length*(m.length+1))+1)*sizeof(char));
  35. showMatrixIn(m,res);
  36. return res;
  37. }
  38.  
  39. void showMatrixIn(Matrix m,char* result){
  40. int i;
  41. int j;
  42. int q=0;
  43. for(i=0; i<m.length; i++){
  44. for(j=0; j<=m.length; j++){
  45. if(j==m.length){
  46. result[q]='\n';
  47. q++;
  48. }
  49. if(j!=m.length&&m.data[i][j]==true){
  50. result[q]='*';
  51. q++;
  52. }
  53. if(j<m.length&&m.data[i][j]==false){
  54. result[q]=' ';
  55. q++;
  56. }
  57. }
  58. }
  59. result[q]='\0';
  60. }
  61.  
  62. unsigned int anzahlBelegterNachbarn(Matrix m,unsigned int x, unsigned int y){
  63. unsigned int result=0;
  64. unsigned int startX=x-1;
  65. unsigned int startY=y-1;
  66. unsigned int stopX= x+1;
  67. unsigned int stopY=y+1;
  68. unsigned int i;
  69. unsigned int j;
  70.  
  71. if (x==0) {
  72. startX=x;
  73. }
  74. if (x==m.length-1) {
  75. stopX=x;
  76. }
  77. if (y==0) {
  78. startY=y;
  79. }
  80. if (y==m.length-1) {
  81. stopY=y;
  82. }
  83.  
  84. for (i=startX; i<=stopX; i++){
  85. for (j=startY;j<=stopY;j++){
  86. if (i==x&& j==y){}
  87. else {
  88. if (m.data[i][j]) result++;
  89. }
  90. }
  91. }
  92. return result;
  93. }
  94.  
  95. Matrix zugGOL(Matrix m){
  96. Matrix res = newMatrix(m.length);
  97. zugGOLIn(m,res);
  98. return res;
  99. }
  100.  
  101. void zugGOLIn(Matrix m, Matrix result){
  102. int i;
  103. int j;
  104. for(i=0; i<m.length; i++){
  105. for(j=0; j<m.length; j++){
  106. int anzahl = anzahlBelegterNachbarn(m,i,j);
  107.  
  108. if(m.data[i][j]==true){
  109. if(anzahl<=1) result.data[i][j]=false;
  110. if(anzahl>=4) result.data[i][j]=false;
  111. if(anzahl==2||anzahl==3) result.data[i][j]=true;
  112. }else{
  113. if(anzahl==3) result.data[i][j]=true;
  114. }
  115. }
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement