Category : C Source Code
Archive   : TKTKTOE.ZIP
Filename : TKTKTOE.C

 
Output of file : TKTKTOE.C contained in archive : TKTKTOE.ZIP
.r:P
.r:B
.r:S
/*********************/
/* Assignment #5 */
/* C Programming */
/* Mon. 5:45 to 9:00 */
/* Brian L. Cassista */
/* Lattice C; IBM PC */
/*********************/

#define PLUS_INFINITY 12345678
#define MINUS_INFINITY -12345678
#define OPEN ' '

main()
{
char board[3][3];
int i,j,move_count=0;
char win_check();

/* initialize board to spaces */
for ( i = 0; i < 3; i++ )
for ( j = 0; j < 3; j++ )
board[i][j] = OPEN;
while ( 1 ) {
/* accept and display human move */
move_count++;
human_move(board);
print_board(board);

/* break out of the loop if the human has won */
if (win_check(board) == 'X') {
printf("\nX has won\n");
break;
}
/* break out of loop if stalemate has occured */
if (move_count == 9) {
printf("\nStalemate\n");
break;
}
/* compute and display computers move */
move_count++;
computer_move(board);
print_board(board);

/* break out of loop if the computer has won */
if (win_check(board) == 'O') {
printf("\nO has won\n");
break;
}
} /* end of while (1) */
} /* end of main() */


computer_move(bd)
char bd[][3];
{
int o_row,x_row,o_col,x_col,save_row,save_col;
int level_1,level_2;
int board_val;

level_1 = MINUS_INFINITY;
/* put 'O' in blank spaces and evaluate board */
for ( o_row = 0; o_row < 3; o_row++ )
for ( o_col = 0; o_col < 3; o_col++ ) {
/* only do something if the position is blank */
if (bd[o_row][o_col] == OPEN) {
/* fill this position with 'O' */
bd[o_row][o_col] = 'O';
if (win_check(bd) == 'O')
return;
/* make all possible 'X' moves & evaluate each one */
level_2 = PLUS_INFINITY;
for ( x_row = 0; x_row < 3; x_row++ )
for ( x_col = 0; x_col < 3; x_col++ )
if (bd[x_row][x_col] == OPEN) {
bd[x_row][x_col] = 'X';
board_val = eval_board(bd);
if (board_val < level_2)
level_2 = board_val;
bd[x_row][x_col] = OPEN;
} /* end of if bd[x_row][x_col] */
/* check for the greatest level_2 value */
if (level_2 > level_1) {
level_1 = level_2;
save_row = o_row;
save_col = o_col;
} /* if level_2 > level_1 */
/* set 'O' back to blank */
bd[o_row][o_col] = OPEN;
} /* end of if bd[o_row][o_col] == OPEN */
} /* end of initial o_col for loop */
bd[save_row][save_col] = 'O';
return;
} /* end of computer_move() function */


human_move(bd)
char bd[][3];
{
int row,col;

do {
printf("Please enter a row (1-3) : ");
scanf("%d",&row);
}while (row < 1 || row > 3);
do {
printf("Please enter a column (1-3) : ");
scanf("%d",&col);
}while (col < 1 || col > 3);
while (bd[row-1][col-1] != OPEN) {
printf("That space is already taken \007\n");
do {
printf("Please enter a row (1-3) : ");
scanf("%d",&row);
}while (row < 1 || row > 3);
do {
printf("Please enter a column (1-3) : ");
scanf("%d",&col);
}while (col < 1 || col > 3);
} /* bd[][] == OPEN */
bd[row-1][col-1] = 'X';
} /* end of human_move() function */


print_board(bd)
char bd[][3];
{
int i,index;

for ( index = 0; index < 11; index++ )
switch (index) {
case 1:
printf(" %c | %c | %c \n",bd[0][0],bd[0][1],bd[0][2]);
break;
case 5:
printf(" %c | %c | %c \n",bd[1][0],bd[1][1],bd[1][2]);
break;
case 9:
printf(" %c | %c | %c \n",bd[2][0],bd[2][1],bd[2][2]);
break;
case 3:
case 7:
for ( i = 0; i < 11; i++ )
printf("-");
printf("\n");
break;
default:
printf(" | | \n");
} /* end of switch */
printf("\n");
} /* end of print_board() function */


eval_board(bd)
char bd[][3];
{
int row,col,temp_val;
int bd_val=0;

for ( row = 0; row < 3; row++ ) {
temp_val = eval_3_positions(bd[row][0],bd[row][1],bd[row][2]);
if (temp_val == 100 || temp_val == -100)
return(temp_val);
bd_val += temp_val;
} /* end of row for() loop */
for ( col = 0; col < 3; col++ ) {
temp_val = eval_3_positions(bd[0][col],bd[1][col],bd[2][col]);
if (temp_val == 100 || temp_val == -100)
return(temp_val);
bd_val += temp_val;
} /* end of col for() loop */
temp_val = eval_3_positions(bd[0][0],bd[1][1],bd[2][2]);
if (temp_val == 100 || temp_val == -100)
return(temp_val);
bd_val += temp_val;
temp_val = eval_3_positions(bd[0][2],bd[1][1],bd[2][0]);
if (temp_val == 100 || temp_val == -100)
return(temp_val);
bd_val += temp_val;
return(bd_val);
} /* end of eval_board() function */


eval_3_positions(c1,c2,c3)
char c1,c2,c3;
{
if ( c1 == ' ' && c2 == ' ' && c3 == ' ' )
return(0);
if ( c1 == 'X' && c2 == 'X' && c3 == 'X' )
return(-100);
if ( c1 == 'O' && c2 == 'O' && c3 == 'O' )
return(100);
if ((c1=='X'|| c2=='X'|| c3=='X') && (c1=='O' || c2=='O' || c3=='O'))
return(0);
if ( (c1=='X' && c2=='X') || (c1=='X' && c3=='X') || (c2=='X' && c3=='X') )
return(-5);
if ( c1 == 'X' || c2 == 'X' || c3 == 'X' )
return(-1);
if ( (c1=='O' && c2=='O') || (c1=='O' && c3=='O') || (c2=='O' && c3=='O') )
return(5);
if ( c1 == 'O' || c2 == 'O' || c3 == 'O' )
return(1);
} /* end of eval_3_positions() function */


char win_check(bd)
char bd[][3];
{
int value;

value = eval_board(bd);
if ( value == -100 )
return('X');
if ( value == 100 )
return('O');
else
return('N'); /* return N if nobody has won */
} /* end of win_check() function */



  3 Responses to “Category : C Source Code
Archive   : TKTKTOE.ZIP
Filename : TKTKTOE.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/