top of page
Click here to go to the home page of AskTheCode.

Tic Tac Toe CodeChef May Long Challenge Solution in Java - AskTheCode

Team ATC

Updated: May 9, 2021

CodeChef May Long Challenge Solution | Tic Tac Toe (TCTCTOE) solution | AskTheCode

 

Tic Tac Toe (TCTCTOE) solution in Java


Problem:

Tic-tac-toe is a game played between two players on a 3×3 grid. In a turn, a player chooses an empty cell and places their symbol on the cell. The players take alternating turns, where the player with the first turn uses the symbol X and the other player uses the symbol O. The game continues until there is a row, column, or diagonal containing three of the same symbol

(X or O), and the player with that token is declared the winner. Otherwise if every cell of the grid contains a symbol and nobody won, then the game ends and it is considered a draw.

You are given a tic-tac-toe board A after a certain number of moves, consisting of symbols O, X, and underscore(_). Underscore signifies an empty cell.

Print

  • 1: if the position is reachable, and the game has drawn or one of the players won.

  • 2: if the position is reachable, and the game will continue for at least one more move.

  • 3: if the position is not reachable.

 

Input:

  • The first line contains an integer T, the number of test cases. Then the test cases follow.

  • Each test case contains 3 lines of input where each line contains a string describing the state of the game in ith row.

 

Output:

For each test case, output in a single line 1, 2 or 3 as described in the problem.

 

Sample Input:

3
XOX
XXO
O_O
XXX
OOO
___
XOX
OX_
XO_
 

Sample Output:

2
3
1
 

Explanation:

Test Case 1: The board is reachable, and although no player can win from this position, still the game continues.

Test Case 2: There can't be multiple winners in the game.

Test Case 3: The first player is clearly a winner with one of the diagonals.

 

Code:

Hey Coders,
We've uploaded the solution to this problem in Java. If you are participating in the contest using other other language, implement the same logic, it'll be working.
To see the solution, kindly visit AskTheCode Members Group, or you can just click here to see the code.
Like the post, after you find this post really useful🙂.

Recent Posts

See All

3 Comments


ttm.amrita
ttm.amrita
May 08, 2021

solution please

Like

sonamchaudhary2000
sonamchaudhary2000
May 08, 2021

solution please

Like

saumitra vashishtha
saumitra vashishtha
May 08, 2021

please upload the solution'

Like
bottom of page