Kernel 0.5.9-r12; UDP protocol stuff support server with sendto
[ZeXOS.git] / apps / openchess / src / game.c
blob1719dac524e636ae0ad88b903fd0fc168d008165
1 /*
2 * ZeX/OS
3 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include "client.h"
25 #include "game.h"
27 game_t game_list;
29 /** Reset game board to default positions */
30 int game_board_reset (game_t *game)
32 //Vez,Jezdec,Strelec,Dama,Kral,Strelec,Jezdec,Vez
33 memcpy (game->board[7], "VJSDKSJV", 8); // radek 1 (bily)
34 memcpy (game->board[6], "PPPPPPPP", 8); // radek 2 (bily)
35 memcpy (game->board[5], "........", 8);
36 memcpy (game->board[4], "........", 8);
37 memcpy (game->board[3], "........", 8);
38 memcpy (game->board[2], "........", 8);
39 memcpy (game->board[1], "pppppppp", 8); // radek 7 (cerny)
40 memcpy (game->board[0], "vjsdksjv", 8); // radek 8 (cerny)
42 return 1;
45 game_t *game_find (char *name)
47 game_t *game;
48 for (game = game_list.next; game != &game_list; game = game->next)
49 if (!strcmp (game->name, name))
50 return game;
52 return 0;
55 game_t *game_findbyclient (client_t *client)
57 game_t *game;
58 for (game = game_list.next; game != &game_list; game = game->next)
59 if (game->player.white == client ||
60 game->player.black == client)
61 return game;
63 return 0;
66 /** Create new game's structure */
67 game_t *game_new (client_t *client, char *name, unsigned name_len)
69 game_t *game = game_find (name);
71 if (game)
72 return 0;
74 /* alloc and init context */
75 game = (game_t *) malloc (sizeof (game_t));
77 if (!game)
78 return 0;
80 /* setup game name */
81 game->name = (char *) malloc (sizeof (char) * (name_len + 1));
83 if (!game->name)
84 return 0;
86 memcpy (game->name, name, name_len);
87 game->name[name_len] = '\0';
89 /* set variables to default value */
90 game->play = 0;
91 game->round = 0;
93 /* white player is each time game creator */
94 game->player.white = client;
95 /* black player have to join to game*/
96 game->player.black = 0;
98 /* reset chess board */
99 game_board_reset (game);
101 /* add into list */
102 game->next = &game_list;
103 game->prev = game_list.prev;
104 game->prev->next = game;
105 game->next->prev = game;
107 return game;
110 /** Client would to join to game */
111 int game_join (client_t *client, game_t *game)
113 game->play = 1;
114 game->player.black = client;
116 return 1;
119 /** Delete game's structure */
120 int game_quit (game_t *game)
122 game->next->prev = game->prev;
123 game->prev->next = game->next;
125 free (game->name);
127 free (game);
129 return 1;
132 /** Synchronize game with client's positions */
133 int game_sync (client_t *client, game_t *game)
135 char str[73];
137 unsigned i = 0;
138 unsigned y = 0;
140 while (i < 8) {
141 y = 0;
143 while (y < 8) {
144 str[y+i*9] = game->board[i][y];
145 y ++;
148 str[8+i*9] = '\n';
150 i ++;
153 client_pmmsg (client, str, 72);
155 return 1;
158 int game_pos (client_t *client, game_t *game, unsigned char x_old, unsigned char y_old, unsigned char x, unsigned char y)
160 int cx;
161 int cy;
163 cx = x_old; // pismena souradnice
164 cx -= 'a';
166 cy = y_old; // ciselna souradnice
167 cy -= ('0' + 1);
169 char f = game->board[cy][cx];
171 game->board[cy][cx] = '.';
173 cx = x; // pismena souradnice
174 cx -= 'a';
176 cy = y; // ciselna souradnice
177 cy -= ('0' + 1);
179 game->board[cx][cy] = f;
181 return 1;
184 /** Client Init function */
185 int init_game ()
187 game_list.next = &game_list;
188 game_list.prev = &game_list;
190 return 1;