-
Notifications
You must be signed in to change notification settings - Fork 0
/
nD-viergewinnt.c
304 lines (290 loc) · 8.32 KB
/
nD-viergewinnt.c
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include <math.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ncurses.h>
#include "datastructures.h"
#define N_DIMS 5
#define SIZE 5
#define N_PLAYER 2
#define N_AXES (pow(2, N_DIMS)-1)
unsigned int make_lin_index(int coords[]){
unsigned int lin_index = 0;
for (int i = 0; i < N_DIMS; i++)
{
lin_index += coords[i]*ipow(SIZE, N_DIMS-1-i);
}
return lin_index;
}
int ipow(int base, int exp)
{
int result = 1;
for (;;)
{
if (exp & 1)
result *= base;
exp >>= 1;
if (!exp)
break;
base *= base;
}
return result;
}
hashlist_t player_owned[N_PLAYER];
int current_player = 1;
int cursors[N_PLAYER][N_DIMS];
int moving_dim[N_PLAYER];
int coords_moving_token[N_DIMS];
int moving_token = 0;
int compare_coords(void * coords1, void * coords2){
int* co1 = (int *) coords1;
int* co2 = (int *) coords2;
for (int i = 0; i < N_DIMS; i++)
{
if(co1[i] != co2[i]){
return 0;
}
}
return 1;
}
unsigned int hash_coords(void* coords){
int* co = (int *) coords;
unsigned int hash = 0;
for (int i = 0; i < N_DIMS; i++)
{
hash += co[i]*ipow(SIZE, i);
}
return hash;
}
void setup(){
for (int i = 0; i < N_PLAYER; i++)
{
moving_dim[i] = 1;
}
for (int i = 0; i < N_PLAYER; i++)
{
for (int j = 1; j < N_DIMS; j++)
{
cursors[i][j] = SIZE/2;
}
cursors[i][0] = 0;
player_owned[i].chain_array = (linked_list_t *) malloc(10*sizeof(linked_list_t));
player_owned[i].length = 10;
player_owned[i].hash = &hash_coords;
player_owned[i].compare = &compare_coords;
}
start_color();
init_pair(0, COLOR_WHITE, COLOR_BLACK);
init_pair(1, COLOR_GREEN, COLOR_BLACK);
init_pair(2, COLOR_RED, COLOR_BLACK);
init_pair(10, COLOR_BLACK, COLOR_YELLOW);
}
int check_win_rec(int dim, int coords[]){
if (dim <= N_DIMS-1){
for (int i = 0; i< SIZE; i++){
// coords array kopieren.
int new_coords[N_DIMS];
for (int d= 0;d<dim;d++){
new_coords[d] = coords[d];
}
new_coords[dim] = i;
int win = check_win_rec(dim+1, new_coords);
if (win == 1){
return 1;
}
}
return 0;
}
else{
int win = check_win_coords(coords);
return win;
}
}
int get_coords(int coords[]){
for(int i = 0; i<N_DIMS;i++){
if (coords[i] >= SIZE || coords[i] < 0){
return 0;
}
}
for (int i = 0; i < N_PLAYER; i++)
{
if(lookup_hashlist(&player_owned[i], coords) == 1){
return i+1;
}
}
return 0;
}
void set_coords(int coords[], int n){
int * coords_append = (int *) malloc(N_DIMS*sizeof(int));
for (int i = 0; i < N_DIMS; i++)
{
coords_append[i] = coords[i];
}
append_to_hashlist(&player_owned[n-1], (void *) coords_append);
}
int inplace_count_axis(int coords[], int new_coords[], int s, int a){
/*
a is an integer whose binary representation is a code for the axis that is counted along.
An point along an axis with offset x has in every element either 0 or +/-x.
An axis is defined by the dimensions in wich the their elemts are zero.
A dimension that has +/-x is represented by a 1 on that doesnt change (stays zero) is represented by 0.
Those 0s and 1s can be represented as an integer.
Each of the axis can be rotated now wich means they can have different signs in every dimension.
The sign combination can be represented in the same manner as the axis is represented.
*/
int count = 0;
for(int j = 0; j < 4; j++){
for(int k = 0; k < N_DIMS; k++){
if((s >> k) & 1 == 1)
new_coords[k] = coords[k] + j*((a >> k) & 1); //signmask positive
else
new_coords[k] = coords[k] - j*((a >> k) & 1); //signmask negative
}
if(get_coords(new_coords) == current_player){
count++;
}
else
break;
}
return count;
}
int check_win_coords(int coords[]){
// really bad algortihm expeonential complexity but no other way around I think
for (int i = 0; i < N_AXES; i++)
{
int axis = i+1; //equal or zero mask
int max_row = 0;
int new_coords[N_DIMS];
for (int s = 0; s<=N_AXES/2;s++){ //s sign mask
int s_up = s;
int s_down = ~s; // bit flipping the signmask to get the opposite direction of the axis
max_row = inplace_count_axis(coords, new_coords, s_up, axis);
max_row += inplace_count_axis(coords, new_coords, s_down, axis);
if (max_row > 4) return 1; // greater than and not not equal because coords are counted double
max_row = 0;
}
}
return 0;
}
int check_win(){
int coords[N_DIMS];
return check_win_rec(0, coords);
}
int is_cursor(int coords[]){
for(int i = 0;i<N_DIMS;i++){
if(coords[i] != cursors[current_player-1][i]){
return 0;
}
}
return 1;
}
int is_moving_token(int coords[]){
if(moving_token==0){
return 0;
}
for(int i = 0;i<N_DIMS;i++){
if(coords[i] != coords_moving_token[i]){
return 0;
}
}
return 1;
}
void print_coords(int coords[]){
if(is_cursor(coords) == 1 || is_moving_token(coords) == 1){
attron(COLOR_PAIR(current_player));
printw(" # ");
attroff(COLOR_PAIR(current_player));
return;
}
int token = get_coords(coords);
attron(COLOR_PAIR(token));
printw(" # ");
attroff(COLOR_PAIR(token));
}
void display(){
clear();
int coords[N_DIMS];
for(int i = 2;i<N_DIMS;i++){
coords[i] = cursors[current_player-1][i];
}
for(int y = 0;y < SIZE;y++){
for(int x = 0; x < SIZE; x++){
coords[0] = y;
coords[1] = x;
print_coords(coords);
}
printw("\n");
}
printw("Spieler %d: ", current_player);
for (int i = 0; i < N_DIMS; i++)
{
if(i == moving_dim[current_player-1]){
attron(COLOR_PAIR(10));
printw("%d ", cursors[current_player-1][i]);
attroff(COLOR_PAIR(10));
}
else
printw("%d ", cursors[current_player-1][i]);
}
refresh();
}
void handle_input(){
char c = getch();
if(c=='a' && moving_dim[current_player-1]>1) moving_dim[current_player-1]--;
else if(c=='d' && moving_dim[current_player-1]<N_DIMS-1) moving_dim[current_player-1]++;
else if(c=='s' && cursors[current_player-1][moving_dim[current_player-1]]<SIZE-1)
cursors[current_player-1][moving_dim[current_player-1]]++;
else if(c=='w' && cursors[current_player-1][moving_dim[current_player-1]]>0)
cursors[current_player-1][moving_dim[current_player-1]]--;
else if(c=='x') {
moving_token = 1;
for(int i = 0; i<N_DIMS; i++){
coords_moving_token[i] = cursors[current_player-1][i];
}
}
while(getch()!=ERR);
}
void update(int frame){
if(moving_token == 1){
int new_moving_token_coords[N_DIMS];
for(int i = 0; i<N_DIMS; i++){
new_moving_token_coords[i] = coords_moving_token[i];
}
new_moving_token_coords[0]++;
if(new_moving_token_coords[0]<SIZE && get_coords(new_moving_token_coords)==0){
for(int i = 0; i<N_DIMS; i++){
coords_moving_token[i] = new_moving_token_coords[i];
}
}
else{
set_coords(coords_moving_token, current_player);
if(check_win_coords(coords_moving_token)==1){
endwin();
printf("Spieler %d hat gewonnen", current_player);
exit(0);
}
current_player = current_player + 1;
if(current_player == N_PLAYER+1){
current_player = 1;
}
moving_token = 0;
}
}
}
int main(){
initscr();
nodelay(stdscr, TRUE);
noecho();
setup();
int frame = 0;
while(1){
display();
handle_input();
refresh();
update(frame);
frame++;
usleep(100*1000);
}
endwin();
return 0;
}