473,241 Members | 1,571 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

Tic Tac Toe Game Help, Please....

Hello,

I have a group assignment that I have to do. We have to write a Tic Tac Toe game. One person in my group has managed to write the code for a multiplayer (human -vs- human) game and I managed to write a code for a single player (human -vs- computer) game.

My problem is, now we want to merge the two games with options to pick which game you would like to play. However, the games were written totally different styles and I can't get them to work, so I am turning to you for help.

Here is the multiplayer game
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4. #include <cstdlib>
  5. using namespace std;
  6. int cols = 3, rows = 3;
  7. void displayboard(char [3][3]);
  8. bool checkwin(char [3][3]);
  9. void end(char [3][3], char, char, string, string);
  10. void instructions();
  11. bool tie(char [3][3]);
  12. int main()
  13. {
  14.     string player1;
  15.     string player2;
  16.     char board[3][3]= {'-', '-', '-',
  17.                         '-', '-', '-',
  18.                         '-', '-', '-',};
  19.     int row, column, count = 1;
  20.     char play1sym, play2sym, X, O, x, o, sel1;
  21.     bool check = false;
  22.     bool scratch;
  23.     cout<<"Welcome Group 2's TicTacToe game!"<< endl;
  24.     cout << "Would you like to read the instructions? Press y for yes or n for no." << endl;
  25. cin >> sel1;
  26. do
  27. {
  28. if (sel1 == 'y' || sel1 == 'Y')
  29. instructions();
  30. }
  31. while (sel1 != 'y' && sel1 != 'Y' && sel1 != 'n' && sel1 != 'N');
  32.  
  33.     cout << "Please input player 1's name." << endl;
  34.     cin >> player1;
  35.     cout << "\nInput player 2's name, please" << endl;
  36.     cin >> player2;
  37.     displayboard(board);
  38.     play1sym = 'a';
  39.     do
  40.     {
  41.         cout << "\n" << player1 << ", Would you like to use X's or O's?" <<endl;
  42.         cin >> play1sym;
  43.     }
  44.     while (play1sym != 'x' && play1sym != 'X' && play1sym != 'o' && play1sym != 'O');
  45.  
  46.     if (play1sym == 'x')
  47.         play1sym = 'X';
  48.     else if (play1sym == 'o')
  49.         play1sym = 'O';
  50.     else if (play1sym == 'X')
  51.         play1sym = 'X';
  52.     else if (play1sym == 'O')
  53.         play1sym = 'O';
  54.  
  55.     if (play1sym == 'X')
  56.         play2sym = 'O';
  57.     else
  58.         play2sym = 'X';
  59.     cout << endl << player2 << ", you'll be using " << play2sym << "'s" << endl;
  60.  
  61.     while (check == false)
  62.     {
  63.         displayboard(board);
  64.         if (count %2 !=0)
  65.     {
  66.         do
  67.         {
  68.         cout<< endl << player1 << ":" << " It's your turn. Pick a column, then a row" <<endl;
  69.         cin >> row >> column;
  70.         }
  71.         while (board[(row - 1)][(column - 1)] != '-');
  72.         board[(row - 1)][(column - 1)] = play1sym;
  73.     }
  74.     else
  75.     {
  76.         do
  77.         {
  78.         cout<< endl << player2 << ":" << " It's your turn. Pick a column, then a row" <<endl;
  79.         cin >> row >> column;
  80.         }
  81.         while (board[(row - 1)][(column - 1)] != '-');
  82.         board[(row - 1)][(column - 1)] = play2sym;
  83.     }
  84.     check = checkwin(board);
  85.     scratch = tie(board);
  86.     if (check == true)
  87.         end(board, play1sym, play2sym, player1, player2);
  88.     else if (scratch == true)
  89.         end(board, play1sym, play2sym, player1, player2);
  90.     else
  91.         count++;
  92.     }
  93.     return 0;
  94. }
  95. void displayboard(char displayboard[3][3])
  96. {
  97.     int rownum = 0, colnum;
  98.     cout << "    1   2   3" << endl;
  99.     for (colnum = 0; colnum <= 2; colnum++, cout<< endl)
  100.     {    
  101.         cout << (colnum + 1);
  102.         for (rownum = 0; rownum <= 2; rownum++)
  103.             cout<< setw(4) << displayboard[rownum][colnum];
  104.     }
  105. }
  106. bool checkwin(char checkboard[3][3])
  107. {
  108. if (checkboard[0][0] == checkboard[1][0] && checkboard[0][0] == checkboard[2][0] && checkboard[0][0] != '-')
  109.     return true;
  110. else if (checkboard[0][1] == checkboard[1][1] && checkboard[0][1] == checkboard[2][1] && checkboard[0][1] != '-')
  111.     return true;
  112. else if (checkboard[0][2] == checkboard[1][2] && checkboard[0][2] == checkboard[2][2] && checkboard[0][2] != '-')
  113.     return true;
  114. else if (checkboard[0][0] == checkboard[0][1] && checkboard[0][0] == checkboard[0][2] && checkboard[0][0] != '-')
  115.     return true;
  116. else if (checkboard[1][0] == checkboard[1][1] && checkboard[1][0] == checkboard[1][2] && checkboard[1][0] != '-')
  117.     return true;
  118. else if (checkboard[2][0] == checkboard[2][1] && checkboard[2][0] == checkboard[2][2] && checkboard[2][0] != '-')
  119.     return true;
  120. else if (checkboard[0][0] == checkboard[1][1] && checkboard[0][0] == checkboard[2][2] && checkboard[0][0] != '-')
  121.     return true;
  122. else if (checkboard[0][2] == checkboard[1][1] && checkboard[0][2] == checkboard[2][0] && checkboard[0][2] != '-')
  123.     return true;
  124. else
  125.     return false;
  126. }
  127. void end(char endgame[3][3], char sym1, char sym2, string play1, string play2 )
  128. {
  129.     char sel;
  130.     displayboard(endgame);
  131. if (endgame[0][0] == endgame[1][0] && endgame[0][0] == endgame[2][0])
  132. {
  133.     if (endgame[0][0] == sym1)
  134.         cout << play1 << " wins!!! Good Job!!!" << endl;
  135.     else
  136.         cout << play2 << " wins!!! Good Job!!!" << endl;
  137. }
  138. else if (endgame[0][1] == endgame[1][1] && endgame[0][1] == endgame[2][1])
  139. {
  140.         if (endgame[0][1] == sym1)
  141.         cout << play1 << " wins!!! Good Job!!!" << endl;
  142.         else
  143.         cout << play2 << " wins!!! Good Job!!!" << endl;
  144. }
  145. else if (endgame[0][2] == endgame[1][2] && endgame[0][2] == endgame[2][2])
  146. {
  147.         if (endgame[0][2] == sym1)
  148.         cout << play1 << " wins!!! Good Job!!!" << endl;
  149.         else
  150.         cout << play2 << " wins!!! Good Job!!!" << endl;
  151. }
  152. else if (endgame[0][0] == endgame[0][1] && endgame[0][0] == endgame[0][2])
  153. {
  154.         if (endgame[0][0] == sym1)
  155.         cout << play1 << " wins!!! Good Job!!!" << endl;
  156.         else
  157.         cout << play2 << " wins!!! Good Job!!!" << endl;
  158. }
  159. else if (endgame[1][0] == endgame[1][1] && endgame[1][0] == endgame[1][2])
  160. {
  161.         if (endgame[1][0] == sym1)
  162.         cout << play1 << " wins!!! Good Job!!!" << endl;
  163.         else
  164.         cout << play2 << " wins!!! Good Job!!!" << endl;
  165. }
  166. else if (endgame[2][0] == endgame[2][1] && endgame[2][0] == endgame[2][2])
  167. {
  168.         if (endgame[2][0] == sym1)
  169.         cout << play1 << " wins!!! Good Job!!!" << endl;
  170.         else
  171.         cout << play2 << " wins!!! Good Job!!!" << endl;
  172. }
  173. else if (endgame[0][0] == endgame[1][1] && endgame[0][0] == endgame[2][2])
  174. {
  175.         if (endgame[0][0] == sym1)
  176.         cout << play1 << " wins!!! Good Job!!!" << endl;
  177.         else
  178.         cout << play2 << " wins!!! Good Job!!!" << endl;
  179. }
  180. else if (endgame[0][2] == endgame[1][1] && endgame[0][2] == endgame[2][0])
  181. {
  182.         if (endgame[0][2] == sym1)
  183.         cout << play1 << " wins!!! Good Job!!!" << endl;
  184.         else
  185.         cout << play2 << " wins!!! Good Job!!!" << endl;
  186. }
  187. else if (endgame[0][0] != '-' &&
  188.         endgame[0][1] != '-' &&
  189.         endgame[0][2] != '-' &&
  190.         endgame[1][0] != '-' &&
  191.         endgame[1][1] != '-' &&
  192.         endgame[1][2] != '-' &&
  193.         endgame[2][0] != '-' &&
  194.         endgame[2][1] != '-' &&
  195.         endgame[2][2] != '-')
  196. {
  197.     cout << "It's a tie. No one wins." << endl;
  198. }
  199. cout << "Would you like to play again? Press y for yes or n for no." <<endl;
  200. cin >> sel;
  201. do
  202. {
  203. if (sel == 'y' || sel == 'Y')
  204. {
  205.     cout << endl;
  206.     main();
  207. }
  208. else if (sel == 'n' || sel == 'N')
  209. exit(1);
  210. }
  211. while (sel != 'y' && sel != 'Y' && sel != 'n' && sel != 'N');
  212. }
  213. void instructions()
  214. {
  215.     cout << "This is a simple 2 player game. You'll input the 2 players' names, then begin to play." << endl << endl
  216.         << "When selecting a space, first enter the column and press 'enter'(counting from the left)." << endl << endl
  217.         << "Then, enter the row and press 'enter' (counting from top)." << endl << endl
  218.         << "Remember to always begin counting at '1'. Therefore, when entering a row and column," << endl << endl
  219.         << " you can enter a number between (and including) 1 and 3." << endl << endl;
  220. }
  221. bool tie(char nowinner[3][3])
  222. {
  223.     if (nowinner[0][0] != '-' &&
  224.         nowinner[0][1] != '-' &&
  225.         nowinner[0][2] != '-' &&
  226.         nowinner[1][0] != '-' &&
  227.         nowinner[1][1] != '-' &&
  228.         nowinner[1][2] != '-' &&
  229.         nowinner[2][0] != '-' &&
  230.         nowinner[2][1] != '-' &&
  231.         nowinner[2][2] != '-')
  232. return true;
  233. else
  234. return false;
  235. }
  236.  
