Advertisement
Guest User

TicTacToe.cxx

a guest
Jun 26th, 2013
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio>
  3. #include <process>
  4.  
  5. #define getch __getch
  6.  
  7. using namespace std;
  8. char a[3][3];
  9.  
  10. void draw(){
  11. cout<<" "<<a[0][0]<<"__|__"<<a[0][1]<<"__|__"<<a[0][2]<<endl;
  12. cout<<" "<<a[1][0]<<"__|__"<<a[1][1]<<"__|__"<<a[1][2]<<endl;
  13. cout<<" "<<a[2][0]<<" | "<<a[2][1]<<" | "<<a[2][2]<<endl;
  14. }
  15.  
  16. int check(){
  17. for(int q=0; q<3; q++){
  18. if(a[q][0]==a[q][1] && a[q][1]== a[q][2] && (a[q][2]=='O' || a[q][2]=='X'))
  19. return 1;
  20. if(a[0][q]== a[1][q] && a[1][q]==a[2][q] && (a[2][q]=='O' || a[2][q]=='X'))
  21. return 1;
  22. }
  23. if(a[0][0]==a[1][1] && a[1][1]== a[2][2] && (a[2][2]=='X' || a[2][2]=='O'))
  24. return 1;
  25. if(a[0][2]==a[1][1] && a[1][1]==a[2][0] && (a[2][0]=='X'|| a[2][0]=='O'))
  26. return 1;
  27. else
  28. return -1;
  29. }
  30.  
  31. void initialise(){
  32. for(int i=0; i<3; i++){
  33. for(int j=0; j<3; j++){
  34. a[i][j]='*';
  35. }
  36. }
  37. }
  38.  
  39. int main(){
  40. clrscr();
  41. int player;
  42. int w,i;
  43. char mark;
  44. int r, c;
  45. initialise();
  46. while(w!=1){
  47. if(i % 2==0) { player=1; mark='X'; }
  48. else { player=2; mark='O'; }
  49. cout<<"PLAYER: "<<player;
  50. cout<<"\nrow: ";
  51. cin>>r;
  52. cout<<"col: " ;
  53. cin>>c;
  54. getch();
  55. clrscr();
  56. //this if statement i tried to write in the check function block but then wo while loop me implement nai ho ra..how to do that?
  57. if(a[0][0]!='*'&& a[0][1]!='*'&&a[0][2]!='*'&&a[1][0]!='*'&&a[1][1]!='*'&&a[1][2]!='*'&&a[2][0]!='*'&&a[2][1]!='*'&&a[2][2]!='*'){
  58. w=0;
  59. break;
  60. }
  61. if(a[r][c]=='*'){
  62. a[r][c]=mark;
  63. draw();
  64. w=check();
  65. i++;
  66. }else{
  67. draw();
  68. i=i+2;
  69. }
  70. }
  71. if(w==1){
  72. clrscr();
  73. cout<<"\n\n\n PLAYER "<<player<<" WON" ;
  74. getch();
  75. exit(0);
  76. }
  77. if(w==0){
  78. clrscr();
  79. cout<<"GAME draw ";
  80. getch();
  81. exit(0);
  82. }
  83. // Something went wrong
  84. return -1;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement