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:
parent
a6a83eee7d
commit
19e4ab2813
@ -15,7 +15,7 @@ namespace ConsoleGame
|
|||||||
|
|
||||||
std::string Board::draw()
|
std::string Board::draw()
|
||||||
{
|
{
|
||||||
std::string board = "\033[H"; // Move cursor to the top left corner
|
std::string board;
|
||||||
|
|
||||||
for (auto row : cells)
|
for (auto row : cells)
|
||||||
{
|
{
|
||||||
|
@ -11,6 +11,12 @@ namespace ConsoleGame
|
|||||||
{
|
{
|
||||||
playerId = addPlayer();
|
playerId = addPlayer();
|
||||||
|
|
||||||
|
if (playerId == -1)
|
||||||
|
{
|
||||||
|
std::cerr << "Failed to add player" << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
std::thread renderThread(&GameClient::renderLoop, this);
|
std::thread renderThread(&GameClient::renderLoop, this);
|
||||||
std::thread inputThread(&GameClient::inputLoop, this);
|
std::thread inputThread(&GameClient::inputLoop, this);
|
||||||
|
|
||||||
@ -29,7 +35,8 @@ namespace ConsoleGame
|
|||||||
// replace all "|" with newline
|
// replace all "|" with newline
|
||||||
std::replace(res.begin(), res.end(), '|', '\n');
|
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));
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,12 @@ namespace ConsoleGame
|
|||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(mutex);
|
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
|
// Add a player to the game at a random position
|
||||||
int x, y;
|
int x, y;
|
||||||
do
|
do
|
||||||
@ -46,8 +52,12 @@ namespace ConsoleGame
|
|||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(mutex);
|
std::lock_guard<std::mutex> lock(mutex);
|
||||||
|
|
||||||
// Check if the new position is out of bounds
|
// 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())
|
if (newPos.first < 0 ||
|
||||||
|
newPos.first >= board.getWidth() ||
|
||||||
|
newPos.second < 0 ||
|
||||||
|
newPos.second >= board.getHeight() ||
|
||||||
|
board.getCell(newPos.first, newPos.second) != board.getFill())
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -5,15 +5,14 @@
|
|||||||
|
|
||||||
int main(int argc, char **argv)
|
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;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string mode = argv[1];
|
std::string mode = argv[1];
|
||||||
int port = std::stoi(argv[2]);
|
int port = std::stoi(argv[2]);
|
||||||
std::string host = argv[3];
|
|
||||||
|
|
||||||
if (mode == "server")
|
if (mode == "server")
|
||||||
{
|
{
|
||||||
@ -21,11 +20,20 @@ int main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
else if (mode == "client")
|
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);
|
ConsoleGame::GameClient client(host, port);
|
||||||
}
|
}
|
||||||
else
|
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;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user