-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3fa6413
commit ae2d994
Showing
8 changed files
with
295 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#pragma once | ||
|
||
#include<vector> | ||
#include <cstddef> | ||
|
||
namespace TTT { | ||
class Board | ||
{ | ||
public: | ||
|
||
//strcuture to main the user provide coordination (xCoord, yCoord) | ||
struct Coordination { | ||
int xCoord; | ||
int yCoord; | ||
}; | ||
|
||
// Constructor to initialize the board | ||
Board(); | ||
|
||
//Methods for checking win/draw | ||
bool checkWin( Coordination coord ) const; | ||
bool checkDraw( int moveCount) const; | ||
|
||
//Method to display the board | ||
void displayBoard() const; | ||
|
||
//method to make move and update the board | ||
bool makeMove( Coordination coord, char playerSymbol); | ||
|
||
private: | ||
//seting board size as 3 for tic tac toe (3*3) | ||
const size_t _boardSize = 3; | ||
|
||
// ' ' for empty 'X' , 'O' as respectively | ||
std::vector<std::vector<char>> _grid; | ||
}; | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#pragma once | ||
|
||
#include "board.h" | ||
#include "player.h" | ||
|
||
namespace TTT | ||
{ | ||
class Game | ||
{ | ||
public: | ||
Game(); | ||
void run(); | ||
private: | ||
Board::Coordination _coord; | ||
Board _board; | ||
Player _playerX; | ||
Player _playerO; | ||
Player *_currentPlayer; | ||
int _moveCount = 0; | ||
|
||
void _changePlayer(); | ||
void _playerTurn(); | ||
void _result(); | ||
void _clearTerminal(); | ||
|
||
}; | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#pragma once | ||
void logo(); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#pragma once | ||
|
||
namespace TTT | ||
{ | ||
class Player | ||
{ | ||
public: | ||
Player(char symbol); | ||
char getSymbol() const; | ||
void changeSymbol(char symbol); | ||
private: | ||
char _playerSymbol; | ||
}; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
#include "board.h" | ||
#include <iostream> | ||
|
||
|
||
namespace TTT | ||
{ | ||
|
||
Board::Board(): _grid(_boardSize, std::vector<char>(_boardSize, ' ')) {} | ||
|
||
void Board::displayBoard() const | ||
{ | ||
for (size_t i = 0; i < _boardSize; i++) | ||
{ | ||
std::cout << "\t\t" << _grid[i][0]; | ||
|
||
for (size_t j = 1; j < _boardSize; j++) | ||
std::cout <<" | " << _grid[i][j]; | ||
|
||
if(i != _boardSize - 1) | ||
std::cout << "\n\t\t__________\n\n"; | ||
else std::cout << "\n"; | ||
} | ||
} | ||
|
||
bool Board::checkWin( Coordination coord ) const | ||
{ | ||
if(_grid[coord.xCoord][coord.yCoord] == ' ') | ||
return false; | ||
char playerSymbol = _grid[coord.xCoord][coord.yCoord]; | ||
|
||
//checking for column | ||
for(size_t i = 0; i < _boardSize; i++) | ||
{ | ||
if(_grid[coord.xCoord][i] != playerSymbol) | ||
break; | ||
else if( i == _boardSize - 1) | ||
return true; | ||
} | ||
|
||
//checking for row | ||
for(size_t i = 0; i < _boardSize; i++) | ||
{ | ||
if(_grid[i][coord.yCoord] != playerSymbol) | ||
break; | ||
else if( i == _boardSize - 1) | ||
return true; | ||
} | ||
|
||
//checking for diagonal | ||
if(coord.xCoord == coord.yCoord) | ||
{ | ||
for(size_t i = 0; i < _boardSize; i++) | ||
{ | ||
if(_grid[i][i] != playerSymbol) | ||
break; | ||
else if( i == _boardSize - 1) | ||
return true; | ||
} | ||
} | ||
|
||
//checking for antidiagonal | ||
if(coord.xCoord + coord.yCoord == (int)_boardSize - 1) | ||
{ | ||
for(size_t i = 0; i < _boardSize; i++) | ||
{ | ||
if(_grid[i][(coord.xCoord + coord.yCoord) - i] != playerSymbol) | ||
break; | ||
else if (i == _boardSize - 1) | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
bool Board::checkDraw(int moveCount) const | ||
{ | ||
if(moveCount == (int)(_boardSize * _boardSize)) | ||
return true; | ||
return false; | ||
} | ||
|
||
bool Board::makeMove(Coordination coord, char playerSymbol) | ||
{ | ||
if((coord.xCoord >= 0 && coord.xCoord < (int)_boardSize) | ||
&& (coord.yCoord >= 0 && coord.yCoord < (int)_boardSize) | ||
&& (_grid[coord.xCoord][coord.yCoord] == ' ')) | ||
{ | ||
_grid[coord.xCoord][coord.yCoord] = playerSymbol; | ||
return true; | ||
} | ||
return false; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#include "game.h" | ||
#include "logo.h" | ||
#include <iostream> | ||
#include <cstdlib> | ||
|
||
namespace TTT | ||
{ | ||
Game::Game(): _coord{0, 0}, _playerX('X'), _playerO('O'), _currentPlayer(&_playerX) {} | ||
|
||
void Game::run() | ||
{ | ||
_clearTerminal(); | ||
logo(); | ||
std::cout << "Welcome to Tic Tac Toe game..."<<std::endl; | ||
_board.displayBoard(); | ||
|
||
while (!_board.checkWin(_coord) && !_board.checkDraw(_moveCount)) | ||
{ | ||
_playerTurn(); | ||
_clearTerminal(); | ||
logo(); | ||
_board.displayBoard(); | ||
if (!_board.checkWin(_coord) && !_board.checkDraw(_moveCount)) | ||
_changePlayer(); | ||
|
||
} | ||
_result(); | ||
} | ||
|
||
void Game::_changePlayer() | ||
{ | ||
if(_currentPlayer == &_playerX) | ||
_currentPlayer = &_playerO; | ||
else _currentPlayer = &_playerX; | ||
} | ||
|
||
void Game::_playerTurn() | ||
{ | ||
// Board::Coordination coord; | ||
|
||
std::cout <<"Player " <<_currentPlayer->getSymbol() <<"'s turn."<< std::endl; | ||
std::cout <<"Enter the position (row column) {row(0..2) colomn(0..2)}: "; | ||
std::cin >> _coord.xCoord >> _coord.yCoord; | ||
|
||
while (!_board.makeMove(_coord, _currentPlayer->getSymbol())) | ||
{ | ||
std::cout <<"Invalid Move !!!\n" | ||
"You entered invalid position.\n" | ||
"Enter position Again: "; | ||
std::cin >> _coord.xCoord >> _coord.yCoord; | ||
} | ||
_moveCount++; | ||
} | ||
|
||
void Game::_result() | ||
{ | ||
if(_board.checkWin(_coord)) | ||
{ | ||
std::cout << "\nCongratulation Player " << _currentPlayer->getSymbol() | ||
<<", You win!\n"; | ||
} | ||
else | ||
{ | ||
std::cout << "\nIt's a draw!\n"; | ||
} | ||
} | ||
|
||
void Game::_clearTerminal() | ||
{ | ||
#ifdef _WIN32 | ||
std::system("cls"); // For Windows | ||
#else | ||
std::system("clear"); // For Linux and macOS | ||
#endif | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include "logo.h" | ||
#include <iostream> | ||
/* | ||
__________________ _______ _________ _______ _______ _________ _______ _______ | ||
\__ __/\__ __/( ____ \ \__ __/( ___ )( ____ \ \__ __/( ___ )( ____ \ | ||
) ( ) ( | ( \/ ) ( | ( ) || ( \/ ) ( | ( ) || ( \/ | ||
| | | | | | | | | (___) || | | | | | | || (__ | ||
| | | | | | | | | ___ || | | | | | | || __) | ||
| | | | | | | | | ( ) || | | | | | | || ( | ||
| | ___) (___| (____/\ | | | ) ( || (____/\ | | | (___) || (____/\ | ||
)_( \_______/(_______/ )_( |/ \|(_______/ )_( (_______)(_______/ | ||
*/ | ||
|
||
void logo() | ||
{ | ||
std::cout <<"__________________ _______ _________ _______ _______ _________ _______ _______ \n" | ||
"\\__ __/\\__ __/( ____ \\ \\__ __/( ___ )( ____ \\ \\__ __/( ___ )( ____ \\\n" | ||
" ) ( ) ( | ( \\/ ) ( | ( ) || ( \\/ ) ( | ( ) || ( \\/\n" | ||
" | | | | | | | | | (___) || | | | | | | || (__ \n" | ||
" | | | | | | | | | ___ || | | | | | | || __) \n" | ||
" | | | | | | | | | ( ) || | | | | | | || ( \n" | ||
" | | ___) (___| (____/\\ | | | ) ( || (____/\\ | | | (___) || (____/\\\n" | ||
" )_( \\_______/(_______/ )_( |/ \\|(_______/ )_( (_______)(_______/\n" | ||
" \n" | ||
"\n"; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#include "player.h" | ||
|
||
namespace TTT | ||
{ | ||
Player::Player(char symbol): _playerSymbol(symbol){} | ||
|
||
char Player::getSymbol() const | ||
{ | ||
return _playerSymbol; | ||
} | ||
|
||
void Player::changeSymbol(char symbol) | ||
{ | ||
_playerSymbol = symbol; | ||
} | ||
} |