commit 5a1b22e8a02b85913e41f336bab2bed91bce303d Author: Philip Johansson Date: Wed Aug 25 17:28:53 2021 +0200 Test result 1h diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..95dd167 --- /dev/null +++ b/.clang-format @@ -0,0 +1,18 @@ +Language: Cpp +BasedOnStyle: LLVM +IndentWidth: 4 +SortIncludes: true +AccessModifierOffset: -4 +AlwaysBreakTemplateDeclarations: true +AllowShortFunctionsOnASingleLine: None +BinPackArguments: false +BinPackParameters: false +BreakBeforeBraces: Custom +BraceWrapping: + BeforeCatch: true + BeforeElse: true +PointerAlignment: Left +ColumnLimit: 100 + +AlwaysBreakAfterReturnType: None +PenaltyReturnTypeOnItsOwnLine: 1000000 \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2a02651 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +build/* +ExampleBinary \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..e0d08c1 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,49 @@ +{ + "files.associations": { + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "cstdint": "cpp", + "deque": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "optional": "cpp", + "random": "cpp", + "string": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "fstream": "cpp", + "initializer_list": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "new": "cpp", + "ostream": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "cinttypes": "cpp", + "typeinfo": "cpp" + } +} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..184ac0b --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,15 @@ +project(example_proj) + +cmake_minimum_required(VERSION 2.8.11) +set(CMAKE_CXX_STANDARD 17) + +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}) + +add_executable(ExampleBinary main.cpp) + +add_subdirectory(src) + +target_link_libraries(ExampleBinary + PUBLIC + src +) \ No newline at end of file diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..19e3fef --- /dev/null +++ b/main.cpp @@ -0,0 +1,37 @@ +#include "src/board.h" + +#include + +int main(int argc, char** argv) { + Board b; + + std::cout << "Starting game. Empty Board" << std::endl; + b.printBoard(); + + while (b.running()) { + for (auto player : {1, 2}) { + bool inputInvalid = true; + while (inputInvalid) { + + int x, y; + + std::cout << "Player " << player << " Input play... " << std::endl; + std::cout << "X pos: " << std::endl; + std::cin >> x; + std::cout << "Y pos: " << std::endl; + std::cin >> y; + + inputInvalid = !b.placeMark(x, y, static_cast(player)); + if (inputInvalid) + std::cout << "Invalid placement, try again" << std::endl; + b.printBoard(); + + auto winner = b.haveWinner(); + if (winner != Board::Mark::Empty) + std::cout << "We have a winner...... Player" << static_cast(player); + } + } + } + + return 0; +} \ No newline at end of file diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..aa0f940 --- /dev/null +++ b/readme.md @@ -0,0 +1,6 @@ +## Build the project +Cd to build folder and run: +``` +cmake . +cmake --build . +``` \ No newline at end of file diff --git a/settings.json b/settings.json new file mode 100644 index 0000000..23fd35f --- /dev/null +++ b/settings.json @@ -0,0 +1,3 @@ +{ + "editor.formatOnSave": true +} \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..62f130f --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,4 @@ +add_library(src + board.cpp + board.h +) \ No newline at end of file diff --git a/src/board.cpp b/src/board.cpp new file mode 100644 index 0000000..b99eecc --- /dev/null +++ b/src/board.cpp @@ -0,0 +1,123 @@ +#include "board.h" +#include +#include + +Board::Board() : m_matrix{} { + m_matrix[0][0] = Mark::Circle; + m_matrix[0][2] = Mark::Cross; +} + +bool Board::running() { +} + +Board::Mark Board::haveWinner() { + + auto haveWinner = [&](int v) -> Board::Mark { + if (v == 3) + return Mark::Circle; + else if (v == 6) + return Mark::Cross; + else + Mark::Empty; + }; + + // Evaluate rows + for (int y = 0; y < 3; y++) { + + int p = 0; + for (int x = 0; x < 3; x++) { + + p += static_cast(m_matrix[x][y]); + } + + auto winner = haveWinner(p); + if (winner != Mark::Empty) + return winner; + } + + // Evaluate columns + for (int x = 0; x < 3; x++) { + + int p = 0; + + for (int y = 0; y < 3; y++) { + p += static_cast(m_matrix[x][y]); + } + + auto winner = haveWinner(p); + if (winner != Mark::Empty) + return winner; + } + + { + int p = 0; + for (int i = 0; i < 3; i++) { + p += static_cast(m_matrix[i][i]); + } + auto winner = haveWinner(p); + if (winner != Mark::Empty) + return winner; + } + + { + int p = 0; + for (int y = 0; y < 3; y++) { + for (int x = 2; x < 0; x++) { + p += static_cast(m_matrix[x][y]); + } + } + auto winner = haveWinner(p); + if (winner != Mark::Empty) + return winner; + } + + return Mark::Empty; +} + +bool Board::boardFull() { + for (auto i : m_matrix) { + for (auto l : i) { + if (l == Mark::Empty) + return false; + } + } + return true; +} + +bool Board::placeMark(int x, int y, Mark m) { + + x -= 1; + y -= 1; + + if (!(m_matrix[x][y] == Mark::Empty)) { + return false; + } + m_matrix[x][y] = m; + return true; +} + +void Board::printBoard() { + + for (int y = 0; y < 3; y++) { + for (int x = 0; x < 3; x++) { + + std::string output; + + switch (m_matrix[x][y]) { + case Mark::Empty: + output = " "; + break; + case Mark::Circle: + output = "O"; + break; + case Mark::Cross: + output = "X"; + break; + } + std::cout << " | " << output; + } + + std::cout << " |"; + std::cout << std::endl; + } +} \ No newline at end of file diff --git a/src/board.h b/src/board.h new file mode 100644 index 0000000..0ea3095 --- /dev/null +++ b/src/board.h @@ -0,0 +1,32 @@ +#include + +class Board { + +public: + enum Mark { + Empty, + Circle, + Cross, + }; + + Board(); + + bool boardFull(); + + //! @brief Check if game is running + //! @return true if game is not finished + bool running(); + + Mark haveWinner(); + + //! @param x in range [1, 3] + //! ... + bool placeMark(int x, int y, Mark); + + void printBoard(); + +private: + using xArr = std::array; + + std::array m_matrix; +}; \ No newline at end of file