# What Data Structure Is Used in Tic-Tac-Toe?

//

Larry Thompson

Tic-Tac-Toe is a classic game that involves two players taking turns to mark spaces on a 3×3 grid. The goal is to get three of your marks in a row, either horizontally, vertically, or diagonally. To implement this game, we need to use an appropriate data structure that represents the grid and keeps track of the moves made by the players.

## The Grid

In Tic-Tac-Toe, the grid can be represented using a two-dimensional array. This allows us to easily access and modify individual cells on the grid.

Let’s define our grid as follows:

``````
let grid = [
['', '', ''],
['', '', ''],
['', '', '']
];
```
```

The above code creates a 3×3 grid with empty strings representing each cell. These empty strings indicate that no player has made a move in that cell yet.

## Player Moves

To keep track of player moves, we can use another data structure such as an array or a list. Each element in this data structure represents a move made by a player.

Let’s define our player moves array:

``````
let playerMoves = [];
```
```

We will push each move into this array as players make their moves on the grid.

## Checking for Winning Conditions

In order to determine if there is a winner in Tic-Tac-Toe, we need to check for winning conditions after each move. There are eight possible ways to win: three rows, three columns, and two diagonals.

• Rows: We can check each row to see if it contains three identical marks.
• Columns: We can check each column to see if it contains three identical marks.
• Diagonals: We can check both diagonals to see if they contain three identical marks.

To perform these checks, we can define separate functions for each winning condition and call them after every move.

### Checking Rows

``````
function checkRows() {
for (let row = 0; row < 3; row++) {
if (grid[row] !== '' && grid[row] === grid[row] && grid[row] === grid[row]) {
return true;
}
}

return false;
}
```
```

The above code checks each row of the grid and returns true if any row contains three identical marks. Otherwise, it returns false.

### Checking Columns

``````
function checkColumns() {
for (let col = 0; col < 3; col++) {
if (grid[col] !== '' && grid[col] === grid[col] && grid[col] === grid[col]) {
return true;
}
}

return false;
}
```
```

The above code checks each column of the grid and returns true if any column contains three identical marks.

### Checking Diagonals

``````
function checkDiagonals() {
if (grid !== '' && grid === grid && grid === grid) {
return true;
}

if (grid !== '' && grid === grid && grid === grid) {
return true;
}

return false;
}
```
```

The above code checks both diagonals of the grid and returns true if either diagonal contains three identical marks.

## Conclusion

In conclusion, the data structures used in Tic-Tac-Toe are a two-dimensional array to represent the grid and an array or list to keep track of player moves. By using these data structures and implementing the necessary functions to check for winning conditions, we can create a fully functional Tic-Tac-Toe game.

Now that you have a better understanding of the data structure used in Tic-Tac-Toe, you can confidently create your own version of this classic game!