Question

In: Computer Science

In a game of tic tac toe. How do you check if all the positions in...

In a game of tic tac toe. How do you check if all the positions in a double array are taken, the double arrays are used to store the x's and o's?

Solutions

Expert Solution

Answer :

In a tic tac toe game there is 8 combinations to win,

3 Columns, 3 Rows and 2 Diagonal.

Let's say we use 2D array to store the tic tac toe game board.

Let the first player letter be 'X' and the second player letter be 'O'. Let second player be computer.

Initially all the cells will be filled with spaces(' ').

For every second player we should check if there is any cell left to fill the letter.('O')

If there is no empty cell remaining then, we should display the game as Draw.

HERE IS CODE FOR THAT:

void second_player_move(void)

{

int i, j;

for(i=0; i<3; i++){

for(j=0; j<3; j++){

if(matrix[i][j]==' ') break;

}

if(matrix[i][j]==' ') break;

}

if(i*j==9) {

printf("draw\n");

exit(0);

}

else

matrix[i][j] = 'O';

}

When there is no empty cell in the board i*j will result to 9. Which displays draw and the game will end.

Player 1 Moves : 1, 3, 5, 7, 9

Player 2 Moves : 2, 4, 6, 8

After 9th move all cells will be filled,

At 10th move there will be no cell left to be filled.

​​​​Else letter 'O' will be entried to empty cell on the board.

Hence, by this way we could check whether all the positions of the double array are used to store 'X' and 'O' s.


Related Solutions

Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3...
Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3 grid as shown below: The game is played by two players, who take turns. The first player marks moves with a circle, the second with a cross. The player who has formed a horizontal, vertical, or diagonal sequence of three marks wins. Your program should draw the game board, ask the user for the coordinates of the next mark (their move), change the players...
How to make tic tac toe game in javascript with vue.js
How to make tic tac toe game in javascript with vue.js
If you were to write a game of tic-tac-toe, you may store the representation of the...
If you were to write a game of tic-tac-toe, you may store the representation of the game board as a two dimensional list such as   [['X', '', 'X'], ['O', 'X', ''], ['', 'O', 'X']] where each sublist is a row in the board.   Empty strings ('') denote a space that does not yet have a value. Assuming this representation, write functions (contained in the same file) that do the following: a) Create a new empty tic-tac-toe board. This function should...
The objective of this assignment is to implement the tic-tac-toe game with a C program. The...
The objective of this assignment is to implement the tic-tac-toe game with a C program. The game is played by two players on a board defined as a 5x5 grid (array). Each board position can contain one of two possible markers, either ‘X’ or ‘O’. The first player plays with ‘X’ while the second player plays with ‘O’. Players place their markers in an empty position of the board in turns. The objective is to place 5 consecutive markers of...
Game of Tic Tac Toe with the following conditions A point system where a  move that leads...
Game of Tic Tac Toe with the following conditions A point system where a  move that leads to a winning game is given 1 point, a move that leads to a tie is given 0 point, and a  lead to a losing game will get -1 point. Grid size of 5x5 A timer that can be set for how long a game can be played 5 symbols in a row to get a point Connected lines cannot be crossed (No diagonal lines)...
please create a tic tac toe game with python using only graphics.py library
please create a tic tac toe game with python using only graphics.py library
If anyone can please write a code for a 5x5 tic tac toe game in matlab...
If anyone can please write a code for a 5x5 tic tac toe game in matlab I would greatly appreciate it. Its extra credit. PLEASE HELP ME :(
Provided is code for checking horizontal and vertical win in a tic tac toe game. Please...
Provided is code for checking horizontal and vertical win in a tic tac toe game. Please provide code for checking diagonal win. function checkWinVertical(player, row, col) { var counter = 0; //each button var btn = document.getElementsByTagName("button"); for (counterC = 0; counterC < col; counterC++){ for (countR = 0; countR < row; countR++){ if (player != btn[counter].innerHTML){ counter += 1; break; } else if (countR + 1 == col) { for (countW = 0; countW < col; countW++){ btn[counter -...
C++ Make a Tic Tac Toe game for 2 players to play using 2D arrays and...
C++ Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes. Do not add more #include functions other than the ones listed. I never said what type of code I needed in a previous question. I apologize and I can't go back and change it so here is the same question with more information Using the tictactoeGame class, write a main program that uses a tictactoeGame to implement a game in which two players...
QUESTION: This is a tic-tac-toe game in react.js. Covert the following class-based components in React.js to...
QUESTION: This is a tic-tac-toe game in react.js. Covert the following class-based components in React.js to Functional Based component in React. //CODE import React from 'react'; import ReactDOM from 'react-dom'; import './index.css'; function calculateWinner(squares) { const lines = [ [0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6] ]; for (let i = 0; i < lines.length; i++) { const [a, b, c] = lines[i];...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT