home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
cenvi23.zip
/
TTT.CMM
< prev
next >
Wrap
Text File
|
1994-09-26
|
5KB
|
177 lines
/* ---------- ttt.cmm ----------- */
/* */
/* A simple game of tic-tac-toe */
/* written in Cmm */
/* contributed by Al Stevens */
/* */
#define TRUE 1
#define FALSE 0
#define BELL 7
/* ---- board markers ---- */
#define PLAYER 'X'
#define COMPUTER 'O'
#define FREE ' '
/* --- game position on screen --- */
#define LEFT 10
#define TOP 5
/* --- game board --- */
board = " ";
/* --- winning combinations --- */
wins = {
/* --- winning rows --- */
{ 1,2,3 },
{ 4,5,6 },
{ 7,8,9 },
/* --- winning columns --- */
{ 1,4,7 },
{ 2,5,8 },
{ 3,6,9 },
/* --- winning diagonals --- */
{ 1,5,9 },
{ 3,5,7 }
};
main()
{
ch = 'y';
while (ch == 'y') {
memset(board, FREE, 9);
displayboard();
/* --- get player's first move --- */
if ((mv = getmove()) == 0)
break;
/* --- set computer's first move --- */
if (mv != 5)
setpiece(5, COMPUTER); /* center if available */
else
setpiece(1, COMPUTER); /* upper left otherwise */
moves = 2;
while (moves < 9) {
getmove(); /* player's next move */
moves++;
if (won()) {
message(1, "You win");
break;
}
if (moves == 9)
message(1, "Tie");
else {
/* --- find computer's next move --- */
if ((mv = canwin(COMPUTER)) != 0)
/* --- win if possible --- */
setpiece(=mv, COMPUTER);
else if ((mv = canwin(PLAYER)) != 0)
/* --- block player's win potential --- */
setpiece(=mv, COMPUTER);
else
nextmove();
if (won()) {
message(1, "I win");
break;
}
moves++;
}
}
message(2, "Play again? (y/n) ");
ch = getch();
}
}
/* --- find next available open space for a dumb move --- */
nextmove()
{
lmv = -1;
for (i = 0; i < 9; i++)
if (board[i] == FREE) {
lmv = i+1;
setpiece(=lmv, COMPUTER);
if (canwin(COMPUTER))
return;
setpiece(=lmv, FREE);
}
if (lmv != -1)
setpiece(=lmv, COMPUTER);
}
/* --- get the player's move and post it --- */
getmove()
{
mv = 0;
while (mv == 0) {
message(0, "Move (1-9)? ");
mv = getch();
mv -= '0';
if (mv < 1 || mv > 9 || board[mv-1] != FREE) {
putchar(BELL);
mv = 0;
}
}
setpiece(=mv, PLAYER);
return mv;
}
/* ------ test to see if the game has been won ------- */
won()
{
for (i = 0; i < 8; i++) {
pl = wins[i][0]-1;
if (board[pl] == FREE)
continue;
for (k = 1; k < 3; k++)
if (board[pl] != board[wins[i][k]-1])
break;
if (k == 3)
return TRUE;
}
return FALSE;
}
/* --- test to see if a player (n) can win this time;
return 0 or winning board position --- */
canwin(n)
{
for (i = 0; i < 8; i++)
if ((w = trywin(n, i)) != 0)
return w;
return 0;
}
/* ---- test a row, column, or diagonal for a win;
return 0 or winning board position --- */
trywin(n, wn)
{
nct = 0;
zct = 0;
for (i = 0; i < 3; i++) {
pl = wins[wn][i]-1;
if (board[pl] == FREE)
zct = i+1;
else if (board[pl] == n)
nct++;
}
if (nct == 2 && zct)
return wins[wn][zct-1];
return 0;
}
/* ------ display the tic-tac-toe board ------ */
displayboard()
{
ln1 = " \xb3 \xb3";
ln2 = "\xc4\xc4\xc4\xc5\xc4\xc4\xc4\xc5\xc4\xc4\xc4";
ScreenClear();
for (y = 0; y < 5; y++) {
ScreenCursor(LEFT,TOP+y);
printf((y&1) ? ln2 : ln1);
}
}
/* ---- set a players mark (O or X) on the board ---- */
setpiece(pos, mark)
{
board[--pos] = mark;
col = pos / 3;
row = pos % 3;
ScreenCursor(LEFT+row*4+1, TOP+col*2);
putchar(mark);
}
/* ---- message to opponent ---- */
message(y, msg)
{
ScreenCursor(LEFT, TOP+8+y);
printf(msg);
}