Posted By

Desoxena on 01/03/11


Tagged

tictactoe


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

DrPepper


TIC-TAC-TOE


 / Published in: C++
 

A simple game of tictactoe; this was done in C++

  1. #include <string>
  2. #include <cstdlib>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. void ticTacToe()
  8. {
  9. int x = 4;
  10. string p1, p2;
  11. string l1 = " | |\n";
  12. string l2 = " | | \n";
  13. string l3 = " ----|----|----\n";
  14. string l4 = " | | \n";
  15. string l5 = " ----|----|----\n";
  16. string l6 = " | | \n";
  17. string l7 = " | |\n";
  18. cout << l1 << l2 << l3 << l4 << l5 << l6 << l7 << endl;
  19. cout << "player1 = X\nplayer2 = O" << endl;
  20. do{
  21. cin >> p1;
  22. if (p1 == "x1"||p1 == "1x")
  23. {
  24. system("cls");
  25. l2.replace(4, 1, "X");
  26. cout << l1 << l2 << l3 << l4 << l5 << l6 << l7 << endl;
  27. }
  28. else if(p1 == "x2"||p1=="2x")
  29. {
  30. system("cls");
  31. l2.replace(9, 1, "X");
  32. cout << l1 << l2 << l3 << l4 << l5 << l6 << l7 << endl;
  33. }
  34. else if(p1 == "x3"||p1 == "3x")
  35. {
  36. system("cls");
  37. l2.replace(14, 1, "X");
  38. cout << l1 << l2 << l3 << l4 << l5 << l6 << l7 << endl;
  39. }
  40. else if(p1 == "x4"||p1 == "4x")
  41. {
  42. system("cls");
  43. l4.replace(4, 1, "X");
  44. cout << l1 << l2 << l3 << l4 << l5 << l6 << l7 << endl;
  45. }
  46. else if(p1 == "x5"||p1 == "5x")
  47. {
  48. system("cls");
  49. l4.replace(9, 1, "X");
  50. cout << l1 << l2 << l3 << l4 << l5 << l6 << l7 << endl;
  51. }
  52. else if(p1 == "x6"||p1 == "6x")
  53. {
  54. system("cls");
  55. l4.replace(14, 1, "X");
  56. cout << l1 << l2 << l3 << l4 << l5 << l6 << l7 << endl;
  57. }
  58. else if(p1 == "x7"||p1 == "7x")
  59. {
  60. system("cls");
  61. l6.replace(4, 1, "X");
  62. cout << l1 << l2 << l3 << l4 << l5 << l6 << l7 << endl;
  63. }
  64. else if(p1 == "x8"||p1 == "8x")
  65. {
  66. system("cls");
  67. l6.replace(9, 1, "X");
  68. cout << l1 << l2 << l3 << l4 << l5 << l6 << l7 << endl;
  69. }
  70. else if(p1 == "x9"||p1 == "9x")
  71. {
  72. system("cls");
  73. l6.replace(14, 1, "X");
  74. cout << l1 << l2 << l3 << l4 << l5 << l6 << l7 << endl;
  75. }
  76. if (p1 == "o1"||p1 == "1o")
  77. {
  78. system("cls");
  79. l2.replace(4, 1, "O");
  80. cout << l1 << l2 << l3 << l4 << l5 << l6 << l7 << endl;
  81. }
  82. else if(p1 == "o2"||p1=="2o")
  83. {
  84. system("cls");
  85. l2.replace(9, 1, "O");
  86. cout << l1 << l2 << l3 << l4 << l5 << l6 << l7 << endl;
  87. }
  88. else if(p1 == "o3"||p1 == "3o")
  89. {
  90. system("cls");
  91. l2.replace(14, 1, "O");
  92. cout << l1 << l2 << l3 << l4 << l5 << l6 << l7 << endl;
  93. }
  94. else if(p1 == "o4"||p1 == "4o")
  95. {
  96. system("cls");
  97. l4.replace(4, 1, "O");
  98. cout << l1 << l2 << l3 << l4 << l5 << l6 << l7 << endl;
  99. }
  100. else if(p1 == "o5"||p1 == "5o")
  101. {
  102. system("cls");
  103. l4.replace(9, 1, "O");
  104. cout << l1 << l2 << l3 << l4 << l5 << l6 << l7 << endl;
  105. }
  106. else if(p1 == "o6"||p1 == "6o")
  107. {
  108. system("cls");
  109. l4.replace(14, 1, "O");
  110. cout << l1 << l2 << l3 << l4 << l5 << l6 << l7 << endl;
  111. }
  112. else if(p1 == "o7"||p1 == "7o")
  113. {
  114. system("cls");
  115. l6.replace(4, 1, "O");
  116. cout << l1 << l2 << l3 << l4 << l5 << l6 << l7 << endl;
  117. }
  118. else if(p1 == "o8"||p1 == "8o")
  119. {
  120. system("cls");
  121. l6.replace(9, 1, "O");
  122. cout << l1 << l2 << l3 << l4 << l5 << l6 << l7 << endl;
  123. }
  124. else if(p1 == "o9"||p1 == "9o")
  125. {
  126. system("cls");
  127. l6.replace(14, 1, "O");
  128. cout << l1 << l2 << l3 << l4 << l5 << l6 << l7 << endl;
  129. }
  130. else if(p1 == "newgame")
  131. {
  132. system("cls");
  133. ticTacToe();
  134. cout << "\n\n\n";
  135. }
  136. } while(x == 4);
  137. }
  138.  
  139.  
  140. void newGame()
  141. {
  142. ticTacToe();
  143. }
  144. int main()
  145. {
  146. string call;
  147. cout << "*-------------*" << endl;
  148. cout << "| TIC-TAC-TOE |" << endl;
  149. cout << "*-------------*" << endl;
  150. cin >> call;
  151. if (call == "newgame")
  152. {
  153. newGame();
  154. }
  155. }

Report this snippet  

You need to login to post a comment.