Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. /*********************************************
  2. BLOG.MAJIDKN.COM
  3. -MAJID BLOG
  4. **********************************************/
  5.  
  6. #include <iostream>
  7. using namespace std;
  8.  
  9. char square[10] = {'o','1','2','3','4','5','6','7','8','9'};
  10.  
  11. int checkwin();
  12. void board();
  13.  
  14. int main()
  15. {
  16. int player = 1,i,choice;
  17.  
  18. char mark;
  19. do
  20. {
  21. board();
  22. player=(player%2)?1:2;
  23.  
  24. cout << "Player " << player << ", enter a number: ";
  25. cin >> choice;
  26.  
  27. mark=(player == 1) ? 'X' : 'O';
  28.  
  29. if (choice == 1 && square[1] == '1')
  30.  
  31. square[1] = mark;
  32. else if (choice == 2 && square[2] == '2')
  33.  
  34. square[2] = mark;
  35. else if (choice == 3 && square[3] == '3')
  36.  
  37. square[3] = mark;
  38. else if (choice == 4 && square[4] == '4')
  39.  
  40. square[4] = mark;
  41. else if (choice == 5 && square[5] == '5')
  42.  
  43. square[5] = mark;
  44. else if (choice == 6 && square[6] == '6')
  45.  
  46. square[6] = mark;
  47. else if (choice == 7 && square[7] == '7')
  48.  
  49. square[7] = mark;
  50. else if (choice == 8 && square[8] == '8')
  51.  
  52. square[8] = mark;
  53. else if (choice == 9 && square[9] == '9')
  54.  
  55. square[9] = mark;
  56. else
  57. {
  58. cout<<"Invalid move ";
  59.  
  60. player--;
  61. cin.ignore();
  62. cin.get();
  63. }
  64. i=checkwin();
  65.  
  66. player++;
  67. }while(i==-1);
  68. board();
  69. if(i==1)
  70.  
  71. cout<<"==>\aPlayer "<<--player<<" win ";
  72. else
  73. cout<<"==>\aGame draw";
  74.  
  75. cin.ignore();
  76. cin.get();
  77. return 0;
  78. }
  79.  
  80. /*********************************************
  81. FUNCTION TO RETURN GAME STATUS
  82. 1 FOR GAME IS OVER WITH RESULT
  83. -1 FOR GAME IS IN PROGRESS
  84. O GAME IS OVER AND NO RESULT
  85. **********************************************/
  86.  
  87. int checkwin()
  88. {
  89. if (square[1] == square[2] && square[2] == square[3])
  90.  
  91. return 1;
  92. else if (square[4] == square[5] && square[5] == square[6])
  93.  
  94. return 1;
  95. else if (square[7] == square[8] && square[8] == square[9])
  96.  
  97. return 1;
  98. else if (square[1] == square[4] && square[4] == square[7])
  99.  
  100. return 1;
  101. else if (square[2] == square[5] && square[5] == square[8])
  102.  
  103. return 1;
  104. else if (square[3] == square[6] && square[6] == square[9])
  105.  
  106. return 1;
  107. else if (square[1] == square[5] && square[5] == square[9])
  108.  
  109. return 1;
  110. else if (square[3] == square[5] && square[5] == square[7])
  111.  
  112. return 1;
  113. else if (square[1] != '1' && square[2] != '2' && square[3] != '3'
  114. && square[4] != '4' && square[5] != '5' && square[6] != '6'
  115. && square[7] != '7' && square[8] != '8' && square[9] != '9')
  116.  
  117. return 0;
  118. else
  119. return -1;
  120. }
  121.  
  122.  
  123. /*******************************************************************
  124. FUNCTION TO DRAW BOARD OF TIC TAC TOE WITH PLAYERS MARK
  125. ********************************************************************/
  126.  
  127.  
  128. void board()
  129. {
  130. system("cls");
  131. cout << "\n\n\tTic Tac Toe\n\n";
  132.  
  133. cout << "Player 1 (X) - Player 2 (O)" << endl << endl;
  134. cout << endl;
  135.  
  136. cout << " | | " << endl;
  137. cout << " " << square[1] << " | " << square[2] << " | " << square[3] << endl;
  138.  
  139. cout << "_____|_____|_____" << endl;
  140. cout << " | | " << endl;
  141.  
  142. cout << " " << square[4] << " | " << square[5] << " | " << square[6] << endl;
  143.  
  144. cout << "_____|_____|_____" << endl;
  145. cout << " | | " << endl;
  146.  
  147. cout << " " << square[7] << " | " << square[8] << " | " << square[9] << endl;
  148.  
  149. cout << " | | " << endl << endl;
  150. }
  151.  
  152. /*******************************************************************
  153. END OF PROJECT
  154. ********************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement