Batman and Tick-tack-toe

Batman has defeated Hugo Strange and now he is getting bored as there is too much peace in Gotham City.

To pass his time he used to play 4 X 4 field Tick-Tac-Toe. One Day While playing, Commissioner Gordan Called Batman and he had to leave immediately so he didn't finish the game. It was Batman's turn in the game when they left it. Find Out whether Batman could have won the game by making a single turn or not.

The rules of tic-tac-toe are as follows.

Before the first turn all the field cells are empty. The two players take turns placing their signs into empty cells (the first player places Xs, the second player places Os). The player who places Xs goes first, the another one goes second. The winner is the player who first gets three of his signs in a row next to each other (horizontal, vertical or diagonal).

Example:

Input:  board = {{"xx.."},{".oo."},{"x..."},{"oox."}}
Output: YES

Approach

C++

#include <bits/stdc++.h>
using namespace std;

vector<stringboard;
bool row()
{
    bool flag = false;
    for (int i = 0i < 4i++)
    {
        for (int j = 0j <= 1j++)
        {
            if (board[i][j] == 'x' &&
                board[i][j + 1] == 'x' &&
                board[i][j + 2] == '.')
            {
                flag = 1;
                break;
            }
            if (board[i][j] == '.' &&
                board[i][j + 1] == 'x' &&
                board[i][j + 2] == 'x')
            {
                flag = 1;
                break;
            }
            if (board[i][j] == 'x' &&
                board[i][j + 1] == '.' &&
                board[i][j + 2] == 'x')
            {
                flag = 1;
                break;
            }
        }
    }
    if (flag == 1)
        return true;
    return false;
}

bool col()
{
    bool flag = false;
    for (int j = 0j < 4j++)
    {
        for (int i = 0i <= 1i++)
        {
            if (board[i][j] == 'x' &&
                board[i + 1][j] == 'x' &&
                board[i + 2][j] == '.')
            {
                flag = 1;
                break;
            }

            if (board[i][j] == 'x' &&
                board[i + 1][j] == '.' &&

                board[i + 2][j] == 'x')
            {
                flag = 1;
                break;
            }

            if (board[i][j] == '.' &&
                board[i + 1][j] == 'x' &&
                board[i + 2][j] == 'x')
            {
                flag = 1;
                break;
            }
        }
    }
    if (flag == 1)
        return true;
    return false;
}

bool diagonal()
{
    bool flag = false;
    for (int i = 0i <= 1i++)
    {
        for (int j = 0j <= 1j++)
        {
            if (board[i][j] == 'x' &&
                board[i + 1][j + 1] == 'x' &&
                board[i + 2][j + 2] == '.')
            {
                flag = 1;
                break;
            }
            if (board[i][j] == '.' &&
                board[i + 1][j + 1] == 'x' &&
                board[i + 2][j + 2] == 'x')
            {
                flag = 1;
                break;
            }
            if (board[i][j] == 'x' &&
                board[i + 1][j + 1] == '.' &&
                board[i + 2][j + 2] == 'x')
            {
                flag = 1;
                break;
            }
            if (board[i + 2][j] == 'x' &&
                board[i + 1][j + 1] == 'x' &&
                board[i][j + 2] == '.')
            {
                flag = 1;
                break;
            }
            if (board[i + 2][j] == 'x' &&
                board[i + 1][j + 1] == '.' &&
                board[i][j + 2] == 'x')
            {
                flag = 1;
                break;
            }
            if (board[i + 2][j] == '.' &&
                board[i + 1][j + 1] == 'x' &&
                board[i][j + 2] == 'x')
            {
                flag = 1;
                break;
            }
        }
    }
    if (flag == 1)
        return true;
    return false;
}

int main()
{

    board = {{"xx.."},
             {".oo."},
             {"x..."},
             {"oox."}};

    bool ans1 = row(), ans2 = col(), ans3 = diagonal();
    if (ans1 == true || ans2 == true || ans3 == true)
        cout << "YES\n";
    else
        cout << "NO\n";

    return 0;
}


No comments:

Post a Comment