| | |
Confused on how to finish Tic Tac Toe program.
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
I am in a beginners programming course, and we have been doing C programs up until now, and the first C++ program we have confused me a bit. We have to make a simple tic tac toe program that plays against you [it doesn't have to be great at playing, it just has to work]. Since I have only done much simpler C++ programs before [simple math and printing to the screen is all I am familiar with] I have no idea how I would make it accept input properly form the user, nor keep up with what boxes of the play field are occupied with an X or an O. Below I've posted my teacher's example, and the only part I fully understand is the loop that sets the board to blank to start the game, and where the playing board is printed to the screen with the variables ready to be manipulated for game play. Any insight anyone could give on this would be very much appreciated! Here is the code.
C++ Syntax (Toggle Plain Text)
#include <cstdlib> #include <stdio.h> class TicTac { char board[9]; //Tic Tac Toe board public: TicTac(); //Constructor void draw_board(); //draw the current board on the screen }; //Constructor TicTac::TicTac(){ for(int i=0; i<9; i++) //initialize board to blank board[i]=' '; } void TicTac::draw_board(){ system("cls"); printf("_____________\n"); printf("| %c | %c | %c |\n", board[6], board[7], board[8]); printf("_____________\n"); printf("| %c | %c | %c |\n", board[3], board[4], board[5]); printf("_____________\n"); printf("| %c | %c | %c |\n", board[0], board[1], board[2]); printf("_____________\n"); } main(){ TicTac my_game; my_game.draw_board(); system("pause"); }
•
•
Join Date: Jan 2008
Posts: 3,818
Reputation:
Solved Threads: 501
Lots of good Tic Tac Toe threads on Daniweb. There are a variety of ways to approach it. Break it up into smaller, more manageable tasks. For instance, I would start with a function that accepts input from the human player. You should probably make it a member function of the TicTac class.
In main, to call it and test it, you could add these lines:
Check out this thread for why to avoid
http://www.gidnetwork.com/b-61.html
You'll need to
Hope this gives you a start.
C++ Syntax (Toggle Plain Text)
void TicTac::GetHumanMove () // assume human is player x { int row, col; std::cout << "Enter row : "; std::cin >> row; std::cout << "Enter column : "; std::cin >> col; // change board[] array based on row and col }
In main, to call it and test it, you could add these lines:
int main(){
TicTac my_game;
my_game.draw_board();
cin.get (); // instead of system ("PAUSE")
my_game.GetHumanMove ();
my_game.draw_board();
cin.get (); // instead of system ("PAUSE")
cin.get (); // instead of system ("PAUSE")
return 0;
}Check out this thread for why to avoid
system ("PAUSE"). http://www.gidnetwork.com/b-61.html
You'll need to
#include <iostream> for cin and cout .Hope this gives you a start.
Thanks for the help so far! I was able to get much further with that. But now I have run into another problem. Now I can't exactly figure out how to loop my program and make it not forget the things that are already on the playing board. I'm also not certain I am making it check for blank spaces correctly in my attempted while loop. In Dev C++, I get the compile errors:"'char TicTac::board[9]' is private" and "invalid use of non static data member 'TicTac::board'. Im not really sure where to go from this point. Again, any help will be very much appreciated!
C++ Syntax (Toggle Plain Text)
#include<iostream> using std::cout; using std::cin; char player[1]; char i[1]; class TicTac { char board[9]; public: TicTac(); //Constructor void draw_board(); void getmove(); }; //Constructor TicTac::TicTac(){ for(int i=0; i<9; i++) //initialize board to blank board[i]=' '; } void TicTac::getmove () // assume human is player x { int row, col; std::cout << "Enter row : "; std::cin >> row; std::cout << "Enter column : "; std::cin >> col; //here is a list of if statements for all the //possible squares of the playing field a player may choose. if (row==1 && col ==1){ board[0]= *player;} if (row==1 && col ==2){ board[1]= *player;} if (row==1 && col ==3){ board[2]= *player;} if (row==2 && col ==1){ board[3]= *player;} if (row==2 && col ==2){ board[4]= *player;} if (row==2 && col ==3){ board[5]= *player;} if (row==3 && col ==1){ board[6]= *player;} if (row==3 && col ==2){ board[7]= *player;} if (row==3 && col ==3){ board[8]= *player;} } void TicTac::draw_board() { system("cls"); cout << ("_____________\n"); cout << "| %c | %c | %c |\n", board[0], board[1], board[2]; cout << "_____________\n"; cout << "| %c | %c | %c |\n", board[3], board[4], board[5]; cout << "_____________\n"; cout << "| %c | %c | %c |\n", board[6], board[7], board[8]; cout << "_____________\n"; } main() { cout << "\nWelcome to Tic Tac Toe.\n"; cout << "\nThe computer player will be represented by a C.\n"; cout << "\nWhat character would you like to represent you? (Enter any letter.) "; cin >> player; while(TicTac::board[i] == ' ') { TicTac my_game; my_game.draw_board(); system("pause"); my_game.getmove (); my_game.draw_board(); system ("PAUSE"); } return 0; }
•
•
Join Date: Jan 2008
Posts: 3,818
Reputation:
Solved Threads: 501
I changed the code slightly.
This won't work from main:
Note the other call from main:
This is correct. You type the name of the object (my_game), then a dot, then the function or data member (if it is public. You can't do it from main if it's private).
The slightly revised code is below:
Lines 5 and 6 - What are you declaring one element arrays instead of simple char variables?
Line 9 - made board private since you said you wanted it private. Private is default, but it never hurts to type in private explicitly.
Line 68 - I commented this line out. You can put it back in later, but for debugging purposes, it's useful to see exactly what is happening. Clearing the screen can make you miss things. Comment it out when debugging. Put it back in when it's working.
Lines 69 - 75 - it looks like you are trying to use cout the way you use printf. You can use either, but don't try to mix them.
Line 77 - One way you can use cout. Only prints the top line. Add two more lines of code to print the rest.
Line 96 - made object declaration BEFORE the loop.
Line 98 - Took out the
This won't work from main:
C++ Syntax (Toggle Plain Text)
TicTac::board[i]
Note the other call from main:
C++ Syntax (Toggle Plain Text)
my_game.draw_board();
This is correct. You type the name of the object (my_game), then a dot, then the function or data member (if it is public. You can't do it from main if it's private).
The slightly revised code is below:
C++ Syntax (Toggle Plain Text)
#include<iostream> using std::cout; using std::cin; char player[1]; char i[1]; class TicTac { private: char board[9]; public: TicTac(); //Constructor void draw_board(); void getmove(); bool GameOver (); }; //Constructor TicTac::TicTac(){ for(int i=0; i<9; i++) //initialize board to blank board[i]=' '; } void TicTac::getmove () // assume human is player x { int row, col; std::cout << "Enter row : "; std::cin >> row; std::cout << "Enter column : "; std::cin >> col; //here is a list of if statements for all the //possible squares of the playing field a player may choose. if (row==1 && col ==1){ board[0]= *player;} if (row==1 && col ==2){ board[1]= *player;} if (row==1 && col ==3){ board[2]= *player;} if (row==2 && col ==1){ board[3]= *player;} if (row==2 && col ==2){ board[4]= *player;} if (row==2 && col ==3){ board[5]= *player;} if (row==3 && col ==1){ board[6]= *player;} if (row==3 && col ==2){ board[7]= *player;} if (row==3 && col ==3){ board[8]= *player;} } void TicTac::draw_board() { // system("cls"); /* cout << ("_____________\n"); cout << "| %c | %c | %c |\n", board[0], board[1], board[2]; cout << "_____________\n"; cout << "| %c | %c | %c |\n", board[3], board[4], board[5]; cout << "_____________\n"; cout << "| %c | %c | %c |\n", board[6], board[7], board[8]; cout << "_____________\n";*/ cout << "|" << board[0] << "|" << board[1] << "|" << board[2] << "|\n"; } bool TicTac::GameOver () { // returns false for now, but add code to check whether game is over. return false; } int main() { cout << "\nWelcome to Tic Tac Toe.\n"; cout << "\nThe computer player will be represented by a C.\n"; cout << "\nWhat character would you like to represent you? (Enter any letter.) "; cin >> player; TicTac my_game; while(!my_game.GameOver ()) { my_game.draw_board(); system("pause"); my_game.getmove (); my_game.draw_board(); system ("PAUSE"); } return 0; }
Line 9 - made board private since you said you wanted it private. Private is default, but it never hurts to type in private explicitly.
Line 68 - I commented this line out. You can put it back in later, but for debugging purposes, it's useful to see exactly what is happening. Clearing the screen can make you miss things. Comment it out when debugging. Put it back in when it's working.
Lines 69 - 75 - it looks like you are trying to use cout the way you use printf. You can use either, but don't try to mix them.
Line 77 - One way you can use cout. Only prints the top line. Add two more lines of code to print the rest.
Line 96 - made object declaration BEFORE the loop.
Line 98 - Took out the
TicTac::board line and replaced it with a new function. Right now the function is trivial. You'll need to add to it. Actually you can replace lines 37 - 63 with:
Or with
board[((row-1)*3)+(col-1)] = *player; ...Or with
board[((row-1)*3)+(col-1)] = player; if you're using a simple char-variable ... Last edited by tux4life; Apr 8th, 2009 at 8:18 am.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
•
•
•
•
Thanks a lot for that suggestion there, it condensed the code and made it much more manageable! All though if I could trouble you to explain exactly how that works and is able to be the equivalent of several if statements, I would be very appreciative and probably a much better programmer.
You can try it out and you'll see it actually works !
Last edited by tux4life; Apr 8th, 2009 at 4:21 pm.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
•
•
•
•
Thanks a lot for that suggestion there, it condensed the code and made it much more manageable! All though if I could trouble you to explain exactly how that works and is able to be the equivalent of several if statements, I would be very appreciative and probably a much better programmer.
C++ Syntax (Toggle Plain Text)
We start looking at the graphical representation of the instruction 'char board[9];' 0 1 2 3 4 5 6 7 8 [ ][ ][ ] | [ ][ ][ ] | [ ][ ][ ] (in total we've 9 elements in the array (element 0-8)) 0 3 6 [ ][ ][ ] | [ ][ ][ ] | [ ][ ][ ] | | | row 1 row 2 row 3 starts starts starts here here here If we know the row where we want to write in, we first have to calculate the actual place of the row in the array before we start writing ... So, after a bit trying you can find the place where the row starts (in the array) by using the following formula: row_place_in_array = (row-1)*3 ; | in each row there are three columns Now we only need to find a formula which is indicating the place in the array if we know the row and the column ... We already know the formula to find the row in the array, so if we start from here and if we adjust it a little bit, we can transform this in the final formula: place_in_array = ((row-1)*3)+(col-1) ; If you use the graphical representation you can quickly understand the formula ... Still I want to explain where the '(col-1)'-part came from: -> If we use the formula to find the place where each row starts in the array, we automatically know the place of the first column, that's the explanation for where the '-1' in '(col-1)' came from ...
P.S: Sorry if my English is bad, my native language is Dutch ...
Last edited by tux4life; Apr 9th, 2009 at 6:42 am.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
![]() |
Other Threads in the C++ Forum
- Previous Thread: Outputing a 2 - dimensional dynamic array
- Next Thread: Shwoing my dynamic array contents
| Thread Tools | Search this Thread |
api array arrays based binary bitmap c++ c/c++ calculator char char* class code coding compile console conversion count data database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






