diff --git a/cpp-console-game/Board.cpp b/cpp-console-game/Board.cpp index f5dc8bb..a34ad97 100644 --- a/cpp-console-game/Board.cpp +++ b/cpp-console-game/Board.cpp @@ -15,7 +15,7 @@ namespace ConsoleGame std::string Board::draw() { - std::string board = "\033[H"; // Move cursor to the top left corner + std::string board; for (auto row : cells) { diff --git a/cpp-console-game/GameClient.cpp b/cpp-console-game/GameClient.cpp index 8372c46..840a17e 100644 --- a/cpp-console-game/GameClient.cpp +++ b/cpp-console-game/GameClient.cpp @@ -11,6 +11,12 @@ namespace ConsoleGame { playerId = addPlayer(); + if (playerId == -1) + { + std::cerr << "Failed to add player" << std::endl; + return; + } + std::thread renderThread(&GameClient::renderLoop, this); std::thread inputThread(&GameClient::inputLoop, this); @@ -26,10 +32,11 @@ namespace ConsoleGame { auto res = client.call("draw", {}); - //replace all "|" with newline + // replace all "|" with newline std::replace(res.begin(), res.end(), '|', '\n'); - std::cout << res << std::endl; + std::cout << "\033[H" << res << std::endl; + std::this_thread::sleep_for(std::chrono::milliseconds(100)); } } diff --git a/cpp-console-game/GameState.cpp b/cpp-console-game/GameState.cpp index d1ab8ff..aee85b1 100644 --- a/cpp-console-game/GameState.cpp +++ b/cpp-console-game/GameState.cpp @@ -15,6 +15,12 @@ namespace ConsoleGame { std::lock_guard lock(mutex); + // Check if the game is full + if (players.size() >= board.getWidth() * board.getHeight()) + { + return -1; + } + // Add a player to the game at a random position int x, y; do @@ -46,8 +52,12 @@ namespace ConsoleGame { std::lock_guard lock(mutex); - // Check if the new position is out of bounds - if (newPos.first < 0 || newPos.first >= board.getWidth() || newPos.second < 0 || newPos.second >= board.getHeight()) + // Check if the new position is out of bounds or occupied + if (newPos.first < 0 || + newPos.first >= board.getWidth() || + newPos.second < 0 || + newPos.second >= board.getHeight() || + board.getCell(newPos.first, newPos.second) != board.getFill()) { return; } diff --git a/cpp-console-game/main.cpp b/cpp-console-game/main.cpp index 30978b2..3c431c6 100644 --- a/cpp-console-game/main.cpp +++ b/cpp-console-game/main.cpp @@ -5,15 +5,14 @@ int main(int argc, char **argv) { - if (argc < 4) + if (argc < 3) { - std::cerr << "Usage: " << argv[0] << " " << std::endl; + std::cerr << "Usage: " << argv[0] << " " << std::endl; return EXIT_FAILURE; } std::string mode = argv[1]; int port = std::stoi(argv[2]); - std::string host = argv[3]; if (mode == "server") { @@ -21,11 +20,20 @@ int main(int argc, char **argv) } else if (mode == "client") { + if (argc < 4) + { + std::cerr << "Usage: " << argv[0] << " client " << std::endl; + return EXIT_FAILURE; + } + + std::string host = argv[3]; + ConsoleGame::GameClient client(host, port); } else { - std::cerr << "Invalid mode" << std::endl; + std::cerr << "Invalid mode \"" << mode << "\"" << std::endl; + std::cerr << "Usage: " << argv[0] << " " << std::endl; return EXIT_FAILURE; }