Skip to content
Programming101
Programmingoneonone
  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

HackerEarth Tic-tac-toe problem solution

YASH PAL, 31 July 2024
In this HackerEarth Tic-tac-toe problem solution, A game of tic-tac-toe is played on a 3 x 3 square grid. Each cell is either empty (denoted by ‘.’) or occupied by one of the two players (‘X’ and ‘O’). X goes first and then they take turns alternatively. If X is written three times in a straight line, then X wins and the game ends. The same goes for O. If there is no empty cell left, then the game ends as a draw. You are given the description of a game of tic-tac-toe. You have to determine the state of the game.
The states of the game are as follows:
  1. If the game is invalid, that is, if there is no possibility of it happening, output “Wait, what?”.
  2. If X has won, then print “X won.” else if O has won print “O won.”.
  3. If it is a draw, then print “It is a draw.”.
  4. Otherwise, print whose turn it is, “X’s turn.” or “O’s turn.” accordingly.
HackerEarth Tic-tac-toe problem solution

HackerEarth Tic-tac-toe problem solution.

#include<bits/stdc++.h>
using namespace std;
string A[3];
int Count(char ch)
{
int cnt = 0;
for (int i = 0; i < 3; i ++)
for (int j = 0; j < 3; j ++)
cnt += A[i][j] == ch;
return (cnt);
}
int Win(char ch)
{
int cnt = 0;
for (int i = 0; i < 3; i ++)
cnt += A[i][0] == ch && A[i][1] == ch && A[i][2] == ch,
cnt += A[0][i] == ch && A[1][i] == ch && A[2][i] == ch;
cnt += A[0][0] == ch && A[1][1] == ch && A[2][2] == ch;
cnt += A[0][2] == ch && A[1][1] == ch && A[2][0] == ch;
return (cnt > 0);
}
int main()
{
cin >> A[0] >> A[1] >> A[2];
int Cx = Count('X'), Wx = Win('X');
int Co = Count('O'), Wo = Win('O');
if (Cx < Co)
return !printf("Wait, what?");
if (Cx > Co + 1)
return !printf("Wait, what?");
if (Wx + Wo > 1)
return !printf("Wait, what?");
if (Wx && Cx != Co + 1)
return !printf("Wait, what?");
if (Wx)
return !printf("X won.");
if (Wo && Cx != Co)
return !printf("Wait, what?");
if (Wo)
return !printf("O won.");
if (Cx + Co == 9)
return !printf("It's a draw.");
if (Cx == Co)
return !printf("X's turn.");
return !printf("O's turn.");
}
coding problems solutions

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes