Created
May 11, 2020 08:43
-
-
Save tasfik007/a528ea13e819d346836df642f729e164 to your computer and use it in GitHub Desktop.
TicTacToe game implemented using C++
This file contains hidden or 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
// *** This Template was created by Tasfik Rahman *** | |
#include <iostream> | |
#include <stdio.h> | |
#include <bits/stdc++.h> | |
#define PI pair<int> | |
#define PII pair<PI> | |
#define VI vector<int> | |
#define VII vector<VI> | |
#define SIZE 3 | |
using namespace std; | |
int board[SIZE][SIZE]; | |
void printBoard() | |
{ | |
for (int i = 0; i < SIZE; i++) | |
{ | |
for (int j = 0; j < SIZE; j++) | |
{ | |
if (board[i][j] == 0) | |
cout << "- "; | |
else if (board[i][j] == 1) | |
cout << "O "; | |
else if (board[i][j] == 2) | |
cout << "X "; | |
} | |
cout << endl; | |
} | |
} | |
bool validMove(int x,int y) | |
{ | |
return board[x][y]==0?true:false; | |
} | |
void makeMove(bool player) | |
{ | |
if(player)cout<<"Player 1(true) move:"<<endl; | |
else cout<<"Player 2(false) move:"<<endl; | |
int x,y; | |
cin>>x>>y; | |
if(validMove(x,y)) | |
board[x][y]= (player)?1:2; | |
else | |
{ | |
cout<<"Not a valid move!! try again."<<endl; | |
makeMove(player); | |
} | |
} | |
bool isRowMatch(bool player) | |
{ | |
int count_match=0; | |
int mv= player?1:2; | |
for(int i=0;i<SIZE;i++) | |
{ | |
for(int j=0;j<SIZE;j++) | |
{ | |
if(board[i][j]==mv)count_match++; | |
} | |
if (count_match==SIZE) return true; | |
count_match=0; | |
} | |
return false; | |
} | |
bool isColMatch(bool player) | |
{ | |
int count_match=0; | |
int mv= player?1:2; | |
for(int i=0;i<SIZE;i++) | |
{ | |
for(int j=0;j<SIZE;j++) | |
{ | |
if(board[j][i]==mv)count_match++; | |
} | |
if (count_match==SIZE) return true; | |
count_match=0; | |
} | |
return false; | |
} | |
bool isLeftDiagMatch(bool player) | |
{ | |
int count_match=0; | |
int mv= player?1:2; | |
for(int i=0;i<SIZE;i++) | |
{ | |
for(int j=0;j<SIZE;j++) | |
{ | |
if(i==j) | |
{ | |
if(board[i][j]==mv) count_match++; | |
} | |
} | |
} | |
return count_match==SIZE; | |
} | |
bool isRightDiagMatch(bool player) | |
{ | |
int count_match=0; | |
int mv= player?1:2; | |
for(int i=0;i<SIZE;i++) | |
{ | |
for(int j=0;j<SIZE;j++) | |
{ | |
if((SIZE-j+1)==i) | |
{ | |
if(board[i][j]==mv) count_match++; | |
} | |
} | |
} | |
return count_match==SIZE; | |
} | |
bool isDraw() | |
{ | |
int count_match=0; | |
for(int i=0;i<SIZE;i++) | |
{ | |
for(int j=0;j<SIZE;j++) | |
{ | |
if(board[i][j]!=0)count_match++; | |
} | |
} | |
return count_match==SIZE*SIZE; | |
} | |
bool isOver(bool player) | |
{ | |
return ( (isRowMatch(player)) || (isColMatch(player)) ||(isLeftDiagMatch(player)) | |
||(isRightDiagMatch(player)) || (isDraw())); | |
} | |
bool playGame() | |
{ | |
printBoard(); | |
bool player = true; | |
while(!isOver(!player)) | |
{ | |
makeMove(player); | |
system("cls"); | |
printBoard(); | |
player=!player; | |
// cout<<isRowMatch(player)<<", "<<isColMatch(player)<<", "<<isRightDiagMatch(player)<<", "<<isLeftDiagMatch(player)<<endl; | |
} | |
if(isDraw()) cout<<"Match is draw!!"; | |
else if(!player) cout<<"Player 1(true) won the match"; | |
else cout<<"Player 2(false) won the match"; | |
} | |
int main() | |
{ | |
playGame(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment