Professional Documents
Culture Documents
HW 8
HW 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;
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.