Test result 1h
This commit is contained in:
commit
5a1b22e8a0
18
.clang-format
Normal file
18
.clang-format
Normal file
@ -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
|
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
build/*
|
||||
ExampleBinary
|
49
.vscode/settings.json
vendored
Normal file
49
.vscode/settings.json
vendored
Normal file
@ -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"
|
||||
}
|
||||
}
|
15
CMakeLists.txt
Normal file
15
CMakeLists.txt
Normal file
@ -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
|
||||
)
|
37
main.cpp
Normal file
37
main.cpp
Normal file
@ -0,0 +1,37 @@
|
||||
#include "src/board.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
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<Board::Mark>(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<int>(player);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
6
readme.md
Normal file
6
readme.md
Normal file
@ -0,0 +1,6 @@
|
||||
## Build the project
|
||||
Cd to build folder and run:
|
||||
```
|
||||
cmake .
|
||||
cmake --build .
|
||||
```
|
3
settings.json
Normal file
3
settings.json
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"editor.formatOnSave": true
|
||||
}
|
4
src/CMakeLists.txt
Normal file
4
src/CMakeLists.txt
Normal file
@ -0,0 +1,4 @@
|
||||
add_library(src
|
||||
board.cpp
|
||||
board.h
|
||||
)
|
123
src/board.cpp
Normal file
123
src/board.cpp
Normal file
@ -0,0 +1,123 @@
|
||||
#include "board.h"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
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<int>(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<int>(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<int>(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<int>(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;
|
||||
}
|
||||
}
|
32
src/board.h
Normal file
32
src/board.h
Normal file
@ -0,0 +1,32 @@
|
||||
#include <array>
|
||||
|
||||
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<Mark, 3>;
|
||||
|
||||
std::array<xArr, 3> m_matrix;
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user