Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.02 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. const int max_array =10;
  6.  
  7.     // fall til ad reikna move x sem kallar a sig sjalft ef illegal move
  8.  
  9.     void x_move(char tic_tac_toe[], char x_input) {
  10.     cout << "X position: ";
  11.     cin >> x_input;
  12.  
  13.         if (x_input =1) {
  14.             if (tic_tac_toe[1]=49) {
  15.                 tic_tac_toe[1]='X';
  16.         }
  17.             else {
  18.                 cout << "Illegal move!" << endl;
  19.                 x_move(tic_tac_toe, x_input);
  20.         }
  21.     }
  22.         else if (x_input =2) {
  23.             if (tic_tac_toe[2]=50) {
  24.                 tic_tac_toe[2]='X';
  25.         }
  26.             else {
  27.                 cout << "Illegal move!" << endl;
  28.                 x_move(tic_tac_toe, x_input);
  29.             }
  30.     }
  31.         else if (x_input =3) {
  32.             if (tic_tac_toe[3]=51) {
  33.                 tic_tac_toe[3]='X';
  34.         }
  35.             else {
  36.                 cout << "Illegal move!" << endl;
  37.                 x_move(tic_tac_toe, x_input);
  38.             }
  39.     }
  40.         else if (x_input =4) {
  41.             if (tic_tac_toe[4]=52) {
  42.                     tic_tac_toe[4]='X';
  43.         }
  44.             else {
  45.                 cout << "Illegal move!" << endl;
  46.                 x_move(tic_tac_toe, x_input);
  47.             }
  48.     }
  49.         else if (x_input =5) {
  50.             if (tic_tac_toe[5]=53) {
  51.                     tic_tac_toe[5]='X';
  52.         }
  53.             else {
  54.                 cout << "Illegal move!" << endl;
  55.                 x_move(tic_tac_toe, x_input);
  56.             }
  57.         }
  58.         else if (x_input =6) {
  59.             if (tic_tac_toe[6]=54) {
  60.                     tic_tac_toe[6]='X';
  61.         }
  62.             else {
  63.                 cout << "Illegal move!" << endl;
  64.                 x_move(tic_tac_toe, x_input);
  65.         }
  66.     }
  67.         else if (x_input =7) {
  68.             if (tic_tac_toe[7]=55) {
  69.                 tic_tac_toe[7]='X';
  70.         }
  71.             else {
  72.                 cout << "Illegal move!" << endl;
  73.                 x_move(tic_tac_toe, x_input);
  74.             }
  75.     }
  76.         else if (x_input =8) {
  77.             if (tic_tac_toe[8]=56) {
  78.                 tic_tac_toe[8]='X';
  79.         }
  80.             else {
  81.                 cout << "Illegal move!" << endl;
  82.                 x_move(tic_tac_toe, x_input);
  83.             }
  84.     }
  85.         else if (x_input =9) {
  86.             if (tic_tac_toe[9]=57) {
  87.                 tic_tac_toe[9]='X';
  88.         }
  89.             else {
  90.                 cout << "Illegal move!" << endl;
  91.                 x_move(tic_tac_toe, x_input);
  92.  
  93.             }
  94.     }
  95.  
  96.     }
  97. // alveg eins fall fyrir O
  98.         void o_move(char tic_tac_toe[], char o_input) {
  99.     cout << "O position: ";
  100.     cin >> o_input;
  101.  
  102.         if (o_input =1) {
  103.             if (tic_tac_toe[1]=49) {
  104.                 tic_tac_toe[1]='O';
  105.         }
  106.             else {
  107.                 cout << "Illegal move!" << endl;
  108.                 o_move(tic_tac_toe, o_input);
  109.         }
  110.     }
  111.         else if (o_input =2) {
  112.             if (tic_tac_toe[2]=50) {
  113.                 tic_tac_toe[2]='O';
  114.         }
  115.             else {
  116.                 cout << "Illegal move!" << endl;
  117.                 o_move(tic_tac_toe, o_input);
  118.             }
  119.     }
  120.         else if (o_input =3) {
  121.             if (tic_tac_toe[3]=51) {
  122.                     tic_tac_toe[3]='O';
  123.         }
  124.             else {
  125.                 cout << "Illegal move!" << endl;
  126.                 o_move(tic_tac_toe, o_input);
  127.             }
  128.     }
  129.         else if (o_input =4) {
  130.             if (tic_tac_toe[4]=52) {
  131.                     tic_tac_toe[4]='O';
  132.         }
  133.             else {
  134.                 cout << "Illegal move!" << endl;
  135.                 o_move(tic_tac_toe, o_input);
  136.             }
  137.     }
  138.         else if (o_input =5) {
  139.             if (tic_tac_toe[5]=53) {
  140.                     tic_tac_toe[5]='O';
  141.         }
  142.             else {
  143.                 cout << "Illegal move!" << endl;
  144.                 o_move(tic_tac_toe, o_input);
  145.             }
  146.         }
  147.  
  148.         else if (o_input =6) {
  149.             if (tic_tac_toe[6]=54) {
  150.                     tic_tac_toe[6]='O';
  151.         }
  152.             else {
  153.                 cout << "Illegal move!" << endl;
  154.                 o_move(tic_tac_toe, o_input);
  155.         }
  156.     }
  157.         else if (o_input =7) {
  158.             if (tic_tac_toe[7]=55) {
  159.                 tic_tac_toe[7]='O';
  160.         }
  161.             else {
  162.                 cout << "Illegal move!" << endl;
  163.                 o_move(tic_tac_toe, o_input);
  164.             }
  165.     }
  166.         else if (o_input =8) {
  167.             if (tic_tac_toe[8]=56) {
  168.                 tic_tac_toe[8]='O';
  169.         }
  170.             else {
  171.                 cout << "Illegal move!" << endl;
  172.                 o_move(tic_tac_toe, o_input);
  173.             }
  174.     }
  175.         else if (o_input =9) {
  176.             if (tic_tac_toe[9]=57) {
  177.                 tic_tac_toe[9]='O';
  178.         }
  179.             else {
  180.                 cout << "Illegal move!" << endl;
  181.                 o_move(tic_tac_toe, o_input);
  182.             }
  183.     }
  184.  
  185.     }
  186. //fall til ad tjekka hvort x se buinn ad vinna
  187. bool did_x_win (char tic_tac_toe[]) {
  188.     if (tic_tac_toe[1]='X' && tic_tac_toe[2]='X' && tic_tac_toe[3]='X'){
  189.         return true;
  190.     }
  191.     else if (tic_tac_toe[4]='X' && tic_tac_toe[5]='X' && tic_tac_toe[6]='X'){
  192.         return true;
  193.     }
  194.     else if (tic_tac_toe[7]='X' && tic_tac_toe[8]='X' && tic_tac_toe[9]='X'){
  195.         return true;
  196.     }
  197.     else if (tic_tac_toe[1]='X' && tic_tac_toe[4]='X' && tic_tac_toe[7]='X'){
  198.         return true;
  199.     }
  200.     else if (tic_tac_toe[2]='X' && tic_tac_toe[5]='X' && tic_tac_toe[8]='X'){
  201.         return true;
  202.     }
  203.     else if (tic_tac_toe[3]='X' && tic_tac_toe[6]='X' && tic_tac_toe[9]='X'){
  204.         return true;
  205.     }
  206.     else if (tic_tac_toe[1]='X' && tic_tac_toe[5]='X' && tic_tac_toe[9]='X'){
  207.         return true;
  208.     }
  209.     else if (tic_tac_toe[3]='X' && tic_tac_toe[5]='X' && tic_tac_toe[7]='X'){
  210.         return true;
  211.     }
  212.     else {
  213.         return false;
  214.     }
  215.  
  216. }
  217.  
  218. // alveg eins fall fyrir O
  219. bool did_o_win (char tic_tac_toe[]) {
  220.     if (tic_tac_toe[1]='O' && tic_tac_toe[2]='O' && tic_tac_toe[3]='O'){
  221.         return true;
  222.     }
  223.     else if (tic_tac_toe[4]='O' && tic_tac_toe[5]='O' && tic_tac_toe[6]='O'){
  224.         return true;
  225.     }
  226.     else if (tic_tac_toe[7]='O' && tic_tac_toe[8]='O' && tic_tac_toe[9]='O'){
  227.         return true;
  228.     }
  229.     else if (tic_tac_toe[1]='O' && tic_tac_toe[4]='O' && tic_tac_toe[7]='O'){
  230.         return true;
  231.     }
  232.     else if (tic_tac_toe[2]='O' && tic_tac_toe[5]='O' && tic_tac_toe[8]='O'){
  233.         return true;
  234.     }
  235.     else if (tic_tac_toe[3]='O' && tic_tac_toe[6]='O' && tic_tac_toe[9]='O'){
  236.         return true;
  237.     }
  238.     else if (tic_tac_toe[1]='O' && tic_tac_toe[5]='O' && tic_tac_toe[9]='O'){
  239.         return true;
  240.     }
  241.     else if (tic_tac_toe[3]='O' && tic_tac_toe[5]='O' && tic_tac_toe[7]='O'){
  242.         return true;
  243.     }
  244.     else {
  245.         return false;
  246.     }
  247.  
  248. }
  249.  
  250. // fall til ad prenta fylkid
  251. void print_board (char tic_tac_toe[]){
  252.     cout << tic_tac_toe[1] << " " << tic_tac_toe[2] << " " << tic_tac_toe[3] << endl;
  253.     cout << tic_tac_toe[4] << " " << tic_tac_toe[5] << " " << tic_tac_toe[6] << endl;
  254.     cout << tic_tac_toe[7] << " " << tic_tac_toe[8] << " " << tic_tac_toe[9] << endl;
  255. }
  256.  
  257. int main()
  258. {
  259.     // búa til fylki af staerd 10 og fylla thad (hunsa 0)
  260.     char tic_tac_toe[max_array];
  261.     tic_tac_toe[1]=49;
  262.     tic_tac_toe[2]=50;
  263.     tic_tac_toe[3]=51;
  264.     tic_tac_toe[4]=52;
  265.     tic_tac_toe[5]=53;
  266.     tic_tac_toe[6]=54;
  267.     tic_tac_toe[7]=55;
  268.     tic_tac_toe[8]=56;
  269.     tic_tac_toe[9]=57;
  270.  
  271.     print_board(tic_tac_toe);
  272.  
  273.  
  274.     //búa til bool gildi sem tjekkar hvort leikurinn sé búinn
  275.     bool is_game_over = false;
  276.  
  277.     // prompta x fyrst og svo o, muna ad tjekka bool á milli og eftir o
  278.     char x_input;
  279.     char o_input;
  280.  
  281.  
  282.     cout << tic_tac_toe[1] << " " << tic_tac_toe[2] << " " << tic_tac_toe[3] << endl;
  283.     cout << tic_tac_toe[4] << " " << tic_tac_toe[5] << " " << tic_tac_toe[6] << endl;
  284.     cout << tic_tac_toe[7] << " " << tic_tac_toe[8] << " " << tic_tac_toe[9] << endl;
  285.  
  286.    
  287.  
  288.  
  289.    
  290.  
  291.  
  292. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement