Search…

Tictactoe Game in C++ for a Technical Interview

La Kiến VinhLa Kiến Vinh
30/05/20234 min read
You can use Tictactoe to start your technical interview with a basic programming ability test.

Why I write this article?

The console application game Tictactoe, written in C++, is not new but it is a good starting point to test programming abilities. In many interviews to recruit programmers for STDIO, I do not use too many quizzes, do not use traps, I directly use only the most basic knowledge. Basic is the foundation.

Why foundation matter?

Support libraries, npm packages, nuget packages or useful APIs are all good for a software project, I love technology and so do many people, if starting a new project, the first thing I need is to look around how technologies, libraries or public APIs (paid or free) help my project. But that will be the second question to test the ability to learn about trends and help increase the stability of the project.

The first question is to check the candidate's foundation, using the support libraries is a must for rapid development, to have a highly customizable projects, libraries with default configuration of the library could not respond.

Why choose Tictactoe in C++, Python?

To test a candidate's understanding of a basic level of a programming language and basic programming techniques, what is required?

  1. Very basic syntax: log, variable, list, function, conditional structure, loop, do not use much library, even if coding convention and ;)
  2. Logical thinking.
  3. Not take much time, time in an interview is usually limited.

To better describe why to use Tictactoe with C++ for one of the programming interview starter questions, you can review the full code of the game Tictactoe below in C++.

#include <iostream>
using namespace std;

#define SIZE 3
char board[SIZE][SIZE] = { {' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '} };
int player = 1;
bool isOver = false;
bool isDraw = false;

void showBoard()
{
	cout << "-------------" << endl;
	for (unsigned int i = 0; i < SIZE; i++)
	{
		for (unsigned int j = 0; j < SIZE; j++)
		{
			cout << "| " << board[i][j] << " ";
		}
		cout << "|" << endl;
		cout << "-------------" << endl;
	}
}

void update()
{
	char mark;
	player = (player % 2) ? 1 : 2;
	mark = (player == 1) ? 'X' : 'O';

	int x, y;
	do 
	{
		cout << "Player " << player << ", please input cell position: ";
		cin >> x >> y;
	} while (x > 2 || y > 2 || board[x][y] != ' ');
	
	if (board[x][y] == ' ')
		board[x][y] = mark;
	player++;
}

bool checkBoard()
{
	if (board[0][0] != ' ' && board[0][0] == board[0][1] && board[0][1] == board[0][2])
		return true;
	else if (board[1][0] != ' ' && board[1][0] == board[1][1] && board[1][1] == board[1][2])
		return true;
	else if (board[2][0] != ' ' && board[2][0] == board[2][1] && board[2][1] == board[2][2])
		return true;
	else if (board[0][0] != ' ' && board[0][0] == board[1][0] && board[1][0] == board[2][0])
		return true;
	else if (board[0][1] != ' ' && board[0][1] == board[1][1] && board[1][1] == board[2][1])
		return true;
	else if (board[0][2] != ' ' && board[0][2] == board[1][2] && board[1][2] == board[2][2])
		return true;
	else if (board[0][0] != ' ' && board[0][0] == board[1][1] && board[1][1] == board[2][2])
		return true;
	else if (board[0][2] != ' ' && board[0][2] == board[1][1] && board[1][1] == board[2][0])
		return true;
	else if (board[0][0] != ' ' && board[0][1] != ' ' && board[0][2] != ' ' &&
		board[1][0] != ' ' && board[1][1] != ' ' && board[1][2] != ' ' &&
		board[2][0] != ' ' && board[2][1] != ' ' && board[2][2] != ' ')
	{
		isDraw = true;
		return true;
	}
	return false;
}

int main()
{
	cout << "\tTICTACTOE C++" << endl;
	cout << "PLAYER 1 - [X] \t PLAYER 2 = [O]\n" << endl;

	while (!isOver)
	{
		update();
		showBoard();
		isOver = checkBoard();

		if (isOver)
		{
			if (isDraw)
				cout << "-----Draw-----";
			else
				cout << "-----Player " << player << " is winner-----" << endl;
		}
	}
}

What you can see from code?

Everything you see from the candidate's code, does not reflect 100% of their ability but has a very high reference value for the candidate's understanding.

From the beginning, the candidate has already prepared the data, and some utilites function

#include <iostream>
using namespace std;

#define SIZE 3
char board[SIZE][SIZE] = { {' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '} };
int player = 1;
bool isOver = false;
bool isDraw = false;

and some utility prototypes, from these functions you can check the candidate's basic programming syntax, basic programming techniques and programming thinking.

  • showBoard: render board.
  • update: update players state and the board state.
  • checkBoard: check to see if there is a winner.

From the main function you can even check the candidate's understanding of the main loop.

while (!isGameOver)
{
	update();
	showBoard();
	isOver = checkBoard();

	if (isOver)
	{
		if (isDraw)
			cout << "-----Draw-----";
		else
			cout << "-----Player " << player << " is winner-----" << endl;
	}
	return 0;
}

This is how I interview a new programmer, if you have a more useful approach, you can share it with me at vinh.lakien@iostream.co or connect to me at: fb.com/lakienvinh.88

IO Stream

IO Stream Co., Ltd

developer@iostream.co
383/1 Quang Trung, ward 10, Go Vap district, Ho Chi Minh city

©IO Stream, 2013 - 2025