The single player code after I tried to merge the two:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4. #include <cstdlib>
  5. using namespace std;
  6. int cols = 3, rows = 3;
  7. void displayboard(char [3][3]);
  8. bool checkwin(char [3][3]);
  9. void end(char [3][3], char, char, string, string);
  10. void instructions();
  11. bool tie(char [3][3]);
  12.  
  13. /*A simple game of Tic-Tac-Toe*/
  14.  
  15. #define SPACE    ' '
  16.  
  17. char board[3][3]= {'-', '-', '-',
  18.                         '-', '-', '-',
  19.                         '-', '-', '-',};
  20.  
  21.  
  22. void get_computer_move(void), get_player_move(void);
  23.  
  24. main()
  25. {
  26.     char done;
  27.     int row, column, count = 1;
  28.     char play1sym, play2sym, X, O, x, o, sel1;
  29.     bool check = false;
  30.     bool scratch;
  31.  
  32.  
  33.     cout<<"This is the game of Tic-Tac-Toe."<<endl;
  34.     cout<<"You will be playing against the computer."<<endl;
  35.  
  36.     done = SPACE;
  37.     do
  38.     {
  39.         displayboard(board);/*display the game board*/
  40.         get_player_move();/*get your move*/
  41.  
  42.         done = check();/*see if winner*/
  43.         if(done != SPACE) 
  44.             break;/*winner!*/
  45.  
  46.         get_computer_move();/*get computer's move*/
  47.  
  48.         done = check();/*see if winner*/
  49.     }
  50.     while(done == SPACE);
  51.  
  52.     if(done == 'X')
  53.         cout<<"You won!"<<endl;
  54.     else 
  55.         cout<<"I won!!!!"<<endl;
  56.     displayboard(board);/*show final positions*/
  57.     //getch();
  58.     return 0;
  59. }
  60.  
  61. /*Input the players move*/
  62. void get_player_move(void)
  63. {
  64.     int row, column;
  65.  
  66.     cout<<"Enter coordinates for your X: ";
  67.     cin>> row>> column;
  68.     row--;
  69.     column--;
  70.     if(matrix[row][column] != SPACE)
  71.     {
  72.     cout<<"Invalid move,try again."<<endl;
  73.     get_player_move();
  74.     }
  75.     else matrix[row][column]= 'X';
  76. }
  77.  
  78. /*Get the computer's move*/
  79. void get_computer_move(void)
  80. {
  81.     register int t;
  82.     char *p;
  83.  
  84.     p = (char *)board;
  85.     for(t = 0; *p != SPACE && t < 9; ++t) p++;
  86.     if(t == 9)
  87.     {
  88.         cout<<"draw"<<endl;
  89.         exit(0);/*game over*/
  90.     }
  91.     else *p = 'O';
  92. }
  93.  
  94. /*Display the game board*/
  95. void displayboard(char displayboard[3][3])
  96. {
  97.     int rownum = 0, colnum;
  98.     cout << "    1   2   3" << endl;
  99.     for (colnum = 0; colnum <= 2; colnum++, cout<< endl)
  100.     {    
  101.         cout << (colnum + 1);
  102.         for (rownum = 0; rownum <= 2; rownum++)
  103.             cout<< setw(4) << displayboard[rownum][colnum];
  104.     }
  105. )
  106.  
  107. /*See if there is a winner*/
  108. bool checkwin(char checkboard[3][3])
  109. {
  110. if (checkboard[0][0] == checkboard[1][0] && checkboard[0][0] == checkboard[2][0] && checkboard[0][0] != '-')
  111.     return true;
  112. else if (checkboard[0][1] == checkboard[1][1] && checkboard[0][1] == checkboard[2][1] && checkboard[0][1] != '-')
  113.     return true;
  114. else if (checkboard[0][2] == checkboard[1][2] && checkboard[0][2] == checkboard[2][2] && checkboard[0][2] != '-')
  115.     return true;
  116. else if (checkboard[0][0] == checkboard[0][1] && checkboard[0][0] == checkboard[0][2] && checkboard[0][0] != '-')
  117.     return true;
  118. else if (checkboard[1][0] == checkboard[1][1] && checkboard[1][0] == checkboard[1][2] && checkboard[1][0] != '-')
  119.     return true;
  120. else if (checkboard[2][0] == checkboard[2][1] && checkboard[2][0] == checkboard[2][2] && checkboard[2][0] != '-')
  121.     return true;
  122. else if (checkboard[0][0] == checkboard[1][1] && checkboard[0][0] == checkboard[2][2] && checkboard[0][0] != '-')
  123.     return true;
  124. else if (checkboard[0][2] == checkboard[1][1] && checkboard[0][2] == checkboard[2][0] && checkboard[0][2] != '-')
  125.     return true;
  126. else
  127.     return false;
  128. }
  129.  
