11 while(Bgetc(b) != '\n')
16 fprettyprintbrd(Cell *board)
20 fd = create("/tmp/sudoku-print", OWRITE|OTRUNC, 0600);
22 perror("can not open save file /tmp/sudoku-save");
26 for(x = 0; x < Brdsize; x++) {
27 for(y = 0; y < Brdsize; y++) {
29 if(board[y*Brdsize + x].digit == -1)
32 fprint(fd, "%d", board[y*Brdsize + x].digit+1);
34 if(((x*Brdsize + y + 1) % Brdsize) == 0 || (x*Brdsize + y + 1) == Psize)
37 if(((x*Brdsize + y + 1) % 3) == 0 && ((x*Brdsize + y + 1) % Brdsize) != 0)
40 if(((x*Brdsize + y + 1) % 27) == 0 && ((x*Brdsize + y + 1) % Psize) != 0)
41 fprint(fd, " -------------------\n");
49 fprintbrd(int fd, Cell *board)
53 for(i = 0; i < Psize; i++) {
54 if(board[i].digit == -1)
57 fprint(fd, "%d", board[i].digit+1);
59 if((i + 1) % Brdsize == 0)
62 for(i = 0; i < Psize; i++) {
63 fprint(fd, "%d", board[i].solve+1);
64 if((i + 1) % Brdsize == 0)
72 loadlevel(char *name, Cell *board)
78 b = Bopen(name, OREAD);
80 fprint(2, "could not open file %s: %r\n", name);
85 while((c = Bgetc(b)) > 0) {
102 board[i].digit = c - 0x31;
110 fprint(2, "unknown character in initial board: %c\n", c);
116 while((c = Bgetc(b)) > 0) {
127 board[i].solve = c - 0x31;
134 fprint(2, "unknown character in board solution: %c\n", c);
142 return i < 81 ? 0 : 1;
146 printboard(Cell *board)
150 fd = create("/tmp/sudoku-board", OWRITE|OTRUNC, 0600);
152 perror("can not open save file /tmp/sudoku-save");
156 fprintbrd(fd, board);
162 savegame(Cell *board)
166 fd = create("/tmp/sudoku-save", OWRITE|OTRUNC, 0600);
168 perror("can not open save file /tmp/sudoku-save");
172 if(write(fd, board, Psize * sizeof(Cell)) != Psize * sizeof(Cell)) {
173 perror("could not save to file");
181 loadgame(Cell *board)
185 fd = open("/tmp/sudoku-save", OREAD);
187 perror("can not open save file /tmp/sudoku-save");
191 if(read(fd, board, Psize * sizeof(Cell)) != Psize * sizeof(Cell)) {
192 perror("insufficient data in save file");