From 37c566567417a6419ba7fccdb9a3500f0b0a3367 Mon Sep 17 00:00:00 2001 From: PoliEcho Date: Tue, 4 Jun 2024 17:01:41 +0200 Subject: [PATCH] reformating --- src/gameplay.hpp | 2 +- src/input.cpp | 90 ++++++++++++++++++++++++------------------------ src/main.cpp | 17 ++++----- src/net.cpp | 80 +++++++++++++++++++++--------------------- 4 files changed, 94 insertions(+), 95 deletions(-) diff --git a/src/gameplay.hpp b/src/gameplay.hpp index 8790843..234929c 100644 --- a/src/gameplay.hpp +++ b/src/gameplay.hpp @@ -1,3 +1,3 @@ #include -void printBoard(const std::vector>& board); +void printBoard(const std::vector> &board); diff --git a/src/input.cpp b/src/input.cpp index 9e7cb4e..9fa6aa3 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -1,6 +1,6 @@ -#include -#include"global.hpp" -#include"input.hpp" +#include +#include "global.hpp" +#include "input.hpp" using std::cout; using std::cin; @@ -14,39 +14,39 @@ std::string serverIp_str; bool repeat = true; void ishost_check() { -char charishost; -while (repeat) { - cout << "will you be host or guest of this game?\n" << "(h/g): "; - cin >> charishost; + char charishost; + while (repeat) { + cout << "will you be host or guest of this game?\n" + << "(h/g): "; + cin >> charishost; - repeat = false; + repeat = false; - switch(charishost) { - case 'h': - ishost = true; - break; - case 'g': - ishost = false; - break; - default: - repeat = true; - } - -} + switch (charishost) { + case 'h': + ishost = true; + break; + case 'g': + ishost = false; + break; + default: + repeat = true; + } + } } void board_size_check() { -repeat = true; + repeat = true; -while (repeat) { - cout << "\nplease enter board size(for example: 3 for 3x3): "; + while (repeat) { + cout << "\nplease enter board size(for example: 3 for 3x3): "; - cin >> board_size; - if(cin.fail()) { - cerr << "integer please\n"; - cin.clear(); - cin.ignore(256,'\n'); + cin >> board_size; + if (cin.fail()) { + cerr << "integer please\n"; + cin.clear(); + cin.ignore(256, '\n'); } else { - if(board_size > 0) { + if (board_size > 0) { repeat = false; } else { cerr << "positive integer please\n"; @@ -62,23 +62,23 @@ void length_check() { cout << "\nplease enter needed length(e.g. how many symbols need to be next to each other to get point): "; cin >> length; - if(cin.fail()) { + if (cin.fail()) { cerr << "integer please\n"; cin.clear(); - cin.ignore(256,'\n'); + cin.ignore(256, '\n'); } else { - if(length > 0) { - if(length > board_size) { + if (length > 0) { + if (length > board_size) { cerr << "length cannot be biger than board size"; } else { repeat = false; } - } else { - cerr << "positive integer please\n"; - } + } else { + cerr << "positive integer please\n"; } } } +} void port_check() { repeat = true; @@ -87,27 +87,27 @@ void port_check() { cout << "\nplease enter port you want to use: "; cin >> port; - if(cin.fail()) { + if (cin.fail()) { cerr << "integer please (range: 1-65535)\n"; cin.clear(); - cin.ignore(256,'\n'); + cin.ignore(256, '\n'); + } else { + if (port > 0) { + repeat = false; } else { - if(port > 0) { - repeat = false; - } else { - cerr << "positive integer please\n"; - } + cerr << "positive integer please\n"; } } + } } void ip_check() { repeat = true; - while(repeat) { + while (repeat) { cout << "enter host ip or hostname: "; cin >> serverIp_str; - if(serverIp_str == "") { + if (serverIp_str == "") { cerr << "enter something!\n"; } else { diff --git a/src/main.cpp b/src/main.cpp index 8b481cb..24ba63f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,10 +1,10 @@ #include -#include +#include #include -#include"global.hpp" -#include"net.hpp" -#include"input.hpp" -#include"gameplay.hpp" +#include "global.hpp" +#include "net.hpp" +#include "input.hpp" +#include "gameplay.hpp" using std::cout; @@ -48,8 +48,9 @@ int main() { - if(ishost) { + if (ishost) { board_size_check(); + length_check(); } else { ip_check(); } @@ -57,7 +58,7 @@ int main() { { int init_net_status = init_net(); - if(init_net_status != 0){ + if (init_net_status != 0) { cerr << "failed to initialize network\n"; return 2; } @@ -73,4 +74,4 @@ int main() { return 0; - } +} diff --git a/src/net.cpp b/src/net.cpp index b556ac0..9235864 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -33,13 +33,13 @@ int init_net() { //buffer to send and receive messages with char msg[1500]; - - if(ishost) { + + if (ishost) { //code for host //setup a socket and connection tools sockaddr_in servAddr; - bzero((char*)&servAddr, sizeof(servAddr)); + bzero((char *)&servAddr, sizeof(servAddr)); servAddr.sin_family = AF_INET; servAddr.sin_addr.s_addr = htonl(INADDR_ANY); servAddr.sin_port = htons(port); @@ -47,14 +47,14 @@ int init_net() { //open stream oriented socket with internet address //also keep track of the socket descriptor int serverSd = socket(AF_INET, SOCK_STREAM, 0); - if(serverSd < 0) { + if (serverSd < 0) { cerr << "Error establishing the server socket\n"; return 2; } //bind the socket to its local address - int bindStatus = bind(serverSd, (struct sockaddr*) &servAddr, + int bindStatus = bind(serverSd, (struct sockaddr *)&servAddr, sizeof(servAddr)); - if(bindStatus < 0) { + if (bindStatus < 0) { cerr << "Error binding socket to local address\n"; return 2; } @@ -68,7 +68,7 @@ int init_net() { //accept, create a new socket descriptor to //handle the new connection with client int newSd = accept(serverSd, (sockaddr *)&newSockAddr, &newSockAddrSize); - if(newSd < 0) { + if (newSd < 0) { cerr << "Error accepting request from guest\n"; return 2; } @@ -79,52 +79,52 @@ int init_net() { //also keep track of the amount of data sent as well int bytesRead, bytesWritten = 0; - memset(&msg, 0, sizeof(msg));//clear the buffer - bytesRead += recv(newSd, (char*)&msg, sizeof(msg), 0); - if( strcmp(msg, "concheck") != 0) { + memset(&msg, 0, sizeof(msg)); //clear the buffer + bytesRead += recv(newSd, (char *)&msg, sizeof(msg), 0); + if (strcmp(msg, "concheck") != 0) { handshake_failed(); } string data = std::to_string(board_size); memset(&msg, 0, sizeof(msg)); //clear the buffer strcpy(msg, data.c_str()); - bytesWritten += send(newSd, (char*)&msg, strlen(msg), 0); + bytesWritten += send(newSd, (char *)&msg, strlen(msg), 0); - memset(&msg, 0, sizeof(msg));//clear the buffer - bytesRead += recv(newSd, (char*)&msg, sizeof(msg), 0); - if(board_size != std::stoi( msg )) { + memset(&msg, 0, sizeof(msg)); //clear the buffer + bytesRead += recv(newSd, (char *)&msg, sizeof(msg), 0); + if (board_size != std::stoi(msg)) { handshake_failed(); } data = std::to_string(length); memset(&msg, 0, sizeof(msg)); //clear the buffer strcpy(msg, data.c_str()); - bytesWritten += send(newSd, (char*)&msg, strlen(msg), 0); + bytesWritten += send(newSd, (char *)&msg, strlen(msg), 0); - memset(&msg, 0, sizeof(msg));//clear the buffer - bytesRead += recv(newSd, (char*)&msg, sizeof(msg), 0); - if(length != std::stoi( msg )) { + memset(&msg, 0, sizeof(msg)); //clear the buffer + bytesRead += recv(newSd, (char *)&msg, sizeof(msg), 0); + if (length != std::stoi(msg)) { handshake_failed(); } - } else { - // code for client - const char *serverIp = serverIp_str.c_str(); + } else { + // code for client + const char *serverIp = serverIp_str.c_str(); //setup a socket and connection tools - struct hostent* host = gethostbyname(serverIp); + struct hostent *host = gethostbyname(serverIp); sockaddr_in sendSockAddr; - bzero((char*)&sendSockAddr, sizeof(sendSockAddr)); + bzero((char *)&sendSockAddr, sizeof(sendSockAddr)); sendSockAddr.sin_family = AF_INET; sendSockAddr.sin_addr.s_addr = - inet_addr(inet_ntoa(*(struct in_addr*)*host->h_addr_list)); + inet_addr(inet_ntoa(*(struct in_addr *)*host->h_addr_list)); sendSockAddr.sin_port = htons(port); int clientSd = socket(AF_INET, SOCK_STREAM, 0); //try to connect... int status = connect(clientSd, - (sockaddr*) &sendSockAddr, sizeof(sendSockAddr)); + (sockaddr *)&sendSockAddr, sizeof(sendSockAddr)); - if(status < 0) { - cout<<"Error connecting to socket!\n"; + if (status < 0) { + cout << "Error connecting to socket!\n"; return 2; } @@ -135,30 +135,28 @@ int init_net() { string data; data = "concheck"; - memset(&msg, 0, sizeof(msg));//clear the buffer + memset(&msg, 0, sizeof(msg)); //clear the buffer strcpy(msg, data.c_str()); - bytesWritten += send(clientSd, (char*)&msg, strlen(msg), 0); + bytesWritten += send(clientSd, (char *)&msg, strlen(msg), 0); - memset(&msg, 0, sizeof(msg));//clear the buffer - bytesRead += recv(clientSd, (char*)&msg, sizeof(msg), 0); - board_size = std::stoi( msg ); + memset(&msg, 0, sizeof(msg)); //clear the buffer + bytesRead += recv(clientSd, (char *)&msg, sizeof(msg), 0); + board_size = std::stoi(msg); data = std::to_string(board_size); - memset(&msg, 0, sizeof(msg));//clear the buffer + memset(&msg, 0, sizeof(msg)); //clear the buffer strcpy(msg, data.c_str()); - bytesWritten += send(clientSd, (char*)&msg, strlen(msg), 0); + bytesWritten += send(clientSd, (char *)&msg, strlen(msg), 0); - memset(&msg, 0, sizeof(msg));//clear the buffer - bytesRead += recv(clientSd, (char*)&msg, sizeof(msg), 0); - length = std::stoi( msg ); + memset(&msg, 0, sizeof(msg)); //clear the buffer + bytesRead += recv(clientSd, (char *)&msg, sizeof(msg), 0); + length = std::stoi(msg); data = std::to_string(length); - memset(&msg, 0, sizeof(msg));//clear the buffer + memset(&msg, 0, sizeof(msg)); //clear the buffer strcpy(msg, data.c_str()); - bytesWritten += send(clientSd, (char*)&msg, strlen(msg), 0); - + bytesWritten += send(clientSd, (char *)&msg, strlen(msg), 0); } return 0; } -