Code:
// initialize board
InitializeBoard(board);
// populate board
PlayerMove(1, 1, board, MARKTWO);
PlayerMove(2, 1, board, MARKTWO);
PlayerMove(3, 1, board, MARKTWO);
// display the board
DisplayBoard(board);
// display victory message
DisplayVictoryMessage( VictoryCheck(CONSECUTIVE_MARKS_REQUIRED, board) );
// PRODUCE A DIAGONALDOWN VICTORY
// initialize board
InitializeBoard(board);
// populate board
PlayerMove(1, 1, board, MARKONE);
PlayerMove(2, 2, board, MARKONE);
PlayerMove(3, 3, board, MARKONE);
// display the board
DisplayBoard(board);
// display victory message
DisplayVictoryMessage( VictoryCheck(CONSECUTIVE_MARKS_REQUIRED, board) );
// PRODUCE A DIAGONALUP VICTORY
// initialize board
InitializeBoard(board);
// populate board
PlayerMove(3, 1, board, MARKTWO);
PlayerMove(2, 2, board, MARKTWO);
PlayerMove(1, 3, board, MARKTWO);
// display the board
DisplayBoard(board);
// display victory message
DisplayVictoryMessage( VictoryCheck(CONSECUTIVE_MARKS_REQUIRED, board) );
// PRODUCE A MULTIPLE PLAYER VICTORY
// initialize board
InitializeBoard(board);
// populate board
PlayerMove(4, 1, board, MARKONE);
PlayerMove(4, 2, board, MARKONE);
PlayerMove(4, 3, board, MARKONE);
PlayerMove(3, 1, board, MARKTWO);
PlayerMove(2, 2, board, MARKTWO);
PlayerMove(1, 3, board, MARKTWO);
// display the board
DisplayBoard(board);
// display victory message
DisplayVictoryMessage( VictoryCheck(CONSECUTIVE_MARKS_REQUIRED, board) );
// exit program
return 0;
}
// FUNCTION IMPLEMENTATIONS
void InitializeBoard(char board[ROWS][COLS]) {
// YOUR PREVIOUS IMPLEMENTATION GOES HERE
for(int i=0; i<ROWS; i++ ) {
for(int j=0; j<COLS; j++ ) {
board[i][j]= BLANK;
}
}
}
void DisplayBoard(char board[ROWS][COLS]) {
// YOUR PREVIOUS IMPLEMENTATION GOES HERE
printf("*************TIC-TAC-TOE**********\n\n");
printf( " " );
for(int i=0; i<COLS; i++ ) {
printf( "%d ", i + 1 );
}
printf( "\n\n" );
for(int i=0; i<ROWS; i++ ) {
printf( "%d ", i + 1);
for(int j=0; j<COLS-1; j++ ) {
printf( "%c|", board[i][j] );
}
printf( "\n -" );
for(int j=0; j<COLS-1; j++ ) {
printf( "+-" , board[i][j]);
}
printf( "\n" );
}
}
int PlayerMove(int row, int col, char board[ROWS][COLS], char symbol) {
// YOUR PREVIOUS IMPLEMENTATION GOES HERE
if ( row <= 0 || col <= 0 || row >= ROWS || col >= COLS ) {
printf( "THE MOVE IS NOT ON THE BOARD\n" );
return FALSE;
}
else if (board[row-1][col-1] == MARKONE ||
board[row-1][col-1] == MARKTWO) {
printf( "THAT SPACE IS ALREADY OCCUPIED\n" );
return FALSE;
}
else {
board[row-1][col-1]=symbol;
return TRUE;
}
return FALSE;
}
int VictoryCheck(int winRequirement, char board[ROWS][COLS]) {
// YOUR IMPLEMENTATION GOES HERE
winRequirement = CONSECUTIVE_MARKS_REQUIRED;
int P1Wins=0,P2Wins=0;
int cnt[2]={0};//count, 0-P1, 1-P2
int i,j,k;
//horizontal
for(int i=0;i<ROWS;i++){
for(int j=0;j<COLS;j++){
if(board[i][j]==MARKONE){
P1Wins+=helpcheckWin(cnt,0,winRequirement);
}
else if(board[i][j]==MARKTWO){
P2Wins+=helpcheckWin(cnt,1,winRequirement);
}
else{
cnt[0]=0;
cnt[1]=0;
}
}//end for j
}//end for i
//vertical
cnt[0]=0;
cnt[1]=0;
for(int j=0;j<COLS;j++){
for(int i=0;i<ROWS;i++){
if(board[i][j]==MARKONE){
P1Wins+=helpcheckWin(cnt,0,winRequirement);
}
else if(board[i][j]==MARKTWO){
P2Wins+=helpcheckWin(cnt,1,winRequirement);
}
else{
cnt[0]=0;
cnt[1]=0;
}
}//end for j
}//end for i
//FILE *fo;
//fo=fopen("trace.txt","w");
////////////////////////////////////////////////////////
//diagonal left
cnt[0]=0;
cnt[1]=0;
i=0;
for(int j=0;j<=COLS-winRequirement;j++){
// fprintf(fo,"\n\n%d\n",i);
for(int k=0;k+j<COLS &&k+i<ROWS;k++){
// fprintf(fo,"\nChecking [%d,%d]",i+k,j+k);
if(board[i+k][j+k]==MARKONE){
P1Wins+=helpcheckWin(cnt,0,winRequirement);
}
else if(board[i+k][j+k]==MARKTWO){
P2Wins+=helpcheckWin(cnt,1,winRequirement);
}
else{
cnt[0]=0;
cnt[1]=0;
}
}
}
cnt[0]=0;
cnt[1]=0;
j=0;
for(int i=1;i<=ROWS-winRequirement;i++){
//fprintf(fo,"\n\n%d\n",i);
for(int k=0;k+j<COLS &&k+i<ROWS;k++){
// fprintf(fo,"\nChecking [%d,%d]",i+k,j+k);
if(board[i+k][j+k]==MARKONE){
P1Wins+=helpcheckWin(cnt,0,winRequirement);
}
else if(board[i+k][j+k]==MARKTWO){
P2Wins+=helpcheckWin(cnt,1,winRequirement);
}
else{
cnt[0]=0;
cnt[1]=0;
}
}
}
////////////////////////////////////////////////////////
//diagonal right
cnt[0]=0;
cnt[1]=0;
i=0;
for(j=COLS-1;j-winRequirement+1>=0;j--){
//fprintf(fo,"\n\n%d\n",i);
for(k=0;j-k>=0 &&k+i<ROWS;k++){
// fprintf(fo,"\nChecking [%d,%d]",i+k,j-k);
if(board[i+k][j-k]==MARKONE){
P1Wins+=helpcheckWin(cnt,0,winRequirement);
}
else if(board[i+k][j-k]==MARKTWO){
P2Wins+=helpcheckWin(cnt,1,winRequirement);
}
else{
cnt[0]=0;
cnt[1]=0;
}
}
}
cnt[0]=0;
cnt[1]=0;
j=COLS-1;
for(i=1;i<=ROWS-winRequirement;i++){
//fprintf(fo,"\n\n%d\n",i);
for(k=0;j-k>=0 &&k+i<ROWS;k++){
//fprintf(fo,"\nChecking [%d,%d]",i+k,j-k);
if(board[i+k][j-k]==MARKONE){
P1Wins+=helpcheckWin(cnt,0,winRequirement);
}
else if(board[i+k][j-k]==MARKTWO){
P2Wins+=helpcheckWin(cnt,1,winRequirement);
}
else{
cnt[0]=0;
cnt[1]=0;
}
}
}
//fclose(fo);
//__________________________________________
if(P1Wins==0&&P2Wins==0){
if(movesleft(board)==1)
return NOWIN;
else
return TIE;
}
else if(P1Wins>0&&P2Wins==0){return MARKONEVICTORY;}
else if(P1Wins==0&&P2Wins>0){return MARKTWOVICTORY;}
else if(P1Wins>0&&P2Wins>0){return ERROR;}
else
return EPIC_FAIL;
}//end ck
int helpcheckWin(int arr[],int idx, int req){ //returns either 1 for win or 0 for not win
arr[idx]++; //increment this idx
arr[(idx+1)%2]=0; //set other idx to 0
if(arr[idx]==req){
arr[idx]=0; //reset to 0
return 1; //return 1 to be added to win
}
return 0;//if not a win, return 0
}
int movesleft(char b[ROWS][COLS]){
int i,j;
for(i=0;i<ROWS;i++){
for(j=0;j<COLS;j++){
if(b[i][j]==BLANK)
return 1;
}
}
return 0;//if no BLANK found
}
void DisplayVictoryMessage(int victoryCode) {
// display the victory condition results
switch(victoryCode) {
case NOWIN:
printf("There is still no winner.\n");
break;
case MARKONEVICTORY:
printf("MARKONE has won the game.\n");
break;
case MARKTWOVICTORY:
printf("MARKTWO has won the game.\n");
break;
case TIE:
printf("The game is a draw.\n");
break;
case ERROR:
printf("Something bad happened... MARKONE and MARKTWO have both won.\n");
break;
case EPIC_FAIL:
printf("Something bad happened... VictoryCheck() has produced an impossible combination of return code indicators.\n");
break;
default:
printf("DisplayVictoryMessage() was passed an invalid victoryCode.\n");
}
}