Skip to content

Instantly share code, notes, and snippets.

@cypher-nullbyte
Created February 22, 2021 10:08
Show Gist options
  • Save cypher-nullbyte/1ef0abc9802d0f03e9270a8e4902c9d9 to your computer and use it in GitHub Desktop.
Save cypher-nullbyte/1ef0abc9802d0f03e9270a8e4902c9d9 to your computer and use it in GitHub Desktop.
TicTacToe Problem | Vpropel
TicTacToe Game
Given the board configuration of the tic tac toe game, determine if the board is in either of the
following states: empty, player1 wins, player2 wins, draw or intermediate.
The board is said to be in initial state if all the cells contain ‘-1’,
player1 uses ‘1’ as his coin and player2 uses ‘2’ as his coin.
The game is draw when the board is full and no one has won the game.
The game is in intermediate state when no one has won and board is not full
First finish up taking the inputs. Then apply logic to find state.
#include<stdio.h>
#include<stdlib.h>
const char * checkStatus(int **arr,int negative_hooker)
{
const char *result=negative_hooker>0?"intermediate":"draw";
for(int i=0;i<3;i++)
{
int count=arr[i][0]+arr[i][1]+arr[i][2];
if(count==3 || count==6)
{
result=count==3?"player1 wins":"player2 wins";
return result;
}
}
for(int j=0;j<3;j++)
{
int count=arr[0][j]+arr[1][j]+arr[2][j];
if(count==3 || count==6)
{
result=count==3?"player1 wins":"player2 wins";
return result;
}
//diag check
if(j=0)
{
count=arr[0][0]+arr[1][1]+arr[2][2];
if(count==3 || count==6)
{
result=count==3?"player1 wins":"player2 wins";
return result;
}
}
if(j=2)
{
count=arr[2][0]+arr[1][1]+arr[0][2];
if(count==3 || count==6)
{
result=count==3?"player1 wins":"player2 wins";
return result;
}
}
}
return result;
}
int main()
{
int **arr=(int**)calloc(3,sizeof(int *));
for(int i=0;i<3;i++)
arr[i]=(int *)calloc(3,sizeof(int));
int negative_hooker=0;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
scanf("%d",&arr[i][j]);
if(arr[i][j]==-1)
negative_hooker++;
}
}
if(negative_hooker==9)
{
printf("empty");
exit(0);
}
printf("%s",checkStatus(arr,negative_hooker));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment