Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 163 additions & 0 deletions src/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,194 @@

#include "common.hpp"
#include "Client.hpp"
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <bits/stdc++.h>
#include <cereal/archives/json.hpp>
#include <cereal/types/vector.hpp>


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<vector<int>> board(board_size, vector<int>(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<vector<int>> uaboard(board_size, vector<int>(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<vector<char>> vect_bd(board_size,vector<char>(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;
}
136 changes: 132 additions & 4 deletions src/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "common.hpp"
#include "Server.hpp"
#include <string>


/**
Expand All @@ -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<vector<char>> s_vect_bd1(board_size,vector<char>(board_size));
// vector<vector<char>> s_vect_bd2(board_size,vector<char>(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;
}
1 change: 0 additions & 1 deletion src/server_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down