Advertisement
theelitenoob

Improved Tic Tac Toe

Oct 9th, 2013
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.48 KB | None | 0 0
  1. #include <stdio.h>  // Tic-Tac-Toe (Random Moves)
  2. #include <stdlib.h> // Code by: The Elite Noob
  3. #include <time.h>
  4.  
  5. int clean();
  6. void display(int* ar);
  7. void getinput(int(*validity)(int* ar, int l), int* ar); // (int l is location)
  8. int checkwin(int* ar);
  9. int validmove(int* ar, int place);
  10. void aimove(int(*validity)(int* ar, int l), int* ar);
  11.  
  12. int main (int argc, char *argv[]){
  13.     int board[] = {32, 32, 32, 32, 32, 32, 32, 32, 32}; // The board (ascii values), 32 is blank, 111 is o, and 120 is x(player);
  14.     display(board);
  15.     while(checkwin(board) == -1){
  16.         getinput(validmove, board);
  17.         display(board);
  18.         if(checkwin(board) != -1)
  19.             break;
  20.         aimove(validmove, board);
  21.         display(board);
  22.         if(checkwin(board) != -1)
  23.             break;
  24.     }
  25.     printf("%c wins!\n", checkwin(board));
  26.     return 0;
  27. }
  28.  
  29. void aimove(int(*validity)(int* ar, int l), int* ar){
  30.     int r;
  31.     srand(time(NULL));
  32.     do{
  33.         r = (rand() % 10) + 1;  // Random num from 1 to 9
  34.     }while((*validity)(ar, r) == -1); // Check if move is valid (Yay function pointers)
  35.     ar[r-1] = 111; // AI move..
  36. }
  37.  
  38. void getinput(int(*validity)(int* ar, int l), int* ar){ // Get and sanatize input
  39.     char c; // First get the input, then make sure it's a valid move... c is used for garbage stuff
  40.     int input;
  41.     do{
  42.         printf("\nEnter move: ");
  43.     }while(!(((scanf("%d%c", &input, &c)!=2 || c!='\n') && clean()) ||input<1 || input>9) && ((*validity)(ar, input) == -1)); // Magic.
  44.     ar[input-1] = 120; // Place/actually set the board piece
  45. }
  46.  
  47. int validmove(int* ar, int place){ // If move is not valid, return -1
  48.     if((ar[place-1] == 111)||(ar[place-1] == 120)){
  49.         printf("\nInvalid move.");
  50.         return -1;
  51.     }
  52.     return 1;
  53. }
  54.  
  55. void display(int* ar){ // Display the board
  56.     int i; // Iterator
  57.     printf("\n");
  58.     for (i = 0; i < 7; i+=3){
  59.         printf("%c | %c | %c\n", ar[i], ar[i+1], ar[i+2]);
  60.         if(i < 4)
  61.             printf("----------\n");
  62.     }
  63. }
  64.  
  65. int checkwin(int* ar){ // Check if we have any winner yet..
  66.     int i; // Iterator
  67.     for(i = 0; i < 7; i+=3) // Check rows
  68.         if((ar[i] == ar[i+1]) && (ar[i] == ar[i+2]) && (ar[i] != 32))
  69.             return ar[i];
  70.     for(i = 0; i < 3; i++)  // Check cols
  71.         if((ar[i] == ar[i+3]) && (ar[i] == ar[i+6]) && (ar[i] != 32))
  72.             return ar[i];
  73.     if((ar[0] == ar[4]) && (ar[0] == ar[8]) && (ar[0] != 32))   // Check diags
  74.         return ar[0];
  75.     if((ar[2] == ar[4]) && (ar[2] == ar[6]) && (ar[2] != 32))   // Check diags
  76.         return ar[2];
  77.     return -1; // No winner yet.
  78. }
  79.  
  80. int clean(){ // Flushes input
  81.     while (getchar()!='\n');
  82.     return 1;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement