diff --git a/PG1/firstchar.cc b/PG1/firstchar.cc index c272276..c4ec4e1 100644 --- a/PG1/firstchar.cc +++ b/PG1/firstchar.cc @@ -7,30 +7,30 @@ #include #include #include -#include #include + using namespace std; -int main(int argc, char **argv){ - cout << "Enter a word: "; +int main(int argc, char **argv) { + cout << "Enter a word: "; - char word[256]; - cin.getline(word, 256); - unsigned int input_length = strlen(word); - char out[input_length + 1]; + char word[256]; + cin.getline(word, 256); + unsigned int input_length = strlen(word); + char out[input_length + 1]; - unsigned int i, j; - for(i = 0, j = input_length - 1; i < input_length; i++, j--) out[i] = word[j]; - out[input_length] = '\0'; + unsigned int i, j; + for (i = 0, j = input_length - 1; i < input_length; i++, j--) out[i] = word[j]; + out[input_length] = '\0'; - pid_t pid = fork(); - if(pid == 0){ // child - execl("./lastchar", "lastchar", out, NULL); - } else { // parent - int status; - wait(&status); - cout << "First char: " << (char) WEXITSTATUS(status) << endl; - } + pid_t pid = fork(); + if (pid == 0) { // child + execl("./lastchar", "lastchar", out, NULL); + } else { // parent + int status; + wait(&status); + cout << "First char: " << (char) WEXITSTATUS(status) << endl; + } - return 0; + return 0; } diff --git a/PG1/forkandsquare.cc b/PG1/forkandsquare.cc index be0b265..6960485 100644 --- a/PG1/forkandsquare.cc +++ b/PG1/forkandsquare.cc @@ -10,64 +10,65 @@ #include #include #include + using namespace std; int send[2]; int ret[2]; -void child(){ - // the child process reads information from the read end of one pipe, - // parses it as an int, squares it, and writes that to the other pipe. +void child() { + // the child process reads information from the read end of one pipe, + // parses it as an int, squares it, and writes that to the other pipe. - close(send[1]); // close write end of send - close(ret[0]); // close read end of ret + close(send[1]); // close write end of send + close(ret[0]); // close read end of ret - fdistream send_in (send[0]); - string s; - getline(send_in, s); - send_in.close(); + fdistream send_in(send[0]); + string s; + getline(send_in, s); + send_in.close(); - int h = atoi(s.c_str()); - h *= h; + int h = atoi(s.c_str()); + h *= h; - fdostream ret_out (ret[1]); - ret_out << h << endl; - ret_out.close(); + fdostream ret_out(ret[1]); + ret_out << h << endl; + ret_out.close(); } -void parent(){ - // the parent process asks the user for an integer, writes that to a pipe, - // waits for the child to die, and prints what it finds on the read end of the other pipe. +void parent() { + // the parent process asks the user for an integer, writes that to a pipe, + // waits for the child to die, and prints what it finds on the read end of the other pipe. - close(send[0]); // close read end of send - close(ret[1]); // close write end of ret + close(send[0]); // close read end of send + close(ret[1]); // close write end of ret - cout << "Enter an integer to square: " << endl; - string s; - getline(cin, s); - fdostream send_out (send[1]); - send_out << s << endl; // pass inpt to child process through a pipe.... - send_out.close(); + cout << "Enter an integer to square: " << endl; + string s; + getline(cin, s); + fdostream send_out(send[1]); + send_out << s << endl; // pass inpt to child process through a pipe.... + send_out.close(); - int status; - wait(&status); + int status; + wait(&status); - fdistream ret_in (ret[0]); - getline(ret_in, s); - ret_in.close(); + fdistream ret_in(ret[0]); + getline(ret_in, s); + ret_in.close(); - cout << "result: " << s << endl; + cout << "result: " << s << endl; } -int main(int argc, char **argv){ - int send_result = pipe(send); - int ret_result = pipe(ret); - if(send_result == -1 || ret_result == -1) exit(1); +int main(int argc, char **argv) { + int send_result = pipe(send); + int ret_result = pipe(ret); + if (send_result == -1 || ret_result == -1) exit(1); - pid_t pid = fork(); - if(pid < 0) exit(1); // error check - else if(pid == 0) child(); - else parent(); + pid_t pid = fork(); + if (pid < 0) exit(1); // error check + else if (pid == 0) child(); + else parent(); - return 0; + return 0; } diff --git a/PG1/lastchar.cc b/PG1/lastchar.cc index b068fd4..3115426 100644 --- a/PG1/lastchar.cc +++ b/PG1/lastchar.cc @@ -2,13 +2,10 @@ // lastchar.cc takes one command line argument // returns the last char of that string -#include -#include #include + using namespace std; -int main(int argc, char **argv){ - string ret = argv[1]; - char c = argv[1][strlen(argv[1]) - 1]; - return c; +int main(int argc, char **argv) { + return argv[1][strlen(argv[1]) - 1]; } diff --git a/PG2/client/client.cc b/PG2/client/client.cc index 7d825a7..90a0d7d 100644 --- a/PG2/client/client.cc +++ b/PG2/client/client.cc @@ -15,82 +15,85 @@ #include #include #include "client.h" + using namespace std; -int main(int argc, char **argv){ +int main(int argc, char **argv) { - if(argc != 2){ - cout << "Please add the remote hostname as an argument." << endl; - exit(1); - } - // set up socket, connect to hostname - struct hostent *h; - sethostent(1); - h = gethostbyname(argv[1]); - endhostent(); - - int s = socket(AF_INET, SOCK_STREAM, 0); - sockaddr_in sin; - sin.sin_family = AF_INET; - sin.sin_port = htons(12346); - sin.sin_addr = (*(in_addr *) h->h_addr_list[0]); - int c = connect(s, (sockaddr *)&sin, sizeof(sockaddr_in)); - if(c < 0) {perror("connect"); exit(1);} - - fdistream sock_in (s); - fdostream sock_out (s); - - string exe, in_path, out_path; - char ch; - - while(1){ // keep running programs until user says N - cout << "Program to run: "; - getline(cin, exe); - cout << exe << " will be run " << endl; - sock_out << exe << endl; - - cout << "Input filepath: "; - getline(cin, in_path); - fstream in_file (in_path.c_str()); - - cout << "Output filepath: "; - getline(cin, out_path); - ofstream out_file (out_path.c_str()); - - // read and encode the input file - // every \0 is replaced with \0\0 - // after file is read completely, send \0\1 to signal eof without closing socket - // send input file now - while(in_file.get(ch)){ - if(ch == '\0') sock_out << '\0' << '\0'; - else sock_out << ch; + if (argc != 2) { + cout << "Please add the remote hostname as an argument." << endl; + exit(1); } - in_file.close(); - sock_out << '\0' << '\1'; - sock_out.flush(); + // set up socket, connect to hostname + struct hostent *h; + sethostent(1); + h = gethostbyname(argv[1]); + endhostent(); - // server should execute exe on in_file now - - // wait for response - while(sock_in.get(ch)){ - if(ch == '\0'){ - sock_in.get(ch); - if(ch == '\1') break; - else out_file << ch; - }else out_file << ch; + int s = socket(AF_INET, SOCK_STREAM, 0); + sockaddr_in sin; + sin.sin_family = AF_INET; + sin.sin_port = htons(12346); + sin.sin_addr = (*(in_addr *) h->h_addr_list[0]); + int c = connect(s, (sockaddr * ) & sin, sizeof(sockaddr_in)); + if (c < 0) { + perror("connect"); + exit(1); } - out_file.close(); - // ask if want to run another program - cout << "Would you like to run another program? [Y/N] "; - string cont; - getline(cin, cont); - if(cont == "n" || cont == "N") break; - else cout << "Ready to run another program." << endl; + fdistream sock_in(s); + fdostream sock_out(s); - } - sock_out.close(); - return 0; + string exe, in_path, out_path; + char ch; + + while (1) { // keep running programs until user says N + cout << "Program to run: "; + getline(cin, exe); + cout << exe << " will be run " << endl; + sock_out << exe << endl; + + cout << "Input filepath: "; + getline(cin, in_path); + fstream in_file(in_path.c_str()); + + cout << "Output filepath: "; + getline(cin, out_path); + ofstream out_file(out_path.c_str()); + + // read and encode the input file + // every \0 is replaced with \0\0 + // after file is read completely, send \0\1 to signal eof without closing socket + // send input file now + while (in_file.get(ch)) { + if (ch == '\0') sock_out << '\0' << '\0'; + else sock_out << ch; + } + in_file.close(); + sock_out << '\0' << '\1'; + sock_out.flush(); + + // server should execute exe on in_file now + + // wait for response + while (sock_in.get(ch)) { + if (ch == '\0') { + sock_in.get(ch); + if (ch == '\1') break; + else out_file << ch; + } else out_file << ch; + } + out_file.close(); + + cout << "Would you like to run another program? [Y/N] "; + string cont; + getline(cin, cont); + if (cont == "n" || cont == "N") break; + else cout << "Ready to run another program." << endl; + + } + sock_out.close(); + return 0; } diff --git a/PG2/client/client.h b/PG2/client/client.h index fe9ad31..bccfb35 100644 --- a/PG2/client/client.h +++ b/PG2/client/client.h @@ -14,7 +14,9 @@ #include #include #include + using namespace std; + // method definitions int main(int argc, char **argv); diff --git a/PG2/server/server.cc b/PG2/server/server.cc index 09ec246..45168f1 100644 --- a/PG2/server/server.cc +++ b/PG2/server/server.cc @@ -17,79 +17,83 @@ #include #include #include "server.h" + using namespace std; -int main(int argc, char **argv){ +int main(int argc, char **argv) { - int sc = socket(AF_INET, SOCK_STREAM, 0); - sockaddr_in sin; - sin.sin_family = AF_INET; - sin.sin_port = htons(12346); - sin.sin_addr.s_addr = INADDR_ANY; + int sc = socket(AF_INET, SOCK_STREAM, 0); + sockaddr_in sin; + sin.sin_family = AF_INET; + sin.sin_port = htons(12346); + sin.sin_addr.s_addr = INADDR_ANY; - int b = bind(sc, (sockaddr *)&sin, sizeof(sockaddr_in)); - if(b < 0) {perror("bind"), exit(1);} + int b = bind(sc, (sockaddr * ) & sin, sizeof(sockaddr_in)); + if (b < 0) { perror("bind"), exit(1); } - listen(sc, 1); + listen(sc, 1); - int s = accept(sc, NULL, NULL); - if(s < 0) {perror("accept"); exit(1);} - - fdistream sock_in(s); - fdostream sock_out(s); - - string exe; - char c; - - while(1){ - getline(sock_in, exe); - if(sock_in.eof()) break; - cout << exe << endl; - // get the input file from the socket and decode it - // input file terminates at \0\1 - ofstream tmp ("input_file.txt"); - while(sock_in.get(c)){ - if(c == '\0'){ - sock_in.get(c); - if(c == '\1'){ // end the input file - cout << "---end of input file---" << endl; - break; - }else tmp << c; - }else tmp << c; + int s = accept(sc, NULL, NULL); + if (s < 0) { + perror("accept"); + exit(1); } - tmp.close(); - pid_t pid = fork(); - if(pid < 0) exit(1); - if(pid > 0){ // parent - int status; - wait(&status); + fdistream sock_in(s); + fdostream sock_out(s); - cout << exe << " done. sending results back down the socket." << endl; + string exe; + char c; - ifstream tm ("temp.tmp"); - while(tm.get(c)){ - if(c == '\0') sock_out << '\0' << '\0'; - else sock_out << c; - } - tm.close(); - sock_out << '\0' << '\1'; - sock_out.flush(); + while (1) { + getline(sock_in, exe); + if (sock_in.eof()) break; + cout << exe << endl; + // get the input file from the socket and decode it + // input file terminates at \0\1 + ofstream tmp("input_file.txt"); + while (sock_in.get(c)) { + if (c == '\0') { + sock_in.get(c); + if (c == '\1') { // end the input file + cout << "---end of input file---" << endl; + break; + } else tmp << c; + } else tmp << c; + } + tmp.close(); - }else{ // child. program executed here + pid_t pid = fork(); + if (pid < 0) exit(1); + if (pid > 0) { // parent + int status; + wait(&status); - int fd = open("input_file.txt", O_RDONLY); - cout << "child process: about to execute " << exe << endl; - dup2(fd, 0); - close(fd); - fd = open("temp.tmp", O_TRUNC | O_CREAT | O_WRONLY, 0700); - dup2(fd, 1); - close(fd); - execl(exe.c_str(), exe.c_str(), NULL); + cout << exe << " done. sending results back down the socket." << endl; + + ifstream tm("temp.tmp"); + while (tm.get(c)) { + if (c == '\0') sock_out << '\0' << '\0'; + else sock_out << c; + } + tm.close(); + sock_out << '\0' << '\1'; + sock_out.flush(); + + } else { // child. program executed here + + int fd = open("input_file.txt", O_RDONLY); + cout << "child process: about to execute " << exe << endl; + dup2(fd, 0); + close(fd); + fd = open("temp.tmp", O_TRUNC | O_CREAT | O_WRONLY, 0700); + dup2(fd, 1); + close(fd); + execl(exe.c_str(), exe.c_str(), NULL); + + } } - } - - return 0; + return 0; } diff --git a/PG2/server/server.h b/PG2/server/server.h index 1cd7a8f..d4f8cf7 100644 --- a/PG2/server/server.h +++ b/PG2/server/server.h @@ -6,5 +6,4 @@ // method definitions int main(int argc, char **argv); - #endif diff --git a/PG3/pg3.cc b/PG3/pg3.cc index 0d6d915..25359e0 100644 --- a/PG3/pg3.cc +++ b/PG3/pg3.cc @@ -19,120 +19,124 @@ #include #include #include "pg3.h" + using namespace std; -char b64ch(char ch){ - static const char * b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - if(ch > 63) return '='; - return b64[(int)ch]; +char b64ch(char ch) { + static const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + if (ch > 63) return '='; + return b64[(int) ch]; } -int main(int argc, char **argv){ - if (argc != 3){ - cout << "Usage:" << endl << "\t./pg3 email mp3path" << endl << endl; - exit(1); - } - cout << "email to: " << argv[1] << endl; - cout << "file: " << argv[2] << endl; - - struct hostent *h; - sethostent(1); - h = gethostbyname("semail.nmu.edu"); - endhostent(); - - int s = socket(AF_INET, SOCK_STREAM, 0); - sockaddr_in sin; - sin.sin_family = AF_INET; - sin.sin_port = htons(25); - sin.sin_addr = (*(in_addr *) h->h_addr_list[0]); - int c = connect(s, (sockaddr *)&sin, sizeof(sockaddr_in)); - if(c < 0) {perror("connect"); exit(1);} - - fdostream out_sock (s); - fdistream in_sock (s); - - string line; - getline(in_sock, line); - - out_sock << "HELO semail.nmu.edu" << endl; - getline(in_sock, line); - out_sock << "MAIL FROM: apoe@nmu.edu" << endl; - getline(in_sock, line); - out_sock << "RCPT TO: " << argv[1] << endl; - getline(in_sock, line); - out_sock << "DATA" << endl; - getline(in_sock, line); - - out_sock << "From: " << argv[1] << endl; - out_sock << "To: " << argv[1] << endl; - out_sock << "Subject: PG3 mail" << endl; - - out_sock << "MIME-Version: 1.0" << endl; - out_sock << "Content-Type: multipart/mixed; boundary=\"DRUMPFWALL\"" << endl; - out_sock << "--DRUMPFWALL" << endl; - out_sock << "Content-Type: text/plain" << endl; - - out_sock << endl << "Happy birthday! Here's an mp3!" << endl; - - out_sock << "--DRUMPFWALL" << endl; - out_sock << "Content-Disposition: attachment; filename="<< argv[2] << endl; - out_sock << "Content-Type: audio/mpeg3" << endl; - out_sock << "Content-Transfer-Encoding: base64" << endl; - - ifstream mp3_in (argv[2], ios::binary); - streampos filesize = mp3_in.tellg(); - mp3_in.seekg(0, ios::end); - unsigned int fsz = mp3_in.tellg() - filesize; - mp3_in.seekg(0, ios::beg); - - unsigned char b3[3]; - int i = 0, j = 0; - string b64; - - while(fsz--){ - // read three bytes - b3[i++] = mp3_in.get(); - // bitshift the chars - if(i == 3){ - // 11111100 >> 2 => 00111111 - b64 += b64ch((b3[0] & 0xFC) >> 2); - // 00000011 << 4 => 00110000 + 11110000 >> 4 => 00001111 - b64 += b64ch(((b3[0] & 0x03) << 4) + ((b3[1] & 0xF0) >> 4)); - // 00001111 << 2 => 00111100 + 11000000 >> 6 => 00000011 - b64 += b64ch(((b3[1] & 0x0F) << 2) + ((b3[2] & 0xC0) >> 6)); - // 00111111 - b64 += b64ch(b3[2] & 0x3F); - - i = 0; +int main(int argc, char **argv) { + if (argc != 3) { + cout << "Usage:" << endl << "\t./pg3 email mp3path" << endl << endl; + exit(1); } - } + cout << "email to: " << argv[1] << endl; + cout << "file: " << argv[2] << endl; - if(i){ // if there are remaining bytes (i is set to 0 when %3 = 0) - for(j = i; j < 3; j++) b3[j] = '\0'; - // 11111100 >> 2 => 00111111 - b64 += b64ch((b3[0] & 0xFC) >> 2); - // 00000011 << 4 => 00110000 + 11110000 >> 4 => 00001111 - b64 += b64ch(((b3[0] & 0x03) << 4) + ((b3[1] & 0xF0) >> 4)); - // 00001111 << 2 => 00111100 + 11000000 >> 6 => 00000011 - b64 += b64ch(((b3[1] & 0x0F) << 2) + ((b3[2] & 0xC0) >> 6)); - // 00111111 - b64 += b64ch(b3[2] & 0x3F); + struct hostent *h; + sethostent(1); + h = gethostbyname("semail.nmu.edu"); + endhostent(); - while(i++ < 3) b64 += b64ch(64); // pad with = - } + int s = socket(AF_INET, SOCK_STREAM, 0); + sockaddr_in sin; + sin.sin_family = AF_INET; + sin.sin_port = htons(25); + sin.sin_addr = (*(in_addr *) h->h_addr_list[0]); + int c = connect(s, (sockaddr * ) & sin, sizeof(sockaddr_in)); + if (c < 0) { + perror("connect"); + exit(1); + } - for(unsigned int q = 0; q < b64.length(); q++){ - out_sock << b64[q]; - // insert endl every 80 chars - if(q % 80 == 79) out_sock << endl; - } + fdostream out_sock(s); + fdistream in_sock(s); - out_sock << endl << "--DRUMPFWALL--" << endl; - out_sock << "." << endl; - getline(in_sock, line); + string line; + getline(in_sock, line); - out_sock.close(); - cout << "message sent" << endl; + out_sock << "HELO semail.nmu.edu" << endl; + getline(in_sock, line); + out_sock << "MAIL FROM: apoe@nmu.edu" << endl; + getline(in_sock, line); + out_sock << "RCPT TO: " << argv[1] << endl; + getline(in_sock, line); + out_sock << "DATA" << endl; + getline(in_sock, line); - return 0; + out_sock << "From: " << argv[1] << endl; + out_sock << "To: " << argv[1] << endl; + out_sock << "Subject: PG3 mail" << endl; + + out_sock << "MIME-Version: 1.0" << endl; + out_sock << "Content-Type: multipart/mixed; boundary=\"DRUMPFWALL\"" << endl; + out_sock << "--DRUMPFWALL" << endl; + out_sock << "Content-Type: text/plain" << endl; + + out_sock << endl << "Happy birthday! Here's an mp3!" << endl; + + out_sock << "--DRUMPFWALL" << endl; + out_sock << "Content-Disposition: attachment; filename=" << argv[2] << endl; + out_sock << "Content-Type: audio/mpeg3" << endl; + out_sock << "Content-Transfer-Encoding: base64" << endl; + + ifstream mp3_in(argv[2], ios::binary); + streampos filesize = mp3_in.tellg(); + mp3_in.seekg(0, ios::end); + unsigned int fsz = mp3_in.tellg() - filesize; + mp3_in.seekg(0, ios::beg); + + unsigned char b3[3]; + int i = 0, j = 0; + string b64; + + while (fsz--) { + // read three bytes + b3[i++] = mp3_in.get(); + // bitshift the chars + if (i == 3) { + // 11111100 >> 2 => 00111111 + b64 += b64ch((b3[0] & 0xFC) >> 2); + // 00000011 << 4 => 00110000 + 11110000 >> 4 => 00001111 + b64 += b64ch(((b3[0] & 0x03) << 4) + ((b3[1] & 0xF0) >> 4)); + // 00001111 << 2 => 00111100 + 11000000 >> 6 => 00000011 + b64 += b64ch(((b3[1] & 0x0F) << 2) + ((b3[2] & 0xC0) >> 6)); + // 00111111 + b64 += b64ch(b3[2] & 0x3F); + + i = 0; + } + } + + if (i) { // if there are remaining bytes (i is set to 0 when %3 = 0) + for (j = i; j < 3; j++) b3[j] = '\0'; + // 11111100 >> 2 => 00111111 + b64 += b64ch((b3[0] & 0xFC) >> 2); + // 00000011 << 4 => 00110000 + 11110000 >> 4 => 00001111 + b64 += b64ch(((b3[0] & 0x03) << 4) + ((b3[1] & 0xF0) >> 4)); + // 00001111 << 2 => 00111100 + 11000000 >> 6 => 00000011 + b64 += b64ch(((b3[1] & 0x0F) << 2) + ((b3[2] & 0xC0) >> 6)); + // 00111111 + b64 += b64ch(b3[2] & 0x3F); + + while (i++ < 3) b64 += b64ch(64); // pad with = + } + + for (unsigned int q = 0; q < b64.length(); q++) { + out_sock << b64[q]; + // insert endl every 80 chars + if (q % 80 == 79) out_sock << endl; + } + + out_sock << endl << "--DRUMPFWALL--" << endl; + out_sock << "." << endl; + getline(in_sock, line); + + out_sock.close(); + cout << "message sent" << endl; + + return 0; } diff --git a/PG4/client/client.cc b/PG4/client/client.cc index 29531f6..3eb333a 100644 --- a/PG4/client/client.cc +++ b/PG4/client/client.cc @@ -16,55 +16,59 @@ #include #include #include "client.h" + using namespace std; -int main(int argc, char **argv){ +int main(int argc, char **argv) { - if(argc != 2){ - cout << "Please add the remote hostname as an argument." << endl; - exit(1); - } - - struct hostent *h; - sethostent(1); - h = gethostbyname(argv[1]); - endhostent(); - - int s = socket(AF_INET, SOCK_STREAM, 0); - sockaddr_in sin; - sin.sin_family = AF_INET; - sin.sin_port = htons(12480); - sin.sin_addr = (*(in_addr *) h->h_addr_list[0]); - int c = connect(s, (sockaddr *)&sin, sizeof(sockaddr_in)); - if(c < 0) {perror("connect"); exit(1);} - - fdistream sock_in (s); - fdostream sock_out (s); - - string curr; - int currval = 0; - - for(;;){ - while(getline(sock_in, curr)){ - if(sock_in.eof()) break; - if(curr == "><") break; - if(curr == "> "){ - cout << ">> "; - break; - }else cout << curr << endl; + if (argc != 2) { + cout << "Please add the remote hostname as an argument." << endl; + exit(1); } - if(curr == "><") break; - getline(cin, curr); - currval = atoi(curr.c_str()); - if(currval < 1 || currval > 7){ - cout << "Not a valid move." << endl; - sock_out << 0 << endl; - }else{ - sock_out << curr << endl; - cout << "Waiting for other player to move." << endl; - } - } - sock_out.close(); - return 0; + struct hostent *h; + sethostent(1); + h = gethostbyname(argv[1]); + endhostent(); + + int s = socket(AF_INET, SOCK_STREAM, 0); + sockaddr_in sin; + sin.sin_family = AF_INET; + sin.sin_port = htons(12480); + sin.sin_addr = (*(in_addr *) h->h_addr_list[0]); + int c = connect(s, (sockaddr * ) & sin, sizeof(sockaddr_in)); + if (c < 0) { + perror("connect"); + exit(1); + } + + fdistream sock_in(s); + fdostream sock_out(s); + + string curr; + int currval = 0; + + for (;;) { + while (getline(sock_in, curr)) { + if (sock_in.eof()) break; + if (curr == "><") break; + if (curr == "> ") { + cout << ">> "; + break; + } else cout << curr << endl; + } + if (curr == "><") break; + getline(cin, curr); + currval = atoi(curr.c_str()); + if (currval < 1 || currval > 7) { + cout << "Not a valid move." << endl; + sock_out << 0 << endl; + } else { + sock_out << curr << endl; + cout << "Waiting for other player to move." << endl; + } + } + + sock_out.close(); + return 0; } diff --git a/PG4/client/client.h b/PG4/client/client.h index a2329fc..1404d8f 100644 --- a/PG4/client/client.h +++ b/PG4/client/client.h @@ -14,6 +14,7 @@ #include #include #include + using namespace std; // method definitions diff --git a/PG4/server/server.cc b/PG4/server/server.cc index 410a806..9b9cf8e 100644 --- a/PG4/server/server.cc +++ b/PG4/server/server.cc @@ -17,208 +17,242 @@ #include #include #include "server.h" + using namespace std; Board board; -int main(int argc, char **argv){ +int main(int argc, char **argv) { - int sc = socket(AF_INET, SOCK_STREAM, 0); - sockaddr_in sin; - sin.sin_family = AF_INET; - sin.sin_port = htons(12480); - sin.sin_addr.s_addr = INADDR_ANY; + int sc = socket(AF_INET, SOCK_STREAM, 0); + sockaddr_in sin; + sin.sin_family = AF_INET; + sin.sin_port = htons(12480); + sin.sin_addr.s_addr = INADDR_ANY; - int b = bind(sc, (sockaddr *)&sin, sizeof(sockaddr_in)); - if(b < 0){perror("bind"); exit(1);} + int b = bind(sc, (sockaddr * ) & sin, sizeof(sockaddr_in)); + if (b < 0) { + perror("bind"); + exit(1); + } - listen(sc, 1); + listen(sc, 1); - while(1){ - for(int i = 0; i < 7; i++) - for(int j = 0; j < 6; j++) - board[i][j] = '|'; + while (1) { + for (int i = 0; i < 7; i++) + for (int j = 0; j < 6; j++) + board[i][j] = '|'; - // cout << "Server started. Waiting for players..." << endl; + // cout << "Server started. Waiting for players..." << endl; - int player1 = accept(sc, NULL, NULL); - if(player1 < 0){perror("accept"); exit(1);} - - fdistream p1in(player1); - fdostream p1out(player1); - - p1out << "Waiting for opponent to connect." << endl; - // cout << "Red player connected." << endl; - - int player2 = accept(sc, NULL, NULL); - if(player2 < 0){perror("accept"); exit(1);} - - fdistream p2in(player2); - fdostream p2out(player2); - - p2out << "You are the blue player. Waiting for Red to make a move." << endl; - // cout << "Blue player connected." << endl; - - //// BOTH CONNECTED NOW //// - // red goes first - // cout << "Both players connected. Game begin." << endl; - - int turn = RED_PLAYER; - string curr_move; - while(!win()){ - // cout << print_board() << endl; // debug to server screen - - // if(turn == RED_PLAYER) cout << "red"; - // else cout << "blue"; - // cout << "'s turn" << endl; - - if(turn == RED_PLAYER){ - p1out << "You are Red (\033[1;31mO\033[0m). Enter the number of the column where you'd like to place a playing piece." << endl; - p1out << print_board() << endl; - p1out << "> " << endl; - getline(p1in, curr_move); - if(place_piece(RED_PLAYER, atoi(curr_move.c_str()))){ - p1out << print_board() << endl; - turn = BLUE_PLAYER; - }else{ // invalid move - p1out << "Invalid move. Cannot place piece." << endl; + int player1 = accept(sc, NULL, NULL); + if (player1 < 0) { + perror("accept"); + exit(1); } - }else{ // BLUE's turn - p2out << "You are Blue (\033[1;34mO\033[0m). Enter the number of the column where you'd like to place a playing piece." << endl; - p2out << print_board() << endl; - p2out << "> " << endl; - getline(p2in, curr_move); - if(place_piece(BLUE_PLAYER, atoi(curr_move.c_str()))){ - p2out << print_board() << endl; - turn = RED_PLAYER; - }else{ // invalid move - p2out << "Invalid move. Cannot place piece." << endl; + fdistream p1in(player1); + fdostream p1out(player1); + + p1out << "Waiting for opponent to connect." << endl; + // cout << "Red player connected." << endl; + + int player2 = accept(sc, NULL, NULL); + if (player2 < 0) { + perror("accept"); + exit(1); } - } + fdistream p2in(player2); + fdostream p2out(player2); + + p2out << "You are the blue player. Waiting for Red to make a move." << endl; + // cout << "Blue player connected." << endl; + + //// BOTH CONNECTED NOW //// + // red goes first + // cout << "Both players connected. Game begin." << endl; + + int turn = RED_PLAYER; + string curr_move; + while (!win()) { + // cout << print_board() << endl; // debug to server screen + + // if(turn == RED_PLAYER) cout << "red"; + // else cout << "blue"; + // cout << "'s turn" << endl; + + if (turn == RED_PLAYER) { + p1out + << "You are Red (\033[1;31mO\033[0m). Enter the number of the column where you'd like to place a playing piece." + << endl; + p1out << print_board() << endl; + p1out << "> " << endl; + getline(p1in, curr_move); + if (place_piece(RED_PLAYER, atoi(curr_move.c_str()))) { + p1out << print_board() << endl; + turn = BLUE_PLAYER; + } else { // invalid move + p1out << "Invalid move. Cannot place piece." << endl; + } + + } else { // BLUE's turn + p2out + << "You are Blue (\033[1;34mO\033[0m). Enter the number of the column where you'd like to place a playing piece." + << endl; + p2out << print_board() << endl; + p2out << "> " << endl; + getline(p2in, curr_move); + if (place_piece(BLUE_PLAYER, atoi(curr_move.c_str()))) { + p2out << print_board() << endl; + turn = RED_PLAYER; + } else { // invalid move + p2out << "Invalid move. Cannot place piece." << endl; + } + + } + } + // game is won! + if (turn == BLUE_PLAYER) { + p1out << "You win!" << endl; + p2out << print_board() << endl; + p2out << "You lose :(" << endl; + + } else { + p1out << print_board() << endl; + p1out << "You lose :(" << endl; + p2out << "You win!" << endl; + } + + p1out << "><" << endl; + p2out << "><" << endl; + + + close(player1); + close(player2); } - // game is won! - if(turn == BLUE_PLAYER){ - p1out << "You win!" << endl; - p2out << print_board() << endl; - p2out << "You lose :(" << endl; + close(b); - }else{ - p1out << print_board() << endl; - p1out << "You lose :(" << endl; - p2out << "You win!" << endl; - } - - p1out << "><" << endl; - p2out << "><" << endl; - - - close(player1); - close(player2); - } - close(b); - - return 0; + return 0; } -bool win(){ - char player; - int i2,j2, counter = 0; +bool win() { + char player; + int i2, j2, counter = 0; - for(int i = 0; i < 7; i++){ - for(int j = 5; j >= 0; j--){ - player = get_board_at(i, j); - if(player == '|') continue; - counter = 1; - i2 = i + 1; j2 = j; - while(get_board_at(i2, j2) == player){ // check going E - i2++; counter++; - if(counter == 4) return true; - } - counter = 1; - i2 = i; j2 = j + 1; - while(get_board_at(i2, j2) == player){ // check going N - j2++; counter++; - if(counter == 4) return true; - } - counter = 1; - i2 = i + 1; j2 = j + 1; - while(get_board_at(i2, j2) == player){ // check going NE - i2++; j2++; counter++; - if(counter == 4) return true; - } - counter = 1; - i2 = i - 1; j2 = j + 1; - while(get_board_at(i2, j2) == player){ // check going NW - i2--; j2++; counter++; - if(counter == 4) return true; - } - counter = 1; - i2 = i - 1; j2 = j; - while(get_board_at(i2, j2) == player){ // check going W - i2--; counter++; - if(counter == 4) return true; - } - counter = 1; - i2 = i - 1; j2 = j - 1; - while(get_board_at(i2, j2) == player){ // check going SW - i2--; j2--; counter++; - if(counter == 4) return true; - } - counter = 1; - i2 = i; j2 = j - 1; - while(get_board_at(i2, j2) == player){ // check going S - j2--; counter++; - if(counter == 4) return true; - } - counter = 1; - i2 = i + 1; j2 = j - 1; - while(get_board_at(i2, j2) == player){ // check going SE - i2++; j2--; counter++; - if(counter == 4) return true; - } + for (int i = 0; i < 7; i++) { + for (int j = 5; j >= 0; j--) { + player = get_board_at(i, j); + if (player == '|') continue; + counter = 1; + i2 = i + 1; + j2 = j; + while (get_board_at(i2, j2) == player) { // check going E + i2++; + counter++; + if (counter == 4) return true; + } + counter = 1; + i2 = i; + j2 = j + 1; + while (get_board_at(i2, j2) == player) { // check going N + j2++; + counter++; + if (counter == 4) return true; + } + counter = 1; + i2 = i + 1; + j2 = j + 1; + while (get_board_at(i2, j2) == player) { // check going NE + i2++; + j2++; + counter++; + if (counter == 4) return true; + } + counter = 1; + i2 = i - 1; + j2 = j + 1; + while (get_board_at(i2, j2) == player) { // check going NW + i2--; + j2++; + counter++; + if (counter == 4) return true; + } + counter = 1; + i2 = i - 1; + j2 = j; + while (get_board_at(i2, j2) == player) { // check going W + i2--; + counter++; + if (counter == 4) return true; + } + counter = 1; + i2 = i - 1; + j2 = j - 1; + while (get_board_at(i2, j2) == player) { // check going SW + i2--; + j2--; + counter++; + if (counter == 4) return true; + } + counter = 1; + i2 = i; + j2 = j - 1; + while (get_board_at(i2, j2) == player) { // check going S + j2--; + counter++; + if (counter == 4) return true; + } + counter = 1; + i2 = i + 1; + j2 = j - 1; + while (get_board_at(i2, j2) == player) { // check going SE + i2++; + j2--; + counter++; + if (counter == 4) return true; + } + } } - } - return false; + return false; } -bool place_piece(int player, int col){ - if(col == 0) return false; - int i = col - 1; - for(int j = 5; j >= 0; j--){ - if(get_board_at(i, j) != '|') continue; - else{ - board[i][j] = player; - return true; +bool place_piece(int player, int col) { + if (col == 0) return false; + int i = col - 1; + for (int j = 5; j >= 0; j--) { + if (get_board_at(i, j) != '|') continue; + else { + board[i][j] = player; + return true; + } } - } - return false; + return false; } -string print_board(){ - string ret = "\n1 2 3 4 5 6 7\n"; - for(int j = 0; j < 6; j++){ - for(int i = 0; i < 7; i++){ - if(get_board_at(i, j) == RED_PLAYER){ - ret += "\033[1;31m"; - ret += "O"; - ret += "\033[0m"; - }else if(get_board_at(i, j) == BLUE_PLAYER){ - ret += "\033[1;34m"; - ret += "O"; - ret += "\033[0m"; - }else{ - ret += get_board_at(i, j); - } - ret += " "; +string print_board() { + string ret = "\n1 2 3 4 5 6 7\n"; + for (int j = 0; j < 6; j++) { + for (int i = 0; i < 7; i++) { + if (get_board_at(i, j) == RED_PLAYER) { + ret += "\033[1;31m"; + ret += "O"; + ret += "\033[0m"; + } else if (get_board_at(i, j) == BLUE_PLAYER) { + ret += "\033[1;34m"; + ret += "O"; + ret += "\033[0m"; + } else { + ret += get_board_at(i, j); + } + ret += " "; + } + ret += "\n"; } - ret += "\n"; - } - return ret; + return ret; } -char get_board_at(int x, int y){ - if(x < 0 || y < 0 || x >= 7 || y >= 6) return '|'; - return board[x][y]; +char get_board_at(int x, int y) { + if (x < 0 || y < 0 || x >= 7 || y >= 6) return '|'; + return board[x][y]; } diff --git a/PG4/server/server.h b/PG4/server/server.h index 5238d2e..592f0f1 100644 --- a/PG4/server/server.h +++ b/PG4/server/server.h @@ -17,6 +17,7 @@ #include #include #include + using namespace std; // method definitions @@ -27,9 +28,13 @@ const int BLUE_PLAYER = 'B'; int main(int argc, char **argv); + bool win(); + bool place_piece(int player, int col); + string print_board(); + char get_board_at(int x, int y); #endif diff --git a/PG5/client/client.cc b/PG5/client/client.cc index 4133ec3..80cdd08 100644 --- a/PG5/client/client.cc +++ b/PG5/client/client.cc @@ -12,55 +12,61 @@ #include #include #include + using namespace std; -int main (int argc, char **argv) { - char *hostaddr; - if(argc == 1) hostaddr = (char*) "euclid.nmu.edu"; - else hostaddr = argv[1]; +int main(int argc, char **argv) { + char *hostaddr; + if (argc == 1) hostaddr = (char *) "euclid.nmu.edu"; + else hostaddr = argv[1]; - struct pollfd pollinfo[2]; - struct hostent *h; - sethostent (1); - h = gethostbyname (hostaddr); - endhostent (); - int s = socket (AF_INET,SOCK_STREAM,0); - sockaddr_in sin; - sin.sin_family = AF_INET; - sin.sin_port = htons (12421); - sin.sin_addr = (*(in_addr *)h->h_addr_list[0]); - int c = connect (s,(sockaddr *)&sin,sizeof(sockaddr_in)); - if (c < 0) {perror ("connect");exit(1);} - - fdistream sinp(s); - fdostream sout(s); - pollinfo[0].fd = 0; //stdin - pollinfo[1].fd = s; //socket - pollinfo[0].events = POLLIN; - pollinfo[1].events = POLLIN; - string line; - - while (true) { - if (poll (pollinfo, 2, -1) < 0) {perror ("poll"); exit(1);} - if (pollinfo[0].revents & POLLIN) { //standard input - if(getline (cin,line)) sout << line << endl; - else break; + struct pollfd pollinfo[2]; + struct hostent *h; + sethostent(1); + h = gethostbyname(hostaddr); + endhostent(); + int s = socket(AF_INET, SOCK_STREAM, 0); + sockaddr_in sin; + sin.sin_family = AF_INET; + sin.sin_port = htons(12421); + sin.sin_addr = (*(in_addr *) h->h_addr_list[0]); + int c = connect(s, (sockaddr * ) & sin, sizeof(sockaddr_in)); + if (c < 0) { + perror("connect"); + exit(1); } - if (pollinfo[1].revents & POLLIN) { //socket - if(getline (sinp,line)){ - if(line == "printscan"){ - for(int i = 0; i < 10; i++){ - getline(sinp, line); - cout << line << endl; - } - }else cout << line << endl; - } - else{ - cout << "connection lost/closed" << endl; - break; - } + + fdistream sinp(s); + fdostream sout(s); + pollinfo[0].fd = 0; //stdin + pollinfo[1].fd = s; //socket + pollinfo[0].events = POLLIN; + pollinfo[1].events = POLLIN; + string line; + + while (true) { + if (poll(pollinfo, 2, -1) < 0) { + perror("poll"); + exit(1); + } + if (pollinfo[0].revents & POLLIN) { //standard input + if (getline(cin, line)) sout << line << endl; + else break; + } + if (pollinfo[1].revents & POLLIN) { //socket + if (getline(sinp, line)) { + if (line == "printscan") { + for (int i = 0; i < 10; i++) { + getline(sinp, line); + cout << line << endl; + } + } else cout << line << endl; + } else { + cout << "connection lost/closed" << endl; + break; + } + } } - } - sout.close(); - return 0; + sout.close(); + return 0; } diff --git a/PG5/client/client.h b/PG5/client/client.h index a2329fc..1404d8f 100644 --- a/PG5/client/client.h +++ b/PG5/client/client.h @@ -14,6 +14,7 @@ #include #include #include + using namespace std; // method definitions diff --git a/PG5/server/server.cc b/PG5/server/server.cc index 7cf4812..22aa542 100644 --- a/PG5/server/server.cc +++ b/PG5/server/server.cc @@ -19,154 +19,166 @@ #include #include #include "server.h" + using namespace std; ppos pos[26]; Board board; struct pollfd fds[26]; -int main(int argc, char **argv){ - for(int r = 1; r < 26; r++) fds[r].fd = -1; - fds[0].fd = socket(AF_INET, SOCK_STREAM, 0); - sockaddr_in sin; - sin.sin_family = AF_INET; - sin.sin_port = htons(12421); - sin.sin_addr.s_addr = INADDR_ANY; - int b = bind(fds[0].fd, (sockaddr *)&sin, sizeof(sockaddr_in)); - if(b < 0){perror("bind"); exit(1);} - fds[0].events = POLLIN; - listen(fds[0].fd, 26); - string line; +int main(int argc, char **argv) { + for (int r = 1; r < 26; r++) fds[r].fd = -1; + fds[0].fd = socket(AF_INET, SOCK_STREAM, 0); + sockaddr_in sin; + sin.sin_family = AF_INET; + sin.sin_port = htons(12421); + sin.sin_addr.s_addr = INADDR_ANY; + int b = bind(fds[0].fd, (sockaddr * ) & sin, sizeof(sockaddr_in)); + if (b < 0) { + perror("bind"); + exit(1); + } + fds[0].events = POLLIN; + listen(fds[0].fd, 26); + string line; - while(1){ - if(poll(fds, 26, -1) < 0){ perror("poll"); exit(1);} - if(fds[0].revents & POLLIN){ - int f = 1; - for(; f <= 25; f++) if(fds[f].fd == -1) break; - new_player(f); - } - for(int i = 1; i <= 25; i++){ - if(fds[i].revents & POLLIN && fds[i].fd != -1){ - if(getline(pos[i].read, line)){ - do_line(line, i); - }else{ - ostringstream msg; - msg << "Player " << i << "'s client disconnected"; - kill_player(i); - alert(msg.str()); + while (1) { + if (poll(fds, 26, -1) < 0) { + perror("poll"); + exit(1); + } + if (fds[0].revents & POLLIN) { + int f = 1; + for (; f <= 25; f++) if (fds[f].fd == -1) break; + new_player(f); + } + for (int i = 1; i <= 25; i++) { + if (fds[i].revents & POLLIN && fds[i].fd != -1) { + if (getline(pos[i].read, line)) { + do_line(line, i); + } else { + ostringstream msg; + msg << "Player " << i << "'s client disconnected"; + kill_player(i); + alert(msg.str()); + } + update(); + } } - update(); - } } - } - return 0; + return 0; } // METHODS //////////////////////////////////////////////////////////////////// -void do_line(string line, int player){ - if(line == "MOVE"){ pos[player].move_forward(); } - else if(line == "LEFT"){ pos[player].turn_left(); } - else if(line == "RIGHT"){ pos[player].turn_right(); } - else if(line == "FIRE"){ shoot(player); } - else if(line == "SCAN"){ scan(player);} - else if(line == "EXIT"){ - kill_player(player); - ostringstream msg; - msg << "Player " << player << " threw in the towel"; +void do_line(string line, int player) { + if (line == "MOVE") { pos[player].move_forward(); } + else if (line == "LEFT") { pos[player].turn_left(); } + else if (line == "RIGHT") { pos[player].turn_right(); } + else if (line == "FIRE") { shoot(player); } + else if (line == "SCAN") { scan(player); } + else if (line == "EXIT") { + kill_player(player); + ostringstream msg; + msg << "Player " << player << " threw in the towel"; + alert(msg.str()); + } else if (line == "HELP" || line == "help") { + pos[player].write + << "Commands are: MOVE, LEFT, RIGHT, SCAN, FIRE, and EXIT. Kill other players. Join and leave at any time." + << endl; + } else { pos[player].write << "invalid command. type HELP for more info." << endl; } + // cout << "Player " << player << " at (" << pos[player].row << ", " << pos[player].col << ") facing " << pos[player].dr << "dr and " << pos[player].dc << "dc" < 25) return; + srand(time(NULL)); + ostringstream msg; // need ostringstream to concatenate int with strings + msg << "Player " << player << " joined the game"; alert(msg.str()); - } - else if(line == "HELP" || line == "help"){ - pos[player].write << "Commands are: MOVE, LEFT, RIGHT, SCAN, FIRE, and EXIT. Kill other players. Join and leave at any time." << endl; - } - else{ pos[player].write << "invalid command. type HELP for more info." << endl; } - // cout << "Player " << player << " at (" << pos[player].row << ", " << pos[player].col << ") facing " << pos[player].dr << "dr and " << pos[player].dc << "dc" < 25) return; - srand(time(NULL)); - ostringstream msg; // need ostringstream to concatenate int with strings - msg << "Player " << player << " joined the game"; - alert(msg.str()); - fds[player].fd = accept(fds[0].fd, NULL, NULL); - int i = rand()%10, j = rand()%10; - while(board[i][j]){ i = rand()%10; j = rand()%10;} - pos[player].row = i; - pos[player].col = j; - pos[player].dr = rand()%3 - 1; - pos[player].dc = pos[player].dr == 0? rand()%2? 1: -1: 0; - fds[player].events = POLLIN; - fds[player].revents = 0; - pos[player].read.clear(); - pos[player].read.attach(fds[player].fd); - pos[player].write.clear(); - pos[player].write.attach(fds[player].fd); - pos[player].write << "You are player " << player << endl; - update(); -} - -void kill_player(int player){ - pos[player].read.close(); - pos[player].write.close(); - fds[player].events = 0; - fds[player].revents = 0; - fds[player].fd = -1; -} - -void alert(string msg){ - for(int i = 1; i <= 25; i++) if(fds[i].fd != -1) pos[i].write << msg << endl; -} - -void shoot(int player){ - update(); - int curr; - for(int i = 1; i <=5; i++){ - curr = board[(pos[player].row + i*pos[player].dr + 10) % 10][(pos[player].col + i*pos[player].dc + 10) % 10]; - if(curr == 0) continue; - else{ - pos[player].write << "You shot player " << curr << endl; - pos[curr].write << "You were shot by player " << player << endl; - ostringstream msg; - msg << "Player " << player << " shot " << curr; - kill_player(curr); - alert(msg.str()); - break; + fds[player].fd = accept(fds[0].fd, NULL, NULL); + int i = rand() % 10, j = rand() % 10; + while (board[i][j]) { + i = rand() % 10; + j = rand() % 10; } - } - update(); + pos[player].row = i; + pos[player].col = j; + pos[player].dr = rand() % 3 - 1; + pos[player].dc = pos[player].dr == 0 ? rand() % 2 ? 1 : -1 : 0; + fds[player].events = POLLIN; + fds[player].revents = 0; + pos[player].read.clear(); + pos[player].read.attach(fds[player].fd); + pos[player].write.clear(); + pos[player].write.attach(fds[player].fd); + pos[player].write << "You are player " << player << endl; + update(); } -void update(){ - for(int i = 0 ; i < 10; i++) for(int j = 0; j < 10; j++) board[i][j] = 0; - for(int i = 1; i <= 25; i++){ - if(fds[i].fd == -1) continue; - if(!board[pos[i].row][pos[i].col]){ - board[pos[i].row][pos[i].col] = i; - }else{ - int other_player = board[pos[i].row][pos[i].col]; - ostringstream msg; - msg << "Players " << i << " and " << other_player << " died in a collision"; - pos[i].write << "You died in a collision with player " << other_player << endl; - kill_player(i); - pos[other_player].write << "You died in a collision with player " << i << endl; - kill_player(other_player); - alert(msg.str()); +void kill_player(int player) { + pos[player].read.close(); + pos[player].write.close(); + fds[player].events = 0; + fds[player].revents = 0; + fds[player].fd = -1; +} + +void alert(string msg) { + for (int i = 1; i <= 25; i++) if (fds[i].fd != -1) pos[i].write << msg << endl; +} + +void shoot(int player) { + update(); + int curr; + for (int i = 1; i <= 5; i++) { + curr = board[(pos[player].row + i * pos[player].dr + 10) % 10][(pos[player].col + i * pos[player].dc + 10) % + 10]; + if (curr == 0) continue; + else { + pos[player].write << "You shot player " << curr << endl; + pos[curr].write << "You were shot by player " << player << endl; + ostringstream msg; + msg << "Player " << player << " shot " << curr; + kill_player(curr); + alert(msg.str()); + break; + } } - } - // for(int i = 0 ; i < 10; i++){ // debug - // for(int j = 0; j < 10; j++) - // cout << board[i][j] << " "; - // cout << endl; - // } + update(); } -void scan(int player){ - update(); - pos[player].write << "printscan" << endl; - for(int i = 5; i >= 1; i--){ - for(int j = 2; j >= -2; j--) - pos[player].write << board[(pos[player].row - j*pos[player].dc + i*pos[player].dr + 10)%10][(pos[player].col + i*pos[player].dc + j*pos[player].dr + 10)%10] << " "; - pos[player].write << "\n_ _ _ _ _\n"; - } - pos[player].write.flush(); +void update() { + for (int i = 0; i < 10; i++) for (int j = 0; j < 10; j++) board[i][j] = 0; + for (int i = 1; i <= 25; i++) { + if (fds[i].fd == -1) continue; + if (!board[pos[i].row][pos[i].col]) { + board[pos[i].row][pos[i].col] = i; + } else { + int other_player = board[pos[i].row][pos[i].col]; + ostringstream msg; + msg << "Players " << i << " and " << other_player << " died in a collision"; + pos[i].write << "You died in a collision with player " << other_player << endl; + kill_player(i); + pos[other_player].write << "You died in a collision with player " << i << endl; + kill_player(other_player); + alert(msg.str()); + } + } + // for(int i = 0 ; i < 10; i++){ // debug + // for(int j = 0; j < 10; j++) + // cout << board[i][j] << " "; + // cout << endl; + // } +} + +void scan(int player) { + update(); + pos[player].write << "printscan" << endl; + for (int i = 5; i >= 1; i--) { + for (int j = 2; j >= -2; j--) + pos[player].write << board[(pos[player].row - j * pos[player].dc + i * pos[player].dr + 10) % 10][ + (pos[player].col + i * pos[player].dc + j * pos[player].dr + 10) % 10] << " "; + pos[player].write << "\n_ _ _ _ _\n"; + } + pos[player].write.flush(); } diff --git a/PG5/server/server.h b/PG5/server/server.h index 34f199f..85361da 100644 --- a/PG5/server/server.h +++ b/PG5/server/server.h @@ -17,39 +17,50 @@ #include #include #include + using namespace std; typedef int Board[10][10]; -struct ppos{ - int row, col, dr, dc; - fdistream read; - fdostream write; +struct ppos { + int row, col, dr, dc; + fdistream read; + fdostream write; - void move_forward(){ - row = (row + dr +10) % 10; - col = (col + dc +10) % 10; - } - void turn_left(){ - int tmp = dr; - dr = -dc; - dc = tmp; - } - void turn_right(){ - int tmp = dr; - dr = dc; - dc = -tmp; - } + void move_forward() { + row = (row + dr + 10) % 10; + col = (col + dc + 10) % 10; + } + + void turn_left() { + int tmp = dr; + dr = -dc; + dc = tmp; + } + + void turn_right() { + int tmp = dr; + dr = dc; + dc = -tmp; + } }; int main(int argc, char **argv); + void kill_player(int player); -void do_line (string line, int player); + +void do_line(string line, int player); + void ready(int player); + void new_player(int player); + void alert(string msg); + void shoot(int player); + void update(); + void scan(int player); #endif