Advertisement
Guest User

Untitled

a guest
Jul 6th, 2015
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "main.h"
  4.  
  5. //read a world file
  6.  
  7. int readFile(FILE *file){
  8.  
  9. int a;
  10. if((a = getc(file)) == EOF){
  11. return 0;
  12. }
  13.  
  14. //convert ASCII codes
  15.  
  16. switch(a){
  17. case 48:
  18. return 0;
  19. case 49:
  20. return 1;
  21. case 50:
  22. return 2;
  23. case 51:
  24. return 3;
  25. case 52:
  26. return 4;
  27. case 53:
  28. return 5;
  29. case 54:
  30. return 6;
  31. case 55:
  32. return 7;
  33. case 56:
  34. return 8;
  35. case 57:
  36. return 9;
  37. }
  38. return 0;
  39. }
  40.  
  41. //initialize a world structure
  42.  
  43. struct world readWorld(char *filename){
  44.  
  45. struct world w;
  46. FILE *file;
  47. int a,b;
  48.  
  49. file = fopen(filename, "r");
  50.  
  51. w.rows = readFile(file);
  52. w.cols = readFile(file);
  53.  
  54. printf("%d",w.rows);
  55.  
  56. w.tile = malloc(w.rows*sizeof(int*));
  57. for(a=0;a<w.rows;a++)
  58. w.tile[a] = malloc(w.cols*sizeof(int*));
  59.  
  60. for(a=0;a<w.rows;a++)
  61. for(b=0;b<w.cols;b++){
  62. w.tile[a][b] = readFile(file);
  63. }
  64.  
  65. fclose(file);
  66.  
  67. for(a=0;a<w.rows;a++)
  68. for(b=0;b<w.cols;b++)
  69. printf("%d",w.tile[a][b]);
  70. return w;
  71. }
  72.  
  73. //free allocated world memory
  74.  
  75. void freeWorld(struct world w){
  76. int a;
  77. for(a=0;a<w.rows;a++)
  78. free(w.tile[a]);
  79. free(w.tile);
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement