943,416 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 57344
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Apr 29th, 2004
0

Re: Tic-Tac-Toe

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:
Reputation Points: 15
Solved Threads: 0
Light Poster
cybergirl is offline Offline
40 posts
since Apr 2004
Apr 29th, 2004
0

Re: Tic-Tac-Toe

Perhaps,
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
asqueella is offline Offline
9 posts
since Apr 2004
Apr 29th, 2004
0

Re: Tic-Tac-Toe

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.

C++ Syntax (Toggle Plain Text)
  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
Team Colleague
Reputation Points: 121
Solved Threads: 57
Posting Virtuoso
kc0arf is offline Offline
1,629 posts
since Mar 2004
Apr 30th, 2004
0

Re: Tic-Tac-Toe

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.
Reputation Points: 113
Solved Threads: 3
Posting Whiz
Asif_NSU is offline Offline
353 posts
since Apr 2004
Apr 30th, 2004
0

Re: Tic-Tac-Toe

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.
Reputation Points: 47
Solved Threads: 2
Junior Poster in Training
infamous is offline Offline
77 posts
since Mar 2004
Apr 30th, 2004
0

Re: Tic-Tac-Toe

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".
Reputation Points: 113
Solved Threads: 3
Posting Whiz
Asif_NSU is offline Offline
353 posts
since Apr 2004
Apr 30th, 2004
0

Re: Tic-Tac-Toe

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.

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 15
Solved Threads: 0
Light Poster
cybergirl is offline Offline
40 posts
since Apr 2004
Apr 30th, 2004
0

Re: Tic-Tac-Toe

I also made a tic tac toe program in C++, if your intrested in my code feel free to email me
Reputation Points: 28
Solved Threads: 9
Posting Whiz in Training
BountyX is offline Offline
222 posts
since Mar 2004
May 1st, 2004
0

Re: Tic-Tac-Toe

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
Reputation Points: 47
Solved Threads: 2
Junior Poster in Training
infamous is offline Offline
77 posts
since Mar 2004
May 3rd, 2004
0

Re: Tic-Tac-Toe

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:
    C++ Syntax (Toggle Plain Text)
    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:
    C++ Syntax (Toggle Plain Text)
    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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
asqueella is offline Offline
9 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Convert this C# declaration to C++
Next Thread in C++ Forum Timeline: Char problem in C++ Calculator





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC