"Untitled Post" - Views: 135 · Hits: 135 - Type: Unlisted

#include <iostream>
#include <cmath>
#include <string>
#include <cstdlib>
using namespace std;

int choice;
string X = "X";
string O = "O";
const string PlayerMarkerX = "|X|";
const string PlayerMarkerO = "|O|";
int inputX;
int inputO;

/* WELCOME MESSAGE */
void welcome(){
cout << "WELCOME TO TIC-TAC-TOE" << endl << endl;
}

/* GAME INSTRUCTIONS */
void instructions(){
cout << endl;
cout << "TIC-TAC TOE is all about getting 3 lines in a row, horizontally or vertacally, or both." << endl << endl;
cout << "It is up to the player whether he/her chooses the 'O' or the 'X' to play with." << endl << endl << endl;
cout << "               NOTE: TYPE '-1' TO EXIT THE GAME ANYTIME!" << endl << endl;
cout << "                 This game was made using the console." << endl << endl;
}

/* GAME INPUT */
void input(){
do{
string Block0 = "|_|";
string Block1 = "|_|";
string Block2 = "|_|";
string Block3 = "|_|";
string Block4 = "|_|";
string Block5 = "|_|";
string Block6 = "|_|";
string Block7 = "|_|";
string Block8 = "|_|";
cout << endl;
cout << "Please enter a number betweeen 0 and 8" << endl;
    for(int times = 0; times < 9; times++){
cout << "< 0 - 2 >" << " " << Block0 << "" << Block1 << "" << Block2 << endl << "< 3 - 5 >" << " " << Block3 << "" << Block4 << "" << Block5 << endl << "< 6 - 8 >" << " " << Block6 << "" << Block7 << "" << Block8;
  if(Block0 == PlayerMarkerX && Block1 == PlayerMarkerX && Block2 == PlayerMarkerX || Block3 == PlayerMarkerX && Block4 == PlayerMarkerX && Block5 == PlayerMarkerX || Block6 == PlayerMarkerX && Block7 == PlayerMarkerX && Block8 == PlayerMarkerX ||
     Block0 == PlayerMarkerX && Block3 == PlayerMarkerX && Block6 == PlayerMarkerX || Block1 == PlayerMarkerX && Block4 == PlayerMarkerX && Block7 == PlayerMarkerX || Block2 == PlayerMarkerX && Block5 == PlayerMarkerX && Block8 == PlayerMarkerX ||
     Block0 == PlayerMarkerX && Block4 == PlayerMarkerX && Block8 == PlayerMarkerX || Block2 == PlayerMarkerX && Block4 == PlayerMarkerX && Block6 == PlayerMarkerX){
    cout << endl << endl << "X WON!" << endl << "NEW ROUND" << endl << endl;
    input();
}
else if(Block0 == PlayerMarkerO && Block1 == PlayerMarkerO && Block2 == PlayerMarkerO || Block3 == PlayerMarkerO && Block4 == PlayerMarkerO && Block5 == PlayerMarkerO || Block6 == PlayerMarkerO && Block7 == PlayerMarkerO && Block8 == PlayerMarkerO ||
        Block0 == PlayerMarkerO && Block3 == PlayerMarkerO && Block6 == PlayerMarkerO || Block1 == PlayerMarkerO && Block4 == PlayerMarkerO && Block7 == PlayerMarkerO || Block2 == PlayerMarkerO && Block5 == PlayerMarkerO && Block8 == PlayerMarkerO ||
        Block0 == PlayerMarkerO && Block4 == PlayerMarkerO && Block8 == PlayerMarkerO || Block2 == PlayerMarkerO && Block4 == PlayerMarkerO && Block6 == PlayerMarkerO){
    cout << endl << endl << "O WON!" << endl << "NEW ROUND" << endl << endl;
    input();
}
    do{
        if(times == 0){
            cout << endl << X << " starts: ";
                cin >> inputX;

            switch(inputX){
    case 0:
        Block0 = PlayerMarkerX;
    break;
    case 1:
        Block1 = PlayerMarkerX;
    break;
    case 2:
        Block2 = PlayerMarkerX;
    break;
    case 3:
        Block3 = PlayerMarkerX;
    break;
    case 4:
        Block4 = PlayerMarkerX;
    break;
    case 5:
        Block5 = PlayerMarkerX;
    break;
    case 6:
        Block6 = PlayerMarkerX;
    break;
    case 7:
        Block7 = PlayerMarkerX;
    break;
    case 8:
        Block8 = PlayerMarkerX;
    break;
    }
        }
        else if(times == 2 || times == 4 || times == 6 || times == 8){
            do{
            cout << endl << X << "'s turn: ";
                cin >> inputX;
if(inputX == 0 && Block0 == PlayerMarkerX || inputX == 0 && Block0 == PlayerMarkerO || inputX == 1 && Block1 == PlayerMarkerX || inputX == 1 && Block1 == PlayerMarkerO || inputX == 2 && Block2 == PlayerMarkerX || inputX == 2 && Block2 == PlayerMarkerO ||
   inputX == 3 && Block3 == PlayerMarkerX || inputX == 3 && Block3 == PlayerMarkerO || inputX == 4 && Block4 == PlayerMarkerX || inputX == 4 && Block4 == PlayerMarkerO || inputX == 5 && Block5 == PlayerMarkerX || inputX == 5 && Block5 == PlayerMarkerO ||
   inputX == 6 && Block6 == PlayerMarkerX || inputX == 6 && Block6 == PlayerMarkerO || inputX == 7 && Block7 == PlayerMarkerX || inputX == 7 && Block7 == PlayerMarkerO || inputX == 8 && Block8 == PlayerMarkerX || inputX == 8 && Block8 == PlayerMarkerO){
        if(Block0 == PlayerMarkerX || Block1 == PlayerMarkerX || Block2 == PlayerMarkerX || Block3 == PlayerMarkerX || Block4 == PlayerMarkerX || Block5 == PlayerMarkerX || Block6 == PlayerMarkerX || Block7 == PlayerMarkerX || Block8 == PlayerMarkerX){
            cout << endl << "board position in use" << endl;
        }
        else{
            cout << endl << "board position in use" << endl;
        }
}
            }while(inputX == 0 && Block0 == PlayerMarkerX || inputX == 0 && Block0 == PlayerMarkerO || inputX == 1 && Block1 == PlayerMarkerX || inputX == 1 && Block1 == PlayerMarkerO || inputX == 2 && Block2 == PlayerMarkerX || inputX == 2 && Block2 == PlayerMarkerO ||
   inputX == 3 && Block3 == PlayerMarkerX || inputX == 3 && Block3 == PlayerMarkerO || inputX == 4 && Block4 == PlayerMarkerX || inputX == 4 && Block4 == PlayerMarkerO || inputX == 5 && Block5 == PlayerMarkerX || inputX == 5 && Block5 == PlayerMarkerO ||
   inputX == 6 && Block6 == PlayerMarkerX || inputX == 6 && Block6 == PlayerMarkerO || inputX == 7 && Block7 == PlayerMarkerX || inputX == 7 && Block7 == PlayerMarkerO || inputX == 8 && Block8 == PlayerMarkerX || inputX == 8 && Block8 == PlayerMarkerO);

            switch(inputX){
    case 0:
        Block0 = PlayerMarkerX;
    break;
    case 1:
        Block1 = PlayerMarkerX;
    break;
    case 2:
        Block2 = PlayerMarkerX;
    break;
    case 3:
        Block3 = PlayerMarkerX;
    break;
    case 4:
        Block4 = PlayerMarkerX;
    break;
    case 5:
        Block5 = PlayerMarkerX;
    break;
    case 6:
        Block6 = PlayerMarkerX;
    break;
    case 7:
        Block7 = PlayerMarkerX;
    break;
    case 8:
        Block8 = PlayerMarkerX;
    }
        }
        else{
        do{
            cout << endl << O << "'s turn: ";
                cin >> inputO;
if(inputO == 0 && Block0 == PlayerMarkerX || inputO == 0 && Block0 == PlayerMarkerO || inputO == 1 && Block1 == PlayerMarkerX || inputO == 1 && Block1 == PlayerMarkerO || inputO == 2 && Block2 == PlayerMarkerX || inputO == 2 && Block2 == PlayerMarkerO ||
   inputO == 3 && Block3 == PlayerMarkerX || inputO == 3 && Block3 == PlayerMarkerO || inputO == 4 && Block4 == PlayerMarkerX || inputO == 4 && Block4 == PlayerMarkerO || inputO == 5 && Block5 == PlayerMarkerX || inputO == 5 && Block5 == PlayerMarkerO ||
 inputO == 6 && Block6 == PlayerMarkerX || inputO == 6 && Block6 == PlayerMarkerO || inputO == 7 && Block7 == PlayerMarkerX || inputO == 7 && Block7 == PlayerMarkerO || inputO == 8 && Block8 == PlayerMarkerX || inputO == 8 && Block8 == PlayerMarkerO){

            if(Block0 == PlayerMarkerO || Block1 == PlayerMarkerO || Block2 == PlayerMarkerO || Block3 == PlayerMarkerO || Block4 == PlayerMarkerO || Block5 == PlayerMarkerO || Block6 == PlayerMarkerO || Block7 == PlayerMarkerO || Block8 == PlayerMarkerO){
            cout << endl << "board position in use" << endl;
        }
            else{
            cout << endl << "board position in use" << endl;
        }
    cout << "TRY AGAIN" << endl;
 }
    }while(inputO == 0 && Block0 == PlayerMarkerX || inputO == 0 && Block0 == PlayerMarkerO || inputO == 1 && Block1 == PlayerMarkerX || inputO == 1 && Block1 == PlayerMarkerO || inputO == 2 && Block2 == PlayerMarkerX || inputO == 2 && Block2 == PlayerMarkerO ||
   inputO == 3 && Block3 == PlayerMarkerX || inputO == 3 && Block3 == PlayerMarkerO || inputO == 4 && Block4 == PlayerMarkerX || inputO == 4 && Block4 == PlayerMarkerO || inputO == 5 && Block5 == PlayerMarkerX || inputO == 5 && Block5 == PlayerMarkerO ||
 inputO == 6 && Block6 == PlayerMarkerX || inputO == 6 && Block6 == PlayerMarkerO || inputO == 7 && Block7 == PlayerMarkerX || inputO == 7 && Block7 == PlayerMarkerO || inputO == 8 && Block8 == PlayerMarkerX || inputO == 8 && Block8 == PlayerMarkerO);

            switch(inputO){
        case 0:
            Block0 = PlayerMarkerO;
        break;
        case 1:
            Block1 = PlayerMarkerO;
        break;
        case 2:
            Block2 = PlayerMarkerO;
        break;
        case 3:
            Block3 = PlayerMarkerO;
        break;
        case 4:
            Block4 = PlayerMarkerO;
        break;
        case 5:
            Block5 = PlayerMarkerO;
        break;
        case 6:
            Block6 = PlayerMarkerO;
        break;
        case 7:
            Block7 = PlayerMarkerO;
        break;
        case 8:
            Block8 = PlayerMarkerO;
            }
        }
        if(inputX == -1 || inputO == -1){
            cout << "Thankyou for playing!" << endl << endl;
            exit(EXIT_FAILURE);
        }
       else if(inputX > 8 || inputO > 8 || inputX < 0 || inputO < 0){
            cout << "Invalid input! You can only declare < 0 - 8 >" << endl << endl;
        }
            }while(inputX > 8 || inputO > 8 || inputX < 0 || inputO < 0);
        cout << endl;
    }
     cout << Block0 << "" << Block1 << "" << Block2 << endl << "" << Block3 << "" << Block4 << "" << Block5 << endl << Block6 << "" << Block7 << "" << Block8;
     cout << endl << "It's a draw!" << endl;
     cout << endl << endl << "NEW ROUND" << endl;
}while( ! (inputX == -1 || inputO == -1) );
 }

/* GAME MENU */
void menu(){
cout << "1. Play" << endl;
cout << "2. Instructions" << endl;
}

/* MENU AFTER READING INSTRUCTIONS */
void menu1(){
cout << "1. Play" << endl << endl;
cin >> choice;
    if(choice == 1){
        input();
    }
    else if(choice == 2){
        cout << endl << "You have already read the instructions!" << endl;
    }
    else{
        cout << endl << "You didn't enter a valid option!" << endl;
    }
}

/* MENU CHOICE */
void menu_choice(){
do{
cout << "< 1 || 2 >?: ";
cin >> choice;
switch(choice){

case 1:
    input();
break;
case 2:
    instructions();
break;
default:
    cout << "You didn't enter a valid option!";
    }
    cout << endl << endl;
    }while( ! (choice == 1 || choice == 2) );
}
int main(){
system("Color 1A");
welcome();
menu();
menu_choice();
    while( ! (choice == 1) ){
        menu1();
        }
return 0;
}