CSCE 2004 Programming Foundations 1 Spring 2019 University of Arkansas, Fayetteville Objective CSCE 2004 S19 Assignment 5 Halfway checkin: April 6, 2019, 11:59pm Final version: Apr. 12, 2019, 11:59pm This assignment may be done in pairs. If done in pairs, print out both names in the code and in the report but only one of the partners should submit to blackboard. The goal of this assignment is to become familiar with the manipulation of 2-dimensional arrays, as well as file input and output. Description In this assignment you will implement a console version of the game battleship. For those who are not familiar with the game, it is a board game where two people face each other and the goal is to sink all 5 ships of your opponent. To do so, each player has 5 ships that they place on their board. These ships may be placed horizontally or vertically. Each player has two boards: one that keeps track of where his ships are located as well as the enemy s hits, and another board that keeps track of the player s own hits on his opponent s ships. During each round each player fires a rocket at a specific coordinate on the enemy s board to try and sink the opponent s ships. A ship is only sunk when all locations on the ship have been hit. You can read more here: https://en.wikipedia.org/wiki/battleship_(game) Implementation In our version, the user will play against a very basic computer. The game boards are represented by 2-dimensional arrays of size 8x8. Note that the coordinate (0,0) is located in the top left corner. Therefore, the row values ( r ) increase as we move down the board and the column values ( c ) increase as we move to the right. Each player (the user and the computer) has 2 boards: The ship_boards store about that player s fleet, i.e., where their ships are and which of their ships have been hit. The hit_boards store the user s play history, i.e., which locations they have already guessed and whether that location was a water or a hit. So, there are 4 boards in the game each of which is a 2-D char array: user_ship_board user_hit_board computer_ship_board computer_hit_board.
Each ship has a size, a name, and a character representation as indicated by the global constants at the top of the program. Starter code has been provided to you and three functions are also provided to you: - print_board that prints the given board in the console - place_a_ship that is used to correctly place one of the user s ships on the board - place_all_ships that is used to place all 5 of the user s ships on the board - You can copy this start code with: $turing: cp ~sgauch/public_html/2004/s19/hw/hw5/hw5.cpp. You will have to implement sevearl functions as well as parts of the main() function. The starter code has many comments to guide you. Many constant variables are provided to you, try and make use of them as much as you can! Please note that when a new game is selected, the player has to place his ships on the board, but the computer s ships are loaded from the file default_computer_ships.txt. When the player wants to save a game, the files user_ships.txt and computer_ships.txt are use to save the ship boards respectively; the contents of their hit boards will be saved in user_hits.txt and computer_hits.txt. Those are the 4 files that will be read from to fill in the 4 arrays if the user chooses to load a saved game. Your program can assume that the files that store the various ship boards are always properly formatted so no error checking is needed when they are read in. Here is an example of the file that stores the computers ship locations ( default_computer_ships.txt ): C C C C C......... D D..... N... B... N... B... N... B....... B... S S S And here is an example of a file that stores the player s hit board: x -.. -... x. - x x x x. x... -... Please note that each character is separated with a space. Error checking will be necessary when asking the player to select a menu item, and when asking the player to enter the coordinates where to fire.
Typically, a game works as follows: The user chooses from quitting, starting a new game, or loading a saved game. The boards get initialized appropriately: New game: both boards are all WATER; computer s ship board is read from default_computer_ships.txt; user places ships by calling place_all_ships and giving orientations and starting locations Load game: all 4 boards are set by reading the 4 save files No, the main function calls play to play the game. You may change the functions in any way you want we provide the headers for your guidance, but they are not meant to restrict your options. For example, the main program could auto-save after each turn or when the user quits, or ask the user about saving before quitting. You may decide what information to display to the user how often. Our starter code is one possible solution, any version that plays battleship properly is fine. But, you need to have play, take_turn, fire, and check_win functions and several others so that you have broken the problem into manageable chunks. Sample Output To observe what one possible final game would look like, you can copy run the following command to copy a finished version of the game: turing$ cp ~sgauch/public_html/2004/s19/hw/hw5/hw5.exe. You don t have to match the output exactly, in fact you are encouraged to communicate with the user in any way you like, as long as it is clear and includes all of the necessary information to play the game. Getting Started You will need to copy the computer s boards and starter code into your directory: turing$ cp ~sgauch/public_html/2004/s19/hw/hw5/default_computer_ships.txt. turing$ cp ~sgauch/public_html/2004/s19/hw/hw5/hw5.cpp. Implementation Strategy You will want to work on one function at a time. Can you initialize one board with water and then display it to the screen? Can you make one hit and echo back to the user what is at that location? Can you then determine that whether or not that one location is a hit or a miss? Don t worry about looping to play multiple turns until you can play a single turn for the user; then a single turn for the computer. This is a much larger and more complex program so you will want to start early and work with your partner. There will be lots of partial credit based on how many of the functions you complete successfully. Halfway Checkin To encourage everyone to start this one early, 10 points of the 50 points will be awarded based on code turned in by April 6 th. On that date, please upload compiling code and a typescript (no report needed). To receive the 10 points, the program must compile and run and do the following steps:
1) ask the use to play a new game, load a saved game, or quit 2) If they choose new game, your program must initialize and print all 4 boards a. Including initializing the computer s ship board from default_computer_ships.txt b. The other 3 boards can be initialized to water c. Then call place_all_ships to place 5 ships on the user s ship board 3) If they choose load a saved game, cout a message a. Optionally load the boards and display them, but this is NOT required by the check in date 4) If they choose quit, the program should end Testing/Requirements Do write a short report for this project describing how much you got done and your approach to implementation. Include at least 2 runs of your game in your typescript, and make sure to demonstrate a starting a new game and also loading a saved game. You will also need to show off your program s error checking during the typescript, by inputting some invalid values. These tests are worth 10% of the assignment grade, and be sure to have MULTIPLE tests.
Submission All late projects will receive reduced credit: 10% off if less than 1 day late 20% off if less than 2 days late 30% off if less than 3 days late No credit if more than 3 days late. IMPORTANT NOTE: You must print both partners name and the assignment number at the very beginning of your program s execution (see the sample code). For full credit, you need to submit the following items to ONLY ONE of the partner s blackboard account: 1. One C++ (.cpp) file that performs the two tasks described Please do not submit code in.docx,.txt, or any other format aside from.cpp! 2. One typescript (.script is recommended) file that includes the compilation of your.cpp files, as well as running the program with multiple tests. 3. One report (.docx or.pdf) that summarizes your approach and your success. Download the necessary files to your desktop from turing (FileZilla is a great tool for this). Then just submit the files to the assignment on Blackboard. Instructions on how to download your code from turing to your laptop/desktop are posted on the class website under Technology Guides. Academic Honesty Statement Students are expected to submit their own work on all programming projects, unless group projects have been explicitly assigned. Students ARE allowed to use any materials on the class website, or in the textbook, or ask the instructor and/or GTAs for assistance This course will be using highly effective program comparison software to calculate the similarity of all programs to each other, and to homework assignments from previous semesters. Please do not be tempted to plagiarize from another student. Violations of the policies above will be reported to the Provost's office and may result in a ZERO on the programming project, an F in the class, or suspension from the university, depending on the severity of the violation and any history of prior violation.