By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,389 Members | 1,857 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,389 IT Pros & Developers. It's quick & easy.

Tic Tac Toe help

P: 16
Hello. I need help creating a tic tac toe program for a final exam using vectors. I am confused with the whole thing in general and not sure how to complete it. I would appreciate it if someone could help me complete this program, with hints, or the whole code, whatever works :) thank you

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. void displayBoard(const vector< vector<char> > &table);
  6. void getMove(vector< vector<char> > &table);
  7.  
  8. int main()
  9. {
  10.     vector<char>column(3,' ');
  11.     vector< vector<char> > table(3,column);
  12.  
  13.     getMove(table);
  14.     displayBoard(table);
  15.  
  16.     cin.get();
  17.     cin.get();
  18.     return 0;
  19. }
  20.  
  21.  
  22. void displayBoard(const vector< vector<char> > &table)
  23. {
  24.     cout << endl;
  25.  
  26.         for(int row=0;row<table.size();row++)
  27.         {
  28.             for(int col=0;col<table[row].size();col++)
  29.             {
  30.                 cout << "[" << table[row][col] << "] ";
  31.             }
  32.             cout << endl;
  33.         }
  34.  
  35. }
  36.  
  37. void getMove(vector< vector<char> > &table)
  38. {
  39.      int row=0;
  40.      int col=0;
  41.      int x=0;
  42.      int y=0;
  43.      char blank=' ';
  44.  
  45.      cout << "X, please enter row of move <0, 1, or 2>: ";
  46.      cin >> x;
  47.  
  48.      cout << "X, please enter column of move <0, 1, or 2>: ";
  49.      cin >> y;
  50.  
  51.      x=row;
  52.      y=col;
  53.      table[x][y]=table[row][col];
  54.  
  55.        cout << endl;
  56.  
  57.     for(int row=0;row<table.size();row++)
  58.     {
  59.         for(int col=0;col<table[row].size();col++)
  60.         {
  61.           table[row][col] = "X";        
  62.           }}
  63. }
  64.  
Jun 8 '07 #1
Share this Question
Share on Google+
3 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
I can't fix your code for you. If you are confused, it may not be about vectors but more about not know what to do.

Have you written down the design of your program?

Generally, just by starting out coding and hoping you will get it to work is a waste of energy.

I can help with specific questions.
Jun 8 '07 #2

P: 16
My problem is when player one says to put X in row 0, column 0, the X doesnt show up (obviously, because i havent put the code there) i am wondering what that specific code is. thanks

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. void displayBoard(const vector< vector<char> > &table);
  6. void getMove(vector< vector<char> > &table);
  7.  
  8. int main()
  9. {
  10.     vector<char>column(3,' ');
  11.     vector< vector<char> > table(3,column);
  12.  
  13.     displayBoard(table);
  14.     getMove(table);
  15.  
  16.     cin.get();
  17.     cin.get();
  18.     return 0;
  19. }
  20.  
  21.  
  22. void displayBoard(const vector< vector<char> > &table)
  23. {
  24.     cout << endl;
  25.  
  26.         for(int row=0;row<table.size();row++)
  27.         {
  28.             for(int col=0;col<table[row].size();col++)
  29.             {
  30.                 cout << "[" << table[row][col] << "] ";
  31.             }
  32.             cout << endl;
  33.         }
  34.  
  35. }
  36.  
  37. void getMove(vector< vector<char> > &table)
  38. {
  39.      int row=0;
  40.      int col=0;
  41.      int x=0;
  42.      int y=0;
  43.      char blank=' ';
  44.  
  45.      cout << "X, please enter row of move <0, 1, or 2>: ";
  46.      cin >>row;
  47.  
  48.      cout << "X, please enter column of move <0, 1, or 2>: ";
  49.      cin >> col;
  50.  
  51.        cout << endl;
  52.  
  53.  
  54. }
  55.  
Jun 10 '07 #3

AdrianH
Expert 100+
P: 1,251
My problem is when player one says to put X in row 0, column 0, the X doesnt show up (obviously, because i havent put the code there) i am wondering what that specific code is. thanks

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. void displayBoard(const vector< vector<char> > &table);
  6. void getMove(vector< vector<char> > &table);
  7.  
  8. int main()
  9. {
  10.     vector<char>column(3,' ');
  11.     vector< vector<char> > table(3,column);
  12.  
  13.     displayBoard(table);
  14.     getMove(table);
  15.  
  16.     cin.get();
  17.     cin.get();
  18.     return 0;
  19. }
  20.  
  21.  
  22. void displayBoard(const vector< vector<char> > &table)
  23. {
  24.     cout << endl;
  25.  
  26.         for(int row=0;row<table.size();row++)
  27.         {
  28.             for(int col=0;col<table[row].size();col++)
  29.             {
  30.                 cout << "[" << table[row][col] << "] ";
  31.             }
  32.             cout << endl;
  33.         }
  34.  
  35. }
  36.  
  37. void getMove(vector< vector<char> > &table)
  38. {
  39.      int row=0;
  40.      int col=0;
  41.      int x=0;
  42.      int y=0;
  43.      char blank=' ';
  44.  
  45.      cout << "X, please enter row of move <0, 1, or 2>: ";
  46.      cin >>row;
  47.  
  48.      cout << "X, please enter column of move <0, 1, or 2>: ";
  49.      cin >> col;
  50.  
  51.        cout << endl;
  52.  
  53.  
  54. }
  55.  
Well, you know how to access an element, assigning it is very similar. Do like you were just accessing it, put an equal to the right and then the value to what you are going to assign to it. Just like a regular array.

Expand|Select|Wrap|Line Numbers
  1. a[x][y]=z;

Adrian
Jun 10 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.