Any ideals as to what Im doing wrong, after I get the two games running together I will add options to choose the type of game to play.

Thanks in advance...
Nov 17 '08 #1
2 5265
weaknessforcats
9,208 Expert Mod 8TB
Wby bother to merge the code? I mean, both games work, right?

So why not:
Expand|Select|Wrap|Line Numbers
  1.     //TODO: determine number of players
  2.     if (SinglePlayer == true)
  3.      {
  4.            MyTicTacToeGame();
  5.      }
  6.      else
  7.      {
  8.            HisTicTacToeGame();
  9.  
Just rename your games.
Nov 17 '08 #2
Yes both games do work, but we wanted to have a menu type game where you have the option to play either game. And I see what you mean. I didnt think of that. Thank you.....
Nov 21 '08 #3

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

Similar topics

23
by: BlackHawke | last post by:
Hello! This is my second post. Ppl really helped me with the first. I hope there are answers for this one as well I own a game company (www.aepoxgames.net) releasing the beta for our first...
7
by: Gasten | last post by:
Hello. The last weeks I've been coding a roguelike (you know, like nethack) in python using the nCurses library. Some week ago I ran into a problem: When I made the object for messagebar-output, I...
1
hpbutterbeer
by: hpbutterbeer | last post by:
We have a Machine Project and my brain is currently in a clouded state. Sorry, I'm just a beginner in C Programming... Text twist is a windows game whose main objective is to form words out of the...
5
by: koonda | last post by:
Hi all, I am a student and I have a project due 20th of this month, I mean May 20, 2007 after 8 days. The project is about creating a Connect Four Game. I have found some code examples on the...
3
by: Untitled123 | last post by:
Hey guys!! I am Planning on Creating a Computer vs. User Chess game involving Artificial Intelligence... by just using TURBO C++ This is NOT impossible!! Please understand that my seniors...
0
by: Shawn Minisall | last post by:
For my final project, I'm trying to do a GUI based game similar to are you smarter then a 5th grader. I've been working on it and am stuck with some code someone helped me with to randomize the...
1
by: Gomi | last post by:
Hi guys I'm new to C++ Programming and I am having trouble in making my Guessing game code replay. I am suppose to give the user that is playing my game the opportunity to play again with options of...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.