Tic-Tac-Toe

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2004
Posts: 40
Reputation: cybergirl is an unknown quantity at this point 
Solved Threads: 0
cybergirl's Avatar
cybergirl cybergirl is offline Offline
Light Poster

Re: Tic-Tac-Toe

 
0
  #11
Apr 29th, 2004
Thank you Christian.

If i'm writing a program (C++) in which i'm using an array[x][y] and its a 3 by 3 array
How do i loop for error checking?
can i do it like this?

int x = 0, y = 0;

if (x<=3)
cout<<"invalid move"<<endl;
if (y<=3)
cout<<"invalid move"<<endl;

cout << "Enter coordinates for your X: ";
cin >> x >>y:
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 9
Reputation: asqueella is an unknown quantity at this point 
Solved Threads: 0
asqueella asqueella is offline Offline
Newbie Poster

Re: Tic-Tac-Toe

 
0
  #12
Apr 29th, 2004
Perhaps,
  1. int x,y;
  2.  
  3. cout << "Enter coordinates for your X: ";
  4. while(true) {
  5. cin >> x;
  6. if ((x>=3) || (x<0))
  7. cout<<endl<<"invalid move. try again with a number from 0 to 2: ";
  8. else break;
  9. }
and then the same for y.
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 1,620
Reputation: kc0arf is a jewel in the rough kc0arf is a jewel in the rough kc0arf is a jewel in the rough 
Solved Threads: 51
Team Colleague
kc0arf kc0arf is offline Offline
Posting Virtuoso

Re: Tic-Tac-Toe

 
0
  #13
Apr 29th, 2004
Hi,

I like your answer asqueella. Thank you for the hint there. As she is just beginning C++, she might not know what the code means. I'd break it down for her to help her understand where you are going with it.

  1.  
  2. // This declares the integers. Don't need to initialize them to
  3. // zero, because we fill them right away, and no decisions are made on them.
  4. int x,y;
  5.  
  6.  
  7. cout << "Enter coordinates for your X: ";
  8. // note that since you are missing a \n, there will be no "return"
  9.  
  10. // while (true) means "loop forever unless we break out of it"
  11. while (true)
  12. {
  13. cin >> x;
  14.  
  15. // check to see if (X is greater or equal to 3) OR "||" (X is less than 0)
  16. // this is the error check code. The lines || mean OR
  17.  
  18. cout << endl .......
  19.  
  20. // the else "break" command means if you get here, break out of the while loop
  21.  
  22. }


Hope that helps you out with the comments on the code. I also hope I didn't insult you by telling things you already knew.

Christian
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 353
Reputation: Asif_NSU is on a distinguished road 
Solved Threads: 2
Asif_NSU's Avatar
Asif_NSU Asif_NSU is offline Offline
Posting Whiz

Re: Tic-Tac-Toe

 
0
  #14
Apr 30th, 2004
Yes there is no need to initialize x, y to 0 for this particular program, bcoz we are eventually filling them up. But I would still suggest u initialize them to zero/something. It's a good programming practice, specially when u will be writing much bigger programs. For example u might have to move part of codes in or out of a loop. It is very difficult to carefully notice if all the variables are initialized /filled up or not. If u miss any of them it may cause unexpected problems. Therefore, I suggest u practice the habit of initializing all the varibles from now on-----I believe it will come in handy later on.
"He who mixes with people and endures the harm they do is better than he who does not mix and endures." (Tirmidhi)
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 77
Reputation: infamous is an unknown quantity at this point 
Solved Threads: 2
infamous infamous is offline Offline
Junior Poster in Training

Re: Tic-Tac-Toe

 
0
  #15
Apr 30th, 2004
agreed, it is always a good habit to get into. the only time u will see good programmers avoiding the practice is when writing kernel code - when every instruction counts, and then it is ur responsibility to be absolutely positive to double check that every variable is not used before given a value.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 353
Reputation: Asif_NSU is on a distinguished road 
Solved Threads: 2
Asif_NSU's Avatar
Asif_NSU Asif_NSU is offline Offline
Posting Whiz

Re: Tic-Tac-Toe

 
0
  #16
Apr 30th, 2004
Yea, that's bcoz kernel codes need to be efficient and as fast as possible; every unnecessary statement needs to be avoided for that purpose. Thanx for reminding me that "infamous".
"He who mixes with people and endures the harm they do is better than he who does not mix and endures." (Tirmidhi)
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 40
Reputation: cybergirl is an unknown quantity at this point 
Solved Threads: 0
cybergirl's Avatar
cybergirl cybergirl is offline Offline
Light Poster

Re: Tic-Tac-Toe

 
0
  #17
Apr 30th, 2004
Thank you guys!!

