Advertisement
Guest User

Untitled

a guest
Apr 26th, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <cstring>
  4.  
  5. using namespace std;
  6. char tabel[4][4];
  7. bool game, corect;
  8. int Player, Player1, pctO, pctX;
  9.  
  10. void display()
  11. {
  12. system("cls");
  13. for(int i=1;i<=3;i++)
  14. {
  15. for(int j=1;j<=3;j++)
  16. {
  17. cout<<tabel[i][j]<<" ";
  18. } cout<<endl;
  19. }
  20. }
  21.  
  22. void winner(string winner)
  23. { char raspuns[10] = " ";
  24. display();
  25. cout<<endl<<"Winner: "<<winner;
  26. game = true;
  27. exit(0);
  28. }
  29.  
  30. void checkForWin()
  31. {
  32.  
  33. for(int i=1;i<=3;i++)
  34. {
  35. for(int j=1;j<=3;j++){
  36. if(tabel[i][j]== 'O')
  37. pctO++;
  38. else if(tabel[i][j]== 'X')
  39. pctX++;
  40. }
  41. if(pctO == 3) winner("O");
  42. if(pctX == 3) winner("X");
  43. pctX = 0, pctO = 0;
  44. }
  45. for(int i=1;i<=3;i++)
  46. {
  47. for(int j=1;j<=3;j++){
  48. if(tabel[j][i]== 'O')
  49. pctO++;
  50. else if(tabel[j][i]== 'X')
  51. pctX++;
  52. }
  53. if(pctO == 3) winner("O");
  54. if(pctX == 3) winner("X");
  55. pctX = 0, pctO = 0;
  56. }
  57.  
  58. for(int j=1;j<=3;j++){
  59. if(tabel[j][j]== 'O')
  60. pctO++;
  61. else if(tabel[j][j]== 'X')
  62. pctX++;
  63. }
  64. if(pctO == 3) winner("O");
  65. if(pctX == 3) winner("X");
  66. pctX = 0, pctO = 0;
  67.  
  68. for(int j=3;j>=1;j--){
  69. if(tabel[3-j+1][j]== 'O')
  70. pctO++;
  71. else if(tabel[3-j+1][j]== 'X')
  72. pctX++;
  73. }
  74.  
  75. if(pctO == 3) winner("O");
  76. if(pctX == 3) winner("X");
  77. pctX = 0, pctO = 0;
  78.  
  79.  
  80. }
  81. int main()
  82. {
  83. for(int i=1;i<=3;i++)
  84. for(int j=1;j<=3;j++)
  85. tabel[i][j]=(char)176;
  86. while(!game)
  87. {
  88. display();
  89. while(!corect)
  90. {
  91. cout<<"X:"<<" ";
  92. cin>>Player>>Player1;
  93. if(tabel[Player][Player1]!=(char)176 || Player<1 || Player1<1 || Player>3 || Player1>3 ) cout<<endl<<"Invalid position."<<endl;
  94. else
  95. {
  96. tabel[Player][Player1] = 'X';
  97. corect = true;
  98. }
  99. }
  100. corect = false;
  101. checkForWin();
  102. while(!corect)
  103. {
  104. cout<<"O:"<<" ";
  105. cin>>Player>>Player1;
  106. if(tabel[Player][Player1]!=(char)176 || Player<1 || Player1<1 || Player>3 || Player1>3 ) cout<<endl<<"Invalid position."<<endl;
  107. else{
  108. tabel[Player][Player1] = 'O';
  109. corect = true;
  110. }
  111. }
  112. corect = false;
  113. checkForWin();
  114. }
  115. return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement