/*-------------------------------------------------------------------------
Include files:
--------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
/*=====
Constants and definitions:
======*/
#define N 10
#define BLANK 0
typedef enum {player_x='x', player_o='o'} Players;
typedef enum {false, true} bool; // You may need to delete this in compilers
// other than Dev-C++
/*=====
Forward declarations:
======*/
int number_of_flips(Players player, int row, int col);
void switch_players(void);
void i_beat_you(int row, int col);
int game_type(void);
void ai(void);
void crash(void);
void print_board(void);
void score(void);
void statistics(void);
void hi(int method,int player_id);
/*=====
Global variables:
======*/
char board[N][N] = {BLANK};
int chose_y, chose_x, y, x, player = 'x', opposite = 'o';
int player_id, x_counter, o_counter, method, changer = 0;
/*-------------------------------------------------------------------------
The main program. (plays Riversi with a computer friend or a human)
-------------------------------------------------------------------------*/
int main()
{
int check, end=2;
board[N/2][N/2] = player_x;
board[N/2][N/2+1] = player_o;
board[N/2+1][N/2] = player_o;
board[N/2+1][N/2+1] = player_x;
method = game_type();
if((method%2)!=0)
player_id=1;
if(method == 3)
player_id=2;
while(1)
{
player_id=(player_id%2)+1;
score();
system("cls");
if (method == 4)
{
ai();
continue;
}
while(1)
{
player_id=(player_id%2)+1;
score();
system("cls");
switch (method)
{
case 1:
{
hi(method,player_id%2+1);
hi(method,player_id);
break;
}
case 2:
{
ai();
system("cls");
hi(method,player_id+1);
break;
}
case 3:
{
hi(method,player_id);
ai();
break;
}
case 4:{
ai();
ai();
break;
}
}
}
}
system("pause");
return 0;
}
/*=====
Function implementation:
======*/
/*-------------------------------------------------------------------------
Calculates the number of flips that will result by locating a coin in
board[row][col] by player.
-------------------------------------------------------------------------*/
int number_of_flips(Players player, int row, int col)
{
static int x_dir[8] = {1, 1, 0, -1, -1, -1, 0, 1};
static int y_dir[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int directions[8] = {1, 1, 1, 1, 1, 1, 1, 1}, dirs_left = 8;
int radius;
int tot_flips = 0;
int d;
if((col <= 0) || (row <= 0) || (col > N) || (row > N) || (board[row][col] != BLANK))
return 0;
for(radius=1; dirs_left>0; radius++)
{
for(d=0; d<8; d++)
{
int x = col+radius*x_dir[d], y = row+radius*y_dir[d];
if(!directions[d])
continue;
if((x <= 0) || (x > N) || (y <= 0) || (y > N) || (board[y][x] == 0) ||
((board[y][x] == player) && (radius == 1)))
{
directions[d] = 0;
dirs_left--;
continue;
}
if((board[y][x] == player) && (radius > 1))
{
tot_flips += (radius-1);
directions[d] = 0;
dirs_left--;
continue;
}
}
}
return (tot_flips);
}
/*-------------------------------------------------------------------------
Prompts the user for the desired game type, validates it
-------------------------------------------------------------------------*/
int game_type()
{
double selection;
printf("Reversi:\n====\n");
printf("Every player at his turn must place a piece with\n");
printf("his side up on the board, in such a position that\n");
printf("there exists at least one straight (horizontal,\n");
printf("vertical, or diagonal) line between the NEW piece\n");
printf("and another of his pieces, with one or more continuous\n");
printf("opponent-pieces between them.\n\n");
printf("Game Types:\n1) x-Human, o-Human\n2) x-Computer, o-Human\n");
printf("3) x-Human, o-Computer\n4) x-Computer, o-Computer\n\n");
while(1)
{
printf("Enter game type (1,2,3,4): ");
if((scanf("%lf", &selection) != 1) ||
(selection != (int)selection) || (selection < 1 || selection > 4))
{
printf("Wrong input\n");
scanf("%c");
}
else
break;
}
print_board();
return (int)selection;
}
/*-------------------------------------------------------------------------
Flips the coins on the board
-------------------------------------------------------------------------*/
void i_beat_you(int row, int col)
{
int y_dir[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int x_dir[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int x, y, radius, i, a, b;
for(i=0; i<8; ++i)
{
for(radius=1; radius<=8; ++radius)
{
y = row + (y_dir[i])*radius;
x = col + (x_dir[i])*radius;
if(((x>0)&&(x<=N)) && ((y>0)&&(y<=N)))
{
if(board[y][x] == opposite)
{
continue;
}
else if(board[y][x] == player)
{
break;
}
else if(board[y][x] == BLANK)
{
x_dir[i] = -2;
y_dir[i] = -2;
break;
}
}
else
break;
}
}
for(i=0; i<8; ++i)
{
for(radius=1; radius<=N; ++radius)
{
y = row + (y_dir[i])*radius;
x = col + (x_dir[i])*radius;
a = row + (y_dir[i])*(radius+1);
b = col + (x_dir[i])*(radius+1);
if((x_dir[i] == -2)||(y_dir[i] == -2)||((x<=0)||(x>N)) || ((y<=0)||(y>N)))
break;
if((board[y][x] == opposite)&&(board[a][b] != BLANK))
{
board[y][x] = player;
}
}
}
switch_players();
}
/*-------------------------------------------------------------------------
Crashes the program when input is invalid
-------------------------------------------------------------------------*/
void crash()
{
printf("Input failure - ending.\n");
system("pause");
exit(1);
}
/*-------------------------------------------------------------------------
Changes back & forth between the players, according tho who'se turn it is
-------------------------------------------------------------------------*/
void switch_players(void)
{
if (player == player_x)
{
player = player_o;
opposite = player_x;
}
else
{
player = player_x;
opposite = player_o;
}
}
/*-----------------------------------------------------------------------------
The function is a computer player which plays a turn
------------------------------------------------------------------------------*/
void ai(void)
{
int col, row = 1, mid = 0, bkup_row, bkup_col, radius=0;
chose_y = 0;
chose_x = 0;
for (col = 1; row <= N; ++col)
{
radius = number_of_flips(player, row, col);
if (mid <= radius)
{
mid = radius;
bkup_row = row;
bkup_col = col;
}
if (col==N)
{
col = 0;
row++;
}
}
chose_y = bkup_row;
chose_x = bkup_col;
board[bkup_row][bkup_col] = player;
print_board();
printf("computer play: row %d column %d \n", bkup_row,bkup_col);
i_beat_you(bkup_row, bkup_col);
}
/*-------------------------------------------------------------------------
Prints the current board status
-------------------------------------------------------------------------*/
void print_board(void)
{
int i,j,k;
putchar(' ');
for(i=0;i<N;i++)
printf(" %d",i+1);
printf(" \n");
for(i=0;i<N;i++)
{
printf(" +");
for(j=1; j<((N*4)); j++)
putchar('-');
printf("+\n%d |",i+1);
for (k=0;k<N;k++)
{
printf(" %c |",board[i+1][k+1]);
}
putchar('\n');
}
printf(" +");
for(j=1;j<((N*4));j++)
putchar('-');
printf("+\n");
}
/*-------------------------------------------------------------------------
Keeps track of the exes and o's on the board
-------------------------------------------------------------------------*/
void score(void)
{
int i, j;
x_counter = 0;
o_counter = 0;
for(i=1; i<=N; ++i)
{
for(j=1; j<=N; ++j)
{
if(board[i][j] == player_x)
x_counter++;
if(board[i][j] == player_o)
o_counter++;
}
}
if ((x_counter == 0)||(o_counter == 0)||(x_counter + o_counter == N*N))
statistics();
}
/*-------------------------------------------------------------------------
Displays the end game statistics
-------------------------------------------------------------------------*/
void statistics(void)
{
int who_won;
if(x_counter > o_counter)
who_won = 1;
else
who_won = 2;
printf("\n\nPlayer 1: %d, Player 2: %d\n", x_counter, o_counter);
printf("Player %d won.\n", who_won);
system("pause");
exit(0);
}
/*-------------------------------------------------------------------------
Plays the human players turn(flips the coins)
-------------------------------------------------------------------------*/
void hi(int method,int player_id)
{
int check;
print_board();
while(1)
{
if(method == 1)
{
player_id=(player_id%2)+1-changer;
if(changer == 1)
changer = 0;
if (player_id == 0)
{
changer == 1;
player_id = 2;
}
}
if(method == 2)
player_id = 2;
if(method == 3)
player_id = 1;
do
{
printf("\n\nPlayer %d (%c) enter row (1-8): ",player_id, player);
if(scanf("%d",&chose_y) != 1)
crash();
} while ( (chose_y < 0 || chose_y > 8));
do
{
printf("Player %d (%c) enter column (1-8): ",player_id,player);
if(scanf("%d",&chose_x) != 1)
crash();
} while ( (chose_x < 0 || chose_x > 8));
check = number_of_flips(player, chose_y, chose_x);
if (check == 0)
{
printf("Your move does not flip any coins, enter another move.\n");
changer = 1;
continue;
}
else
{
y=chose_y;
x=chose_x;
board[y][x] = player;
i_beat_you(y, x);
system("cls");
print_board();
if (method == 2 || method == 3)
break;
else
continue;
}
}
}
/******
End of file ex4.c
***/