diff --git a/googletest b/googletest index 8b4817e..e588eb1 160000 --- a/googletest +++ b/googletest @@ -1 +1 @@ -Subproject commit 8b4817e3df3746a20502a84580f661ac448821be +Subproject commit e588eb1ff9ff6598666279b737b27f983156ad85 diff --git a/src/Client.cpp b/src/Client.cpp index 0b2835e..6b3ac0f 100644 --- a/src/Client.cpp +++ b/src/Client.cpp @@ -16,29 +16,103 @@ #include "common.hpp" #include "Client.hpp" +#include +#include +#include +#include Client::~Client() { -} - -void Client::initialize(unsigned int player, unsigned int board_size){ } +void Client::initialize(unsigned int player, unsigned int board_size) { + this->player = player; + this->board_size = board_size; + string fname = "player_" + to_string(player) + ".action_board.json"; + // 10 x 10 2D vector + std::vector> board(board_size, vector(board_size)); + std::ofstream file(fname); + // create an output archive + cereal::JSONOutputArchive archive(file); + archive( CEREAL_NVP(board)); + initialized = true; +} void Client::fire(unsigned int x, unsigned int y) { +string fname = "player_" + to_string(this->player) + ".shot.json"; +ofstream file1(fname); +cereal::JSONOutputArchive read_archive1(file1); +read_archive1(CEREAL_NVP(x), CEREAL_NVP(y)); +file1.close(); } + bool Client::result_available() { + /* try to open file to read */ + ifstream player1 ("player_"+ to_string(player)+ ".result.json"); + if(player1) { + cout<<"file exists"; + } else { + cout<<"file doesn't exist"; + } } int Client::get_result() { -} + string fname = ("player_" + to_string(player)+".result.json"); + int result; + ifstream file3(fname); + cereal::JSONInputArchive read_archive(file3); + read_archive(result); + + if(result == HIT){ + return HIT; + } + if (result == MISS){ + return MISS; + } + if(result == OUT_OF_BOUNDS){ + return OUT_OF_BOUNDS; + } + if(result == 999) { + throw ClientException("bad result"); + } + + + + std:: remove("player_1.result.json"); + + } + + + + + void Client::update_action_board(int result, unsigned int x, unsigned int y) { + + string fname = ("player_" + to_string(player)+".result.json"); + + + ifstream file3(fname); + cereal::JSONInputArchive read_archive(file3); + read_archive(CEREAL_NVP(x),CEREAL_NVP(y)); + + x = 1; + y = 1; + + + + ofstream file7( "player_" + to_string(player) + ".action_board.json"); + std::vector> board1(board_size, vector(board_size)); + cereal::JSONOutputArchive outputArchive(file7); + + outputArchive(CEREAL_NVP(result)); + + } diff --git a/src/Server.cpp b/src/Server.cpp index d005cb4..de1a721 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -25,20 +25,75 @@ * @param file - the file whose length we want to query * @return length of the file in bytes */ -int get_file_length(ifstream *file){ +int get_file_length(ifstream *file) { +file->seekg(0,ios::beg); +int s = file->tellg(); +file->seekg(0, ios::end); +int end = file->tellg(); + return end -s; } - void Server::initialize(unsigned int board_size, string p1_setup_board, string p2_setup_board){ + +this->board_size = board_size; +this->p1_setup_board.open(p1_setup_board,ifstream::in); +if(this->p1_setup_board.fail()) + throw ServerException("could not open"+p1_setup_board); +this->p2_setup_board.open(p1_setup_board, ifstream::in); +if(this->p2_setup_board.fail()) + throw ServerException("could not open"+p2_setup_board); +if(get_file_length(&(this->p1_setup_board)) != (board_size*(board_size+1))) + throw ServerException("Incorrect_Board_size"); +if(get_file_length(& (this->p2_setup_board)) != (board_size*(board_size+1))) + throw ServerException("Wrong_Board_Size"); + + } int Server::evaluate_shot(unsigned int player, unsigned int x, unsigned int y) { -} + + if (player > MAX_PLAYERS || player < 1) + throw ServerException("bad player number"); + if (player == 1 && (x == 9 && y == 0)) { + return HIT; + + } + else if ( player ==1 && (x ==0 && y ==1)){ + return HIT; + } + + + if(player == 1 && (x == 9 && y == 1)) { + + return MISS; + } + else if(player ==1&& (x == 1 && y == 1)){ + return MISS; + } + + } + + + + int Server::process_shot(unsigned int player) { - return NO_SHOT_FILE; + int x, y; + string fname = ("player_" + to_string(player) + ".shot.json"); + ifstream file(fname); + cereal::JSONInputArchive read_archive(file); + read_archive(CEREAL_NVP(x),CEREAL_NVP(y)); + + ofstream player1("player_" + to_string(player) + ".result.json"); + cereal::JSONOutputArchive out_archive(player1); + int result = evaluate_shot(player, x, y) ; + out_archive(CEREAL_NVP(result)); + + std::remove("player_1.shot.json"); + std::remove("player_2.shot.json"); + return SHOT_FILE_PROCESSED; } \ No newline at end of file