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/>.
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)
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
->player
.white
== client
||
60 game
->player
.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 */
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
);
102 game
->next
= &game_list
;
103 game
->prev
= game_list
.prev
;
104 game
->prev
->next
= game
;
105 game
->next
->prev
= game
;
110 /** Client would to join to game */
111 int game_join (client_t
*client
, game_t
*game
)
114 game
->player
.black
= client
;
119 /** Delete game's structure */
120 int game_quit (game_t
*game
)
122 game
->next
->prev
= game
->prev
;
123 game
->prev
->next
= game
->next
;
132 /** Synchronize game with client's positions */
133 int game_sync (client_t
*client
, game_t
*game
)
144 str
[y
+i
*9] = game
->board
[i
][y
];
153 client_pmmsg (client
, str
, 72);
158 int game_pos (client_t
*client
, game_t
*game
, unsigned char x_old
, unsigned char y_old
, unsigned char x
, unsigned char y
)
163 cx
= x_old
; // pismena souradnice
166 cy
= y_old
; // ciselna souradnice
169 char f
= game
->board
[cy
][cx
];
171 game
->board
[cy
][cx
] = '.';
173 cx
= x
; // pismena souradnice
176 cy
= y
; // ciselna souradnice
179 game
->board
[cx
][cy
] = f
;
184 /** Client Init function */
187 game_list
.next
= &game_list
;
188 game_list
.prev
= &game_list
;