Advertisement
Guest User

tic tac toe cpp

a guest
Apr 30th, 2012
455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. #include "std_lib_facilities.h"
  2. int main()
  3. {
  4. vector<int>board;
  5. vector<int>ex;
  6. vector<int>oh;
  7. int num;
  8. int over=0;
  9. int xscore=0;
  10. int oscore=0;
  11. string again="y";
  12. cout<<endl;
  13. for(int i=0;i<10;++i)
  14. {
  15. ex.push_back(0);
  16. }
  17. for(int i=0;i<10;++i)
  18. {
  19. oh.push_back(0);
  20. }
  21. while(again=="y"||again=="Y")
  22. {
  23.  
  24. for(int i=0;i<10;++i)
  25. {
  26. board.push_back(i);
  27. if(i!=0)
  28. {
  29. cout<<" "<<board[i]<<" ";
  30. if(i==3||i==6)
  31. cout<<"\n ---------\n";
  32. else if(i==9)
  33. cout<<endl;
  34. else
  35. cout<<"|";
  36. }
  37. }
  38. while(over==0)
  39. {
  40. if(over==0)
  41. {
  42. num=0;
  43. cout<<"\n\nX turn: ";
  44. cin>>num;
  45. while(num>9||num<1||oh[num]==1||ex[num]==1)
  46. {
  47. cout<<"\nCan't move there!\n";
  48. cout<<"\n\nX turn: ";
  49. cin>>num;
  50. }
  51. ex[num]=1;
  52. cout<<"\n\n\n\n";
  53. for(int i=0;i<10;++i)
  54. {
  55. board.push_back(i);
  56. if(i!=0)
  57. {
  58. if(ex[i]==1)
  59. cout<<" X ";
  60. else if(oh[i]==1)
  61. cout<<" O ";
  62. else
  63. cout<<" "<<board[i]<<" ";
  64. if(i==3||i==6)
  65. cout<<"\n ---------\n";
  66. else if(i==9)
  67. cout<<endl;
  68. else
  69. cout<<"|";
  70. }
  71. }
  72. }
  73. if((ex[1]==1&&ex[2]==1&&ex[3]==1)||(ex[4]==1&&ex[5]==1&&ex[6]==1)||(ex[7]==1&&ex[8]==1&&ex[9]==1)||(ex[1]==1&&ex[4]==1&&ex[7]==1)||(ex[2]==1&&ex[5]==1&&ex[8]==1)||(ex[3]==1&&ex[6]==1&&ex[9]==1)||(ex[1]==1&&ex[5]==1&&ex[9]==1)||(ex[3]==1&&ex[5]==1&&ex[7]==1))
  74. over=1;
  75. if((ex[1]==1||oh[1]==1)&&(ex[2]==1||oh[2]==1)&&(ex[3]==1||oh[3]==1)&&(ex[4]==1||oh[4]==1)&&(ex[5]==1||oh[5]==1)&&(ex[6]==1||oh[6]==1)&&(ex[7]==1||oh[7]==1)&&(ex[8]==1||oh[8]==1)&&(ex[9]==1||oh[9]==1))
  76. over=3;
  77. if(over==0)
  78. {
  79. num=0;
  80. cout<<"\n\nO turn: ";
  81. cin>>num;
  82. while(num>9||num<1||oh[num]==1||ex[num]==1)
  83. {
  84. cout<<"\nCan't move there!\n";
  85. cout<<"\n\nO turn: ";
  86. cin>>num;
  87. }
  88. oh[num]=1;
  89. cout<<"\n\n\n\n";
  90. for(int i=0;i<10;++i)
  91. {
  92. board.push_back(i);
  93. if(i!=0)
  94. {
  95. if(ex[i]==1)
  96. cout<<" X ";
  97. else if(oh[i]==1)
  98. cout<<" O ";
  99. else
  100. cout<<" "<<board[i]<<" ";
  101. if(i==3||i==6)
  102. cout<<"\n ---------\n";
  103. else if(i==9)
  104. cout<<endl;
  105. else
  106. cout<<"|";
  107. }
  108. }
  109. }
  110. if((oh[1]==1&&oh[2]==1&&oh[3]==1)||(oh[4]==1&&oh[5]==1&&oh[6]==1)||(oh[7]==1&&oh[8]==1&&oh[9]==1)||(oh[1]==1&&oh[4]==1&&oh[7]==1)||(oh[2]==1&&oh[5]==1&&oh[8]==1)||(oh[3]==1&&oh[6]==1&&oh[9]==1)||(oh[1]==1&&oh[5]==1&&oh[9]==1)||(oh[3]==1&&oh[5]==1&&oh[7]==1))
  111. over=2;
  112. }
  113. if(over==1)
  114. {
  115. xscore+=1;
  116. cout<<"\nX wins!!!\n\nX: "<<xscore<<" O: "<<oscore<<"\n\nWould you like to play again?\n(Y/N): ";
  117. }
  118. else if(over==2)
  119. {
  120. oscore+=1;
  121. cout<<"\nO wins!!!\n\nX: "<<xscore<<" O: "<<oscore<<"\n\nWould you like to play again?\n(Y/N): ";
  122. }
  123. else
  124. {
  125. cout<<"\nIt's a Tie!!!\n\nX: "<<xscore<<" O: "<<oscore<<"\n\nWould you like to play again?\n(Y/N): ";
  126. }
  127. cin>>again;
  128. if(again=="n"||again=="N")
  129. cout<<"\n\nThanks for Playing!";
  130. else
  131. over=0;
  132. while(again!="Y"&&again!="y"&&again!="N"&&again!="n")
  133. {
  134. cout<<"Please type either a 'Y' or a 'N': ";
  135. cin>>again;
  136. if(again=="n"||again=="N")
  137. cout<<"\nThanks for Playing!";
  138. else if(again=="y"||again=="Y")
  139. over=0;
  140. }
  141. cout<<"\n\n";
  142. for(int i=0;i<10;++i)
  143. {
  144. ex[i]=0;
  145. oh[i]=0;
  146. }
  147. }
  148. cout<<"\n\n";
  149. keep_window_open();
  150. return 0;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement