You are on page 1of 5

8

: ,
:
. .
: !


.
. ,
. 4
, - .
, :

:
define# HEIGHT- .WIDTH-
- HEIGHT WIDTH -
.
, .
:
#define RED X
#define YELLOW O
#define EMPTY
. .
) (1 :
)]void print(char board[][WIDTH
) (2 win ) +(
,
1

4 . ) TRUE (0-
) FALSE- (0
)int win(char board[][WIDTH], int row, int col
) (3 main:


) ,"
42-( .
.
:
.
.1

.2


.3
. ,

.

.4
.
win
.5
4 .
.
.
: )(.

#include <stdio.h>
#define HEIGHT 6
#define WIDTH 7
#define FALSE 0
#define TRUE (!FALSE)
#define RED 'X'
#define YELLOW 'O'
#define EMPTY ' '
void init_board(char board[][WIDTH])
/* The function receives a board and turns it to an EMPTY board */
{

int i,j;
for (i=0; i < HEIGHT; i++)
for (j=0; j < WIDTH; j++)
board[i][j]= EMPTY;
return;

}
void print(char board[][WIDTH])
/* The function receives the board and prints it in its current status */
{
int i,j;
for (i=0; i < HEIGHT; i++)
{
for (j=0; j < WIDTH; j++)
printf("|%c", board[i][j]);
printf("|\n");
}
for (j=1; j <= WIDTH; j++)
printf("|%d", j);
printf("|\n");
return;
}
int check (char board[][WIDTH], int row, int col, int add_row, int add_col)
/* A utility function for "int win": It counts how long is the streak of a certain game
piece from a certain index in the board,

and continues along a specific direction until it reaches an empty space or a game
piece that belongs to the other player */
{

char mark=board[row][col];
int count=0;
row += add_row;
col += add_col;
while (((row >= 0) && (row < HEIGHT)) && ((col >= 0) && (col < WIDTH)))
if (board[row][col] == mark)
{
count++;
row += add_row;
col += add_col;
}
else break;

return count;

int win(char board[][WIDTH], int row, int col)


/* This function receives the board (2D array) and an index within that board (row and column
number),
and checks if the piece within the index is part of a continuous streak of at least 4 identical pieces*/
{

//Checks left diagonal


if ((1 + (check(board, row,
return TRUE;
//Checks right diagonal
if ((1 + (check(board, row,
return TRUE;
//Checks line
if ((1 + (check(board, row,
return TRUE;
//Checks row
if ((1 + (check(board, row,
return TRUE;

col, -1, -1)) + (check(board, row, col, 1, 1))) >= 4)


col, 1, -1)) + (check(board, row, col, -1, 1))) >= 4)
col, 0, 1)) + (check(board, row, col, 0, -1))) >= 4)
col, -1, 0)) + (check(board, row, col, 1, 0))) >= 4)

return FALSE;

int main()
/************************ Main Program ***********************/
{
char board[HEIGHT][WIDTH];
int i,j, player=TRUE, column, tie=TRUE;
init_board(board);
printf("Player 1 is 'X', Player 2 is 'O'\n\n");
print(board);
printf("\n");
for (i=1; i <= (HEIGHT*WIDTH); i++)
//Beginning of a turn.
{
if (player)
printf("Player 1, 'X', it's your turn!\n\n");
else printf("Player 2, 'O', it's your turn!\n\n");
printf("Please insert the column number to drop your piece into: ");

scanf("%d", &column);
/* Checks if piece can be entered:
*.) Piece must be between 1 and 7 (number of columns)
*.) Column cannot be full all the way up */
while ((column < 1) || (column > WIDTH) || (board[0][column-1] != EMPTY))
{
printf("Column number not valid, please try again: ");

scanf("%d", &column);
}
//Enters piece into the correct place in the column.

for (j=HEIGHT-1; j>=0; j--)


if (board[j][column-1] == EMPTY)
if (player)
{
board[j][column-1]= RED;
break;
}
else
{
board[j][column-1]= YELLOW;
break;
}
//Prints board after insertion.
printf("\n");
print(board);
printf("\n");
if (win(board, j, column-1))
{
printf("Congratulations! ");
if (player)
{
printf("Player 1 is the winner!\n");
tie= FALSE;
break;
}
else
{
printf("Player 2 is the winner!\n");
tie= FALSE;
break;
}
}
//Switching Players.
if (player)
player= FALSE;
else player= TRUE;
//Ending the turn.
}
if (tie)
printf("It's a tie! Thank you for playing!\n");
return 0;
}

You might also like