| | |
Tic-Tac-Toe
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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:
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:
•
•
Join Date: Apr 2004
Posts: 9
Reputation:
Solved Threads: 0
Perhaps,
and then the same for y.
C++ Syntax (Toggle Plain Text)
int x,y; cout << "Enter coordinates for your X: "; while(true) { cin >> x; if ((x>=3) || (x<0)) cout<<endl<<"invalid move. try again with a number from 0 to 2: "; else break; }
•
•
Join Date: Mar 2004
Posts: 1,620
Reputation:
Solved Threads: 51
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.
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
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)
// This declares the integers. Don't need to initialize them to // zero, because we fill them right away, and no decisions are made on them. int x,y; cout << "Enter coordinates for your X: "; // note that since you are missing a \n, there will be no "return" // while (true) means "loop forever unless we break out of it" while (true) { cin >> x; // check to see if (X is greater or equal to 3) OR "||" (X is less than 0) // this is the error check code. The lines || mean OR cout << endl ....... // the else "break" command means if you get here, break out of the while loop }
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
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)
•
•
Join Date: Mar 2004
Posts: 77
Reputation:
Solved Threads: 2
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.
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.
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)
# include <iostream.h> class TicTacToe { public: TicTacToe::TicTacToe(int array[][3]);//constructor void disp_array(void); void get_player1_move(int x1, int y1); void get_player2_move(int x2, int y2); int winner(); private: int array[3][3]; }; //constructor TicTacToe::TicTacToe(int array[][3]) { for (int i=0; i<3; i++) //loop array for (int j=0; j<3; j++) array[i][j]=0; } void TicTacToe::disp_array(void) //display board game { cout<<"Tic-Tac-Toe Game"<<endl<<endl; array[3][3]=0; for (int i=0; i<3; i++) { cout<<array[i][0]<<" | "<<array[i][1]<<" | "<<array[i][2]; if (i!=2) {cout<<"\n---------\n";} } cout<<endl<<endl; } void TicTacToe:: get_player1_move(int x1, int y1) {cout<<"Enter your coordinates player1: "; cin>>x1; x1--; while(true) { if ((x1>=3) || (x1<0)) cout<<endl<<"Invalid move. Try again with a number from 1 to 3: "; else break;} cin>>y1; y1--; while(true) { if ((y1>=3) || (y1<0)) cout<<endl<<"Invalid move. Try again with a number from 1 to 3: "; else break;} array[x1][y1]=1; get_player1_move(); } void TicTacToe::get_player2_move(int x2, int y2) { cout<<"\nEnter your coordinates player2: "; cin>>x2; x2--; while(true) { if ((x2>=3) || (x2<0)) cout<<endl<<"Invalid move. Try again with a number from 1 to 3: "; else break;} cin>>y2; y2--; while(true) { if ((y2>=3) || (y2<0)) cout<<endl<<"Invalid move. Try again with a number from 1 to 3: "; else break;} array[x2][y2]=2; get_player2_move(); } int TicTacToe:: winner(void); //see if there is a winner { int *p; for (int t=0; t<3; t++) //rows {p=&array[t][0]; if (*p==*(p+1) && *(p=1)==*(p+2)) return *p; } for(t=0; t<3; t++)//columns {p=&array[0][t]; if (*p==*(p+3) && *(p+3)==*(p+6)) return *p; } if(array[0][0]==array[1][1] && array[1][1]==array[2][2])//diagonal return array[0][0]; if(array[0][2]==array[1][1] && array[1][1]==array[2][0]) return array[0][2]; } int main() { int end; TicTacToe x; do { x.disp_array(); x.get_player1_move(); x.get_player2_move(); x.winner(); } while(end==0); if(end==1) {cout<<"player1 wins\n";} else {cout<<"player2 wins\n";} return 0; }
•
•
Join Date: Apr 2004
Posts: 9
Reputation:
Solved Threads: 0
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)- 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)- class TicTacToe {
- public:
- TicTacToe(); // constructor
- <snip>
- };
- <snip>
- //constructor
- TicTacToe::TicTacToe()
- {
- for (int i=0; i<3; i++) //loop array
- for (int j=0; j<3; j++)
- array[i][j]=0;
- }
- 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.
![]() |
Similar Threads
- Tic Tac Toe Homework (C++)
- Tic Tac toe help (C)
- Tic Tac Toe AI help, where to reset variables. (C++)
Other Threads in the C++ Forum
- Previous Thread: Help me please!!!!!!!!
- Next Thread: Visual studio C++ 2005
Views: 49606 | Replies: 38
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






