fix formatting

This commit is contained in:
Ben Harris 2022-01-15 23:52:50 -05:00
parent 114521748a
commit 9eb1c7ada1
Signed by: ben
GPG Key ID: 4E0AF802FFF7960C
16 changed files with 801 additions and 717 deletions

View File

@ -7,8 +7,8 @@
#include <cstdlib>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
using namespace std;
int main(int argc, char **argv) {

View File

@ -10,6 +10,7 @@
#include <sys/types.h>
#include <sys/wait.h>
#include <fdstream/fdstream.h>
using namespace std;
int send[2];

View File

@ -2,13 +2,10 @@
// lastchar.cc takes one command line argument
// returns the last char of that string
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
int main(int argc, char **argv) {
string ret = argv[1];
char c = argv[1][strlen(argv[1]) - 1];
return c;
return argv[1][strlen(argv[1]) - 1];
}

View File

@ -15,6 +15,7 @@
#include <fdstream/fdstream.h>
#include <fcntl.h>
#include "client.h"
using namespace std;
int main(int argc, char **argv) {
@ -35,7 +36,10 @@ int main(int argc, char **argv){
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);}
if (c < 0) {
perror("connect");
exit(1);
}
fdistream sock_in(s);
fdostream sock_out(s);
@ -81,7 +85,6 @@ int main(int argc, char **argv){
}
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);

View File

@ -14,7 +14,9 @@
#include <netdb.h>
#include <fdstream/fdstream.h>
#include <fcntl.h>
using namespace std;
// method definitions
int main(int argc, char **argv);

View File

