Advertisement
Beyrin

Tic Tac Toe

Mar 9th, 2018
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <windows.h>
  4. typedef enum {false, true } bool;
  5. char square[10] = { 'o', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
  6.  
  7. int checkwin();
  8. void board();
  9.  
  10. int main()
  11. {
  12.     int PVPOneWins = 0, PVPTwoWins = 0,PVEOneWins= 0, AIWins = 0;
  13.     char GameType;
  14.     do
  15.     {
  16.  
  17.         printf("Choose the game session type:\n 1 for pvp\n 2 for pve\n 3 for stats\n 4 for exit");
  18.         int player = 1, choice;
  19.         int i;
  20.         char stop = '1';
  21.         char mark;
  22.         GameType = getch();
  23.         //OCHEN MNOGO BUKAV RADI PVP
  24.         if ( GameType == '1')
  25.         //OCHEN MNOGO BUKAV RADI PVP
  26.         //choice = getch();
  27.         while ( stop != '0')
  28.         {
  29.             do
  30.             {
  31.  
  32.                 COORD crd = {70, 20};
  33.                 CONSOLE_SCREEN_BUFFER_INFO csbi;
  34.                 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  35.                 GetConsoleScreenBufferInfo(hStdOut, &csbi);
  36.                 if ((csbi.srWindow.Right - csbi.srWindow.Left) != 70 || (csbi.srWindow.Bottom - csbi.srWindow.Top) !=20)
  37.                 {
  38.                     HANDLE out_handle = GetStdHandle(STD_OUTPUT_HANDLE);
  39.                     COORD crd = {70, 20};
  40.                     SMALL_RECT src = {0, 0, crd.X-1, crd.Y-1};
  41.                     SetConsoleWindowInfo(out_handle, true, &src);
  42.                     SetConsoleScreenBufferSize(out_handle, crd);
  43.                 }
  44.  
  45.                 board();
  46.                 printf("PVP MODE IS ON\nTo stop the game press '0' and any key\n");
  47.                 player = (player % 2) ? 1 : 2;
  48.                 printf("Player %d, enter a number:  ", player);
  49.                 choice = getch();
  50.                 mark = (player == 1) ? 'X' : 'O';
  51.                 if (choice == '0')
  52.                 {
  53.                     stop = '0';
  54.                     break;
  55.  
  56.                 }
  57.  
  58.  
  59.                 if (choice == '1' && square[1] == '1')
  60.                     square[1] = mark;
  61.  
  62.                 else if (choice == '2' && square[2] == '2')
  63.                     square[2] = mark;
  64.  
  65.                 else if (choice == '3' && square[3] == '3')
  66.                     square[3] = mark;
  67.  
  68.                 else if (choice == '4' && square[4] == '4')
  69.                     square[4] = mark;
  70.  
  71.                 else if (choice == '5' && square[5] == '5')
  72.                     square[5] = mark;
  73.  
  74.                 else if (choice == '6' && square[6] == '6')
  75.                     square[6] = mark;
  76.  
  77.                 else if (choice == '7' && square[7] == '7')
  78.                     square[7] = mark;
  79.  
  80.                 else if (choice == '8' && square[8] == '8')
  81.                     square[8] = mark;
  82.  
  83.                 else if (choice == '9' && square[9] == '9')
  84.                     square[9] = mark;
  85.  
  86.                 else
  87.                 {
  88.                     printf("Invalid move ");
  89.  
  90.                     player--;
  91.                     getch();
  92.                 }
  93.                 i = checkwin();
  94.  
  95.                 player++;
  96.             }
  97.             while (i ==  - 1);
  98.  
  99.             //PROVERKA KTO WIN
  100.             if (i == 1)
  101.             {
  102.                 board();
  103.                 if( player %2 == 1)
  104.                 PVPTwoWins++;
  105.                 else
  106.                 PVPOneWins++;
  107.                 printf("==>\aPlayer %d win\n Press any key to continue\n ", --player);
  108.  
  109.             }
  110.             if (i == 0)
  111.             {
  112.                 board();
  113.                 printf("==>\aGame draw");
  114.             }
  115.             //CHISTKA
  116.             getch();
  117.             for (i = 0; i != 10; i++)//PEREPISIVAYU MASSIV
  118.             {
  119.                 square[i] = i +'0';
  120.             }
  121.                 system("cls");
  122.         }
  123.  
  124.         //OCHEN MNOGO BUKAV RADI PVE
  125.         if (GameType == '2')
  126.         //OCHEN MNOGO BUKAV RADI PVE
  127.         while( stop != '0')
  128.         {
  129.             do
  130.             {
  131.  
  132.                     COORD crd = {70, 20};
  133.                     CONSOLE_SCREEN_BUFFER_INFO csbi;
  134.                     HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  135.                     GetConsoleScreenBufferInfo(hStdOut, &csbi);
  136.                     if ((csbi.srWindow.Right - csbi.srWindow.Left) != 70 || (csbi.srWindow.Bottom - csbi.srWindow.Top) !=20)
  137.                     {
  138.                         HANDLE out_handle = GetStdHandle(STD_OUTPUT_HANDLE);
  139.                         COORD crd = {70, 20};
  140.                         SMALL_RECT src = {0, 0, crd.X-1, crd.Y-1};
  141.                         SetConsoleWindowInfo(out_handle, true, &src);
  142.                         SetConsoleScreenBufferSize(out_handle, crd);
  143.                     }
  144.  
  145.                     board();
  146.                     printf("PVE MODE IS ON\nTo stop the game press '0' and any key\n");
  147.                     player = (player % 2) ? 1 : 2;
  148.                     printf("Player %d, enter a number:  ", player);
  149.                     if (player == 1 )
  150.                     {
  151.                         choice = getch();
  152.                     }
  153.                     else
  154.  
  155.                     choice = (rand()%9) +1 + '0';
  156.                     mark = (player == 1) ? 'X' : 'O';
  157.                     if (choice == '0')
  158.                     {
  159.                         stop = '0';
  160.                         break;
  161.  
  162.                     }
  163.                     if (choice == '1' && square[1] == '1')
  164.                         square[1] = mark;
  165.  
  166.                     else if (choice == '2' && square[2] == '2')
  167.                         square[2] = mark;
  168.  
  169.                     else if (choice == '3' && square[3] == '3')
  170.                         square[3] = mark;
  171.  
  172.                     else if (choice == '4' && square[4] == '4')
  173.                         square[4] = mark;
  174.  
  175.                     else if (choice == '5' && square[5] == '5')
  176.                         square[5] = mark;
  177.  
  178.                     else if (choice == '6' && square[6] == '6')
  179.                         square[6] = mark;
  180.  
  181.                     else if (choice == '7' && square[7] == '7')
  182.                         square[7] = mark;
  183.  
  184.                     else if (choice == '8' && square[8] == '8')
  185.                         square[8] = mark;
  186.  
  187.                     else if (choice == '9' && square[9] == '9')
  188.                         square[9] = mark;
  189.  
  190.                     else
  191.                     {
  192.                         printf("AI made a mistake- ");
  193.                         choice = (rand()%9) +1 + '0';
  194.                         player--;
  195.                         //getch();
  196.                     }
  197.                     i = checkwin();
  198.  
  199.                     player++;
  200.                 }
  201.                 while (i ==  - 1);
  202.                 //PROVERKA KTO WIN
  203.                 if (i == 1)
  204.                 {
  205.                     board();
  206.                 if ( player %2 == 1)
  207.                     {
  208.                         AIWins++;
  209.                     }
  210.                 else
  211.                     {
  212.                         PVEOneWins++;
  213.                     }
  214.                     printf("==>\aPlayer %d win Press any key to continue\n ", --player);
  215.                 }
  216.                 if (i == 0)
  217.                 {
  218.                     board();
  219.                     printf("==>\aGame draw");
  220.                 }
  221.                 //CHISTKA
  222.  
  223.                 getch();
  224.                 for (i = 0; i != 10; i++)//PEREPISIVAYU MASSIV
  225.                 {
  226.                     square[i] = i +'0';
  227.                 }
  228.                 system("cls");
  229.  
  230.  
  231.  
  232.         }
  233.         if (GameType == '3')
  234.         {
  235.             printf("\nPVP_1:%d\n", PVPOneWins);
  236.             printf("PVP_2:%d\n", PVPTwoWins);
  237.             printf("PVE:%d\n", PVEOneWins);
  238.             printf("AI:%d\n", AIWins);
  239.         }
  240.  
  241.         //VIHOD
  242.         if( GameType == '4')
  243.         {
  244.  
  245.         system("cls");
  246.         break;
  247.         }
  248.  
  249.  
  250.     }
  251.     while  (1==1);
  252.     return 0;
  253. }
  254.  
  255. /*********************************************
  256.  
  257. FUNCTION TO RETURN GAME STATUS
  258. 1 FOR GAME IS OVER WITH RESULT
  259. -1 FOR GAME IS IN PROGRESS
  260. O GAME IS OVER AND NO RESULT
  261.  **********************************************/
  262.  
  263. int checkwin()
  264. {
  265.     if (square[1] == square[2] && square[2] == square[3])
  266.         return 1;
  267.  
  268.     else if (square[4] == square[5] && square[5] == square[6])
  269.         return 1;
  270.  
  271.     else if (square[7] == square[8] && square[8] == square[9])
  272.         return 1;
  273.  
  274.     else if (square[1] == square[4] && square[4] == square[7])
  275.         return 1;
  276.  
  277.     else if (square[2] == square[5] && square[5] == square[8])
  278.         return 1;
  279.  
  280.     else if (square[3] == square[6] && square[6] == square[9])
  281.         return 1;
  282.  
  283.     else if (square[1] == square[5] && square[5] == square[9])
  284.         return 1;
  285.  
  286.     else if (square[3] == square[5] && square[5] == square[7])
  287.         return 1;
  288.  
  289.     else if (square[1] != '1' && square[2] != '2' && square[3] != '3' &&
  290.         square[4] != '4' && square[5] != '5' && square[6] != '6' && square[7]
  291.         != '7' && square[8] != '8' && square[9] != '9')
  292.  
  293.         return 0;
  294.     else
  295.         return  - 1;
  296. }
  297.  
  298.  
  299. /*******************************************************************
  300. FUNCTION TO DRAW BOARD OF TIC TAC TOE WITH PLAYERS MARK
  301.  ********************************************************************/
  302.  
  303.  
  304. void board()
  305. {
  306.  
  307.         system("cls");
  308.         HANDLE out_handle = GetStdHandle(STD_OUTPUT_HANDLE);
  309.         COORD crd = {70, 20};
  310.         SMALL_RECT src = {0, 0, crd.X-1, crd.Y-1};
  311.         SetConsoleWindowInfo(out_handle, true, &src);
  312.         SetConsoleScreenBufferSize(out_handle, crd);
  313.  
  314.     printf("\n\n\tTic Tac Toe\n\n");
  315.  
  316.     printf("Player 1 (X)  -  Player 2 (O)\n\n\n");
  317.  
  318.  
  319.     printf("     |     |     \n");
  320.     printf("  %c  |  %c  |  %c \n", square[1], square[2], square[3]);
  321.  
  322.     printf("_____|_____|_____\n");
  323.     printf("     |     |     \n");
  324.  
  325.     printf("  %c  |  %c  |  %c \n", square[4], square[5], square[6]);
  326.  
  327.     printf("_____|_____|_____\n");
  328.     printf("     |     |     \n");
  329.        /* HANDLE out_handle = GetStdHandle(STD_OUTPUT_HANDLE);
  330.         COORD crd = {100, 50};
  331.         SMALL_RECT src = {0, 0, crd.X-1, crd.Y-1};
  332.         //SetConsoleWindowInfo(out_handle, true, &src);
  333.         SetConsoleScreenBufferSize(out_handle, crd);
  334. */
  335.     printf("  %c  |  %c  |  %c \n", square[7], square[8], square[9]);
  336.  
  337.     printf("     |     |     \n\n");
  338. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement