2 * ttt3p: An amazing version of the famous tic tac toe game that
3 * supports up to 3 players!
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 2 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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * Luiz Fernando N. Capitulino
20 * <lcapitulino@gmail.com>
30 int game_current_player
= GAME_PLAYER_1
; /* player playing right now */
31 enum game_mode game_mode
= GAME_MODE_NORMAL
;
33 static void read_input(int *row
, int *col
)
39 memset(line
, '\0', sizeof(line
));
40 fgets(line
, sizeof(line
), stdin
);
41 sscanf(line
, "%c%d", (char *) row
, col
);
45 /* get_next_move(): read the next move from the user */
46 static enum game_state
get_next_move(int *row
, int *col
)
48 if (is_computer_turn()) {
49 computer_move(row
, col
);
53 print_message("Player '%c' turn: ", game_current_player
);
56 if (*row
< 'a' || *row
> 'd')
57 return GAME_REST_TURN
;
60 /* In the screen, the board column starts at 1,
61 * but in arrays it starts at 0. */
63 if (*col
< 0 || *col
>= COL
)
64 return GAME_REST_TURN
;
69 static void set_next_player(void)
71 if (++game_current_player
== GAME_NEW_TURN
)
72 game_current_player
= GAME_PLAYER_1
;
75 static void usage(void)
77 printf("usage: ttt3p [ nr-computers <-s> ]\n");
80 int main(int argc
, char *argv
[])
86 fprintf(stderr
, "ERROR: wrong number of parameters\n");
89 } else if (argc
>= 2) {
90 if (!strcmp(argv
[1], "-h") || !strcmp(argv
[1], "--help")) {
95 computer_players
= atoi(argv
[1]);
96 if (computer_players
< 1 || computer_players
> 3) {
98 "ERROR: max number of computer players is 3\n");
102 if (argc
== 3 && !strcmp(argv
[2], "-s"))
110 st
= get_next_move(&row
, &col
);
111 if (st
== GAME_REST_TURN
)
114 st
= board_set_pos(row
, col
);
115 if (st
== GAME_REST_TURN
)
120 if (st
== GAME_FINISHED
)
126 print_message("%c won!!\n", game_current_player
);