@ -17,6 +17,7 @@
#include <fdstream/fdstream.h>
#include <fcntl.h>
#include "server.h"
using namespace std;
int main(int argc, char **argv) {
@ -33,7 +34,10 @@ int main(int argc, char **argv){
listen(sc, 1);
int s = accept(sc, NULL, NULL);
if(s < 0) {perror("accept"); exit(1);}
if (s < 0) {
perror("accept");
exit(1);
}
fdistream sock_in(s);
fdostream sock_out(s);

View File

@ -6,5 +6,4 @@
// method definitions
int main(int argc, char **argv);
#endif

View File

@ -19,6 +19,7 @@
#include <fdstream/fdstream.h>
#include <fcntl.h>
#include "pg3.h"
using namespace std;
char b64ch(char ch) {
@ -46,7 +47,10 @@ int main(int argc, char **argv){
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);}
if (c < 0) {
perror("connect");
exit(1);
}
fdostream out_sock(s);
fdistream in_sock(s);

View File

@ -16,6 +16,7 @@
#include <fdstream/fdstream.h>
#include <fcntl.h>
#include "client.h"
using namespace std;
int main(int argc, char **argv) {
@ -36,7 +37,10 @@ int main(int argc, char **argv){
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);}
if (c < 0) {
perror("connect");
exit(1);
}
fdistream sock_in(s);
fdostream sock_out(s);

View File

@ -14,6 +14,7 @@
#include <netdb.h>
#include <fdstream/fdstream.h>
#include <fcntl.h>
using namespace std;
// method definitions

View File

@ -17,6 +17,7 @@
#include <fdstream/fdstream.h>
#include <fcntl.h>
#include "server.h"
using namespace std;
Board board;
@ -30,7 +31,10 @@ int main(int argc, char **argv){
sin.sin_addr.s_addr = INADDR_ANY;
int b = bind(sc, (sockaddr * ) & sin, sizeof(sockaddr_in));
if(b < 0){perror("bind"); exit(1);}
if (b < 0) {
perror("bind");
exit(1);
}
listen(sc, 1);
@ -42,7 +46,10 @@ int main(int argc, char **argv){
// cout << "Server started. Waiting for players..." << endl;
int player1 = accept(sc, NULL, NULL);
if(player1 < 0){perror("accept"); exit(1);}
if (player1 < 0) {
perror("accept");
exit(1);
}
fdistream p1in(player1);
fdostream p1out(player1);
@ -51,7 +58,10 @@ int main(int argc, char **argv){
// cout << "Red player connected." << endl;
int player2 = accept(sc, NULL, NULL);
if(player2 < 0){perror("accept"); exit(1);}
if (player2 < 0) {
perror("accept");
exit(1);
}
fdistream p2in(player2);
fdostream p2out(player2);
@ -73,7 +83,9 @@ int main(int argc, char **argv){
// 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
<< "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);
@ -85,7 +97,9 @@ int main(int argc, char **argv){
}
} 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
<< "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);
@ -131,51 +145,71 @@ bool win(){
player = get_board_at(i, j);
if (player == '|') continue;
counter = 1;
i2 = i + 1; j2 = j;
i2 = i + 1;
j2 = j;
while (get_board_at(i2, j2) == player) { // check going E
i2++; counter++;
i2++;
counter++;
if (counter == 4) return true;
}
counter = 1;
i2 = i; j2 = j + 1;
i2 = i;
j2 = j + 1;
while (get_board_at(i2, j2) == player) { // check going N
j2++; counter++;
j2++;
counter++;
if (counter == 4) return true;
}
counter = 1;
i2 = i + 1; j2 = j + 1;
i2 = i + 1;
j2 = j + 1;
while (get_board_at(i2, j2) == player) { // check going NE
i2++; j2++; counter++;
i2++;
j2++;
counter++;
if (counter == 4) return true;
}
counter = 1;
i2 = i - 1; j2 = j + 1;
i2 = i - 1;
j2 = j + 1;
while (get_board_at(i2, j2) == player) { // check going NW
i2--; j2++; counter++;
i2--;
j2++;
counter++;
if (counter == 4) return true;
}
counter = 1;
i2 = i - 1; j2 = j;
i2 = i - 1;
j2 = j;
while (get_board_at(i2, j2) == player) { // check going W
i2--; counter++;
i2--;
counter++;
if (counter == 4) return true;
}
counter = 1;
i2 = i - 1; j2 = j - 1;
i2 = i - 1;
j2 = j - 1;
while (get_board_at(i2, j2) == player) { // check going SW
i2--; j2--; counter++;
i2--;
j2--;
counter++;
if (counter == 4) return true;
}
counter = 1;
i2 = i; j2 = j - 1;
i2 = i;
j2 = j - 1;
while (get_board_at(i2, j2) == player) { // check going S
j2--; counter++;
j2--;
counter++;
if (counter == 4) return true;
}
counter = 1;
i2 = i + 1; j2 = j - 1;
i2 = i + 1;
j2 = j - 1;
while (get_board_at(i2, j2) == player) { // check going SE
i2++; j2--; counter++;
i2++;
j2--;
counter++;
if (counter == 4) return true;
}
}

View File

@ -17,6 +17,7 @@
#include <netdb.h>
#include <fdstream/fdstream.h>
#include <fcntl.h>
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

View File

@ -12,6 +12,7 @@
#include <netdb.h>
#include <fdstream/fdstream.h>
#include <poll.h>
using namespace std;
int main(int argc, char **argv) {
@ -30,7 +31,10 @@ int main (int argc, char **argv) {
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 (c < 0) {
perror("connect");
exit(1);
}
fdistream sinp(s);
fdostream sout(s);
@ -41,7 +45,10 @@ int main (int argc, char **argv) {
string line;
while (true) {
if (poll (pollinfo, 2, -1) < 0) {perror ("poll"); exit(1);}
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;
@ -54,8 +61,7 @@ int main (int argc, char **argv) {
cout << line << endl;
}
} else cout << line << endl;
}
else{
} else {
cout << "connection lost/closed" << endl;
break;
}

View File

@ -14,6 +14,7 @@
#include <netdb.h>
#include <fdstream/fdstream.h>
#include <fcntl.h>
using namespace std;
// method definitions

View File

@ -19,6 +19,7 @@
#include <poll.h>
#include <fcntl.h>
#include "server.h"
using namespace std;
ppos pos[26];
@ -33,13 +34,19 @@ int main(int argc, char **argv){
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);}
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 (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;
@ -74,11 +81,11 @@ void do_line(string line, int 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; }
} 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" <<endl;
}
@ -90,7 +97,10 @@ void new_player(int player){
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;}
while (board[i][j]) {
i = rand() % 10;
j = rand() % 10;
}
pos[player].row = i;
pos[player].col = j;
pos[player].dr = rand() % 3 - 1;
@ -121,7 +131,8 @@ 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];
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;
@ -165,7 +176,8 @@ void scan(int player){
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 << 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();

View File

@ -17,6 +17,7 @@
#include <netdb.h>
#include <fdstream/fdstream.h>
#include <fcntl.h>
using namespace std;
typedef int Board[10][10];
@ -30,11 +31,13 @@ struct ppos{
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;
@ -43,13 +46,21 @@ struct ppos{
};
int main(int argc, char **argv);
void kill_player(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