Last active
March 24, 2018 00:02
-
-
Save Ottzoa/589d8e5937f976ee2aa4ca093921bd0e to your computer and use it in GitHub Desktop.
Main
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 "TicTacToe.h" | |
#include<iostream> | |
using namespace std; | |
int main() | |
{ | |
int choice; | |
int count; | |
int winner = 0; | |
char computer = 'O'; | |
char user= 'X'; | |
TicTacToe gameStart; | |
cout<< "The user is first" << endl; | |
cout << "User : X" << endl; | |
cout << "Computer : O" <<endl; | |
// Counter for game. If there are 4 loops(total of 8 slots of 9 on the board filled from both user and computer) | |
// without a winner then the game will be a draw. | |
for (count = 0; count < 4 && winner == 0; count ++) | |
{ | |
gameStart.printBoard(); | |
gameStart.playerTurn(user); | |
gameStart.computerTurn(computer); | |
winner = gameStart.winCheck(); | |
} | |
if (winner == 0) | |
{ | |
cout << "Game Draw" << endl; | |
} | |
else if (winner == computer) | |
{ | |
cout << "Computer wins" << endl; | |
} | |
else | |
{ | |
cout << "User won" << endl; | |
} | |
return 0; | |
} |
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 "TicTacToe.h" | |
#include<iostream> | |
#include<stdlib.h> | |
using namespace std; | |
TicTacToe::TicTacToe() | |
{ | |
int n = 1; | |
int i = 0; | |
int j = 0; | |
for (i = 0; i < 3; i++) | |
{ | |
for (j = 0; j < 3; j++) | |
{ | |
game[i][j] = '0' + n; | |
n++; | |
} | |
} | |
} | |
void TicTacToe::printBoard() | |
{ | |
for(int i = 0; i < 3; i++) | |
{ | |
for (int j = 0; j < 3; j++) | |
if (j != 2) | |
cout << game[i][j] << "|"; | |
else | |
cout << game[i][j]; | |
cout << endl; | |
} | |
} | |
void TicTacToe::computerTurn(char ch) | |
{ | |
bool validPosition= false; | |
do{ | |
int number = rand()%9+1; | |
char num = '0' + number; | |
int i = 0; | |
int j = 0; | |
for (i = 0; i < 3; i++) | |
{ | |
if (validPosition == true) | |
break; | |
for ( j = 0; j < 3; j++) | |
{ | |
if (game[i][j] == num) | |
{ | |
game[i][j] = ch; | |
validPosition = true; | |
break; | |
} | |
else | |
{ | |
validPosition = false; | |
} | |
} | |
} | |
}while((!validPosition)); | |
} | |
void TicTacToe::playerTurn(char ch) | |
{ | |
bool validPosition= false; | |
do{ | |
cout << "Enter a position: "; | |
int pos; | |
cin >> pos; | |
char num = '0'+ pos; | |
int i = 0; | |
int j = 0; | |
for (i = 0; i < 3; i++) | |
{ | |
if (validPosition == true) | |
break; | |
for ( j = 0; j < 3; j++) | |
{ | |
if (game[i][j] == num) | |
{ | |
cout << endl; | |
game[i][j] = ch; | |
validPosition = true; | |
break; | |
} | |
else | |
{ | |
validPosition = false; | |
} | |
} | |
} | |
}while((!validPosition)); | |
} | |
char TicTacToe::winCheck() | |
{ | |
int i, j; | |
for (i = 0; i < 3; i++) | |
{ | |
for (j = 0; j < 3; j++) | |
if (game[i][j] != 'X') | |
break; | |
if (j == 3) | |
return 'X'; | |
for (j = 0; j < 3; j++) | |
if (game[i][j] != 'O') | |
break; | |
if (j == 3) | |
return 'O'; | |
for (j = 0; j < 3; j++) | |
if (game[j][i] != 'X') | |
break; | |
if (j == 3) | |
return 'X'; | |
for (j = 0; j < 3; j++) | |
if (game[j][i] != 'O') | |
break; | |
if (j == 3) | |
return 'O'; | |
} | |
if (game[0][0] == game[1][1] == game[2][2] =='X') | |
return 'X'; | |
if (game[0][2] == game[1][1] == game[2][0]=='X') | |
return 'X'; | |
return 0; | |
} |
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
#ifndef TICTACTOE_H | |
#define TICTACTOE_H | |
#include<iostream> | |
class TicTacToe | |
{ | |
private: char game[3][3]; | |
public: | |
TicTacToe(); | |
void printBoard(); | |
void computerTurn(char ch); | |
void playerTurn (char ch); | |
char winCheck(); | |
}; | |
#endif // TICTACTOE_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment