# programming challenge – Tick-tack-toe (Jumping to C ++)

The exercise is from Alex Allain's book "Jumping in C ++".
From Chapter 10, Practical problem 4:

1. Write a small tic-tac-toe program that allows two players to play
tic-tac-toe competitively. Your program should check if
The player has won, or if the board is completely full (with the game
ending in a tie). Bonus: you can make your program detect if the game
Can it not be won by either side before the entire grid is filled?

Here is my code. My main objective was a game that worked as "tick-tack-toe" with little concern for the user experience (eg, the coordinates that a player enters start from 0, or the coordinate (0,0) is in the upper left corner, not lower left as you would intuitively think). As for the coding style, I wanted the code to be as DRY as possible, with small, atomic, and easy-to-understand functions that do most of the work.

``````#include

using namespace std;

bool checkWin (int board[][3], int player) {
// horizontal
for (int i = 0; i <3; i ++) {
yes[i][0]    == player and board[i][1]    == player and board[i][2]    == player) returns true;
}

// vertical
for (int i = 0; i < 3; i++) {
if (board[0][i] == player && board[1][i] == player && board[2][i] == player) return true;
}

// diagonal
if ((board[0][0] == player && board[1][1] == player && board[2][2] == player) ||
(board[0][2] == player && board[1][1] == player && board[2][0] == player))
return true;

return false;
}

// TODO implement function if the game is a tie before the last move

bool fillPlace(int board[][3], int x, int y, int value) {
if (x > 2 || x <0) {
cout << "Invalid x coordinate. Try again.n";
return false;
} else if (y > 2 || and <0) {
cout << "Coordinate and not valid. Try again.  n";
false return;
} else if (board[x][y]    ! = 0) {
cout << "The place is already full, try again.  n";
false return;
} else {
board[x][y]    = value;
true returns
}
}

void initBoard (int board[][3], int. size) {
for (int i = 0; i <size; i ++) {
for (int j = 0; j <3; j ++) {
board[i][j]    = 0;
}
}
}

void printBoard (int board[][3], int. size) {
cout << "-----" << endl;
for (int i = 0; i <size; i ++) {
for (int j = 0; j <3; j ++) {
cout << board[i][j]    << "";
}
cout << endl;
}
cout << "-----" << endl;
}

bool isBoardFilled (int board[][3]) {
bool isFilled = true;
for (int i = 0; i <3; i ++) {
for (int j = 0; j <3; j ++) {
yes[i][j]    == 0) isFilled = false;
}
}
the return is Stuffed;
}

int main () {

board of int[3][3];
initBoard (board, 3);
printBoard (board, 3);

int playerOnesTurn = true;
while (true) {
int x, y;

if (playerOnesTurn) {
cout << "player 1 turn" << endl;
do {
cout << "Enter x coordinate: "; cin >> x;
cout << "Enter y coordinate: "; cin >> and;
} while (! fillPlace (board, x, y, 1));
} else {
cout << "player 2 turn" << endl;
do {
cout << "Enter x coordinate: "; cin >> x;
cout << "Enter y coordinate: "; cin >> and;
} while (! fillPlace (board, x, y, 2));
}
playerOnesTurn =! playerOnesTurn;

if (isBoardFilled (board)) {
cout << "The game is a draw.  n";
break;
}

printBoard (board, 3);
if (checkWin (board, 1)) {
cout << "Player 1 has won  n";
break;
} else if (checkWin (board, 2)) {
cout << "Player 2 has won  n";
break;
}
}

returns 0;
}
``````