Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3.  
  4. using namespace std;
  5.  
  6. char n[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', }; // numbers on the game board
  7. char player = 'X'; // player symbol
  8. int win = 0;
  9. int ms[10] = {0,2,7,6,9,5,1,4,3,8}; //magic square template
  10. int wins[10]; // magic square to work out win
  11.  
  12. void debug()
  13. {
  14.     cout << ms[1] << ms[2] << ms[3] << endl;
  15.     cout << ms[4] << ms[5] << ms[6] << endl;
  16.     cout << ms[7] << ms[8] << ms[9] << endl;
  17.  
  18.     cout << wins[1] << wins[2] << wins[3] << endl;
  19.     cout << wins[4] << wins[5] << wins[6] << endl;
  20.     cout << wins[7] << wins[8] << wins[9] << endl;
  21. }
  22.  
  23. void checkwin()
  24. {
  25.     if(
  26.     (wins[1] + wins[2] + wins[3] == 15) ||
  27.     (wins[4] + wins[5] + wins[6] == 15) ||
  28.     (wins[7] + wins[8] + wins[9] == 15) ||
  29.     (wins[1] + wins[4] + wins[7] == 15) ||
  30.     (wins[2] + wins[5] + wins[8] == 15) ||
  31.     (wins[3] + wins[6] + wins[9] == 15) ||
  32.     (wins[1] + wins[5] + wins[9] == 15) ||
  33.     (wins[3] + wins[5] + wins[7] == 15))
  34.  
  35.     {
  36.         win = 1;
  37.     }
  38.  
  39.     else if(
  40.     (wins[1] + wins[2] + wins[3] == -15) ||
  41.     (wins[4] + wins[5] + wins[6] == -15) ||
  42.     (wins[7] + wins[8] + wins[9] == -15) ||
  43.     (wins[1] + wins[4] + wins[7] == -15) ||
  44.     (wins[2] + wins[5] + wins[8] == -15) ||
  45.     (wins[3] + wins[6] + wins[9] == -15) ||
  46.     (wins[1] + wins[5] + wins[9] == -15) ||
  47.     (wins[3] + wins[5] + wins[7] == -15))
  48.  
  49.     {
  50.         win = 1; // will change this later for score
  51.     }
  52. }
  53.  
  54. void turn()
  55. {
  56.     if (player == 'X')
  57.     {
  58.         player = 'O';
  59.     }
  60.     else
  61.     {
  62.         player = 'X';
  63.     }
  64. }
  65.  
  66. void choice()
  67. {
  68.     int a;
  69.     cout << "Enter a number:" << endl;
  70.     cin >> a;
  71.  
  72.     if( isdigit(n[a]) == 1 ) // checks to see if square is already taken
  73.     {
  74.  
  75.         if((a > 0) && (a < 10)) // checks to see if valid choice
  76.         {
  77.             n[a] = player;
  78.  
  79.             if(player == 'X')
  80.             {
  81.                 wins[a] = wins[a] + ms[a]; // if winning magic square tiles add up to +15, X wins
  82.             }
  83.             else
  84.             {
  85.                 wins[a] = wins[a] - ms[a]; // if they add up to -15 O wins.
  86.             }
  87.         }
  88.                 turn();
  89.     }
  90.  
  91.  
  92.         else
  93.     {
  94.         cout << "try again" << endl;
  95.         Sleep(1500);
  96.     }
  97.  
  98.  
  99. }
  100.  
  101. void board()
  102. {
  103.     system("CLS");
  104.     cout << "     |   |   " << endl; // gameboard start
  105.     cout << "   " << n[1] << " | " << n[2] << " | " << n[3] << "  " << endl;
  106.     cout << "  ___|___|___" << endl;
  107.     cout << "     |   |   " << endl;
  108.     cout << "   " << n[4] << " | " << n[5] << " | " << n[6] << "  " << endl;
  109.     cout << "  ___|___|___" << endl;
  110.     cout << "     |   |   " << endl;
  111.     cout << "   " << n[7] << " | " << n[8] << " | " << n[9] << "  " << endl;
  112.     cout << "     |   |   " << endl; // gameboard end
  113.     cout << endl;
  114. }
  115.  
  116. int main()
  117. {
  118.     board();
  119.     //debug();
  120.  
  121.     while (win == 0)
  122.     {
  123.     choice();
  124.     board();
  125.     //debug();
  126.     checkwin();
  127.     }
  128.     turn();
  129.     system("CLS");
  130.     cout << "Player " << player << " wins!" << endl;
  131.  
  132.     system("pause");
  133.     return 0;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement