File: textmain.c

package info (click to toggle)
mancala 1.0.0-5
  • links: PTS
  • area: contrib
  • in suites: woody
  • size: 152 kB
  • ctags: 153
  • sloc: ansic: 928; makefile: 102; sh: 5
file content (119 lines) | stat: -rw-r--r-- 3,471 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* $Id: textmain.c,v 1.1.1.1 1995/07/25 11:55:21 sverrehu Exp $ */
/**************************************************************************
 *
 *  FILE            textmain.c
 *  MODULE OF       The board game Mancala
 *
 *  DESCRIPTION     Main function / frontend for using a textbased display.
 *
 *  WRITTEN BY      Sverre H. Huseby
 *
 **************************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#include "minimax.h"
#include "mancala.h"

/**************************************************************************
 *                                                                        *
 *                   P R I V A T E    F U N C T I O N S                   *
 *                                                                        *
 **************************************************************************/

/*-------------------------------------------------------------------------
 *
 *  NAME          showBoard
 *
 *  FUNCTION      Display the current board on screen.
 *
 *  DESCRIPTION   Very simple thing.
 */
static void showBoard(void)
{
    int q;

    printf("    ");
    for (q = 0; q < MAX_HOLES; q++)
	printf((q < MAX_HOLES - 1) ? "%c:%2d | " : "%c:%2d\n",
	       'a' + MAX_HOLES - q - 1, getHole(1, q));
    printf(" %2d ", getMancala(1));
    for (q = 0; q < MAX_HOLES; q++)
	printf((q < MAX_HOLES - 1) ? "-----+-" : "----");
    printf(" %2d\n", getMancala(0));
    printf("    ");
    for (q = 0; q < MAX_HOLES; q++)
	printf((q < MAX_HOLES - 1) ? "%c:%2d | " : "%c:%2d\n",
	       'a' + q, getHole(0, MAX_HOLES - q - 1));
}



/**************************************************************************
 *                                                                        *
 *                    P U B L I C    F U N C T I O N S                    *
 *                                                                        *
 **************************************************************************/

int main(int argc, char *argv[])
{
    char   s[80];
    char   playerName[2][20] = { "player at bottom", "player at top" };
    int    maxPly[2] = { MAX_DEPTH, MAX_DEPTH }, status;
    PMove  move;
    Move   move2;
    Player player, winner;
    
    initGame();
    if (argc == 2)
	maxPly[0] = maxPly[1] = atoi(argv[1]);
    else if (argc == 3) {
	maxPly[1] = atoi(argv[1]);
	maxPly[0] = atoi(argv[2]);
    } else {
	printf("usage: mancala max-ply\n"
	       "       mancala max-ply-top max-ply-bottom\n"
	       "\n"
	       "max-ply of 0 indicates human user.\n"
	       "Player at top starts.\n"
	       "\n");
	return 1;
    }

    player = 1;
    for (;;) {
	showBoard();
	printf("\n");
	if ((status = checkAndFixWin(&winner)) != 0) {
	    printf("=== Game over; ");
	    if (status == 1)
		printf("%s wins", playerName[winner]);
	    else
		printf("we have a draw");
	    printf(" ===\n\n");
	    showBoard();
	    printf("\n");
	    return 0;
	}
	printf("Move for %s: ", playerName[player]);
	if (!maxPly[player]) {
	    move = &move2;
	    for (;;) {
		fgets(s, 80, stdin);
		move->hole = 'A' + (MAX_HOLES - 1) - toupper(s[0]);
		if (legalMove(player, move))
		    break;
		printf("not a legal move. try again: ");
	    }
	    printf("\n");
	} else {
	    move = getBestMove(player, maxPly[player]);
	    printf("%c\n\n", 'A' + (MAX_HOLES - 1) - move->hole);
	}
	player = doMove(player, move);
    }
    return 0;
}