Advertisement
Guest User

Untitled

a guest
May 10th, 2013
631
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <time.h>
  4. #include <stdlib.h>
  5. #include <windows.h>
  6.  
  7. using namespace std;
  8.  
  9. int table[3][3];
  10. string drawXO[3][3];
  11. int xplayer,yplayer;
  12. int xcomp,ycomp;
  13. int xy;
  14. int srand1;
  15. bool isFull;
  16. bool playerwin,compwin;
  17. char restart;
  18.  
  19.  
  20. void computerplace();
  21. void drawtable();
  22. void playerplace();
  23. void showdata();
  24. void randomizing();
  25. void exiting();
  26. void tableIsFull();
  27. void checktable();
  28. void initialiseDrawXO();
  29. void checkanddraw();
  30. void checkwin();
  31. void gameloop();
  32.  
  33.  
  34. int main()
  35. {
  36.    srand(time(NULL));
  37.    initialiseDrawXO();
  38.    gameloop();
  39.  
  40.     return 0;
  41. }
  42.  
  43.  
  44. void gameloop(){
  45.     srand(time(NULL));
  46.    initialiseDrawXO();
  47.     while((isFull == false) && (playerwin == false) && (compwin == false)){
  48.     srand1 = rand() % 100;
  49.     checkanddraw();
  50.     randomizing();
  51.     playerplace();
  52.     tableIsFull();
  53.     if (isFull == true){
  54.         checkwin();
  55.         break;
  56.     }
  57.     computerplace();
  58.     checkwin();
  59.     }
  60.  
  61.     checkanddraw();
  62.     if (playerwin == true){
  63.         cout << "You Won!" << endl;
  64.     }else if(compwin == true){
  65.     cout << "You Lost!" << endl;
  66.     }else{
  67.     cout << "Draw!" << endl;
  68.     }
  69.  
  70. }
  71.  
  72. void checkwin(){
  73. HANDLE hConsole;
  74.     hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  75. if((table[0][2] == 1) && (table[1][1] == 1) && (table[2][0] == 1)){
  76. playerwin = true;
  77. }else if((table[0][2] == 2) && (table[1][1] == 2) && (table[2][0] == 2)){
  78. compwin = true;
  79. }
  80.  
  81. if((table[0][0] == 1) && (table[1][1] == 1) && (table[2][2] == 1)){
  82. playerwin = true;
  83. }else if((table[0][0] == 2) && (table[1][1] == 2) && (table[2][2] == 2)){
  84. compwin = true;
  85. }
  86.  
  87. if((table[0][0] == 1) && (table[0][1] == 1) && (table[0][2] == 1)){
  88. playerwin = true;
  89. }else if((table[0][0] == 2) && (table[0][1] == 2) && (table[0][2] == 2)){
  90. compwin = true;
  91. }
  92.  
  93. if((table[1][0] == 1) && (table[1][1] == 1) && (table[1][2] == 1)){
  94. playerwin = true;
  95. }else if((table[1][0] == 2) && (table[1][1] == 2) && (table[1][2] == 2)){
  96. compwin = true;
  97. }
  98.  
  99. if((table[2][0] == 1) && (table[2][1] == 1) && (table[2][2] == 1)){
  100. playerwin = true;
  101. }else if((table[2][0] == 2) && (table[2][1] == 2) && (table[2][2] == 2)){
  102. compwin = true;
  103. }
  104.  
  105. if((table[0][0] == 1) && (table[1][0] == 1) && (table[2][0] == 1)){
  106. playerwin = true;
  107. }else if((table[0][0] == 2) && (table[1][0] == 2) && (table[2][0] == 2)){
  108. compwin = true;
  109. }
  110.  
  111. if((table[0][1] == 1) && (table[1][1] == 1) && (table[2][1] == 1)){
  112. playerwin = true;
  113. }else if((table[0][1] == 2) && (table[1][1] == 2) && (table[2][1] == 2)){
  114. compwin = true;
  115. }
  116.  
  117. if((table[0][2] == 1) && (table[1][2] == 1) && (table[2][2] == 1)){
  118. playerwin = true;
  119. }else if((table[0][2] == 2) && (table[1][2] == 2) && (table[2][2] == 2)){
  120. compwin = true;
  121. }
  122.  
  123. }
  124.  
  125. void checkanddraw(){
  126.     system("CLS");
  127.     checktable();
  128.     drawtable();
  129. }
  130.  
  131. void checktable(){
  132. for(int v = 0; v <= 2; v++){
  133.     for(int b = 0; b <= 2; b++){
  134.         if (table[v][b] == 1){
  135.             drawXO[v][b] = "X";
  136.         }else if (table[v][b] == 2){
  137.         drawXO[v][b] = "O";
  138.         }
  139.     }
  140. }
  141. }
  142.  
  143. void initialiseDrawXO(){
  144. for (int z = 0; z <= 2; z++){
  145.     for(int x = 0; x <= 2; x++){
  146.         drawXO[z][x] = " ";
  147.     }
  148. }
  149. }
  150.  
  151. void tableIsFull(){
  152.      int m,n,z;
  153.      z = 0;
  154. for (m = 0; m <= 2; m++){
  155.     for(n = 0; n <= 2; n++){
  156.         if (table[m][n] == 0){
  157.                 isFull = false;
  158.                 z+= 1;
  159.                 break;
  160.         }
  161.     }
  162.     if (table[m][n] == 0){
  163.                     break;
  164.                     }
  165. }
  166.  
  167. if((m >= 2) && (n >= 2) && (z == 0)){
  168.       isFull = true;
  169.       }
  170. }
  171.  
  172. void randomizing(){
  173. srand(srand1);
  174. xcomp = rand()%3;
  175. ycomp = rand()%3;
  176. }
  177.  
  178. void computerplace(){
  179.     srand1 += 1;
  180.     randomizing();
  181. if(table[xcomp][ycomp] != 1 && table[xcomp][ycomp] != 2){
  182.     table[xcomp][ycomp] = 2;
  183. }else{
  184. computerplace();
  185. }
  186. }
  187.  
  188. void playerplace(){
  189.  
  190. cout << "Your Turn."  << endl;
  191. cout << "X/Y: ";
  192. cin >> xy;
  193.  
  194. xplayer = xy / 10;
  195. yplayer = xy % 10;
  196.  
  197. if (((xplayer >= 0 && xplayer <= 2) && (yplayer >= 0 && yplayer <= 2)) && ((table[xplayer][yplayer] != 1 && table[xplayer][yplayer] != 2))){
  198. table[xplayer][yplayer] = 1;
  199. }else{
  200. cout << "Choose another position!" << endl;
  201. _sleep(1000);
  202. system("CLS");
  203. drawtable();
  204. playerplace();
  205. }
  206. }
  207.  
  208. void showdata(){
  209. for (int z = 0 ; z <= 2 ; z++){
  210.     for(int c = 0 ; c <= 2; c++){
  211.         cout << "table[" << z << "][" << c << "] = " << table[z][c] << endl;
  212.     }
  213. }
  214. cout << endl;
  215. }
  216.  
  217. void drawtable(){
  218.     HANDLE hConsole;
  219.     hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  220.     cout << "    0  1  2";
  221.     cout << endl;
  222.     cout << endl;
  223.     cout << "0  " << drawXO[0][0]; SetConsoleTextAttribute(hConsole,11); cout << " | "; SetConsoleTextAttribute(hConsole,7); cout << drawXO[0][1]; SetConsoleTextAttribute(hConsole,11); cout << " | "; SetConsoleTextAttribute(hConsole,7); cout << drawXO[0][2] << " " << endl;
  224.     SetConsoleTextAttribute(hConsole,11);
  225.     cout << "   - - - - - " << endl;
  226.     SetConsoleTextAttribute(hConsole,7);
  227.     cout << "1  " << drawXO[1][0]; SetConsoleTextAttribute(hConsole,11); cout << " | "; SetConsoleTextAttribute(hConsole,7); cout << drawXO[1][1]; SetConsoleTextAttribute(hConsole,11); cout << " | "; SetConsoleTextAttribute(hConsole,7); cout << drawXO[1][2] << " " << endl;
  228.     SetConsoleTextAttribute(hConsole,11);
  229.     cout << "   - - - - - " << endl;
  230.     SetConsoleTextAttribute(hConsole,7);
  231.     cout << "2  " << drawXO[2][0]; SetConsoleTextAttribute(hConsole,11); cout << " | "; SetConsoleTextAttribute(hConsole,7); cout << drawXO[2][1]; SetConsoleTextAttribute(hConsole,11); cout << " | "; SetConsoleTextAttribute(hConsole,7); cout << drawXO[2][2] << " " << endl;
  232.     cout << endl;
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement