2 * mballs: An amazing micro-balls game!
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Luiz Fernando N. Capitulino
19 * <lcapitulino@gmail.com>
29 enum game_piece current_player
= GAME_PLAYER1
;
31 /* set_next_player(): select who plays next */
32 static void set_next_player(void)
34 if (current_player
== GAME_PLAYER1
)
35 current_player
= GAME_PLAYER2
;
37 current_player
= GAME_PLAYER1
;
40 /* read_input(): read input from user */
41 static void read_input(struct user_move
*move
)
45 move
->from_row
= move
->from_col
= 0;
46 move
->to_row
= move
->to_col
= 0;
48 memset(line
, '\0', sizeof(line
));
49 fgets(line
, sizeof(line
), stdin
);
50 sscanf(line
, "%c%d-%c%d\n", (char *) &move
->from_row
, &move
->from_col
,
51 (char *) &move
->to_row
, &move
->to_col
);
54 /* get_next_move(): read the next move from the user */
55 static void get_next_move(const struct game_board
*board
,
56 struct user_move
*move
)
58 if (is_computer_turn()) {
59 computer_move(board
, move
);
63 print_message("Player '%c' turn: ", current_player
);
66 move
->from_row
-= 'a';
73 static void usage(void)
75 printf("mballs [ -h ] [ -c ] [ -e<size> ]\n");
78 int main(int argc
, char *argv
[])
81 struct user_move move
;
82 struct game_board board
;
83 int size
= BOARD_DEF_SIZE
;
86 fprintf(stderr
, "ERROR: supports only 3 options\n");
92 if (!strcmp(argv
[1], "-h") || !strcmp(argv
[1], "--help")) {
97 if (!strcmp(argv
[1], "-c"))
100 if (argc
== 3 && !memcmp(argv
[2], "-e", 2)) {
101 size
= (int) strtol(argv
[2] + 2, NULL
, 10);
102 if (size
<= BOARD_DEF_SIZE
|| size
> BOARD_MAX_SIZE
) {
104 "ERROR: invalid board size: %d\n", size
);
110 board_init(&board
, size
);
115 get_next_move(&board
, &move
);
117 st
= board_move(&board
, &move
);
118 if (st
== GAME_REST_TURN
)
120 else if (st
== GAME_FINISHED
)
127 print_message("%c won!!\n\n", current_player
);