1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
int Choice; //Because i want to
char* Player1 ="Player 1";//For the player name
char* Player2 ="Player 2";
int State[10]={0}; //State of the squares from 1 to 9
bool Exit = false; //I want this global too
int Win=3;//Just to make the code a little bit shorter when checking wich one wins int he display.
class Game{public: //All the functions are here
static void Square(int square) { //Used in the display
if(State[square]==0){ //If state is 0, shows the square number
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 8);
if(square==1){cout << " 1 ";}
if(square==2){cout << " 2 ";}
if(square==3){cout << " 3 ";}
if(square==4){cout << " 4 ";}
if(square==5){cout << " 5 ";}
if(square==6){cout << " 6 ";}
if(square==7){cout << " 7 ";}
if(square==8){cout << " 8 ";}
if(square==9){cout << " 9 ";}
}
else if(State[square]==1){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
if(square==1 || square==4 || square==7){cout << " ";}
cout << " X "; //If state is 1, X
}
else if(State[square]==2){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14);
if(square==1 || square==4 || square==7){cout << " ";}
cout << " O "; //If state is 2, O
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
};
static void Check(int c) { //Used in the main game code
if(c==0){ //If everything is used...
if((State[1]>0 && State[2]>0 && State[3]>0)
&& (State[4]>0 && State[5]>0 && State[6]>0)
&& (State[7]>0 && State[8]>0 && State[9]>0)
){Win=0;} //It's a draw
return;
}
else{ //All the possible options of winning
if((State[1]==c && State[2]==c && State[3]==c)
|| (State[4]==c && State[5]==c && State[6]==c)
|| (State[7]==c && State[8]==c && State[9]==c)
|| (State[1]==c && State[4]==c && State[7]==c)
|| (State[2]==c && State[5]==c && State[8]==c)
|| (State[3]==c && State[6]==c && State[9]==c)
|| (State[1]==c && State[5]==c && State[9]==c)
|| (State[3]==c && State[5]==c && State[7]==c)
){Win=c; return;} //If someone wins, it shows it in the display
}
};
static void Display(int turn) { //Finally, the display itself.
system("cls");
if(Win>2){//If Win is'nt 0 or 1 or 2
if(turn==1){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
cout << "\n --" << Player1 << "'s turn--";
}
if(turn==2){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14);
cout << "\n --" << Player2 << "'s turn--";
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
cout << "\n Choose the square with numbers 1-9 to place an ";
if(turn==1){cout << "\" X \".\n";} else{cout << "\" O \".\n";}
}
//All the square displaying
cout << endl;
Game::Square(1); cout << "|";
Game::Square(2); cout << "|";
Game::Square(3); cout << "";
cout << "\n -----------\n";
Game::Square(4); cout << "|";
Game::Square(5); cout << "|";
Game::Square(6); cout << "";
cout << "\n -----------\n";
Game::Square(7); cout << "|";
Game::Square(8); cout << "|";
Game::Square(9); cout << endl;
//You understand this, no?
if(Win==0){cout << "\n --Draw--"; getch(); return;}
if(Win==1){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
cout << "\n --" << Player1 << " wins!--"; getch(); Exit=true; return;
}
if(Win==2){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14);
cout << "\n --" << Player2 << " wins!--"; getch(); Exit=true; return;
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
};
static void Main(int turn) { // And finally, the main game code.
Display(turn); //Displays and chooses wich turn it is
Loop: Choice=getch();
switch(Choice) { //All the keys to display an X or O in the screen
default: goto Loop;
/*1*/ case '1':
if(State[1]==0){State[1]=turn;} //Only editable if it has'nt been edited before
else{goto Loop;} //If it has been edited before, it loops the key scanning
/*2*/ case '2':
if(State[2]==0){State[2]=turn;}
else{goto Loop;}
break;
/*3*/ case '3':
if(State[3]==0){State[3]=turn;}
else{goto Loop;}
break;
/*4*/ case '4':
if(State[4]==0){State[4]=turn;}
else{goto Loop;}
break;
/*5*/ case '5':
if(State[5]==0){State[5]=turn;}
else{goto Loop;}
break;
/*6*/ case '6':
if(State[6]==0){State[6]=turn;}
else{goto Loop;}
break;
/*7*/ case '7':
if(State[7]==0){State[7]=turn;}
else{goto Loop;}
break;
/*8*/ case '8':
if(State[8]==0){State[8]=turn;}
else{goto Loop;}
break;
/*9*/ case '9':
if(State[9]==0){State[9]=turn;}
else{goto Loop;}
break;
}
Game::Check(1); //Checks if player 1 or player 2 wins
Game::Check(2);
if(Win==3){Game::Check(0);} //Then checks if it's a draw
};
}Game;
int main()
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
/* cout << "\n Player 1, choose a name\n";
cin >> Player1;
cout << "\n Player 2, choose a name\n";
cin >> Player2; */ //Just a fail, i'll try again later
while(!Exit) //You understand this too
{ //The turns
Game.Main(1);
Game.Main(2);
} //If exit...
cout << "\n Press any key to exit.\n";
getch();
cout << "\n Closing Window...\n";
return 0;
}
|