Kill mega-sena.sh
[lcapit-junk-code.git] / games / microballs / mballs.c
blob0b8338cd1239716a046d404a8c2fdd0cc60b8b43
1 /*
2 * mballs: An amazing micro-balls game!
3 *
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.
8 *
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>
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include "mballs.h"
25 #include "board.h"
26 #include "computer.h"
27 #include "misc.h"
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;
36 else
37 current_player = GAME_PLAYER1;
40 /* read_input(): read input from user */
41 static void read_input(struct user_move *move)
43 char line[32];
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);
60 return;
63 print_message("Player '%c' turn: ", current_player);
64 read_input(move);
66 move->from_row -= 'a';
67 move->from_col--;
69 move->to_row -= 'a';
70 move->to_col--;
73 static void usage(void)
75 printf("mballs [ -h ] [ -c ] [ -e<size> ]\n");
78 int main(int argc, char *argv[])
80 enum game_state st;
81 struct user_move move;
82 struct game_board board;
83 int size = BOARD_DEF_SIZE;
85 if (argc > 3) {
86 fprintf(stderr, "ERROR: supports only 3 options\n");
87 usage();
88 exit(1);
91 if (argc >= 2) {
92 if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
93 usage();
94 exit(0);
97 if (!strcmp(argv[1], "-c"))
98 computer_init();
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) {
103 fprintf(stderr,
104 "ERROR: invalid board size: %d\n", size);
105 exit(1);
110 board_init(&board, size);
112 for (;;) {
113 board_draw(&board);
115 get_next_move(&board, &move);
117 st = board_move(&board, &move);
118 if (st == GAME_REST_TURN)
119 continue;
120 else if (st == GAME_FINISHED)
121 break;
123 set_next_player();
126 board_draw(&board);
127 print_message("%c won!!\n\n", current_player);
128 return 0;