This repository was archived by the owner on May 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
113 lines (112 loc) · 2.94 KB
/
Copy pathclient.c
File metadata and controls
113 lines (112 loc) · 2.94 KB
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
#include "client.h"
#include "game.h"
int main() {
int server = client_setup();
char move[2];
read(server, move, sizeof(move));
int turn = move[1] % 2;
int counter = 0;
//SDL
if (SDL_Init(SDL_INIT_VIDEO) != 0){
printf("Initializing SDL2 failed%s\n", SDL_GetError());
return EXIT_FAILURE;
}
SDL_Window * window = SDL_CreateWindow("Hello, SDL2", 100, 100, SCREEN_WIDTH, SCREEN_HEIGHT,SDL_WINDOW_SHOWN);
if (window == NULL){
printf("Create Window error %s\n", SDL_GetError());
return EXIT_FAILURE;
}
SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (renderer == NULL){
SDL_DestroyWindow(window);
printf("Renderer Error: %s\n", SDL_GetError());
return EXIT_FAILURE;
}
struct game_t game = {{{}}, move[1], RUNNING_STATE, -1};
SDL_Event first;
SDL_PollEvent(&first);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
rendering_game(renderer, &game);
SDL_RenderPresent(renderer);
while (!game.state) {
if (turn) {
int valid = 0;
while (!valid) {
while (SDL_PollEvent(&first)) {
switch (first.type) {
case SDL_QUIT:
valid = 1;
game.state = QUIT_STATE;
break;
case SDL_MOUSEBUTTONDOWN:
valid = on_click(&game, first.button.y / CELL_HEIGHT, first.button.x / CELL_WIDTH, move);
break;
default: {}
}
break;
}
}
write(server, move, sizeof(move));
while (SDL_PollEvent(&first)) {}
} else {
read(server, move, sizeof(move));
if (move[0] == 127) {
return 0;
}
edit_board(&game, move[0], move[1], (game.player) % 2 + 1);
}
counter++;
if (game.state == QUIT_STATE) {
break;
}
if (counter == 81) {
game.state = TIE;
}
int state = victory(game.board);
if (state) {
if (state == game.player) {
game.state = WON;
} else {
game.state = LOST;
}
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
rendering_game(renderer, &game);
SDL_RenderPresent(renderer);
//display win, if applicable
turn = !turn;
}
if (game.state != QUIT_STATE) {
SDL_Delay(5000);
}
SDL_Quit();
return 0;
}
//x = 1; o = 2;
int victory(char board[9][9]) {
int i, j;
for (i = 0; i < 9; i++) {
//rows
for (j = 0; j < 3; j++) {
if (board[i][3*j] && board[i][3*j+0] == board[i][3*j+1] && board[i][3*j+1] == board[i][3*j+2]) {
return board[i][3*j];
}
}
//columns
for (j = 0; j < 3; j++) {
if (board[i][j] && board[i][j+0] == board[i][j+3] && board[i][j+3] == board[i][j+6]) {
return board[i][j];
}
}
//diagonal
if (board[i][0] && board[i][0] == board[i][4] && board[i][4] == board[i][8]) {
return board[i][0];
}
if (board[i][2] && board[i][2] == board[i][4] && board[i][4] == board[i][6]) {
return board[i][2];
}
}
return 0;
}