Introduce pet-projects dir
[lcapit-junk-code.git] / pet-projects / games / ttt3p / ttt3p.c
blob394f88b566fa82864a33d2cc87ed5b9a72d33391
1 /*
2 * ttt3p: An amazing version of the famous tic tac toe game that
3 * supports up to 3 players!
4 *
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.
9 *
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>
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include "ttt3p.h"
26 #include "board.h"
27 #include "computer.h"
28 #include "misc.h"
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)
35 char line[16];
37 *row = *col = 0;
39 memset(line, '\0', sizeof(line));
40 fgets(line, sizeof(line), stdin);
41 sscanf(line, "%c%d", (char *) row, col);
42 fflush(stdout);
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);
50 return GAME_OK;
53 print_message("Player '%c' turn: ", game_current_player);
54 read_input(row, col);
56 if (*row < 'a' || *row > 'd')
57 return GAME_REST_TURN;
58 *row -= 'a';
60 /* In the screen, the board column starts at 1,
61 * but in arrays it starts at 0. */
62 --*col;
63 if (*col < 0 || *col >= COL)
64 return GAME_REST_TURN;
66 return GAME_OK;
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[])
82 int row, col;
83 enum game_state st;
85 if (argc > 3) {
86 fprintf(stderr, "ERROR: wrong number of parameters\n");
87 usage();
88 exit(1);
89 } else if (argc >= 2) {
90 if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
91 usage();
92 exit(0);
95 computer_players = atoi(argv[1]);
96 if (computer_players < 1 || computer_players > 3) {
97 fprintf(stderr,
98 "ERROR: max number of computer players is 3\n");
99 usage();
100 exit(1);
102 if (argc == 3 && !strcmp(argv[2], "-s"))
103 computer_watch = 1;
104 computer_init();
107 for (;;) {
108 board_draw();
110 st = get_next_move(&row, &col);
111 if (st == GAME_REST_TURN)
112 continue;
114 st = board_set_pos(row, col);
115 if (st == GAME_REST_TURN)
116 continue;
118 computer_wait();
120 if (st == GAME_FINISHED)
121 break;
122 set_next_player();
125 board_draw();
126 print_message("%c won!!\n", game_current_player);
128 return 0;