/* Filename: HW5.c */ /* This program plays tic tac toe */ /****************************************************************** * * * Programmer: Matthew Glynn * * Date : 02/17/2007 * * Section Number: B36 * * Filename: HW5.c * * Description: This program plays tic tac toe * * Code from example tictac toe program by john robinson * ******************************************************************/ #include void print_board( char a[3][3]); // print board prototype int getMove( int a[3][3]); // get user move function prototype int getMove2( int a[3][3]); // get user move function prototype int main(void) { int board[3][3]; int play=1; int movenumber = 1; printf("\n | | \n"); printf(" ---+---+---\n"); printf(" | | \n"); printf(" ---+---+---\n"); printf(" | | \n\n"); do if(movenumber % 2) { { play = getMove( board ); // Get move from the user and if( play == 0) // check if user wants to quit break; movenumber = movenumber + 1; } } else { { play = getMove2( board ); // Get move from the user and if( play == 0) // check if user wants to quit break; movenumber = movenumber + 1; } } while(play == 1); // bottom do loop return 0; } // end main /****************************************************************** * * * Programmer: Matthew Glynn * * Name: print_board * * Function: print_board function: Prompts user for next move of* * X. * * Algorithm: This function is a series of printf statements. * * Input: none * * Output: changes the global variable choice, so no real output * * * ******************************************************************/ void print_board( int a[3][3] ) { int i, j; // i and column indices printf("\n"); // blank line preceeding for ( i=0; i<3; i++) // 3 rows, one at a time { if ( i != 0) printf("\n---+---+---\n"); // line between rows for (j = 0; j<3; j++) // print along i { if (j != 0) printf(" |"); // print a "|" between columns if (a[i][j] == 0) printf(" X"); else if (a[i][j] == 1) printf(" O"); else printf(" "); } } printf("\n\n"); // blank line following } /****************************************************************** * * * Programmer: Matthew Glynn * * Name: getMove * * Function: This function receives move from user * * Algorithm: receives move from user * * * ******************************************************************/ int getMove( int a[3][3] ) { int valid = 0, play = 1 ; // set move flag to not valid; // play flag to play while ((valid ==0)&&(play==1)) // loop until valid move or quit entry { int i,j; printf("Player ""X"" enter your Move:\n"); printf("Row = "); scanf("%d", &i); if( i == -1 ) { play = 0; break; } else if((i>=0)&&(i<3)) // get column {printf("Col = "); scanf("%d", &j);} { a[i][j] = 0; // make move valid = 1; // set valid flag to exit while print_board( a ); // display board with new move } } // end while loop return play; // return quit/no quit flag } // end function /****************************************************************** * * * Programmer: Matthew Glynn * * Name: getMove * * Function: This function receives move from user * * Algorithm: receives move from user * * * ******************************************************************/ int getMove2( int a[3][3] ) { int valid = 0, play = 1 ; // set move flag to not valid; // play flag to play while ((valid ==0)&&(play==1)) // loop until valid move or quit entry { int i,j; printf("Player ""O"" enter your Move:\n"); printf("Row = "); scanf("%d", &i); if( i == -1 ) { play = 1; break; } else if((i>=0)&&(i<3)) // get column {printf("Col = "); scanf("%d", &j);} // check; selected cell blank ? { a[i][j] = 1; // make move valid = 1; // set valid flag to exit while print_board( a ); // display board with new move } } // end while loop return play; // return quit/no quit flag } // end function