levels.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <draw.h>
  5. #include "sudoku.h"
  6. void
  7. consumeline(Biobuf *b)
  8. {
  9. while(Bgetc(b) != '\n')
  10. ;
  11. }
  12. void
  13. fprettyprintbrd(Cell *board)
  14. {
  15. int x, y, fd;
  16. fd = create("/tmp/sudoku-print", OWRITE|OTRUNC, 0600);
  17. if(fd < 0) {
  18. perror("can not open save file /tmp/sudoku-save");
  19. return;
  20. }
  21. for(x = 0; x < Brdsize; x++) {
  22. for(y = 0; y < Brdsize; y++) {
  23. fprint(fd, " ");
  24. if(board[y*Brdsize + x].digit == -1)
  25. fprint(fd, ".");
  26. else
  27. fprint(fd, "%d", board[y*Brdsize + x].digit+1);
  28. if(((x*Brdsize + y + 1) % Brdsize) == 0 || (x*Brdsize + y + 1) == Psize)
  29. fprint(fd, "\n");
  30. if(((x*Brdsize + y + 1) % 3) == 0 && ((x*Brdsize + y + 1) % Brdsize) != 0)
  31. fprint(fd, "|");
  32. if(((x*Brdsize + y + 1) % 27) == 0 && ((x*Brdsize + y + 1) % Psize) != 0)
  33. fprint(fd, " -------------------\n");
  34. }
  35. }
  36. close(fd);
  37. }
  38. void
  39. fprintbrd(int fd, Cell *board)
  40. {
  41. int i;
  42. for(i = 0; i < Psize; i++) {
  43. if(board[i].digit == -1)
  44. fprint(fd, ".");
  45. else
  46. fprint(fd, "%d", board[i].digit+1);
  47. if((i + 1) % Brdsize == 0)
  48. fprint(fd, "\n");
  49. }
  50. for(i = 0; i < Psize; i++) {
  51. fprint(fd, "%d", board[i].solve+1);
  52. if((i + 1) % Brdsize == 0)
  53. fprint(fd, "\n");
  54. }
  55. close(fd);
  56. }
  57. int
  58. loadlevel(char *name, Cell *board)
  59. {
  60. Biobuf *b;
  61. char c;
  62. int i;
  63. b = Bopen(name, OREAD);
  64. if(b == nil) {
  65. fprint(2, "could not open file %s: %r\n", name);
  66. return -1;
  67. }
  68. i = 0;
  69. while((c = Bgetc(b)) > 0) {
  70. switch(c) {
  71. case '.':
  72. board[i].digit = -1;
  73. board[i].locked = 0;
  74. if(++i == 81)
  75. goto next;
  76. break;
  77. case 0x31:
  78. case 0x32:
  79. case 0x33:
  80. case 0x34:
  81. case 0x35:
  82. case 0x36:
  83. case 0x37:
  84. case 0x38:
  85. case 0x39:
  86. board[i].digit = c - 0x31;
  87. board[i].locked = 1;
  88. if(++i == 81)
  89. goto next;
  90. break;
  91. case '\n':
  92. break;
  93. default:
  94. fprint(2, "unknown character in initial board: %c\n", c);
  95. goto done;
  96. }
  97. }
  98. next:
  99. i = 0;
  100. while((c = Bgetc(b)) > 0) {
  101. switch(c) {
  102. case 0x31:
  103. case 0x32:
  104. case 0x33:
  105. case 0x34:
  106. case 0x35:
  107. case 0x36:
  108. case 0x37:
  109. case 0x38:
  110. case 0x39:
  111. board[i].solve = c - 0x31;
  112. if(++i == 81)
  113. goto done;
  114. break;
  115. case '\n':
  116. break;
  117. default:
  118. fprint(2, "unknown character in board solution: %c\n", c);
  119. goto done;
  120. }
  121. }
  122. done:
  123. Bterm(b);
  124. return i < 81 ? 0 : 1;
  125. }
  126. void
  127. printboard(Cell *board)
  128. {
  129. int fd;
  130. fd = create("/tmp/sudoku-board", OWRITE|OTRUNC, 0600);
  131. if(fd < 0) {
  132. perror("can not open save file /tmp/sudoku-save");
  133. return;
  134. }
  135. fprintbrd(fd, board);
  136. close(fd);
  137. }
  138. void
  139. savegame(Cell *board)
  140. {
  141. int fd;
  142. fd = create("/tmp/sudoku-save", OWRITE|OTRUNC, 0600);
  143. if(fd < 0) {
  144. perror("can not open save file /tmp/sudoku-save");
  145. return;
  146. }
  147. if(write(fd, board, Psize * sizeof(Cell)) != Psize * sizeof(Cell)) {
  148. perror("could not save to file");
  149. close(fd);
  150. }
  151. close(fd);
  152. }
  153. int
  154. loadgame(Cell *board)
  155. {
  156. int fd;
  157. fd = open("/tmp/sudoku-save", OREAD);
  158. if(fd < 0) {
  159. perror("can not open save file /tmp/sudoku-save");
  160. return -1;
  161. }
  162. if(read(fd, board, Psize * sizeof(Cell)) != Psize * sizeof(Cell)) {
  163. perror("insufficient data in save file");
  164. close(fd);
  165. return -1;
  166. }
  167. close(fd);
  168. return 1;
  169. }