Improve existing features

Improve command line experience:
- No longer need to provide host when running a server
- More helpful error messages for invalid arguments

Add player collision

Reduce RPC message sizes by having `Board::draw` not send the escape character to move the cursor to the top left, instead the client can do that
This commit is contained in:
ethanglide 2024-07-13 16:57:00 -04:00
parent a6a83eee7d
commit 19e4ab2813
4 changed files with 34 additions and 9 deletions

View File

@ -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)
{

View File

@ -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);
@ -29,7 +35,8 @@ namespace ConsoleGame
// 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));
}
}

View File

@ -15,6 +15,12 @@ namespace ConsoleGame
{
std::lock_guard<std::mutex> 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<std::mutex> 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;
}

View File

@ -5,15 +5,14 @@
int main(int argc, char **argv)
{
if (argc < 4)
if (argc < 3)
{
std::cerr << "Usage: " << argv[0] << "<client/server> <port> <host>" << std::endl;
std::cerr << "Usage: " << argv[0] << " <client/server> <port>" << 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 <port> <host>" << 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] << " <client/server> <port>" << std::endl;
return EXIT_FAILURE;
}