Well, here it is what i have so far for my TicTacToe game(C++). It not running, I don't know what's wrong with it. Take a look at it. Maybe you can help me.
Remember i had to create a class TicTacToe that will enable me to write a complete program to play the game tictactoe. The class has to contain as private data a 3-by-3 doublesubscripted array of integers. The constructor should initialize the empty board to all Zeros. i had to allow 2 players to used it. Wherever the player1 moves, i have to place a 1 in the specified square. For player2, i have to place a 2.

  1. # include <iostream.h>
  2. class TicTacToe
  3. {
  4. public:
  5. TicTacToe::TicTacToe(int array[][3]);//constructor
  6. void disp_array(void);
  7. void get_player1_move(int x1, int y1);
  8. void get_player2_move(int x2, int y2);
  9. int winner();
  10. private:
  11. int array[3][3];
  12. };
  13. //constructor
  14. TicTacToe::TicTacToe(int array[][3])
  15. {
  16. for (int i=0; i<3; i++) //loop array
  17. for (int j=0; j<3; j++)
  18. array[i][j]=0;
  19. }
  20. void TicTacToe::disp_array(void) //display board game
  21. {
  22. cout<<"Tic-Tac-Toe Game"<<endl<<endl;
  23. array[3][3]=0;
  24. for (int i=0; i<3; i++)
  25. {
  26. cout<<array[i][0]<<" | "<<array[i][1]<<" | "<<array[i][2];
  27. if (i!=2)
  28. {cout<<"\n---------\n";}
  29. }
  30. cout<<endl<<endl;
  31. }
  32.  
  33. void TicTacToe:: get_player1_move(int x1, int y1)
  34. {cout<<"Enter your coordinates player1: ";
  35.  
  36. cin>>x1;
  37. x1--;
  38. while(true) {
  39. if ((x1>=3) || (x1<0))
  40. cout<<endl<<"Invalid move. Try again with a number from 1 to 3: ";
  41. else break;}
  42. cin>>y1;
  43. y1--;
  44. while(true) {
  45. if ((y1>=3) || (y1<0))
  46. cout<<endl<<"Invalid move. Try again with a number from 1 to 3: ";
  47. else break;}
  48. array[x1][y1]=1;
  49. get_player1_move();
  50.  
  51. }
  52. void TicTacToe::get_player2_move(int x2, int y2)
  53. {
  54. cout<<"\nEnter your coordinates player2: ";
  55. cin>>x2;
  56. x2--;
  57. while(true) {
  58. if ((x2>=3) || (x2<0))
  59. cout<<endl<<"Invalid move. Try again with a number from 1 to 3: ";
  60. else break;}
  61. cin>>y2;
  62. y2--;
  63. while(true) {
  64. if ((y2>=3) || (y2<0))
  65. cout<<endl<<"Invalid move. Try again with a number from 1 to 3: ";
  66. else break;}
  67. array[x2][y2]=2;
  68. get_player2_move();
  69. }
  70. int TicTacToe:: winner(void); //see if there is a winner
  71. {
  72. int *p;
  73. for (int t=0; t<3; t++) //rows
  74. {p=&array[t][0];
  75. if (*p==*(p+1) && *(p=1)==*(p+2))
  76. return *p;
  77. }
  78. for(t=0; t<3; t++)//columns
  79. {p=&array[0][t];
  80. if (*p==*(p+3) && *(p+3)==*(p+6))
  81. return *p;
  82. }
  83. if(array[0][0]==array[1][1] && array[1][1]==array[2][2])//diagonal
  84. return array[0][0];
  85. if(array[0][2]==array[1][1] && array[1][1]==array[2][0])
  86. return array[0][2];
  87. }
  88.  
  89. int main()
  90. { int end;
  91. TicTacToe x;
  92. do {
  93. x.disp_array();
  94. x.get_player1_move();
  95. x.get_player2_move();
  96. x.winner();
  97. }
  98. while(end==0);
  99. if(end==1)
  100. {cout<<"player1 wins\n";}
  101. else
  102. {cout<<"player2 wins\n";}
  103.  
  104.  
  105. return 0;
  106. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 219
Reputation: BountyX is an unknown quantity at this point 
Solved Threads: 7
BountyX's Avatar
BountyX BountyX is offline Offline
Code Guru

Re: Tic-Tac-Toe

 
0
  #18
Apr 30th, 2004
I also made a tic tac toe program in C++, if your intrested in my code feel free to email me
A Hacker's Mind:
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 77
Reputation: infamous is an unknown quantity at this point 
Solved Threads: 2
infamous infamous is offline Offline
Junior Poster in Training

Re: Tic-Tac-Toe

 
0
  #19
May 1st, 2004
cybergirl << could u at least tell us WHY it isn't running?? can u compile it? does it fault when it runs? where does it fault?? some INFO
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 9
Reputation: asqueella is an unknown quantity at this point 
Solved Threads: 0
asqueella asqueella is offline Offline
Newbie Poster

Re: Tic-Tac-Toe

 
0
  #20
May 3rd, 2004
I didn't try to run it, or even compile, but the most obvious issues for me are:
  • do..while loop in main() doesn't assign any value to end. Perhaps it should have this line:
    1. end = x.winner();
  • Your TicTacToe class doesn't have any default constructor (TicTacToe::TicTacToe()), only quite a strange TicTacToe::TicTacToe(int array[][3]). What I would do is write:
    1. class TicTacToe {
    2. public:
    3. TicTacToe(); // constructor
    4. <snip>
    5. };
    6. <snip>
    7.  
    8. //constructor
    9. TicTacToe::TicTacToe()
    10. {
    11. for (int i=0; i<3; i++) //loop array
    12. for (int j=0; j<3; j++)
    13. array[i][j]=0;
    14. }
  • Also the get_player1_move() functions have their parameters passed "by value", which means that any changes you make to x1 or y1 inside the function will not affect the variable in calling code - you should to use references or pointers here instead. The same applies to get_player2_move() - I'd merge them into one.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 49606 | Replies: 38
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC