Created
July 22, 2017 12:11
-
-
Save Ashley-Upson/81cf51b0aed4e1c1229754d94be9a060 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<iomanip> | |
using namespace std; | |
const int size = 3; | |
bool isGameOver = false; | |
int player = 1; | |
char player1 = 'X'; | |
char player2 = 'O'; | |
int input = 0; | |
int win = 0; | |
int n; | |
bool getinput(); | |
void draw(); | |
int check(); | |
bool Toggleplayer(); | |
char a[3][3] = { | |
{ '1', '2', '3' }, | |
{ '4', '5', '6' }, | |
{ '7', '8', '9' } | |
}; | |
// This is working kind of. | |
// It doesn't assign values in the array correctly. | |
// It doesn't end if the move-count is 9. | |
// Figure it out. | |
void draw(){ | |
system("cls"); | |
cout << "Player 1 'X' and Player 2 'O' " << endl; | |
for (int i = 0; i < size; i++){ | |
for (int j = 0; j < size; j++){ | |
cout << setw(5) << a[i][j]; | |
} | |
cout << endl << endl; | |
} | |
} | |
int main(){ | |
cout << "--------WELCOME TO TIC TAC TOE--------" << endl; | |
cout << "---------ISHFAQ FARIQ----------" << endl; | |
n = 0; | |
while (1) { | |
draw(); | |
getinput(); | |
n++; | |
} | |
while (check() == 0) { | |
if (check() == 1) { | |
cout << "Player1 Winz!!" << endl; | |
win = 1; | |
break; | |
} else if (check() == 2) { | |
cout << "player2 Winz!!!" << endl; | |
win = 1; | |
break; | |
} else if (check() == 0 && n == 9) { | |
cout << "Draw" << endl; | |
break; | |
} | |
} | |
system("pause"); | |
return 0; | |
} | |
bool getinput(){ | |
draw(); | |
if(player == 1) { | |
cout << "Player 1, Enter your input: "; | |
} else { | |
cout << "Player 2, Enter your input: "; | |
} | |
cin >> input; | |
if (input == 1 && a[0][0] == '1') | |
a[0][0] = player; | |
else if (input == 2 && a[0][1] == '2') | |
a[0][1] = player; | |
else if (input == 3 && a[0][2] == '3') | |
a[0][2] = player; | |
else if (input == 4 && a[1][0] == '4') | |
a[1][0] = player; | |
else if (input == 5 && a[1][1] == '5') | |
a[1][1] = player; | |
else if (input == 6 && a[1][2] == '6') | |
a[1][2] = player; | |
else if (input == 7 && a[2][0] == '7') | |
a[2][0] = player; | |
else if (input == 8 && a[2][1] == '8') | |
a[2][1] = player; | |
else if (input == 9 && a[2][2] == '9') | |
a[2][2] = player; | |
player = Toggleplayer(); | |
return true; | |
} | |
bool Toggleplayer(){ | |
return (player == 1 ? 2 : 1); | |
} | |
int check() { | |
// Player 1 checks. | |
if (a[0][0] == player1 && a[0][1] == player1 && a[0][2] == player1) return 1; | |
if (a[1][0] == player1 && a[1][1] == player1 && a[1][2] == player1) return 1; | |
if (a[2][0] == player1 && a[2][1] == player1 && a[2][2] == player1) return 1; | |
if (a[0][0] == player1 && a[1][0] == player1 && a[2][0] == player1) return 1; | |
if (a[0][1] == player1 && a[1][1] == player1 && a[2][1] == player1) return 1; | |
if (a[0][2] == player1 && a[1][2] == player1 && a[2][2] == player1) return 1; | |
if (a[0][0] == player1 && a[1][1] == player1 && a[2][2] == player1) return 1; | |
if (a[2][0] == player1 && a[1][1] == player1 && a[0][2] == player1) return 1; | |
// Player 2 checks. | |
if (a[0][0] == player2 && a[0][1] == player2 && a[0][2] == player2) return 2; | |
if (a[1][0] == player2 && a[1][1] == player2 && a[1][2] == player2) return 2; | |
if (a[2][0] == player2 && a[2][1] == player2 && a[2][2] == player2) return 2; | |
if (a[0][0] == player2 && a[1][0] == player2 && a[2][0] == player2) return 2; | |
if (a[0][1] == player2 && a[1][1] == player2 && a[2][1] == player2) return 2; | |
if (a[0][2] == player2 && a[1][2] == player2 && a[2][2] == player2) return 2; | |
if (a[0][0] == player2 && a[1][1] == player2 && a[2][2] == player2) return 2; | |
if (a[2][0] == player2 && a[1][1] == player2 && a[0][2] == player2) return 2; | |
else return '0'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment