Tic Tac Toe - With Drawing

Now we'll add the capability to draw the tic tac toe board and the game status. Two functions are added: show_board() and show_status(). Note that these two functions are called from the redraw function, after the GIL windows have been redrawn.

The show_board() function works as follows: First, it gets the dimensions of the board's dragarea. Next it draws two verticle lines through points 1/3 and 2/3 through the dragarea. Finally it draws two ghorizontal lines 1/3 and 2/3 through the dragarea.

The show_status() function "erases" any old status messages by filling the status field with the status window's color. Then a status string is drawn into the status field.


[...]

#define GAME_DRAG "Game"
#define STATUS "Status"

#define X_WIN "X won!"
#define O_WIN "O won!"
#define TIE   "Tie game."
#define X_MOVE "X's turn."
#define O_MOVE "O's turn."

typedef enum {X, O, PNONE} PlayerType;

#define NO_MOVE 9
#define MAX_MOVES 9

PlayerType board[3][3];
PlayerType winner = PNONE;
bool       x_turn = TRUE;
int        move_count = 0, last_move = NO_MOVE;

[...]

/*  ---  Prototypes - display functions    ---  */
void show_board();
void show_status();
/*  */

[...]

bool redraw_func()
{
	GSredrawnamedwindow("root");
	GSredrawnamedwindow(GAME_WIND);
	GSredrawnamedwindow(STATUS_WIND);
	show_board();
	show_status();
	
	return TRUE;
}

[...]

void show_board()
{
	int x, y, wd, ht;
	
	GSgetdragareaspecs(GAME_WIND, GAME_DRAG, &x, &y, &wd, &ht, NULL, NULL);
	GSsetcurrentnamedwindow(GAME_WIND);
	
	GSsetlinesize(5);
	GSsetcolor(GSconvertcolor("black"));
	GSwdrawline(x+(wd/3), y, x+(wd/3), y+ht);
	GSwdrawline(x+(2*wd/3), y, x+(2*wd/3), y+ht);
	GSwdrawline(x, y+(ht/3), x+wd, y+(ht/3));
	GSwdrawline(x, y+(2*ht/3), x+wd, y+(2*ht/3));
}

void show_status()
{
	int x, y, wd, ht;
	GSColor col;
	BigString msg;
	
	GSgetfieldrect(STATUS, &x, &y, &wd, &ht, &col);
	GSsetcurrentnamedwindow(STATUS_WIND);
	
	GSsetcolor(col);
	GSwfillrect(x, y, wd, ht);
	
	if (winner != PNONE)
		sprintf(msg, "%s", (winner == X)? X_WIN : O_WIN);
	else if (move_count == MAX_MOVES)
		sprintf(msg, "%s", TIE);
	else
		sprintf(msg, "%s", x_turn? X_MOVE : O_MOVE);
	
	GSsetfont(LARGE);
	GSsetcolor(GSconvertcolor("black"));
	GSwwriteclippedmsg(x, y, wd, ht, x, y+((ht-SMALL)/2), msg);
}

[...]

back forward