3 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
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/>.
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
[0], "VJSDKSJV", 8); // radek 1 (bily)
34 memcpy (game
->board
[1], "PPPPPPPP", 8); // radek 2 (bily)
35 memcpy (game
->board
[2], "........", 8);
36 memcpy (game
->board
[3], "........", 8);
37 memcpy (game
->board
[4], "........", 8);
38 memcpy (game
->board
[5], "........", 8);
39 memcpy (game
->board
[6], "pppppppp", 8); // radek 7 (cerny)
40 memcpy (game
->board
[7], "vjsdksjv", 8); // radek 8 (cerny)
45 game_t
*game_find (char *name
)
48 for (game
= game_list
.next
; game
!= &game_list
; game
= game
->next
)
49 if (!strcmp (game
->name
, name
))
55 game_t
*game_findbyclient (client_t
*client
)
58 for (game
= game_list
.next
; game
!= &game_list
; game
= game
->next
)
59 if (game
->white
== client
||
60 game
->black
== client
)
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
);
74 /* alloc and init context */
75 game
= (game_t
*) malloc (sizeof (game_t
));
81 game
->name
= (char *) malloc (sizeof (char) * (name_len
+ 1));
86 memcpy (game
->name
, name
, name_len
);
87 game
->name
[name_len
] = '\0';
89 /* set variables to default value */
94 /* white player is each time game creator */
96 /* black player have to join to game*/
99 /* reset chess board */
100 game_board_reset (game
);
103 game
->next
= &game_list
;
104 game
->prev
= game_list
.prev
;
105 game
->prev
->next
= game
;
106 game
->next
->prev
= game
;
111 /** Client would to join to game */
112 int game_join (client_t
*client
, game_t
*game
)
114 /* you can join to game, when it not running */
118 /* player, what create game have to be connected */
122 /* player, what create game, cant join as opponent too */
123 if (game
->white
== client
)
127 game
->black
= client
;
129 unsigned white_len
= strlen (game
->white
->nick
);
130 unsigned black_len
= strlen (game
->black
->nick
);
132 char *str
= (char *) malloc (white_len
+white_len
+3);
137 memcpy (str
, "s ", 4);
141 memcpy (str
+4, game
->white
->nick
, white_len
);
143 client_pmmsg (game
->black
, str
, 4+white_len
);
148 memcpy (str
+4, game
->black
->nick
, black_len
);
150 client_pmmsg (game
->white
, str
, 4+black_len
);
155 /** Delete game's structure */
156 int game_quit (game_t
*game
)
158 game
->next
->prev
= game
->prev
;
159 game
->prev
->next
= game
->next
;
168 /** Synchronize game with client's positions */
169 int game_sync (client_t
*client
, game_t
*game
)
180 str[y+i*9] = game->board[i][y];
194 for (x
= 7; x
>= 0; x
--) {
195 for (y
= 0; y
< 8; y
++) {
196 str
[i
] = game
->board
[x
][y
];
204 client_pmmsg (client
, str
, 72);
209 int game_pos (client_t
*client
, game_t
*game
, unsigned char x_old
, unsigned char y_old
, unsigned char x
, unsigned char y
)
211 char f
= game
->board
[y_old
][x_old
];
213 game
->board
[y_old
][x_old
] = '.';
215 game
->board
[y
][x
] = f
;
220 int game_getlist (client_t
*client
)
226 /* first calculate length of all names */
227 for (game
= game_list
.next
; game
!= &game_list
; game
= game
->next
)
228 len
+= (strlen (game
->name
) + 3);
233 char *list
= (char *) malloc (sizeof (char) * (len
+ 1));
242 /* we can save all game names to list now */
243 for (game
= game_list
.next
; game
!= &game_list
; game
= game
->next
) {
244 l
= strlen (game
->name
);
251 memcpy (list
+1+len
, " ", 1);
252 memcpy (list
+2+len
, game
->name
, l
);
253 memcpy (list
+2+len
+l
, "\n", 1);
260 client_pmmsg (client
, list
, len
);
267 /** Client Init function */
270 game_list
.next
= &game_list
;
271 game_list
.prev
= &game_list
;