Skip to content

Commit

Permalink
moved over everything and deleted curses content
Browse files Browse the repository at this point in the history
  • Loading branch information
r3w0p committed May 14, 2024
1 parent 38785e0 commit 2e3d58c
Show file tree
Hide file tree
Showing 10 changed files with 387 additions and 4 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Windows

on:
push:
branches: [ main ]
branches: [ tui ]

env:
BUILD_TYPE: Debug
Expand All @@ -17,10 +17,10 @@ jobs:
uses: actions/checkout@v3

- name: Install CMake and Ninja
uses: lukka/get-cmake@v3.25.2
uses: lukka/get-cmake@latest
with:
cmakeVersion: 3.23.2
ninjaVersion: 1.11.1
cmakeVersion: "~3.27.0" # use most recent 3.27.x version
ninjaVersion: "^1.0" # use most recent 1.x version

- name: CMake Setup
run: cmake -S . -B ${{github.workspace}}/build -G Ninja -D CMAKE_C_COMPILER=gcc -D CMAKE_CXX_COMPILER=g++ -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}
Expand Down
26 changes: 26 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ FetchContent_MakeAvailable(googletest)

include_directories(include)

add_library(controller
"include/caravan/controller/controller.h"

"src/caravan/controller/controller.cpp"
)

add_library(core
"include/caravan/core/common.h"
"include/caravan/core/exceptions.h"
Expand All @@ -53,14 +59,31 @@ add_library(model
"src/caravan/model/table.cpp"
)

add_library(user
"include/caravan/user/bot_easy.h"
"include/caravan/user/user.h"

"src/caravan/user/bot_easy.cpp"
"src/caravan/user/user.cpp"
)

add_library(view
"include/caravan/view/view.h"

"src/caravan/view/view.cpp"
)

# caravan.exe
add_executable(caravan
"src/caravan/main.cpp"
)

target_link_libraries(caravan
controller
core
model
user
view
ftxui::screen
ftxui::dom
ftxui::component
Expand All @@ -79,8 +102,11 @@ add_executable(tests

target_link_libraries(tests
GTest::gtest_main
controller
core
model
user
view
)

include(GoogleTest)
Expand Down
28 changes: 28 additions & 0 deletions include/caravan/controller/controller.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) 2022-2024 r3w0p
// The following code can be redistributed and/or
// modified under the terms of the GPL-3.0 License.

#ifndef CARAVAN_CONTROLLER_H
#define CARAVAN_CONTROLLER_H

#include <array>
#include "caravan/core/common.h"
#include "caravan/model/game.h"
#include "caravan/user/user.h"
#include "caravan/view/view.h"


class Controller {
protected:
Game *game_ptr;
View *view_ptr;
User *user_a_ptr;
User *user_b_ptr;
public:
explicit Controller(Game *g, View *v, User *ua, User *ub) :
game_ptr(g), view_ptr(v), user_a_ptr(ua), user_b_ptr(ub) {};

void run();
};

#endif //CARAVAN_CONTROLLER_H
18 changes: 18 additions & 0 deletions include/caravan/user/bot_easy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) 2022-2024 r3w0p
// The following code can be redistributed and/or
// modified under the terms of the GPL-3.0 License.

#ifndef CARAVAN_BOT_EASY_H
#define CARAVAN_BOT_EASY_H


#include "caravan/user/user.h"

class UserBotEasy : public UserBot {
public:
explicit UserBotEasy(PlayerName pn) : UserBot(pn) {};

GameOption generate_option(Game *g) override;
};

#endif //CARAVAN_BOT_EASY_H
37 changes: 37 additions & 0 deletions include/caravan/user/user.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) 2022-2024 r3w0p
// The following code can be redistributed and/or
// modified under the terms of the GPL-3.0 License.

#ifndef CARAVAN_USER_H
#define CARAVAN_USER_H

#include "caravan/core/common.h"
#include "caravan/model/game.h"

class User {
protected:
PlayerName name;
public:
explicit User(PlayerName pn) : name(pn) {};

PlayerName get_name() { return name; }

virtual bool is_human() = 0;
};

class UserHuman : public User {
public:
explicit UserHuman(PlayerName pn) : User(pn) {};

bool is_human() override;
};

class UserBot : public User {
public:
explicit UserBot(PlayerName pn) : User(pn) {};

bool is_human() override;
virtual GameOption generate_option(Game *g) = 0;
};

#endif //CARAVAN_USER_H
20 changes: 20 additions & 0 deletions include/caravan/view/view.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) 2022-2024 r3w0p
// The following code can be redistributed and/or
// modified under the terms of the GPL-3.0 License.

#ifndef CARAVAN_VIEW_H
#define CARAVAN_VIEW_H

#include <string>
#include "caravan/model/game.h"
#include "caravan/user/user.h"

class View {
public:
virtual void update(Game *g, User *ubottom, User *utop, GameOption* go_bottom, GameOption* go_top) = 0;
virtual GameOption option(Game *g, User *u) = 0;
virtual void close() = 0;
virtual void error_message(std::string msg) = 0;
};

#endif //CARAVAN_VIEW_H
66 changes: 66 additions & 0 deletions src/caravan/controller/controller.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) 2022-2024 r3w0p
// The following code can be redistributed and/or
// modified under the terms of the GPL-3.0 License.

#include "caravan/controller/controller.h"
#include <string>


void Controller::run() {
GameOption go_bottom;
GameOption go_top;
GameOption go_temp;
User *user_turn;
PlayerCaravanNames cvns;

go_bottom = {NO_OPTION};
go_top = {NO_OPTION};

do {
user_turn = game_ptr->get_player_turn() == user_a_ptr->get_name() ?
user_a_ptr : user_b_ptr;

// Only update view if current user is a human
if (user_turn->is_human())
view_ptr->update(game_ptr, user_a_ptr, user_b_ptr,
&go_bottom, &go_top);

try {
// Get user's next move
if (user_turn->get_name() == PLAYER_BOTTOM) {
go_temp = go_bottom;
go_bottom = view_ptr->option(game_ptr, user_turn);

// Immediately quit on exit request
if (go_bottom.type == OPTION_EXIT)
return;

// Attempt to play user's desired move
game_ptr->play_option(&go_bottom);

} else {
go_temp = go_top;
go_top = view_ptr->option(game_ptr, user_turn);

// Immediately quit on exit request
if (go_top.type == OPTION_EXIT)
return;

// Attempt to play user's desired move
game_ptr->play_option(&go_top);
}

} catch (CaravanException &e) {
view_ptr->error_message(e.what());

if (user_turn->get_name() == PLAYER_BOTTOM)
go_bottom = go_temp;
else
go_top = go_temp;
}

} while (game_ptr->get_winner() == NO_PLAYER);

// Update one last time in order to display the winner
view_ptr->update(game_ptr, user_a_ptr, user_b_ptr, &go_bottom, &go_top);
}
Loading

0 comments on commit 2e3d58c

Please sign in to comment.