diff --git a/src/Client.cpp b/src/Client.cpp index 0b2835e..b834368 100644 --- a/src/Client.cpp +++ b/src/Client.cpp @@ -16,31 +16,194 @@ #include "common.hpp" #include "Client.hpp" +#include +#include +#include +#include +#include +#include +#include + Client::~Client() { + } void Client::initialize(unsigned int player, unsigned int board_size){ + string player_name; + + if(player == 1){ + player_name = "player_1.action_board.json"; + }else if(player == 2){ + player_name = "player_2.action_board.json"; + } + + vector> board(board_size, vector(board_size, 0)); + ofstream ofp(player_name); + if(ofp){ + { + cereal::JSONOutputArchive jason(ofp); + jason(cereal::make_nvp("board", board)); + } + ofp.close(); + initialized = true; + } + +// } + + + +// in.open(player_board); +// +// if(in.is_open()){ +// while (!in.eof()) { +// for (int row = 0; row < board_size; row++) { +// for (int col = 0; col < board_size; col++) { +// in >> vect_bd[row][col]; +// } +// } +// } +// } +// +// for (int row = 0; row < vect_bd.size(); row++){ +// for (int col = 0; col < vect_bd[row].size(); col++){ +// cout << vect_bd[row][col]; +// } +// cout << '\n'; +// } +// in.close(); +// cout << render_action_board(); + + } void Client::fire(unsigned int x, unsigned int y) { + string player_board = "player_1.shot.json"; + + int x_co = x; + int y_co = y; + +// if (player == 1) { +// player_board = "player_1.shot.json"; +// } else{ +// player_board = "player_2.shot.json"; +// } + + ofstream shot_f(player_board); + + cereal::JSONOutputArchive out_res(shot_f); + out_res(cereal::make_nvp("x", x_co), cereal::make_nvp("y", y_co)); + shot_f.flush(); + + + + + + } bool Client::result_available() { + //1 is hit, -1 is miss, 0 is out of bounds + } int Client::get_result() { + string board = "player_1.result.json"; +// if(player == 1){ +// board = "player_1.result.json"; +// }else if(player == 2){ +// board = "player_2.result.json"; +// } + int resul; + ifstream ress_info(board); + + cereal::JSONInputArchive read(ress_info); + read(resul); + ress_info.close(); + + remove("player_1.result.json"); + + // if (player == 1) { +// remove("player_1.result.json"); +// } else if(player == 2) { +// remove("player_2.result.json"); +// } + + if(resul == 1){ + return HIT; + }else if(resul == -1){ + return MISS; + }else if(resul == 0){ + return OUT_OF_BOUNDS; + }else{ + throw new ClientException("That's a negative"); + } + + + + } void Client::update_action_board(int result, unsigned int x, unsigned int y) { + string board = "player_1.action_board.json"; + + vector> uaboard(board_size, vector(board_size, 0)); + + ifstream in(board); + + cereal::JSONInputArchive upd(in); + upd(uaboard); + in.close(); + + uaboard[x][y] = result; + + ofstream ouab(board); + + cereal::JSONOutputArchive out_uab(ouab); + out_uab(cereal::make_nvp("board", uaboard)); + ouab.flush(); + + //PLUNKBAT! } string Client::render_action_board(){ + vector> vect_bd(board_size,vector(board_size)); + + string output = ""; + string player_board; + + if(player == 1){ + player_board = "player_1.setup_board.txt"; + }else if(player == 2){ + player_board = "player_2.setup_board.txt"; + } + + ifstream in; + in.open(player_board); + + if(in.is_open()){ + while (!in.eof()) { + for (int row = 0; row < board_size; row++) { + for (int col = 0; col < board_size; col++) { + in >> vect_bd[row][col]; + } + } + } + } + + for (int row = 0; row < vect_bd.size(); row++){ + for (int col = 0; col < vect_bd[row].size(); col++){ + output += vect_bd[row][col]; + } + output += '\n'; + } + in.close(); + return output; } \ No newline at end of file diff --git a/src/Server.cpp b/src/Server.cpp index d005cb4..f983252 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -17,6 +17,7 @@ #include "common.hpp" #include "Server.hpp" +#include /** @@ -29,16 +30,143 @@ int get_file_length(ifstream *file){ } -void Server::initialize(unsigned int board_size, - string p1_setup_board, - string p2_setup_board){ +void Server::initialize(unsigned int board_size, string p1_setup_board, string p2_setup_board){ + Server::board_size = board_size; + int size_o_board = board_size; + + if (board_size != BOARD_SIZE) + throw new ServerException("Wrong board size"); + + if(p1_setup_board.length() == 0 && p2_setup_board.length() == 0) + throw new ServerException("Bad Files"); +// +// vector> s_vect_bd1(board_size,vector(board_size)); +// vector> s_vect_bd2(board_size,vector(board_size)); +// +// string player_board1; +// string player_board2; +// +// player_board1 = "player_1.setup_board.txt"; +// player_board2 = "player_2.setup_board.txt"; +// +// ifstream in1; +// in1.open(player_board1); +// +// if(in1.is_open()){ +// while (!in1.eof()) { +// for (int row = 0; row < board_size; row++) { +// for (int col = 0; col < board_size; col++) { +// in1 >> s_vect_bd1[row][col]; +// } +// } +// } +// } +// in1.close(); +// +// ifstream in2; +// in2.open(player_board2); +// +// if(in2.is_open()){ +// while (!in2.eof()) { +// for (int row = 0; row < board_size; row++) { +// for (int col = 0; col < board_size; col++) { +// in2 >> s_vect_bd2[row][col]; +// } +// } +// } +// } +// in2.close(); } int Server::evaluate_shot(unsigned int player, unsigned int x, unsigned int y) { + if(player > 2 || player < 1){ + throw new ServerException("NOPE!"); + } + if(x > BOARD_SIZE){ + return OUT_OF_BOUNDS; + } + if(y > BOARD_SIZE){ + return OUT_OF_BOUNDS; + } + string player_board; + if(player == 1){ + player_board = "player_2.setup_board.txt"; + }else{ + player_board = "player_1.setup_board.txt"; + } + + ifstream in; + + char temp; + in.open(player_board); + if(in.is_open()){ + while (!in.eof()) { + for (int row = 0; row < board_size; row++) { + for (int col = 0; col < board_size; col++) { + in >> temp; + if(x == row && y == col){ + if(temp == '_'){ + return MISS; + }else{ + return HIT; + } + } + } + } + } + + } + in.close(); + + } int Server::process_shot(unsigned int player) { - return NO_SHOT_FILE; + + if(player > 2 || player < 1){ + throw new ServerException("NOPE!"); + } + string player_board; + if (player == 1) { + player_board = "player_1.shot.json"; + } else if(player == 2) { + player_board = "player_2.shot.json"; + }else{ + return NO_SHOT_FILE; + } + + unsigned int x; + unsigned int y; + + ifstream shot_info(player_board); + + cereal::JSONInputArchive read(shot_info); + read(x,y); + shot_info.close(); + + + int res = evaluate_shot(player, x, y); + + string res_file; + + if(player == 1){ + res_file = "player_1.result.json"; + }else if(player == 2){ + res_file = "player_2.result.json"; + } + + ofstream ofres(res_file); + + cereal::JSONOutputArchive out_res(ofres); + out_res(cereal::make_nvp("result", res)); + ofres.flush(); + + if (player == 1) { + remove("player_1.shot.json"); + } else if(player == 2) { + remove("player_2.shot.json"); + } + return SHOT_FILE_PROCESSED; } \ No newline at end of file diff --git a/src/server_main.cpp b/src/server_main.cpp index 121643a..401dc63 100644 --- a/src/server_main.cpp +++ b/src/server_main.cpp @@ -26,7 +26,6 @@ int main(){ Server s; s.initialize(BOARD_SIZE, "player_1.setup_board.txt", "player_2.setup_board.txt"); - // run the server process in a loop while(true){ while(s.process_shot(1) == NO_SHOT_FILE) {