Advertisement
theelitenoob

Tic-Tac-Toe

Mar 7th, 2012
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.17 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5.  
  6. struct state{ // defined
  7.     int state; // 0 is tie, 1 is user loss, 2 is user win, 3 is ongoing game
  8.     int moves;
  9. };
  10.  
  11. struct square{ // one square of the board
  12.     int value; // 1 is x, 2 is o
  13.     char sign; // no space used
  14. };
  15.  
  16. struct square s[9]; //set up the struct
  17. struct state gamestate = {0,0}; //nothing
  18.  
  19. void setUpGame(){ // setup the game
  20.     int i = 0;
  21.     for(i = 0; i < 9; i++){
  22.         s[i].value = 0;
  23.         s[i].sign = ' ';
  24.     }
  25.     gamestate.moves=0;
  26.     printf("\nHi user! You're \"x\"! I'm \"o\"! Good Luck :)\n");
  27. }
  28.  
  29. void displayBoard(){// displays the game board
  30.     printf("\n %c | %c | %c\n", s[6].sign, s[7].sign, s[8].sign);
  31.     printf("-----------\n");
  32.     printf(" %c | %c | %c\n", s[3].sign, s[4].sign, s[5].sign);
  33.     printf("-----------\n");
  34.     printf(" %c | %c | %c\n\n", s[0].sign, s[1].sign, s[2].sign);
  35. }
  36.  
  37. void getHumanMove(){ // get move from human
  38.     int i;
  39.     while(1){
  40.         printf(">>:");
  41.         char line[255]; // input the move to play
  42.         fgets(line, sizeof(line), stdin);
  43.             while(sscanf(line, "%d", &i) != 1) { //1 match of defined specifier on input line
  44.             printf("Sorry, that's not a valid move!\n");
  45.             fgets(line, sizeof(line), stdin);
  46.         }
  47.         if(s[i-1].value != 0){printf("Sorry, That moves already been taken!\n\n");continue;}
  48.         break;
  49.     }
  50.     s[i-1].value = 1;
  51.     s[i-1].sign = 'x';
  52.     gamestate.moves++;
  53. }
  54.  
  55. int sum(int x, int y, int z){return(x*y*z);}
  56.  
  57. void getCompMove(){ // get the move from the computer
  58.     srand(time(NULL)); //seed the time
  59.     int t;
  60.     while(1){
  61.         t = (rand()%9)+1; //temporary
  62.         if(s[t-1].value != 0) continue;
  63.         else break;
  64.     }
  65.     s[t-1].value = 2;
  66.     s[t-1].sign = 'o';
  67. }
  68.  
  69. void checkWinner(){ // check the winner
  70.     int i;
  71.     for(i = 6; i < 9; i++){ // check cols
  72.         if((sum(s[i].value,s[i-3].value,s[i-6].value)) == 8){printf("The Winner is o!\n");gamestate.state=1;}
  73.         if((sum(s[i].value,s[i-3].value,s[i-6].value)) == 1){printf("The Winner is x!\n");gamestate.state=2;}  
  74.     }
  75.     for(i = 0; i < 7; i+=3){ // check rows
  76.         if((sum(s[i].value,s[i+1].value,s[i+2].value)) == 8){printf("The Winner is o!\n");gamestate.state=1;}
  77.         if((sum(s[i].value,s[i+1].value,s[i+2].value)) == 1){printf("The Winner is x!\n");gamestate.state=2;}
  78.     }
  79.     if((sum(s[0].value,s[4].value,s[8].value)) == 83){printf("The Winner is o!\n");gamestate.state=1;}
  80.     if((sum(s[0].value,s[4].value,s[8].value)) == 1){printf("The Winner is x!\n");gamestate.state=2;}
  81.     if((sum(s[2].value,s[4].value,s[6].value)) == 8){printf("The Winner is o!\n");gamestate.state=1;}
  82.     if((sum(s[2].value,s[4].value,s[6].value)) == 1){printf("The Winner is x!\n");gamestate.state=2;}
  83. }
  84.  
  85. void playGame(){ // start playing the game
  86.     gamestate.state = 3; //set-up the gamestate
  87.     srand(time(NULL));
  88.     int temp = (rand()%2) + 1;
  89.     if(temp == 2){ // if two comp goes first
  90.         temp = (rand()%2) + 1;
  91.         if(temp == 2){
  92.             s[4].value = 2; s[4].sign = 'o';
  93.             gamestate.moves++;
  94.         }else{
  95.             s[2].value = 2; s[2].sign = 'o';
  96.             gamestate.moves++;
  97.         }
  98.     }
  99.     displayBoard();
  100.     while(gamestate.state == 3){
  101.         if(gamestate.moves==9);
  102.             getHumanMove();
  103.         if(gamestate.moves==9 && gamestate.state ==3)displayBoard();
  104.         checkWinner(); 
  105.         if(gamestate.moves==9);
  106.             getCompMove();
  107.         checkWinner();
  108.         if(gamestate.state == 3 && gamestate.moves==9){
  109.             printf("The game is a tie :p\n");
  110.             break;
  111.         }
  112.         displayBoard();
  113.     }
  114. }
  115.  
  116. int main(int argc, const char *argv[]){
  117.     printf("Welcome to Tic Tac Toe\nby The Elite Noob\nEnter 1-9 To play a move, standard numpad\n1 is bottom-left, 9 is top-right\n");
  118.     while(1){ // while game is being played
  119.         printf("\nPress 1 to play a new game, or any other number to exit;\n>>:");
  120.         char line[255]; // input whether or not to play the game
  121.         fgets(line, sizeof(line), stdin);
  122.         int choice; // user's choice about playing or not  
  123.         while(sscanf(line, "%d", &choice) != 1) { //1 match of defined specifier on input line
  124.             printf("Sorry, that's not a valid option!\n");
  125.             fgets(line, sizeof(line), stdin);
  126.         }
  127.         if(choice == 1){
  128.             setUpGame(); // set's up the game
  129.             playGame(); // Play a Game
  130.         }else {break;} // exit the application
  131.     }
  132.     printf("\nThank's For playing!\nHave a good Day!\n");
  133.     return 0;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement