Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <stdlib.h>
- #include <time.h>
- using namespace std;
- void hideandSeek();
- void tictacToe();
- int tttWin();
- void tttBoard();
- char matrix[10] = {'0','1','2','3','4','5','6','7','8','9'};
- int main()
- {
- string gameSelect;
- while (true)
- {
- cout << "\nWelcome to the game pack featuring Tic Tac Toe and Hide and Seek" << endl;
- cout << "Please choose Tic Tac Toe by entering 1 or Hide and Seek by entering 2" << endl;
- cin >> gameSelect;
- if (gameSelect == "1")
- {
- hideandSeek();
- gameSelect = "0";
- }
- else if (gameSelect == "2")
- {
- tictacToe();
- gameSelect = "0";
- }
- else if (gameSelect == "0")
- {
- cout << "\n\nWelcome to the game pack featuring Tic Tac Toe and Hide and Seek" << endl;
- cout << "Please choose Tic Tac Toe by entering 1 or Hide and Seek by entering 2" << endl;
- cin >> gameSelect;
- }
- else
- {
- cout << "Invalid selection, please enter again" << endl;
- }
- }
- return 0;
- }
- void hideandSeek()
- {
- cout << "\nWelcome to the hide and seek game!\nGuess where the computer is hiding among the 4 quadrants.\n";
- cout << "-----------------" << endl;
- cout << " | " << endl;
- cout << " | " << endl;
- cout << "-----------------" << endl;
- cout << " | " << endl;
- cout << " | " << endl;
- cout << "-----------------" << endl;
- string player_choice;
- srand (time(NULL));
- int comp_choice = rand() % 4 + 1;
- int user_choice;
- while (true)
- {
- cin >> user_choice;
- if (user_choice != comp_choice)
- {
- cout << "Incorrect choice! Try again.";
- }
- else
- {
- break;
- }
- }
- cout << "You guessed it! The computer was hiding in quadrant " << comp_choice;
- }
- void tictacToe()
- {
- int tttPlayer = 1, i;
- string tttChoice;
- char gameMark;
- do
- {
- tttBoard();
- tttPlayer = (tttPlayer%2) ?1:2;
- cout << "Welcome to the Tic Tac Toe game!\nGuess Requires two players to play. X is the first player.\n";
- cout << "Player " << tttPlayer << " , enter a number: ";
- cin >> tttChoice;
- gameMark = (tttPlayer == 1) ? 'X' : 'O';
- if (tttChoice == "1" && matrix[1] == '1')
- matrix[1] = gameMark;
- else if (tttChoice == "2" && matrix[2] == '2')
- matrix[2] = gameMark;
- else if (tttChoice == "3" && matrix[3] == '3')
- matrix[3] = gameMark;
- else if (tttChoice == "4" && matrix[4] == '4')
- matrix[4] = gameMark;
- else if (tttChoice == "5" && matrix[5] == '5')
- matrix[5] = gameMark;
- else if (tttChoice == "6" && matrix[6] == '6')
- matrix[6] = gameMark;
- else if (tttChoice == "7" && matrix[7] == '7')
- matrix[7] = gameMark;
- else if (tttChoice == "8" && matrix[8] == '8')
- matrix[8] = gameMark;
- else if (tttChoice == "9" && matrix[9] == '9')
- matrix[9] = gameMark;
- else
- {
- cout << "Invalid move ";
- tttPlayer --;
- cin.ignore();
- cin.get();
- }
- i=tttWin();
- tttPlayer++;
- }
- while (i== -1);
- tttBoard();
- if(i==1)
- {
- cout << "==>\aPlayer " <<--tttPlayer<<" win ";
- }
- else
- {
- cout << "==>\aGame draw";
- }
- cin.ignore();
- cin.get();
- }
- int tttWin()
- {
- if (matrix[1] == matrix[2] && matrix[2] == matrix[3])
- return 1;
- else if (matrix[4] == matrix[5] && matrix[5] == matrix[6])
- return 1;
- else if (matrix[7] == matrix[8] && matrix[8] == matrix[9])
- return 1;
- else if (matrix[1] == matrix[4] && matrix[4] == matrix[7])
- return 1;
- else if (matrix[2] == matrix[5] && matrix[5] == matrix[8])
- return 1;
- else if (matrix[3] == matrix[6] && matrix[6] == matrix[9])
- return 1;
- else if (matrix[1] == matrix[5] && matrix[5] == matrix[9])
- return 1;
- else if (matrix[3] == matrix[5] && matrix[5] == matrix[7])
- return 1;
- else if (matrix[1] != '1' && matrix[2] != '2' && matrix[3] != '3'&& matrix[4] != '4' && matrix[5] != '5' && matrix[6] !='6'&& matrix[7] != '7' && matrix[8] != '8' && matrix[9] != '9')
- return 0;
- else
- return -1;
- }
- void tttBoard()
- {
- cout << " | | " << endl;
- cout << " " << matrix[1] << " | " << matrix[2] << " | " << matrix[3] << endl;
- cout << " | | " << endl;
- cout << "--------------------" << endl;
- cout << " | | " << endl;
- cout << " " << matrix[4] << " | " << matrix[5] << " | " << matrix[6] << endl;
- cout << " | | " << endl;
- cout << "--------------------" << endl;
- cout << " | | " << endl;
- cout << " " << matrix[7] << " | " << matrix[8] << " | " << matrix[9] << endl;
- cout << " | | " << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement