Open 30044364 opened 1 month ago
When the client presses 'y' the terminal will show a message asking the client to wait until the server has made its move.
Once the server has made its move the client will then be able to see the move that the server made and vice-versa. This will make the game a lot quicker.
Server's move being printed on the client terminal:
Note: that I have a separate loop for the client and server, both loops are located in the server file and all calculations are being done on the server side and passed on as a string (converted to a char pointer) to the client side.
Note: I have more single-use sending and receiving code at the top. However, for this purpose I have only shown the main sending and receiving loop within the client side, which is every simplistic due to it only needing to receive and sending a string. No calculation needed as it is done on the server side.
// Message to server loop
while (true) {
// Receive response from server
char buffer[1024] = { 0 };
int valread = recv(clientSocket, buffer, 1024, 0);
system("cls");
std::cout << "Server: " << buffer << std::endl;
// Sending message to sevrer
std::string message;
if (!message.empty())
std::cin.ignore();
// looking for the first word in the buffer
std::string firstWordBuffer = static_cast<std::string>(buffer).substr(0, static_cast<std::string>(buffer).find(" "));
if (firstWordBuffer == "Congratulations!") {
message = "Client matched a pair of cards\n";
send(clientSocket, message.c_str(), strlen(message.c_str()), 0);
std::cout << "Please wait for the server to make a move." << std::endl;
continue;
}
if (firstWordBuffer == "Sorry,") {
message = "Your turn\n";
send(clientSocket, message.c_str(), strlen(message.c_str()), 0);
std::cout << "Please wait for the server to make a move." << std::endl;
continue;
}
std::cout << "You: ";
std::getline(std::cin, message);
send(clientSocket, message.c_str(), strlen(message.c_str()), 0);
}
On the client side I have sent the game rules from the server. The client is given sometime to read the rules of the game and when ready simply press the 'y' key to continue.
Client terminal:![image](https://github.com/30044364/Metaverse_Networking_30044364/assets/91484544/1a30d360-9431-4058-8952-0575598bc00d)