summaryrefslogtreecommitdiff
path: root/sys/src/games/sudoku/levels.c
blob: c4b3fad507e626f7986e32919e871a98c43e8e36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <draw.h>

#include "sudoku.h"

void
consumeline(Biobuf *b)
{
	while(Bgetc(b) != '\n')
		;
}

void
fprettyprintbrd(Cell *board)
{
	int x, y, fd;

	fd = create("/tmp/sudoku-print", OWRITE|OTRUNC, 0600);
	if(fd < 0) {
		perror("can not open save file /tmp/sudoku-save");
		return;
	}

	for(x = 0; x < Brdsize; x++) {
		for(y = 0; y < Brdsize; y++) {
			fprint(fd, " ");
			if(board[y*Brdsize + x].digit == -1)
				fprint(fd, ".");
			else
				fprint(fd, "%d", board[y*Brdsize + x].digit+1);

			if(((x*Brdsize + y + 1) % Brdsize) == 0 || (x*Brdsize + y + 1) == Psize)
				fprint(fd, "\n");

			if(((x*Brdsize + y + 1) % 3) == 0 && ((x*Brdsize + y + 1) % Brdsize) != 0)
				fprint(fd, "|");

			if(((x*Brdsize + y + 1) % 27) == 0 && ((x*Brdsize + y + 1) % Psize) != 0)
				fprint(fd, " -------------------\n");

		}
	}
	close(fd);
}

void
fprintbrd(int fd, Cell *board)
{
	int i;
	
	for(i = 0; i < Psize; i++) {
		if(board[i].digit == -1)
			fprint(fd, ".");
		else
			fprint(fd, "%d", board[i].digit+1);

		if((i + 1) % Brdsize == 0)
			fprint(fd, "\n");
	}
	for(i = 0; i < Psize; i++) {
		fprint(fd, "%d", board[i].solve+1);
		if((i + 1) % Brdsize == 0)
			fprint(fd, "\n");
	}

	close(fd);
}

int
loadlevel(char *name, Cell *board)
{
	Biobuf *b;
	char c;
	int i;
	
	b = Bopen(name, OREAD);
	if(b == nil) {
		fprint(2, "could not open file %s: %r\n", name);
		return -1;
	}

	i = 0;
	while((c = Bgetc(b)) > 0) {
		switch(c) {
		case '.':
			board[i].digit = -1;
			board[i].locked = 0;
			if(++i == 81)
				goto next;
			break;
		case 0x31:
		case 0x32:
		case 0x33:
		case 0x34:
		case 0x35:
		case 0x36:
		case 0x37:
		case 0x38:
		case 0x39:
			board[i].digit = c - 0x31;
			board[i].locked = 1;
			if(++i == 81)
				goto next;
			break;
		case '\n':
			break;
		default:
			fprint(2, "unknown character in initial board: %c\n", c);
			goto done;
		}
	}
next:
	i = 0;
	while((c = Bgetc(b)) > 0) {
		switch(c) {
		case 0x31:
		case 0x32:
		case 0x33:
		case 0x34:
		case 0x35:
		case 0x36:
		case 0x37:
		case 0x38:
		case 0x39:
			board[i].solve = c - 0x31;
			if(++i == 81)
				goto done;
			break;
		case '\n':
			break;
		default:
			fprint(2, "unknown character in board solution: %c\n", c);
			goto done;
		}
	}

done:
	Bterm(b);

	return i < 81 ? 0 : 1;
}

void
printboard(Cell *board)
{
	int fd;
	
	fd = create("/tmp/sudoku-board", OWRITE|OTRUNC, 0600);
	if(fd < 0) {
		perror("can not open save file /tmp/sudoku-save");
		return;
	}

	fprintbrd(fd, board);

	close(fd);
}

void
savegame(Cell *board)
{
	int fd;
	
	fd = create("/tmp/sudoku-save", OWRITE|OTRUNC, 0600);
	if(fd < 0) {
		perror("can not open save file /tmp/sudoku-save");
		return;
	}

	if(write(fd, board, Psize * sizeof(Cell)) != Psize * sizeof(Cell)) {
		perror("could not save to file");
		close(fd);
	}

	close(fd);
}

int
loadgame(Cell *board)
{
	int fd;

	fd = open("/tmp/sudoku-save", OREAD);
	if(fd < 0) {
		perror("can not open save file /tmp/sudoku-save");
		return -1;
	}

	if(read(fd, board, Psize * sizeof(Cell)) != Psize * sizeof(Cell)) {
		perror("insufficient data in save file");
		close(fd);
		return -1;
	}
	
	close(fd);

	return 1;
}