Skip to content

Instantly share code, notes, and snippets.

@porthunt
Last active August 29, 2015 13:59
Show Gist options
  • Save porthunt/10504356 to your computer and use it in GitHub Desktop.
Save porthunt/10504356 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define rows 4
#define cols 4
void checkIfWinner(char boardMap[rows][cols], int k);
int checkDiagonal(char board[rows][cols], char side);
int checkGameStatus(char board[rows][cols]);
void concatStrings(char first[], int k, char second[], char f, char third[]);
void initializeBoard(char boardMap[rows][cols]);
char resultCases[1002][50];
int main(void) {
char board[rows][cols];
int i, j;
int cases=0;
int k=1;
scanf("%d", &cases);
getchar();
while(k<=cases) {
for(j=0; j<rows;j++) {
for(i=0; i<cols; i++) {
board[j][i]=getchar();
}
getchar();
}
checkIfWinner(board, k);
//puts("\n");
getchar();
k++;
}
for (i=0; i<cases; i++) {
if (resultCases[i]!=NULL)
printf("%s\n", resultCases[i]);
}
}
void checkIfWinner(char boardMap[rows][cols], int k) {
char draw[10] = "Draw";
char caseN[10] = "Case #";
char won[10] = " won";
char point[5] = ": ";
char gameNotCompleted[30] = "Game has not completed";
char f;
int i, j, qt=0;
for (i=0; i<cols; i++) {
f = boardMap[0][i]; qt=0;
if (f!='-') {
for (j=0; j<rows; j++) {
if (boardMap[j][i]==f || boardMap[j][i]=='T')
qt++;
if (qt==4 && f!='.') {
concatStrings(caseN, k, point, f, won);
return;
}
}
}
}
for (i=0; i<rows; i++) {
f = boardMap[i][0]; qt=0;
if (f!='-') {
for (j=0; j<cols; j++) {
if (boardMap[i][j]==f || boardMap[i][j]=='T')
qt++;
if (qt==4 && f!='.') {
concatStrings(caseN, k, point, f, won);
return;
}
}
}
}
if(checkDiagonal(boardMap, 'l') && boardMap[0][0]!='.' && boardMap[1][1]!='.') {
if(boardMap[0][0]=='T')
concatStrings(caseN, k, point, boardMap[1][1], won);
else
concatStrings(caseN, k, point, boardMap[0][0], won);
return;
}
if(checkDiagonal(boardMap, 'r') && boardMap[0][cols-1]!='.' && boardMap[1][2]!='.') {
if(boardMap[0][cols-1]=='T')
concatStrings(caseN, k, point, boardMap[1][cols-2], won);
else
concatStrings(caseN, k, point, boardMap[0][cols-1], won);
return;
}
if(checkGameStatus(boardMap)) {
concatStrings(caseN, k, point, 'n', gameNotCompleted);
return;
}
concatStrings(caseN, k, point, 'n', draw);
}
int checkGameStatus(char board[rows][cols]) {
int i,j;
for(j=0; j<rows;j++) {
for(i=0; i<cols; i++) {
if(board[i][j]=='.') {
return 1;
}
}
}
return 0;
}
int checkDiagonal(char board[rows][cols], char side) {
char mirrorBoard[rows][cols];
initializeBoard(mirrorBoard);
if (side=='l') {
if(board[0][0]!='T')
mirrorBoard[0][0]=board[0][0];
else
mirrorBoard[0][0]=board[1][1];
if (board[1][1]!='T')
mirrorBoard[1][1]=board[1][1];
else
mirrorBoard[1][1]=board[0][0];
if (board[2][2]!='T')
mirrorBoard[2][2]=board[2][2];
else
mirrorBoard[2][2]=board[0][0];
if (board[3][3]!='T')
mirrorBoard[3][3]=board[3][3];
else
mirrorBoard[3][3]=board[0][0];
if (mirrorBoard[0][0] == mirrorBoard[1][1] && mirrorBoard[0][0] == mirrorBoard[2][2] && mirrorBoard[0][0] == mirrorBoard[3][3]) {
return 1;
} else {
return 0;
}
}
else if (side=='r') {
if(board[0][3]!='T')
mirrorBoard[0][3]=board[0][3];
else
mirrorBoard[0][3]=board[1][2];
if (board[1][2]!='T')
mirrorBoard[1][2]=board[1][2];
else
mirrorBoard[1][2]=board[0][3];
if (board[2][1]!='T')
mirrorBoard[2][1]=board[2][1];
else
mirrorBoard[2][1]=board[0][3];
if (board[3][0]!='T')
mirrorBoard[3][0]=board[3][0];
else
mirrorBoard[3][0]=board[0][3];
if (mirrorBoard[0][3] == mirrorBoard[1][2] && mirrorBoard[0][3] == mirrorBoard[2][1] && mirrorBoard[0][3] == mirrorBoard[3][0]) {
return 1;
} else {
return 0;
}
}
return 0;
}
void initializeBoard(char boardMap[rows][cols]) {
int i, j;
for (i=0; i<rows;i++) {
for (j=0;j<cols;j++)
boardMap[i][j] = '-';
}
}
void concatStrings(char first[], int k, char second[], char f, char third[]) {
char buffer[5];
char phrase[50];
char matrix[2];
sprintf (buffer, "%d", k);
matrix[0] = f;
matrix[1] = '\0';
strcpy(phrase, first);
strcat(phrase, buffer);
strcat(phrase, second);
if(f!='n')
strcat(phrase, matrix);
strcat(phrase, third);
strcpy(resultCases[k-1], phrase);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment