C++ Battleships Assignment

Posted Under: C

Ask A Question
DESCRIPTION
Posted
Modified
Viewed 17
The project contains 10 steps that you must implement, there is already code functions written and the steps are shown where you need to put them. If you open the battlebit folder and look at the read me, there is more information about the task. You are trying to make the game battle ships. You will also need to have a specific virtual box image (previous version of Ubuntu) to be able to compile and run the code. I can provide this via email. The language is mostly focused on C and has a small amount of Assembly code in it as well.
Attachments
csci-366-fall2020-private-master/.gitignore /battlebit/cmake-build-debug/ repos/student_repos /battlebit/CMakeFiles/ /battlebit/googletest/CMakeFiles/ csci-366-fall2020-private-master/.idea/.gitignore # Default ignored files /shelf/ /workspace.xml # Datasource local storage ignored files /dataSources/ /dataSources.local.xml # Editor-based HTTP Client requests /httpRequests/ csci-366-fall2020-private-master/.idea/.name battleship csci-366-fall2020-private-master/.idea/csci-366-fall2020.iml csci-366-fall2020-private-master/.idea/misc.xml csci-366-fall2020-private-master/.idea/modules.xml csci-366-fall2020-private-master/.idea/vcs.xml csci-366-fall2020-private-master/README.md # CSCI 366 - Systems Programming This is the base upstream repository for CSCI 366. It contains homeworks as well as the class project information. ## Getting Your Private Copy (Clone) Rather than the traditional Forking model, we are going to use a Copy model for the class. Please use the following steps to create a *private* version of this repo for your work: - Create a *private* repository in your own account by - Going to <https://github.com/new> - Enter the name `csci-366-fall2020-private` - Select `Private` - Navigate to the `Settings` -> `Manage Access` section - Add `1cg` as a collaborator - Now run the following git commands, substituting your Github user name and NetID where required: ```bash $ git clone https://github.com/msu/csci-366-fall2020.git csci-366-fall2020-private $ cd csci-366-fall2020-private $ git remote set-url origin git@github.com:<YOUR GITHUB USER NAME>/csci-366-fall2020-private.git $ git remote add upstream https://github.com/msu/csci-366-fall2020.git ``` Whew! You now have a private copy of the repository on github. You can push and pull to this repository and branch with the standard `git pull` and `git push` commands. When you want to get an update from the public class repository you can run this command: ``` $ git pull upstream master ``` ## Getting Your Public Copy & Registering (Fork) Next, please fork this repository to your personal account. You will now have two copies of the project, one public and one private. The private repository will be used for your work and our grading. The public repository can be used for contibuting back to the class project. DO NOT PUSH WORK CODE TO THIS REPOSITORY I highly recommend against cloning the public respoitory to your local system, to avoid confusion between the two. You can edit files in the public repository via the web interface, and that will be much safer. Please add a file to `/repos` named `<YOUR NET ID>.txt` with the git URL of your *private* repository, e.g. `git@github.com:1cg/csci-366-fall2020-private.git` And then create a pull request against this repository. We will accept the pull request, and your private work repo will be registered for the class at that point. ## Homeworks Each homework has it's own directory (`/hwk0`, `/hwk1`, etc.). Please do your work in the homework directory ## Project: BattleBit You will implement a simplified, networked version of the gaming classic [BattleShip](https://en.wikipedia.org/wiki/Battleship_(game)). More information can be found in the `/battlebit` directory csci-366-fall2020-private-master/battlebit/CMakeLists.txt cmake_minimum_required(VERSION 3.16) project(battleship C) enable_language(ASM_NASM) set(CMAKE_ASM_NASM_LINK_EXECUTABLE "ld <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>") set(CMAKE_ASM_NASM_OBJECT_FORMAT elf64) add_executable(battleBit src/main.c src/server.c src/server.h src/repl.c src/repl.h src/game.c src/game.h src/char_buff.c src/char_buff.h src/assembly_demo.asm) target_link_libraries(battleBit PUBLIC pthread) set_source_files_properties(assembly_demo.asm PROPERTIES LANGUAGE ASM_NASM) add_library(battleBit_lib src/main.c src/server.c src/server.h src/repl.c src/repl.h src/game.c src/game.h src/char_buff.c src/char_buff.h src/assembly_demo.asm) add_subdirectory(googletest) csci-366-fall2020-private-master/battlebit/DEMO_SCRIPT.md # Demo Script Below is the script and expected behavior of battleBit. You are expected to record a video and upload it to Youtube or another alternative. If you do not have any screen recording experience, [OBS](https://obsproject.com/) is a cross platform screen recording software package that can be used. ## Steps 1. ### Server Startup 1. Open three terminals in the following configuration: ``` |------------------|----------------| | | TERMINAL 2 | | TERMINAL 1 |----------------| | | TERMINAL 3 | |------------------|----------------| ``` 1. Terminal 1 will be used to launch battleBit, which can be found in `/battlbit/cmake-build-debug/battleBit` and which should present you with the following command line options: ```shell script /home/carson/CLionProjects/battleship/cmake-build-debug/battleBit Welcome to BattleBit battleBit (? for help) > ``` 1. From the command prompt, run the `server` command, which should start the server listening on port 9876 1. Terminal 2 will be used to connect as Player 0, using the following command: ``` telnet localhost 9876 ``` When a player connects through this manner, the server should show the following prompt: ```shell script Trying 0.0.0.0... Connected to 0.0.0.0. Escape character is '^]'. Welcome to the battleBit server Player 0 battleBit (? for help) > ``` 1. Run the `?` command, which should show the following output ```shell script battleBit (? for help) > ? ? - show help load <string> - load a ship layout show - shows the board fire [0-7] [0-7] - fires at the given position say <string> - Send the string to all players as part of a chat exit battleBit (? for help) > ``` 1. ### Game Play Demo 1. ##### Player 0 Out Of Order Fire Player 0 should try to fire before Player 1 has connected, and see the following output ```shell script battleBit (? for help) > fire 1 1 Game Has Not Begun! battleBit (? for help) > ``` 1. ##### Player 0 Board Load Player 0 should then load the following configuration: ```shell script battleBit (? for help) > load C00b02D23S47p71 Waiting On Player 1 battleBit (? for help) > ``` 1. ##### Player 1 Connect In Terminal 3, Player 1 should connect: ```shell script $ telnet localhost 9876 Trying 0.0.0.0... Connected to 0.0.0.0. Escape character is '^]'. Welcome to the battleBit server Player 1 battleBit (? for help) > ``` 1. ##### Player 1 Board Load Player 1 should then load the following configuration: ```shell script battleBit (? for help) > load C07B00d23s61p72 All Player Boards Loaded Player 0 Turn battleBit (? for help) > ``` 1. ##### Player 1 Out Of Order Fire Player 0 should try to fire, and see the following output ```shell script battleBit (? for help) > fire 1 1 Player 0 Turn battleBit (? for help) > ``` 1. ##### Player 1 Talk Functionality Player 1 should use the `say` command to encourage Player 0 to fire ```shell script battleBit (? for help) > say Please fire battleBit (? for help) > ``` Player 0 and the server (Terminal 1) should see the following: ```shell script battleBit (? for help) > Player 1 says: Please fire battleBit (? for help) > ``` 1. ##### Firing In Sequence Player 0 and Player 1 should make the following shots in squence: 1. Player 0: `fire 0 0` - Should show `Player 0 fires at 0 0 - HIT` on all terminals 1. Player 1: `fire 1 1` - Should show `Player 1 fires at 1 1 - MISS` on all terminals 1. Player 0: `fire 7 7` - Should show `Player 0 fires at 1 1 - MISS` on all terminals 1. Player 1: `fire 0 3` - Should show `Player 1 fires at 0 3 - HIT` on all terminals 1. ##### Player 0 board state In terminal 2, run the show command to show the Player 0's board, which should show the following output ```shell script battleBit (? for help) > show battleBit......... -----[ ENEMY ]---- 0 1 2 3 4 5 6 7 0 H 1 2 3 4 5 6 7 M ================== -----[ SHIPS ]---- 0 1 2 3 4 5 6 7 0 * * * * * 1 * 2 * * 3 * * * 4 * 5 * 6 7 * * * .........battleBit ``` 1. ##### Player 1 board state In terminal 3, run the show command to show the Player 1's board, which should show the following output ```shell script battleBit (? for help) > show battleBit......... -----[ ENEMY ]---- 0 1 2 3 4 5 6 7 0 1 M 2 3 H 4 5 6 7 ================== -----[ SHIPS ]---- 0 1 2 3 4 5 6 7 0 * * * 1 * 2 * * 3 * * * 4 * 5 * 6 7 * * * * * .........battleBit ``` 1. ##### Shortcut From terminal 1 (the server terminal) run the following command: ```shell script battleBit (? for help) > shortcut ``` This will set up player 1 to have a single ship bit in position (0, 0) 1. ##### Player 0 Wins In terminal 2, fire a single shot from Player 0's at position 0, 0, which should show the following output in all terminals ```shell script battleBit (? for help) > fire 0 0 Player 0 fires at 0 0 - HIT - PLAYER 0 WINS! ``` csci-366-fall2020-private-master/battlebit/README.md ## battleBit: A bit-wise implementation of BattleShip The project for CS366 is a bit-based version of [Battleship](https://en.wikipedia.org/wiki/Battleship_(game)). Rather than a 10x10 board as in the original game, battleBit will use an 8x8 board, allowing board state to be stored in a set of 64-bit integers. ### Ships & Board Specifications A battleBit board consists of five ships: * Carrier - 5 spaces * Battleship - 4 spaces * Destroyer - 3 spaces * Submarine - 3 spaces * PatrolBoat - 2 spaces A battleBit board is specified as follows: * Five triples of a letter L taken from the set [c, C, b, B, d, D, s, S, p, P] followed by two numbers between 0 and 7. * Each letter corresponds to a ship with the same starting letter. * The numbers indicate the X and Y position of the start of the ship * A capital letter indicates that the ship is oriented horizontially, starting at the X, Y postion * A lower case letter indicates that the ship is oriented vertically, starting at the X, Y position * By convention, 0, 0 is the upper left position in the board, and 7,7 is the lower left * So the top row would consist of the positions (0, 0), (1, 0), (2, 0), ..., (7,0) An example of a board specifications is: * `C00b02D23S47p71` ``` CCCCC... .......P b......P b.DDD... b....... b....... ........ ....SSS. ``` ### Project Goals #### Board Display The initial goal of the project will be to display a board to a player. The struct used to capture a players state in a game looks like this: ```c typedef struct game_state { enum Status status; long long shots; long long hits; long long ships; } game_state; ``` Using this information, students will need to print out an ASCII board representation that looks like this: ``` battleBit........ ----[ ENEMY ]---- 0 1 2 3 4 5 6 7 0 1 H H 2 3 M 4 5 6 7 ================= ----[ SHIPS ]---- 0 1 2 3 4 5 6 7 0 * * * * * 1 * 2 * * 3 * * * * 4 * 5 * 6 7 * * * ........battleBit ``` Note that as a bit is hit on a board, it flips from 1 to 0, and thus a losing board is empty. #### Board Load & Verification The next step in the project is loading board given the specification above into the `ships` field. Board specifications must be verified to ensure that: * All ship positions lie within the board boundaries * No ships overlap It is sufficient to report one error. If a specification is erroneous, it should not update the `ships` board. #### Local Gameplay Implementation The next step in the project involves implementing local gameplay mechanics. You are provided with a basic Read-Evaluate-Print-Loop (REPL) in `main.c` and `repl.c`. You will need to implement the following commands: ``` ? - show help load [0-1] <string> - load a ship layout specification for the given player show [0-1] - shows the board for the given player say <string> - Send the string to all players as part of a chat fire [0-1] [0-7] [0-7] - the given player fires at the given position reset - reset the game server - start the server exit - quit ``` You have been provided with a helper library `char_buff.h/char_buff.c` that makes it easier to deal with strings in the project. You can play a full, if fake, game with just a server by loading two player boards and then alternating fires between them. #### Network Gameplay Implementation The final step in the project is adding network functionality that allows two players to connect via `telnet` to the server and begin a game. The server waits until two players have connected and then begins the game, coordinating responses and managing turns between the two players. The server also supports a `say` command that allows the players to talk with one another during the game, even if it is not a given players turn. It also allows the server command line to observe the chat as well as participate in it. Note that this functionality requires synchronization to avoid corruption of the chat order. The client commands are: ``` ? - show help load <string> - load a ship layout specification for the current player show - shows the board for the current player say <string> - Send the string to all players as part of a chat fire [0-7] [0-7] - fire at the given position of the opponent exit - quit the server ``` #### Grading Rubric The grading will be mainly based on automated tests. However, a major component of the project is network programming, which is difficult to test in an automated manner, and will require us to run and test your application manually. Additionally, you will be required to record a demo of the application running in network mode, with a specfic set of commands. The grading will break down like so: * 60% automated tests * 15% recorded demo * 15% manual testing of the network component by instructors * 10% code quality and proper synchronization csci-366-fall2020-private-master/battlebit/src/assembly_demo.asm global nasm_hello_world section .text nasm_hello_world: mov rax, 1 ;; sys_write - see http://blog.rchapman.org/posts/Linux_System_Call_Table_for_x86_64/ mov rdi, 1 ;; stdout file handle is 1, by convention mov rsi, message ;; the message we want to print mov rdx, 13 ;; print 13 characters of data syscall ret message: db "NASM Rocks!", 0xA, 0xD csci-366-fall2020-private-master/battlebit/src/char_buff.c // // Created by carson on 7/7/20. // #include "char_buff.h" #include "stdlib.h" #include <string.h> #include <stdio.h> #include <unistd.h> //write struct char_buff * cb_create(int size) { struct char_buff * char_buff = malloc(sizeof(struct char_buff)); char_buff->buffer = malloc(sizeof(char) * (size + 1)); char_buff->size = size; char_buff->buffer[char_buff->size] = '\0'; // null terminate end as well char_buff->buffer[0] = '\0'; // null terminate char_buff->append_offset = 0; return char_buff; } int cb_append(struct char_buff * buffer, const char * str) { long content_len = (long) strlen(str); char *append_point = buffer->buffer + buffer->append_offset; long limit = buffer->size - buffer->append_offset; strncpy(append_point, str, limit); buffer->append_offset += (content_len > limit ? limit : content_len); } int cb_append_int(struct char_buff * buffer, int arg) { char *append_point = buffer->buffer + buffer->append_offset; long limit = buffer->size - buffer->append_offset; int len = snprintf(append_point, limit, "%i", arg); buffer->append_offset += (len > limit ? limit : len); } void cb_print(struct char_buff * buffer) { printf("size: %li, append_offset: %li\n", buffer->size, buffer->append_offset); printf("string: %s\n", buffer->buffer); } char * cb_tokenize(struct char_buff * buffer, const char * split_on){ buffer->tokienzed_on = split_on; return strtok_r(buffer->buffer, split_on, &buffer->tokenization_save_pointer); } char * cb_next_token(struct char_buff * buffer){ return strtok_r(NULL, buffer->tokienzed_on, &buffer->tokenization_save_pointer); } void cb_free(struct char_buff * buffer) { free(buffer->buffer); free(buffer); } void cb_reset(struct char_buff * buffer) { buffer->buffer[0] = '\0'; // null terminate buffer->append_offset = 0; buffer->tokienzed_on = NULL; buffer->tokenization_save_pointer = NULL; } void cb_write(int fd, struct char_buff * buffer) { write(fd, buffer->buffer, strlen(buffer->buffer)); } csci-366-fall2020-private-master/battlebit/src/char_buff.h // // Created by carson on 7/7/20. // #ifndef BATTLESHIP_CHAR_BUFF_H #define BATTLESHIP_CHAR_BUFF_H typedef struct char_buff { char * buffer; long append_offset; long size; char * tokenization_save_pointer; const char *tokienzed_on; } char_buff; struct char_buff * cb_create(int size); int cb_append(char_buff * buffer, const char * str); int cb_append_int(char_buff * buffer, int arg); void cb_print(char_buff * buffer); char * cb_tokenize(char_buff * buffer, const char * split_on); char * cb_next_token(char_buff * buffer); void cb_free(char_buff * buffer); void cb_reset(char_buff * buffer); void cb_write(int fd, char_buff * buffer); #endif //BATTLESHIP_CHAR_BUFF_H csci-366-fall2020-private-master/battlebit/src/game.c // // Created by carson on 5/20/20. // #include <stdlib.h> #include <stdio.h> #include "game.h" // STEP 10 - Synchronization: the GAME structure will be accessed by both players interacting // asynchronously with the server. Therefore the data must be protected to avoid race conditions. // Add the appropriate synchronization needed to ensure a clean battle. static game * GAME = NULL; void game_init() { if (GAME) { free(GAME); } GAME = malloc(sizeof(game)); GAME->status = CREATED; game_init_player_info(&GAME->players[0]); game_init_player_info(&GAME->players[1]); } void game_init_player_info(player_info *player_info) { player_info->ships = 0; player_info->hits = 0; player_info->shots = 0; } int game_fire(game *game, int player, int x, int y) { // Step 5 - This is the crux of the game. You are going to take a shot from the given player and // update all the bit values that store our game state. // // - You will need up update the players 'shots' value // - you You will need to see if the shot hits a ship in the opponents ships value. If so, record a hit in the // current players hits field // - If the shot was a hit, you need to flip the ships value to 0 at that position for the opponents ships field // // If the opponents ships value is 0, they have no remaining ships, and you should set the game state to // PLAYER_1_WINS or PLAYER_2_WINS depending on who won. } unsigned long long int xy_to_bitval(int x, int y) { // Step 1 - implement this function. We are taking an x, y position // and using bitwise operators, converting that to an unsigned long long // with a 1 in the position corresponding to that x, y // // x:0, y:0 == 0b00000...0001 (the one is in the first position) // x:1, y: 0 == 0b00000...10 (the one is in the second position) // .... // x:0, y: 1 == 0b100000000 (the one is in the eighth position) // // you will need to use bitwise operators and some math to produce the right // value. return 1ull; } struct game * game_get_current() { return GAME; } int game_load_board(struct game *game, int player, char * spec) { // Step 2 - implement this function. Here you are taking a C // string that represents a layout of ships, then testing // to see if it is a valid layout (no off-the-board positions // and no overlapping ships) // // if it is valid, you should write the corresponding unsigned // long long value into the Game->players[player].ships data // slot and return 1 // // if it is invalid, you should return -1 } int add_ship_horizontal(player_info *player, int x, int y, int length) { // implement this as part of Step 2 // returns 1 if the ship can be added, -1 if not // hint: this can be defined recursively } int add_ship_vertical(player_info *player, int x, int y, int length) { // implement this as part of Step 2 // returns 1 if the ship can be added, -1 if not // hint: this can be defined recursively } csci-366-fall2020-private-master/battlebit/src/game.h // // Created by carson on 5/20/20. // #include <stdbool.h> #ifndef BATTLESHIP_GAME_H #define BATTLESHIP_GAME_H #define BOARD_DIMENSION 8 #define SHIP_TYPES 5 enum game_status {CREATED, INITIALIZED, PLAYER_0_TURN, PLAYER_1_TURN, PLAYER_0_WINS, PLAYER_1_WINS}; typedef struct player_info { unsigned long long hits; unsigned long long shots; unsigned long long ships; } player_info; typedef struct game { enum game_status status; player_info players[2]; } game; struct game * game_get_current(); void game_init(); void game_init_player_info(player_info *player_info); int game_fire(game *game, int player, int x, int y); int game_load_board(game *game, int player, char * spec); int add_ship_horizontal(player_info *player, int x, int y, int length); int add_ship_vertical(player_info *player, int x, int y, int length); unsigned long long int xy_to_bitval(int x, int y); #endif //BATTLESHIP_GAME_H csci-366-fall2020-private-master/battlebit/src/main.c #include <stdio.h> #include <stdlib.h> #include "repl.h" #include "game.h" int main() { printf("Welcome to BattleBit\n\n"); /** * Step 0 - Debug this main function with a break point on the line after * the repl_read_command, and step through some input of the various * commands you are expected to implement. Notice how we are reading * into a buffer and then freeing the buffer. */ char_buff * command; game_init(); // NB: game init initializes the game state, all held in game.c do { // This is the classic Read, Evaluate, Print Loop, hence REPL command = repl_read_command("battleBit (? for help) > "); repl_execute_command(command); cb_free(command); } while (command); return 0; } csci-366-fall2020-private-master/battlebit/src/repl.c // // Created by carson on 5/20/20. // #include <stdio.h> #include <stdlib.h> #include <string.h> #include "repl.h" #include "server.h" #include "char_buff.h" extern void nasm_hello_world(); struct char_buff * repl_read_command(char * prompt) { printf("%s", prompt); char *line = NULL; size_t buffer_size = 0; // let getline autosize it if (getline(&line, &buffer_size, stdin) == -1) { if (feof(stdin)) { exit(EXIT_SUCCESS); // We received an EOF } else { perror("readline"); exit(EXIT_FAILURE); } } if (strcmp(line, "") == 0) { free(line); return NULL; } else { struct char_buff *buffer = cb_create(2000); cb_append(buffer, line); free(line); return buffer; } } void repl_execute_command(struct char_buff * buffer) { char* command = cb_tokenize(buffer, " \n"); if (command) { char* arg1 = cb_next_token(buffer); char* arg2 = cb_next_token(buffer); char* arg3 = cb_next_token(buffer); if (strcmp(command, "exit") == 0) { printf("goodbye!"); exit(EXIT_SUCCESS); } else if(strcmp(command, "?") == 0) { printf("? - show help\n"); printf("load [0-1] <string> - load a ship layout file for the given player\n"); printf("show [0-1] - shows the board for the given player\n"); printf("fire [0-1] [0-7] [0-7] - fire at the given position\n"); printf("say <string> - Send the string to all players as part of a chat\n"); printf("reset - reset the game\n"); printf("server - start the server\n"); printf("exit - quit the server\n"); } else if(strcmp(command, "server") == 0) { server_start(); } else if(strcmp(command, "show") == 0) { // work with repl_print_board } else if(strcmp(command, "reset") == 0) { game_init(); } else if (strcmp(command, "load") == 0) { // work with game_load_board } else if (strcmp(command, "fire") == 0) { // work with game_fire } else if (strcmp(command, "nasm") == 0) { nasm_hello_world(); } else if (strcmp(command, "shortcut") == 0) { // update player 1 to only have a single ship in position 0, 0 game_get_current()->players[1].ships = 1ull; } else { printf("Unknown Command: %s\n", command); } } } void repl_print_board(game *game, int player, char_buff * buffer) { player_info player_info = game->players[player]; cb_append(buffer, "battleBit.........\n"); cb_append(buffer, "-----[ ENEMY ]----\n"); repl_print_hits(&player_info, buffer); cb_append(buffer, "==================\n"); cb_append(buffer, "-----[ SHIPS ]----\n"); repl_print_ships(&player_info, buffer); cb_append(buffer, ".........battleBit\n\n"); } void repl_print_ships(player_info *player_info, char_buff *buffer) { // Step 4 - Implement this to print out the visual ships representation // for the console. You will need to use bit masking for each position // to determine if a ship is at the position or not. If it is present // you need to print an X. If not, you need to print a space character ' ' } void repl_print_hits(struct player_info *player_info, struct char_buff *buffer) { // Step 6 - Implement this to print out a visual representation of the shots // that the player has taken and if they are a hit or not. You will again need // to use bit-masking, but this time you will need to consult two values: both // hits and shots values in the players game struct. If a shot was fired at // a given spot and it was a hit, print 'H', if it was a miss, print 'M'. If // no shot was taken at a position, print a space character ' ' } csci-366-fall2020-private-master/battlebit/src/repl.h // // Created by carson on 5/20/20. // #ifndef BATTLESHIP_REPL_H #define BATTLESHIP_REPL_H #include "game.h" #include "char_buff.h" struct char_buff * repl_read_command(char *prompt); void repl_execute_command(struct char_buff *buffer); void repl_print_board(game *state, int player, char_buff *buffer); void repl_print_ships( player_info *player_info, char_buff *buffer); void repl_print_hits(player_info *player_info, char_buff *buffer); #endif //BATTLESHIP_REPL_H csci-366-fall2020-private-master/battlebit/src/server.c // // Created by carson on 5/20/20. // #include "stdio.h" #include "stdlib.h" #include "server.h" #include "char_buff.h" #include "game.h" #include "repl.h" #include "pthread.h" #include<string.h> //strlen #include<sys/socket.h> #include<arpa/inet.h> //inet_addr #include<unistd.h> //write static game_server *SERVER; void init_server() { if (SERVER == NULL) { SERVER = calloc(1, sizeof(struct game_server)); } else { printf("Server already started"); } } int handle_client_connect(int player) { // STEP 9 - This is the big one: you will need to re-implement the REPL code from // the repl.c file, but with a twist: you need to make sure that a player only // fires when the game is initialized and it is there turn. They can broadcast // a message whenever, but they can't just shoot unless it is their turn. // // The commands will obviously not take a player argument, as with the system level // REPL, but they will be similar: load, fire, etc. // // You must broadcast informative messages after each shot (HIT! or MISS!) and let // the player print out their current board state at any time. // // This function will end up looking a lot like repl_execute_command, except you will // be working against network sockets rather than standard out, and you will need // to coordinate turns via the game::status field. } void server_broadcast(char_buff *msg) { // send message to all players } int run_server() { // STEP 8 - implement the server code to put this on the network. // Here you will need to initalize a server socket and wait for incoming connections. // // When a connection occurs, store the corresponding new client socket in the SERVER.player_sockets array // as the corresponding player position. // // You will then create a thread running handle_client_connect, passing the player number out // so they can interact with the server asynchronously } int server_start() { // STEP 7 - using a pthread, run the run_server() function asynchronously, so you can still // interact with the game via the command line REPL init_server(); } csci-366-fall2020-private-master/battlebit/src/server.h // // Created by carson on 5/20/20. // #ifndef BATTLESHIP_SERVER_H #define BATTLESHIP_SERVER_H #include "char_buff.h" #define BATTLEBIT_PORT 9876 typedef struct game_server { pthread_t server_thread; // main server thread that listens for connections pthread_t player_threads[2]; // threads managing I/O from the two sockets int player_sockets[2]; // the client sockets that are inputing and outputting from the network } game_server; int server_start(); void server_broadcast(char_buff *msg); #endif //BATTLESHIP_SERVER_H csci-366-fall2020-private-master/grading/project_results.txt
Explanations and Answers 0

No answers posted

Post your Answer - free or at a fee

Login to your tutor account to post an answer

Posting a free answer earns you +20 points.

Login

NB: Post a homework question for free and get answers - free or paid homework help.

Get answers to: C++ Battleships Assignment or similar questions only at Tutlance.

Related Questions