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
- #include <iostream>
- #include <string>
- #include <iomanip>
- #include <cstdlib>
- using namespace std;
- int cols = 3, rows = 3;
- void displayboard(char [3][3]);
- bool checkwin(char [3][3]);
- void end(char [3][3], char, char, string, string);
- void instructions();
- bool tie(char [3][3]);
- int main()
- {
- string player1;
- string player2;
- char board[3][3]= {'-', '-', '-',
- '-', '-', '-',
- '-', '-', '-',};
- int row, column, count = 1;
- char play1sym, play2sym, X, O, x, o, sel1;
- bool check = false;
- bool scratch;
- cout<<"Welcome Group 2's TicTacToe game!"<< endl;
- cout << "Would you like to read the instructions? Press y for yes or n for no." << endl;
- cin >> sel1;
- do
- {
- if (sel1 == 'y' || sel1 == 'Y')
- instructions();
- }
- while (sel1 != 'y' && sel1 != 'Y' && sel1 != 'n' && sel1 != 'N');
- cout << "Please input player 1's name." << endl;
- cin >> player1;
- cout << "\nInput player 2's name, please" << endl;
- cin >> player2;
- displayboard(board);
- play1sym = 'a';
- do
- {
- cout << "\n" << player1 << ", Would you like to use X's or O's?" <<endl;
- cin >> play1sym;
- }
- while (play1sym != 'x' && play1sym != 'X' && play1sym != 'o' && play1sym != 'O');
- if (play1sym == 'x')
- play1sym = 'X';
- else if (play1sym == 'o')
- play1sym = 'O';
- else if (play1sym == 'X')
- play1sym = 'X';
- else if (play1sym == 'O')
- play1sym = 'O';
- if (play1sym == 'X')
- play2sym = 'O';
- else
- play2sym = 'X';
- cout << endl << player2 << ", you'll be using " << play2sym << "'s" << endl;
- while (check == false)
- {
- displayboard(board);
- if (count %2 !=0)
- {
- do
- {
- cout<< endl << player1 << ":" << " It's your turn. Pick a column, then a row" <<endl;
- cin >> row >> column;
- }
- while (board[(row - 1)][(column - 1)] != '-');
- board[(row - 1)][(column - 1)] = play1sym;
- }
- else
- {
- do
- {
- cout<< endl << player2 << ":" << " It's your turn. Pick a column, then a row" <<endl;
- cin >> row >> column;
- }
- while (board[(row - 1)][(column - 1)] != '-');
- board[(row - 1)][(column - 1)] = play2sym;
- }
- check = checkwin(board);
- scratch = tie(board);
- if (check == true)
- end(board, play1sym, play2sym, player1, player2);
- else if (scratch == true)
- end(board, play1sym, play2sym, player1, player2);
- else
- count++;
- }
- return 0;
- }
- void displayboard(char displayboard[3][3])
- {
- int rownum = 0, colnum;
- cout << " 1 2 3" << endl;
- for (colnum = 0; colnum <= 2; colnum++, cout<< endl)
- {
- cout << (colnum + 1);
- for (rownum = 0; rownum <= 2; rownum++)
- cout<< setw(4) << displayboard[rownum][colnum];
- }
- }
- bool checkwin(char checkboard[3][3])
- {
- if (checkboard[0][0] == checkboard[1][0] && checkboard[0][0] == checkboard[2][0] && checkboard[0][0] != '-')
- return true;
- else if (checkboard[0][1] == checkboard[1][1] && checkboard[0][1] == checkboard[2][1] && checkboard[0][1] != '-')
- return true;
- else if (checkboard[0][2] == checkboard[1][2] && checkboard[0][2] == checkboard[2][2] && checkboard[0][2] != '-')
- return true;
- else if (checkboard[0][0] == checkboard[0][1] && checkboard[0][0] == checkboard[0][2] && checkboard[0][0] != '-')
- return true;
- else if (checkboard[1][0] == checkboard[1][1] && checkboard[1][0] == checkboard[1][2] && checkboard[1][0] != '-')
- return true;
- else if (checkboard[2][0] == checkboard[2][1] && checkboard[2][0] == checkboard[2][2] && checkboard[2][0] != '-')
- return true;
- else if (checkboard[0][0] == checkboard[1][1] && checkboard[0][0] == checkboard[2][2] && checkboard[0][0] != '-')
- return true;
- else if (checkboard[0][2] == checkboard[1][1] && checkboard[0][2] == checkboard[2][0] && checkboard[0][2] != '-')
- return true;
- else
- return false;
- }
- void end(char endgame[3][3], char sym1, char sym2, string play1, string play2 )
- {
- char sel;
- displayboard(endgame);
- if (endgame[0][0] == endgame[1][0] && endgame[0][0] == endgame[2][0])
- {
- if (endgame[0][0] == sym1)
- cout << play1 << " wins!!! Good Job!!!" << endl;
- else
- cout << play2 << " wins!!! Good Job!!!" << endl;
- }
- else if (endgame[0][1] == endgame[1][1] && endgame[0][1] == endgame[2][1])
- {
- if (endgame[0][1] == sym1)
- cout << play1 << " wins!!! Good Job!!!" << endl;
- else
- cout << play2 << " wins!!! Good Job!!!" << endl;
- }
- else if (endgame[0][2] == endgame[1][2] && endgame[0][2] == endgame[2][2])
- {
- if (endgame[0][2] == sym1)
- cout << play1 << " wins!!! Good Job!!!" << endl;
- else
- cout << play2 << " wins!!! Good Job!!!" << endl;
- }
- else if (endgame[0][0] == endgame[0][1] && endgame[0][0] == endgame[0][2])
- {
- if (endgame[0][0] == sym1)
- cout << play1 << " wins!!! Good Job!!!" << endl;
- else
- cout << play2 << " wins!!! Good Job!!!" << endl;
- }
- else if (endgame[1][0] == endgame[1][1] && endgame[1][0] == endgame[1][2])
- {
- if (endgame[1][0] == sym1)
- cout << play1 << " wins!!! Good Job!!!" << endl;
- else
- cout << play2 << " wins!!! Good Job!!!" << endl;
- }
- else if (endgame[2][0] == endgame[2][1] && endgame[2][0] == endgame[2][2])
- {
- if (endgame[2][0] == sym1)
- cout << play1 << " wins!!! Good Job!!!" << endl;
- else
- cout << play2 << " wins!!! Good Job!!!" << endl;
- }
- else if (endgame[0][0] == endgame[1][1] && endgame[0][0] == endgame[2][2])
- {
- if (endgame[0][0] == sym1)
- cout << play1 << " wins!!! Good Job!!!" << endl;
- else
- cout << play2 << " wins!!! Good Job!!!" << endl;
- }
- else if (endgame[0][2] == endgame[1][1] && endgame[0][2] == endgame[2][0])
- {
- if (endgame[0][2] == sym1)
- cout << play1 << " wins!!! Good Job!!!" << endl;
- else
- cout << play2 << " wins!!! Good Job!!!" << endl;
- }
- else if (endgame[0][0] != '-' &&
- endgame[0][1] != '-' &&
- endgame[0][2] != '-' &&
- endgame[1][0] != '-' &&
- endgame[1][1] != '-' &&
- endgame[1][2] != '-' &&
- endgame[2][0] != '-' &&
- endgame[2][1] != '-' &&
- endgame[2][2] != '-')
- {
- cout << "It's a tie. No one wins." << endl;
- }
- cout << "Would you like to play again? Press y for yes or n for no." <<endl;
- cin >> sel;
- do
- {
- if (sel == 'y' || sel == 'Y')
- {
- cout << endl;
- main();
- }
- else if (sel == 'n' || sel == 'N')
- exit(1);
- }
- while (sel != 'y' && sel != 'Y' && sel != 'n' && sel != 'N');
- }
- void instructions()
- {
- cout << "This is a simple 2 player game. You'll input the 2 players' names, then begin to play." << endl << endl
- << "When selecting a space, first enter the column and press 'enter'(counting from the left)." << endl << endl
- << "Then, enter the row and press 'enter' (counting from top)." << endl << endl
- << "Remember to always begin counting at '1'. Therefore, when entering a row and column," << endl << endl
- << " you can enter a number between (and including) 1 and 3." << endl << endl;
- }
- bool tie(char nowinner[3][3])
- {
- if (nowinner[0][0] != '-' &&
- nowinner[0][1] != '-' &&
- nowinner[0][2] != '-' &&
- nowinner[1][0] != '-' &&
- nowinner[1][1] != '-' &&
- nowinner[1][2] != '-' &&
- nowinner[2][0] != '-' &&
- nowinner[2][1] != '-' &&
- nowinner[2][2] != '-')
- return true;
- else
- return false;
- }
Expand|Select|Wrap|Line Numbers
- #include <iostream>
- #include <string>
- #include <iomanip>
- #include <cstdlib>
- using namespace std;
- int cols = 3, rows = 3;
- void displayboard(char [3][3]);
- bool checkwin(char [3][3]);
- void end(char [3][3], char, char, string, string);
- void instructions();
- bool tie(char [3][3]);
- /*A simple game of Tic-Tac-Toe*/
- #define SPACE ' '
- char board[3][3]= {'-', '-', '-',
- '-', '-', '-',
- '-', '-', '-',};
- void get_computer_move(void), get_player_move(void);
- main()
- {
- char done;
- int row, column, count = 1;
- char play1sym, play2sym, X, O, x, o, sel1;
- bool check = false;
- bool scratch;
- cout<<"This is the game of Tic-Tac-Toe."<<endl;
- cout<<"You will be playing against the computer."<<endl;
- done = SPACE;
- do
- {
- displayboard(board);/*display the game board*/
- get_player_move();/*get your move*/
- done = check();/*see if winner*/
- if(done != SPACE)
- break;/*winner!*/
- get_computer_move();/*get computer's move*/
- done = check();/*see if winner*/
- }
- while(done == SPACE);
- if(done == 'X')
- cout<<"You won!"<<endl;
- else
- cout<<"I won!!!!"<<endl;
- displayboard(board);/*show final positions*/
- //getch();
- return 0;
- }
- /*Input the players move*/
- void get_player_move(void)
- {
- int row, column;
- cout<<"Enter coordinates for your X: ";
- cin>> row>> column;
- row--;
- column--;
- if(matrix[row][column] != SPACE)
- {
- cout<<"Invalid move,try again."<<endl;
- get_player_move();
- }
- else matrix[row][column]= 'X';
- }
- /*Get the computer's move*/
- void get_computer_move(void)
- {
- register int t;
- char *p;
- p = (char *)board;
- for(t = 0; *p != SPACE && t < 9; ++t) p++;
- if(t == 9)
- {
- cout<<"draw"<<endl;
- exit(0);/*game over*/
- }
- else *p = 'O';
- }
- /*Display the game board*/
- void displayboard(char displayboard[3][3])
- {
- int rownum = 0, colnum;
- cout << " 1 2 3" << endl;
- for (colnum = 0; colnum <= 2; colnum++, cout<< endl)
- {
- cout << (colnum + 1);
- for (rownum = 0; rownum <= 2; rownum++)
- cout<< setw(4) << displayboard[rownum][colnum];
- }
- )
- /*See if there is a winner*/
- bool checkwin(char checkboard[3][3])
- {
- if (checkboard[0][0] == checkboard[1][0] && checkboard[0][0] == checkboard[2][0] && checkboard[0][0] != '-')
- return true;
- else if (checkboard[0][1] == checkboard[1][1] && checkboard[0][1] == checkboard[2][1] && checkboard[0][1] != '-')
- return true;
- else if (checkboard[0][2] == checkboard[1][2] && checkboard[0][2] == checkboard[2][2] && checkboard[0][2] != '-')
- return true;
- else if (checkboard[0][0] == checkboard[0][1] && checkboard[0][0] == checkboard[0][2] && checkboard[0][0] != '-')
- return true;
- else if (checkboard[1][0] == checkboard[1][1] && checkboard[1][0] == checkboard[1][2] && checkboard[1][0] != '-')
- return true;
- else if (checkboard[2][0] == checkboard[2][1] && checkboard[2][0] == checkboard[2][2] && checkboard[2][0] != '-')
- return true;
- else if (checkboard[0][0] == checkboard[1][1] && checkboard[0][0] == checkboard[2][2] && checkboard[0][0] != '-')
- return true;
- else if (checkboard[0][2] == checkboard[1][1] && checkboard[0][2] == checkboard[2][0] && checkboard[0][2] != '-')
- return true;
- else
- return false;
- }
Thanks in advance...