473,795 Members | 2,854 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Tic Tac Toe Game Help, Please....

7 New Member
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 5317
weaknessforcats
9,208 Recognized Expert Moderator Expert
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
LilMeechc20
7 New Member
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
3665
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 sci-fi space game in about 2 weeks (www.andromedaonline.net) We have multiple servers, but the game engine (Game Server Program) is a Java program (don't ask why) on a windows 2003 enterprise system with dual
7
2856
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 found a bug that I can't figure out (believe me; I've tried everything and asked for help several times on the IRC-channel). The updateMsg() and iMsg() takes a list of message-lines as argument (or process a string to a list), and it'll output...
1
4427
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 letters shown on the screen, you move on to the next level when you are able to form a word with all the letters given. In the actual game you click on the letters of the word, in this text version you simply type the words. To be able to...
5
3556
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 internet which helped me little bit. But there are lot of problems I am facing developing the whole game. I have drawn the Board and the two players can play. The players numbers can fill the board, but I have problem implementing the winner for the...
3
9996
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 achieved this last year and they had hardly a month's time to do it!! But then i want to do it and develop it better than them! This is for a school project BTW.
0
1682
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 A,B,C,D letters that the correct answer is assigned too. The code that does this is highlighted in bold and the code that assigns it to a variable is also in bold so I can test it in the console window. Problem is, it's always stuck at letter A...
1
3102
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 'Yes' and 'No' and if Yes then the game starts up again and if No then the game tells the user bye and quits. "Do you want to play again? (y/n)" Please Help if possible thanks. ^^ -----------------------------------------
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9522
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10167
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7544
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5566
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2922
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.