Problem
Tic-Tac-Toe-Tomek is a game played on a 4 x 4 square board. The board starts empty, except that a single 'T' symbol may appear in one of the 16 squares. There are two players: X and O. They take turns to make moves, with X starting. In each move a player puts her symbol in one of the empty squares. Player X's symbol is 'X', and player O's symbol is 'O'.After a player's move, if there is a row, column or a diagonal containing 4 of that player's symbols, or containing 3 of her symbols and the 'T' symbol, she wins and the game ends. Otherwise the game continues with the other player's move. If all of the fields are filled with symbols and nobody won, the game ends in a draw. See the sample input for examples of various winning positions.
Given a 4 x 4 board description containing 'X', 'O', 'T' and '.' characters (where '.' represents an empty square), describing the current state of a game, determine the status of the Tic-Tac-Toe-Tomek game going on. The statuses to choose from are:
- "X won" (the game is over, and X won)
- "O won" (the game is over, and O won)
- "Draw" (the game is over, and it ended in a draw)
- "Game has not completed" (the game is not over yet)
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each test case consists of 4 lines with 4 characters each, with each character being 'X', 'O', '.' or 'T' (quotes for clarity only). Each test case is followed by an empty line.Output
For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y is one of the statuses given above. Make sure to get the statuses exactly right. When you run your code on the sample input, it should create the sample output exactly, including the "Case #1: ", the capital letter "O" rather than the number "0", and so on.Limits
The game board provided will represent a valid state that was reached through play of the game Tic-Tac-Toe-Tomek as described above.Small dataset
1 ≤ T ≤ 10.Large dataset
1 ≤ T ≤ 1000.Sample
Input |
Output |
6
|
Case #1: X won |
code:
#include <stdio.h>
#include <stdlib.h>
char A[16];
int sta(int a,int b,int c,int d);
int main()
{
char msg[8]="Case #",ch=':',chr,win;
FILE *fp,*fp1;
int tc,i,j,k;
char file[10];
printf("Enter the input file name : ");
scanf("%s",file);
fp=fopen(file,"r");
fp1=fopen("output2.txt","w");
fscanf(fp,"%d",&tc);
for(i=0;i<tc;i++)
{
k=0;
while(k<16)
{
fscanf(fp,"%c",&chr);
if(chr=='.'||chr=='T'||chr=='X'||chr=='O')
A[k++]=chr;
}
/*1*/ if(sta(0,1,2,3))
{
win=A[0];
if(win=='T')
win=A[1];
fprintf(fp1,"%s%d%c %c %s\n",msg,i+1,ch,win,"won");
}
/*2*/ else if(sta(4,5,6,7))
{
win=A[4];
if(win=='T')
win=A[5];
fprintf(fp1,"%s%d%c %c %s\n",msg,i+1,ch,win,"won");
}
/*3*/ else if(sta(8,9,10,11))
{
win=A[8];
if(win=='T')
win=A[9];
fprintf(fp1,"%s%d%c %c %s\n",msg,i+1,ch,win,"won");
}
/*4*/ else if(sta(12,13,14,15))
{
win=A[12];
if(win=='T')
win=A[13];
fprintf(fp1,"%s%d%c %c %s\n",msg,i+1,ch,win,"won");
}
/*5*/ else if(sta(0,4,8,12))
{
win=A[0];
if(win=='T')
win=A[4];
fprintf(fp1,"%s%d%c %c %s\n",msg,i+1,ch,win,"won");
}
/*6*/ else if(sta(1,5,9,13))
{
win=A[1];
if(win=='T')
win=A[5];
fprintf(fp1,"%s%d%c %c %s\n",msg,i+1,ch,win,"won");
}
/*7*/ else if(sta(2,6,10,14))
{
win=A[2];
if(win=='T')
win=A[6];
fprintf(fp1,"%s%d%c %c %s\n",msg,i+1,ch,win,"won");
}
/*8*/ else if(sta(3,7,11,15))
{
win=A[3];
if(win=='T')
win=A[7];
fprintf(fp1,"%s%d%c %c %s\n",msg,i+1,ch,win,"won");
}
/*9*/ else if(sta(0,5,10,15))
{
win=A[0];
if(win=='T')
win=A[5];
fprintf(fp1,"%s%d%c %c %s\n",msg,i+1,ch,win,"won");
}
/*10*/ else if(sta(12,9,6,3))
{
win=A[12];
if(win=='T')
win=A[3];
fprintf(fp1,"%s%d%c %c %s\n",msg,i+1,ch,win,"won");
}
else if((A[0]=='.')||(A[1]=='.')||(A[2]=='.')||(A[3]=='.')||(A[4]=='.')||(A[5]=='.')||(A[6]=='.')||(A[7]=='.')||(A[8]=='.')||(A[9]=='.')||(A[10]=='.')||(A[11]=='.')||(A[12]=='.')||(A[13]=='.')||(A[14]=='.')||(A[15]=='.'))
{
fprintf(fp1,"%s%d%c %s\n",msg,i+1,ch,"Game has not completed");
}
else
{
fprintf(fp1,"%s%d%c %s\n",msg,i+1,ch,"Draw");
}
}
fclose(fp);
fclose(fp1);
return(0);
}
int sta(int a,int b,int c,int d)
{
if(A[a]=='.'||A[b]=='.'||A[c]=='.'||A[d]=='.')
return(0);
if(A[a]==A[b]&&A[b]==A[c]&&A[c]==A[d])
return(1);
if(A[a]==A[b]&&A[b]==A[c]&&A[d]=='T')
return(1);
if(A[a]==A[b]&&A[b]==A[d]&&A[c]=='T')
return(1);
if(A[d]==A[b]&&A[b]==A[c]&&A[a]=='T')
return(1);
if(A[a]==A[d]&&A[d]==A[c]&&A[b]=='T')
return(1);
return(0);
}