Software & Finance - Monthly Magazine Online

Volume 4 - Issue 7

July 2014

Tic Tac Toe Game using C++ Graphics


Here is the sample outpur and source code for Tic Tac Toe Games Developed in C programming Language with graphics.

 

 

#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define NUM_SQUARES  9
#define ESC 0x1b
#define F1_KEY 0x3b
#define F2_KEY 0x3c
#define F3_KEY 0x3d
#define F4_KEY 0x33
#define F5_KEY 0x3f
#define F6_KEY 0x40
#define F7_KEY 0x41
#define F8_KEY 0x42
#define F9_KEY 0x43

typedef enum _OWNER
{
	UNASSIGNED, COMPUTER, PLAYER
} OWNER, WINNER;

static int pattern[8][3] =
{
	{ 0, 1, 2 },
	{ 3, 4, 5 },
	{ 6, 7, 8 },
	{ 0, 3, 6 },
	{ 1, 4, 7 },
	{ 2, 5, 8 },
	{ 0, 4, 8 },
	{ 2, 4, 6 }
};

const int left = 105;
const int right = 375;
const int top = 45;
const int bottom = 315;
const int size = 90;

typedef struct Square
{
	OWNER pos[NUM_SQUARES];
} TicTacToe;

static TicTacToe game;

void init_game()
{
	int i;

	for(i = 0; i < NUM_SQUARES; i++)
	{
		game.pos[i] = UNASSIGNED;
	}
	setfillstyle(SOLID_FILL,BLUE);
	bar(0, 0, 639, 479);
	setcolor(WHITE);
	rectangle(0, 0, 639, 479);
}

int draw_gameboard()
{
	int index = 0;
	int i, j;
	int xpos, ypos;
	char buf[32];
	int num = 1;

	xpos = left;
	for(i = 0; i < 3; i++)
	{
		ypos = top;
		for(j = 0; j < 3; j++)
		{
			rectangle(xpos, ypos, xpos + size, ypos + size);
			rectangle(xpos + size - 30, ypos + size - 15, xpos + size, ypos + size);
			sprintf(buf, "F%d", num++);
			outtextxy(xpos + size - 22, ypos + size - 10, buf);
			if(game.pos[index] == PLAYER)
			{
				setfillstyle(SOLID_FILL, GREEN);
				floodfill(xpos + 1, ypos + 1, WHITE);
			}
			else if(game.pos[index] == COMPUTER)
			{
				setfillstyle(SOLID_FILL, RED);
				floodfill(xpos + 1, ypos + 1, WHITE);
			}
			else
			{
				setfillstyle(SOLID_FILL, BLUE);
				floodfill(xpos + 1, ypos + 1, WHITE);
			}
			index++;
			ypos += size;
		}
		xpos += size;
	}
	setfillstyle(SOLID_FILL,BLUE);
	return is_game_over();
}

void draw_patterline(int patternidx)
{
	int xpos, ypos;
	int i, j, k, p, index = 0;

	for(k = 0; k < 3; k++)
	{
		index = 0;
		p = pattern[patternidx][k];
		xpos = left;
		for(i = 0; i < 3; i++)
		{
			ypos = top;
			for(j = 0; j < 3; j++)
			{
				if(index == p)
				{
					line(xpos,  ypos, xpos + size, ypos + size);
					line(xpos + size, ypos, xpos, ypos + size);
				}
				ypos += size;
				index++;
			}
			xpos += size;
		}
	}
}

int valid_selection(int index)
{
	if(index < 0 || index > 8)
	{
		return -1;
	}
	if(game.pos[index] != UNASSIGNED)
		return -2;
	game.pos[index] = PLAYER;
	return 0;
}

void computer_move()
{
	int i = 0;
	if(game.pos[4] == UNASSIGNED)
	{
			game.pos[4] = COMPUTER;
			return;
	}
	// Add more intelligent
	for(i = 0; i < NUM_SQUARES; i++)
	{
		if(game.pos[i] == UNASSIGNED)
		{
			game.pos[i] = COMPUTER;
			break;
		}
	}
}

int handle_key(int key)
{
	int errcode = 0;
	if( (key & 0x00FF) > 0)
		key = key & 0x00FF;
	else
		key = (key & 0xFF00) >> 8;
	errcode = valid_selection(key - F1_KEY);
	switch(errcode)
	{
		case -1:
			outtextxy(left, bottom + 20, "Invalid Key Pressed - Select F1 - F9");
			break;
		case -2:
			outtextxy(left, bottom + 20, "This position is already selected, try other option");
			break;
		default:
			bar(1, bottom + 20, 638, bottom + 60);
			break;
	}
	return errcode;
}

int is_game_over()
{
	int i, p1, p2, p3;
	int patternidx;
	WINNER winner = UNASSIGNED;
	// check the winner
	for(i = 0; i < 8; i++)
	{
		p1 = pattern[i][0];
		p2 = pattern[i][1];
		p3 = pattern[i][2];
		if(game.pos[p1] == COMPUTER &&
			game.pos[p2] == COMPUTER &&
			game.pos[p3] == COMPUTER)
		{
			winner = COMPUTER;
			break;
		}
		if(game.pos[p1] == PLAYER &&
			game.pos[p2] == PLAYER &&
			game.pos[p3] == PLAYER)
		{
			winner = PLAYER;
			break;
		}
	}
	patternidx = i;

	if(winner == UNASSIGNED)
	{
		for(i = 0; i < NUM_SQUARES; i++)
		{
			if(game.pos[i] == UNASSIGNED)
				return 0;
		}
	}

	switch(winner)
	{
		case UNASSIGNED:
			outtextxy(left, bottom + 20, "Game is Tie.");
			break;
		case PLAYER:
			draw_patterline(patternidx);
			outtextxy(left, bottom + 20, "Congrats! You have own...!");
			break;
		case COMPUTER:
			draw_patterline(patternidx);
			outtextxy(left, bottom + 20, "Computer Win! Better Luck Next Time...!");
			break;
	}
	outtextxy(left, bottom + 40, "Press any key to contine!");
	getch();
	return 1;
}

void main()
{
	int whoplays = 0; // 0 for player plays first
	char ch;
	int key = 0;
	int grd, grm;

	detectgraph(&grd,&grm);
	initgraph(&grd, &grm, "");

	rectangle(left, top, right, bottom);
	while(1)
	{
		init_game();
		while(1)
		{
			if(draw_gameboard() == 1)
				break;
			if(whoplays == 0)
			{
				key = bioskey(0);
				while(handle_key(key) < 0)
				{
					key = bioskey(0);
					if(draw_gameboard() == 1)
						break;
				}
				if(draw_gameboard() == 1)
					break;
				computer_move();
			}
			else
			{
				computer_move();
				key = bioskey(0);
				while(handle_key(key) < 0)
				{
					key = bioskey(0);
					if(draw_gameboard() == 1)
						break;
				}
				if(draw_gameboard() == 1)
					break;
			}
		}
		init_game();
		draw_gameboard();
		outtextxy(left, bottom + 20, "Play again (Y/N)?");
		ch = getch();
		if(ch == 'y' || ch == 'Y')
		{
			if(whoplays == 0)
				whoplays = 1;
			else
				whoplays = 0;
			continue;
		}
		else
			break;
	}
	closegraph();
}