diff --git a/include/board.h b/include/board.h new file mode 100644 index 0000000..36d1941 --- /dev/null +++ b/include/board.h @@ -0,0 +1,38 @@ +#pragma once + +#include +#include + +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> _grid; + }; + +} \ No newline at end of file diff --git a/include/game.h b/include/game.h new file mode 100644 index 0000000..7365793 --- /dev/null +++ b/include/game.h @@ -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(); + + }; + +} \ No newline at end of file diff --git a/include/logo.h b/include/logo.h new file mode 100644 index 0000000..1ad47d7 --- /dev/null +++ b/include/logo.h @@ -0,0 +1,2 @@ +#pragma once +void logo(); \ No newline at end of file diff --git a/include/player.h b/include/player.h new file mode 100644 index 0000000..c0ace49 --- /dev/null +++ b/include/player.h @@ -0,0 +1,14 @@ +#pragma once + +namespace TTT +{ + class Player + { + public: + Player(char symbol); + char getSymbol() const; + void changeSymbol(char symbol); + private: + char _playerSymbol; + }; +} \ No newline at end of file diff --git a/src/board.cpp b/src/board.cpp new file mode 100644 index 0000000..d23b88b --- /dev/null +++ b/src/board.cpp @@ -0,0 +1,94 @@ +#include "board.h" +#include + + +namespace TTT +{ + + Board::Board(): _grid(_boardSize, std::vector(_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; + } +} \ No newline at end of file diff --git a/src/game.cpp b/src/game.cpp new file mode 100644 index 0000000..263558d --- /dev/null +++ b/src/game.cpp @@ -0,0 +1,76 @@ +#include "game.h" +#include "logo.h" +#include +#include + +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..."<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 + } +} \ No newline at end of file diff --git a/src/logo.cpp b/src/logo.cpp new file mode 100644 index 0000000..1b62036 --- /dev/null +++ b/src/logo.cpp @@ -0,0 +1,27 @@ +#include "logo.h" +#include +/* +__________________ _______ _________ _______ _______ _________ _______ _______ +\__ __/\__ __/( ____ \ \__ __/( ___ )( ____ \ \__ __/( ___ )( ____ \ + ) ( ) ( | ( \/ ) ( | ( ) || ( \/ ) ( | ( ) || ( \/ + | | | | | | | | | (___) || | | | | | | || (__ + | | | | | | | | | ___ || | | | | | | || __) + | | | | | | | | | ( ) || | | | | | | || ( + | | ___) (___| (____/\ | | | ) ( || (____/\ | | | (___) || (____/\ + )_( \_______/(_______/ )_( |/ \|(_______/ )_( (_______)(_______/ + +*/ + +void logo() +{ + std::cout <<"__________________ _______ _________ _______ _______ _________ _______ _______ \n" + "\\__ __/\\__ __/( ____ \\ \\__ __/( ___ )( ____ \\ \\__ __/( ___ )( ____ \\\n" + " ) ( ) ( | ( \\/ ) ( | ( ) || ( \\/ ) ( | ( ) || ( \\/\n" + " | | | | | | | | | (___) || | | | | | | || (__ \n" + " | | | | | | | | | ___ || | | | | | | || __) \n" + " | | | | | | | | | ( ) || | | | | | | || ( \n" + " | | ___) (___| (____/\\ | | | ) ( || (____/\\ | | | (___) || (____/\\\n" + " )_( \\_______/(_______/ )_( |/ \\|(_______/ )_( (_______)(_______/\n" + " \n" + "\n"; +} \ No newline at end of file diff --git a/src/player.cpp b/src/player.cpp new file mode 100644 index 0000000..e49ee9e --- /dev/null +++ b/src/player.cpp @@ -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; + } +} \ No newline at end of file