Arrays and File I/O Create a program in a class called PlayGame.java that reads data from two files ShipPositionsPlayerOne.txt and ShipPositionsPlayerTwo.txt and plays the board game Battleship. A log...

1 answer below ยป

Arrays and File I/O


Create a program in a class calledPlayGame.javathat reads data from two filesShipPositionsPlayerOne.txtandShipPositionsPlayerTwo.txtand plays the board game Battleship. A log will be printed with the results. A class calledGameLog.javawill also be created to help print the log of the results. The input files are provided in the starter code.



Overview of Battleship:

Battleship is a two player game where each player has a board with 10 rows labeled letters A-J and 14 columns labeled numbers 1-14. Each player chooses coordinates to place five ships on their board. One ship spans 5 cells, another ship spans 4 cells, two additional ships span 3 cells, and the final ship spans 2 cells. The coordinate at the top left of the board is A1 and the coordinate at the bottom left is J14. As an example, the 5 cell ship can be placed with start coordinate C3 and end coordinate C7 which means that it spans C3, C4, C5, C6, and C7. Ships can be placed horizontally or vertically. Ships cannot be placed diagonally. Ships cannot be placed on top of each other (their coordinates cannot overlap).
The two players should not show their ship positions to each other. They will take turns guessing a coordinate where they think the opponent may have placed a ship. If a ship is placed at that location, that guess is a successful hit. Otherwise, it is a miss. It is important to note that the same location cannot be hit twice. Once a hit is made at a location, any subsequent guesses for the same location result in a miss. The goal for each player is to sink all of the opponent's ships (hit every coordinate of every ship). The player that is able to accomplish this more quickly is the winner. It is possible player 1 gets one guess more than player 2 (if player 1 wins on a turn, player 2 does not get a turn even though player 1 has taken one guess more than player 2 at this point). Note: The description above focuses on the information that will help complete this project. More information about battleship can be found online, such ashere.





GameLog.java


Fields











































Field Name




Type




Access Modifier




Description




winningPlayer



int



private



The player number that won the game (1 for player 1 or 2 for player 2)




losingPlayerHits



int



private



The number of times the losing player hit a location on one of the other player's ships.




numTurns



int



private



The number of turns (guesses at coordinates) that it took the winning player to find all of the opponent's ships.




boardPatternOne



String



private



The board pattern for player one's board ("Top Heavy", "Middle Heavy", "Bottom Heavy", or "Scattered").




boardPatternTwo



String



private



The board pattern for player two's board ("Top Heavy", "Middle Heavy", "Bottom Heavy", or "Scattered").



Note: Details on board patterns are in the next section.



Constructor



















Access Modifier




Constructor Name




Input Parameters




Description



public



GameLog



int winningPlayer, int losingPlayerHits, int numTurns, String boardPatternOne, String boardPatternTwo



Construct a newly allocatedGameLogobject and instantiate the fields to their respective parameters.






Methods





















Method Name




Return Type




Access Modifier




Input Parameters




Description



toString



String



public



None



Returns the String representation of thisSession.


For Example, given the following fields:




  • winningPlayer= 2


  • losingPlayerHits= 0


  • numTurns= 17


  • boardPatternOne= "Bottom Heavy"


  • boardPatternTwo= "Bottom Heavy"


The result of callingtoString()would be:



  • "Battleship Game Log:\nWinning Player: Player 2\nHits: 0 - 17\nNumber of Turns To Win: 17\nPlayer 1 Board Pattern: Bottom Heavy\nPlayer 2 Board Pattern: Bottom Heavy\n"







PlayGame.java


This class will include a main method in which the game of battleship will be played. For the purposes of this task, the ship arrangements for the two players are predetermined and are stored in input files. These input files are provided in the startercode and are named ShipPositionsPlayerOne.txt and ShipPositionsPlayerTwo.txt. See the example section for information on how these input files are formatted. Below are the steps to play the game that the program should perform. The examples section has more detail about the format of the output.



  • Read the ship arrangements for both players from the input filesShipPositionsPlayerOne.txtandShipPositionsPlayerTwo.txt. Create variables to save these arrangements. You may assume that each file is valid and contains exactly 17 cells with ships and 123 empty cells.

  • Display the welcome message. Then, create a scanner. During the game, prompt both players to enter a row letter and a column number for the coordinate that they want to guess. Use the ship arrangements stored previously to determine whether the attempt was a hit or a miss. Keep prompting players for their guesses until one player has has won the game. During the game, keep track of the number of hits for each player and the number of turns.

  • Print the congratulations message for the winning player.

  • Determine the board pattern of each player's board. The criteria for identifying the pattern is explained below:


    • If the majority of hits (9 or more) are found in the top 3 rows of the board, the board pattern is"Top Heavy"



    • If the majority of hits (9 or more) are found in the middle 4 rows of the board, the board pattern is"Middle Heavy"



    • If the majority of hits (9 or more) are found in the bottom 3 rows of the board, the board pattern is"Bottom Heavy"



    • If none of the three areas above contain 9 or more hits, the board pattern is"Scattered"





  • Create aGameLogobject with the necessary parameters. Call thetoString()method on this object and write the String that is returned to a file called GameLog.txt.


Implementation Recommendations


This project is more open-ended than most of the previous assignments. While this gives you more freedom in how you approach your solution, it does increase the risk of difficult to identify bugs. We have a few recommendations that will help you succeed:



  • Before you begin programming, make a list of all the fields and methods you believe you will need. Consider what types of data you'll need to store and how to access it.

  • Design your solution! Again, before you begin programming, write out a flow of control that documents how the solution will perform. Use this to guide your implementation

  • Test your code! We provide samples in this handout, as well as a local test case. Write additional test cases using the same format and tools. This will help you debug issues and ensure you receive a good score on Vocareum! Given the long lines of input for this assignment, we highly recommend testing locally before you submit.

  • Lastly, start early! Waiting until the last moment to design your program increases the likelihood of logical errors.





Example 1


ShipPositionsPlayerOne.txt


Note that the character 'M' denotes an empty cell (a guess there is a "Miss") and the character 'H' denotes part of a ship (a guess there is a "Hit").

MMMMMMMMMMMMMM
MMMMMMMMMMMMMM
MMMMMMMMHMMMHH
MMMMMMMMHMMMMM
MMMMMMMMHMMMMM
MMMMMMMMHMMMMM
MMMMMMMMHMMMMM
MMMMMHHHHMMMMM
HHHMMMMMMMMMMM
MMMMMMHHHMMMMM



ShipPositionsPlayerTwo.txt

MMMMMMMMMMMMMM
MMMMMMMMMMMMMM
MMMMMMMMHMMMMH
MMMMMMMMHMMMMH
MMMMMMMMHMMMMM
MMMMMMMMHMMMMM
MMMMMMMMHMMMMM
MMMMMHHHHMMMMM
HHHMMMMMMMMMMM
MMMMMMHHHMMMMM





Console Output With User Input in Braces []

Hello, Welcome to Battleship!
Player 1 - Enter a row letter from A - J
[A]
Player 1 - Enter a column number from 1 - 14
[1]
Value:M
Player 2 - Enter a row letter from A - J
[C]
Player 2 - Enter a column number from 1 - 14
[9]
Value:H
Player 1 - Enter a row letter from A - J
[A]
Player 1 - Enter a column number from 1 - 14
[1]
Value:M
Player 2 - Enter a row letter from A - J
[C]
Player 2 - Enter a column number from 1 - 14
[13]
Value:H
Player 1 - Enter a row letter from A - J
[A]
Player 1 - Enter a column number from 1 - 14
[1]
Value:M
Player 2 - Enter a row letter from A - J
[C]
Player 2 - Enter a column number from 1 - 14
[14]
Value:H
Player 1 - Enter a row letter from A - J
[A]
Player 1 - Enter a column number from 1 - 14
[1]
Value:M
Player 2 - Enter a row letter from A - J
[D]
Player 2 - Enter a column number from 1 - 14
[9]
Value:H
Player 1 - Enter a row letter from A - J
[A]
Player 1 - Enter a column number from 1 - 14
[1]
Value:M
Player 2 - Enter a row letter from A - J
[E]
Player 2 - Enter a column number from 1 - 14
[9]
Value:H
Player 1 - Enter a row letter from A - J
[A]
Player 1 - Enter a column number from 1 - 14
[1]
Value:M
Player 2 - Enter a row letter from A - J
[F]
Player 2 - Enter a column number from 1 - 14
[9]
Value:H
Player 1 - Enter a row letter from A - J
[A]
Player 1 - Enter a column number from 1 - 14
[1]
Value:M
Player 2 - Enter a row letter from A - J
[G]
Player 2 - Enter a column number from 1 - 14
[9]
Value:H
Player 1 - Enter a row letter from A - J
[A]
Player 1 - Enter a column number from 1 - 14
[1]
Value:M
Player 2 - Enter a row letter from A - J
[H]
Player 2 - Enter a column number from 1 - 14
[6]
Value:H
Player 1 - Enter a row letter from A - J
[A]
Player 1 - Enter a column number from 1 - 14
[1]
Value:M
Player 2 - Enter a row letter from A - J
[H]
Player 2 - Enter a column number from 1 - 14
[7]
Value:H
Player 1 - Enter a row letter from A - J
[A]
Player 1 - Enter a column number from 1 - 14
[1]
Value:M
Player 2 - Enter a row letter from A - J
[H]
Player 2 - Enter a column number from 1 - 14
[8]
Value:H
Player 1 - Enter a row letter from A - J
[A]
Player 1 - Enter a column number from 1 - 14
[1]
Value:M
Player 2 - Enter a row letter from A - J
[H]
Player 2 - Enter a column number from 1 - 14
[9]
Value:H
Player 1 - Enter a row letter from A - J
[A]
Player 1 - Enter a column number from 1 - 14
[1]
Value:M
Player 2 - Enter a row letter from A - J
[I]
Player 2 - Enter a column number from 1 - 14
[1]
Value:H
Player 1 - Enter a row letter from A - J
[A]
Player 1 - Enter a column number from 1 - 14
[1]
Value:M
Player 2 - Enter a row letter from A - J
[I]
Player 2 - Enter a column number from 1 - 14
[2]
Value:H
Player 1 - Enter a row letter from A - J
[A]
Player 1 - Enter a column number from 1 - 14
[1]
Value:M
Player 2 - Enter a row letter from A - J
[I]
Player 2 - Enter a column number from 1 - 14
[3]
Value:H
Player 1 - Enter a row letter from A - J
[A]
Player 1 - Enter a column number from 1 - 14
[1]
Value:M
Player 2 - Enter a row letter from A - J
[J]
Player 2 - Enter a column number from 1 - 14
[7]
Value:H
Player 1 - Enter a row letter from A - J
[A]
Player 1 - Enter a column number from 1 - 14
[1]
Value:M
Player 2 - Enter a row letter from A - J
[J]
Player 2 - Enter a column number from 1 - 14
[8]
Value:H
Player 1 - Enter a row letter from A - J
[A]
Player 1 - Enter a column number from 1 - 14
[1]
Value:M
Player 2 - Enter a row letter from A - J
[J]
Player 2 - Enter a column number from 1 - 14
[9]
Value:H
Enemy fleet destroyed. Congratulations player 2!



File Output in GameLog.txt

Battleship Game Log:
Winning Player: Player 2
Hits: 0 - 17
Number of Turns To Win: 17
Player 1 Board Pattern: Bottom Heavy
Player 2 Board Pattern: Bottom Heavy

Example 2


ShipPositionsPlayerOne.txt

MMMMMMMMMMMMMM
MMMMMMMMMMMMMM
MMMMMMMMMMMMMM
HHHHHMMMHMMMMM
MMMMMMMMHMMMMM
MMMMMMMMHMMMMM
MMMMMMMMHMMMMM
MMMMMHHMMMMMMM
HHHMMMMMMMMMMM
MMMMMMHHHMMMMM



ShipPositionsPlayerTwo.txt

HHMMMMMMMMMMMM
MMHHHMMMHHHHMM
MMMMMMMMMMMMMM
MMMMMMMMMMMMMM
MMMMMMMMMMMMMM
MMMMMMMMMMMMMM
MMMMMMMMMMMMMM
MMMMMMMMMMMMMM
HHHMMMMMMMMMMM
MMMMMMHHHHHMMM





Console Output With User Input in Braces []

Hello, Welcome to Battleship!
Player 1 - Enter a row letter from A - J
[C]
Player 1 - Enter a column number from 1 - 14
[1]
Value:M
Player 2 - Enter a row letter from A - J
[D]
Player 2 - Enter a column number from 1 - 14
[9]
Value:H
Player 1 - Enter a row letter from A - J
[A]
Player 1 - Enter a column number from 1 - 14
[1]
Value:H
Player 2 - Enter a row letter from A - J
[A]
Player 2 - Enter a column number from 1 - 14
[1]
Value:M
Player 1 - Enter a row letter from A - J
[B]
Player 1 - Enter a column number from 1 - 14
[1]
Value:M
Player 2 - Enter a row letter from A - J
[D]
Player 2 - Enter a column number from 1 - 14
[9]
Value:M
Player 1 - Enter a row letter from A - J
[A]
Player 1 - Enter a column number from 1 - 14
[2]
Value:H
Player 2 - Enter a row letter from A - J
[H]
Player 2 - Enter a column number from 1 - 14
[6]
Value:H
Player 1 - Enter a row letter from A - J
[B]
Player 1 - Enter a column number from 1 - 14
[5]
Value:H
Player 2 - Enter a row letter from A - J
[H]
Player 2 - Enter a column number from 1 - 14
[7]
Value:H
Player 1 - Enter a row letter from A - J
[B]
Player 1 - Enter a column number from 1 - 14
[5]
Value:M
Player 2 - Enter a row letter from A - J
[A]
Player 2 - Enter a column number from 1 - 14
[3]
Value:M
Player 1 - Enter a row letter from A - J
[B]
Player 1 - Enter a column number from 1 - 14
[4]
Value:H
Player 2 - Enter a row letter from A - J
[C]
Player 2 - Enter a column number from 1 - 14
[3]
Value:M
Player 1 - Enter a row letter from A - J
[B]
Player 1 - Enter a column number from 1 - 14
[3]
Value:H
Player 2 - Enter a row letter from A - J
[J]
Player 2 - Enter a column number from 1 - 14
[7]
Value:H
Player 1 - Enter a row letter from A - J
[J]
Player 1 - Enter a column number from 1 - 14
[10]
Value:H
Player 2 - Enter a row letter from A - J
[J]
Player 2 - Enter a column number from 1 - 14
[8]
Value:H
Player 1 - Enter a row letter from A - J
[I]
Player 1 - Enter a column number from 1 - 14
[10]
Value:M
Player 2 - Enter a row letter from A - J
[J]
Player 2 - Enter a column number from 1 - 14
[9]
Value:H
Player 1 - Enter a row letter from A - J
[J]
Player 1 - Enter a column number from 1 - 14
[9]
Value:H
Player 2 - Enter a row letter from A - J
[A]
Player 2 - Enter a column number from 1 - 14
[7]
Value:M
Player 1 - Enter a row letter from A - J
[J]
Player 1 - Enter a column number from 1 - 14
[8]
Value:H
Player 2 - Enter a row letter from A - J
[B]
Player 2 - Enter a column number from 1 - 14
[8]
Value:M
Player 1 - Enter a row letter from A - J
[J]
Player 1 - Enter a column number from 1 - 14
[7]
Value:H
Player 2 - Enter a row letter from A - J
[I]
Player 2 - Enter a column number from 1 - 14
[2]
Value:H
Player 1 - Enter a row letter from A - J
[J]
Player 1 - Enter a column number from 1 - 14
[6]
Value:M
Player 2 - Enter a row letter from A - J
[I]
Player 2 - Enter a column number from 1 - 14
[3]
Value:H
Player 1 - Enter a row letter from A - J
[J]
Player 1 - Enter a column number from 1 - 14
[11]
Value:H
Player 2 - Enter a row letter from A - J
[J]
Player 2 - Enter a column number from 1 - 14
[7]
Value:M
Player 1 - Enter a row letter from A - J
[I]
Player 1 - Enter a column number from 1 - 14
[1]
Value:H
Player 2 - Enter a row letter from A - J
[J]
Player 2 - Enter a column number from 1 - 14
[13]
Value:M
Player 1 - Enter a row letter from A - J
[I]
Player 1 - Enter a column number from 1 - 14
[2]
Value:H
Player 2 - Enter a row letter from A - J
[J]
Player 2 - Enter a column number from 1 - 14
[12]
Value:M
Player 1 - Enter a row letter from A - J
[I]
Player 1 - Enter a column number from 1 - 14
[4]
Value:M
Player 2 - Enter a row letter from A - J
[G]
Player 2 - Enter a column number from 1 - 14
[11]
Value:M
Player 1 - Enter a row letter from A - J
[I]
Player 1 - Enter a column number from 1 - 14
[6]
Value:M
Player 2 - Enter a row letter from A - J
[H]
Player 2 - Enter a column number from 1 - 14
[12]
Value:M
Player 1 - Enter a row letter from A - J
[I]
Player 1 - Enter a column number from 1 - 14
[10]
Value:M
Player 2 - Enter a row letter from A - J
[I]
Player 2 - Enter a column number from 1 - 14
[13]
Value:M
Player 1 - Enter a row letter from A - J
[A]
Player 1 - Enter a column number from 1 - 14
[11]
Value:M
Player 2 - Enter a row letter from A - J
[J]
Player 2 - Enter a column number from 1 - 14
[14]
Value:M
Player 1 - Enter a row letter from A - J
[I]
Player 1 - Enter a column number from 1 - 14
[3]
Value:H
Player 2 - Enter a row letter from A - J
[J]
Player 2 - Enter a column number from 1 - 14
[1]
Value:M
Player 1 - Enter a row letter from A - J
[D]
Player 1 - Enter a column number from 1 - 14
[4]
Value:M
Player 2 - Enter a row letter from A - J
[D]
Player 2 - Enter a column number from 1 - 14
[1]
Value:H
Player 1 - Enter a row letter from A - J
[G]
Player 1 - Enter a column number from 1 - 14
[9]
Value:M
Player 2 - Enter a row letter from A - J
[D]
Player 2 - Enter a column number from 1 - 14
[2]
Value:H
Player 1 - Enter a row letter from A - J
[G]
Player 1 - Enter a column number from 1 - 14
[9]
Value:M
Player 2 - Enter a row letter from A - J
[D]
Player 2 - Enter a column number from 1 - 14
[3]
Value:H
Player 1 - Enter a row letter from A - J
[B]
Player 1 - Enter a column number from 1 - 14
[12]
Value:H
Player 2 - Enter a row letter from A - J
[D]
Player 2 - Enter a column number from 1 - 14
[4]
Value:H
Player 1 - Enter a row letter from A - J
[B]
Player 1 - Enter a column number from 1 - 14
[11]
Value:H
Player 2 - Enter a row letter from A - J
[D]
Player 2 - Enter a column number from 1 - 14
[5]
Value:H
Player 1 - Enter a row letter from A - J
[B]
Player 1 - Enter a column number from 1 - 14
[10]
Value:H
Player 2 - Enter a row letter from A - J
[D]
Player 2 - Enter a column number from 1 - 14
[6]
Value:M
Player 1 - Enter a row letter from A - J
[B]
Player 1 - Enter a column number from 1 - 14
[9]
Value:H
Enemy fleet destroyed. Congratulations player 1!



File Output in GameLog.txt

Battleship Game Log:
Winning Player: Player 1
Hits: 17 - 13
Number of Turns To Win: 29
Player 1 Board Pattern: Middle Heavy
Player 2 Board Pattern: Top Heavy



Testing


We have included a program that will allow you to create and run output tests automatically in the Starter Code. This will make it easier for you to verify that each possible progression through your solution is correct. Take a look atRunLocalTest.java. There are many utility features and tools that you do not need to worry about at the moment, instead, focus on the test case. It is included in the Starter Code. Read through it.



You can modify the test to test any method you like by following the same format. You can either download the program and run the main method or use the "Run" button on Vocareum to run the test. You can repeat this process for each path.


Public Test Cases Note


For many homeworks and projects, we will give you test cases that correspond to several of the ways we will be testing your program. But, we will not give you test cases for ALL of the ways we will be testing your program. You should think of other test cases to use that will fully test every aspect of every feature of your program. Just because your program passes all the test cases we give you does not mean that it is fully correct and will receive a score of 100.




Submit


After testing your solution and verifying that it meets the requirements described in this document, you can submit on Vocareum. You have 10 submission attempts to achieve full points.


Starter code-


import org.junit.Test;


import org.junit.After;


import java.lang.reflect.Field;


import org.junit.Assert;


import org.junit.Before;






import org.junit.runner.JUnitCore;


import org.junit.runner.Result;


import org.junit.runner.notification.Failure;






import java.io.*;


import static org.hamcrest.CoreMatchers.containsString;






import static org.junit.Assert.*;






/**


* A framework to run public test cases.


*


*


Purdue University -- CS18000 -- Fall 2021



*


* @author Purdue CS


* @version August 23, 2021


*/


public class RunLocalTest {



public static void main(String[] args) {



Result result = JUnitCore.runClasses(TestCase.class);



if (result.wasSuccessful()) {



System.out.println("Excellent - Test ran successfully");



} else {



for (Failure failure : result.getFailures()) {



System.out.println(failure.toString());



}



}



}







/**



* A set of public test cases.



*



*


Purdue University -- CS18000 -- Fall 2021




*



* @author Purdue CS



* @version August 23, 2021



*/



public static class TestCase {



private final InputStream originalInput = System.in;



private final PrintStream originalOutput = System.out;



@SuppressWarnings("FieldCanBeLocal")



private ByteArrayInputStream testIn;



private final String TA_ERROR_MESSAGE = "You bumped into an error! Please contact a TA immediately.";



private ByteArrayOutputStream testOut;







@Before



public void outputStart() {



testOut = new ByteArrayOutputStream();



System.setOut(new PrintStream(testOut));



}







@Test(timeout = 1000)



public void testShipPositions1() {





String input = "A\n1\nC\n9\nA\n1\nC\n13\nA\n1\nC\n14\nA\n1\nD\n" +



"9\nA\n1\nE\n9\nA\n1\nF\n9\nA\n1\nG\n9\nA\n1\nH\n6\nA\n1\n" +



"H\n7\nA\n1\nH\n8\nA\n1\nH\n9\nA\n1\nI\n1\nA\n1\nI\n2\nA\n1\nI\n" +



"3\nA\n1\nJ\n7\nA\n1\nJ\n8\nA\n1\nJ\n9\n";





receiveInput(input);







try {



PlayGame.main(new String[0]);



} catch (IOException e) {



e.printStackTrace();



fail("Ensure your program handles file reading and writing correctly and has the correct number of scanner calls!");



}



String out = getOutput();







String expectedFull = "Hello, Welcome to Battleship!\n"



+ "Player 1 - Enter a row letter from A - J\n"



+ "Player 1 - Enter a column number from 1 - 14\n"



+ "Value:M\n"



+ "Player 2 - Enter a row letter from A - J\n"



+ "Player 2 - Enter a column number from 1 - 14\n"



+ "Value:H\n"



+ "Player 1 - Enter a row letter from A - J\n"



+ "Player 1 - Enter a column number from 1 - 14\n"



+ "Value:M\n"



+ "Player 2 - Enter a row letter from A - J\n"



+ "Player 2 - Enter a column number from 1 - 14\n"



+ "Value:H\n"



+ "Player 1 - Enter a row letter from A - J\n"



+ "Player 1 - Enter a column number from 1 - 14\n"



+ "Value:M\n"



+ "Player 2 - Enter a row letter from A - J\n"



+ "Player 2 - Enter a column number from 1 - 14\n"



+ "Value:H\n"



+ "Player 1 - Enter a row letter from A - J\n"



+ "Player 1 - Enter a column number from 1 - 14\n"



+ "Value:M\n"



+ "Player 2 - Enter a row letter from A - J\n"



+ "Player 2 - Enter a column number from 1 - 14\n"



+ "Value:H\n"



+ "Player 1 - Enter a row letter from A - J\n"



+ "Player 1 - Enter a column number from 1 - 14\n"



+ "Value:M\n"



+ "Player 2 - Enter a row letter from A - J\n"



+ "Player 2 - Enter a column number from 1 - 14\n"



+ "Value:H\n"



+ "Player 1 - Enter a row letter from A - J\n"



+ "Player 1 - Enter a column number from 1 - 14\n"



+ "Value:M\n"



+ "Player 2 - Enter a row letter from A - J\n"



+ "Player 2 - Enter a column number from 1 - 14\n"



+ "Value:H\n"



+ "Player 1 - Enter a row letter from A - J\n"



+ "Player 1 - Enter a column number from 1 - 14\n"



+ "Value:M\n"



+ "Player 2 - Enter a row letter from A - J\n"



+ "Player 2 - Enter a column number from 1 - 14\n"



+ "Value:H\n"



+ "Player 1 - Enter a row letter from A - J\n"



+ "Player 1 - Enter a column number from 1 - 14\n"



+ "Value:M\n"



+ "Player 2 - Enter a row letter from A - J\n"



+ "Player 2 - Enter a column number from 1 - 14\n"



+ "Value:H\n"



+ "Player 1 - Enter a row letter from A - J\n"



+ "Player 1 - Enter a column number from 1 - 14\n"



+ "Value:M\n"



+ "Player 2 - Enter a row letter from A - J\n"



+ "Player 2 - Enter a column number from 1 - 14\n"



+ "Value:H\n"



+ "Player 1 - Enter a row letter from A - J\n"



+ "Player 1 - Enter a column number from 1 - 14\n"



+ "Value:M\n"



+ "Player 2 - Enter a row letter from A - J\n"



+ "Player 2 - Enter a column number from 1 - 14\n"



+ "Value:H\n"



+ "Player 1 - Enter a row letter from A - J\n"



+ "Player 1 - Enter a column number from 1 - 14\n"



+ "Value:M\n"



+ "Player 2 - Enter a row letter from A - J\n"



+ "Player 2 - Enter a column number from 1 - 14\n"



+ "Value:H\n"



+ "Player 1 - Enter a row letter from A - J\n"



+ "Player 1 - Enter a column number from 1 - 14\n"



+ "Value:M\n"



+ "Player 2 - Enter a row letter from A - J\n"



+ "Player 2 - Enter a column number from 1 - 14\n"



+ "Value:H\n"



+ "Player 1 - Enter a row letter from A - J\n"



+ "Player 1 - Enter a column number from 1 - 14\n"



+ "Value:M\n"



+ "Player 2 - Enter a row letter from A - J\n"



+ "Player 2 - Enter a column number from 1 - 14\n"



+ "Value:H\n"



+ "Player 1 - Enter a row letter from A - J\n"



+ "Player 1 - Enter a column number from 1 - 14\n"



+ "Value:M\n"



+ "Player 2 - Enter a row letter from A - J\n"



+ "Player 2 - Enter a column number from 1 - 14\n"



+ "Value:H\n"



+ "Player 1 - Enter a row letter from A - J\n"



+ "Player 1 - Enter a column number from 1 - 14\n"



+ "Value:M\n"



+ "Player 2 - Enter a row letter from A - J\n"



+ "Player 2 - Enter a column number from 1 - 14\n"



+ "Value:H\n"



+ "Player 1 - Enter a row letter from A - J\n"



+ "Player 1 - Enter a column number from 1 - 14\n"



+ "Value:M\n"



+ "Player 2 - Enter a row letter from A - J\n"



+ "Player 2 - Enter a column number from 1 - 14\n"



+ "Value:H\n"



+ "Player 1 - Enter a row letter from A - J\n"



+ "Player 1 - Enter a column number from 1 - 14\n"



+ "Value:M\n"



+ "Player 2 - Enter a row letter from A - J\n"



+ "Player 2 - Enter a column number from 1 - 14\n"



+ "Value:H\n"



+ "Enemy fleet destroyed. Congratulations player 2!\n";



assertEquals("Ensure your PlayGame.java output contains the correct winning information!", expectedFull, out);





String gameLog = "";



try {



FileReader fr = new FileReader("GameLog.txt");



BufferedReader br = new BufferedReader(fr);



String line = br.readLine();



while (line != null) {



gameLog += line;



gameLog += "\n";



line = br.readLine();



}



fr.close();



br.close();



} catch (Exception ex) {



ex.printStackTrace();



Assert.fail("Unexpected Exception!");



}





String expectedGameLog = "Battleship Game Log:\nWinning Player: Player 2\nHits: 0 - 17\n" +



"Number of Turns To Win: 17\nPlayer 1 Board Pattern: Bottom Heavy\nPlayer 2 Board Pattern: Bottom Heavy\n";





assertEquals("Ensure your GameLog.txt file output is correct",



expectedGameLog, gameLog);







}







/**



* UTILITY METHODS BELOW



*/







private void receiveInput(String str) {



testIn = new ByteArrayInputStream(str.getBytes());



System.setIn(testIn);



}







private String getOutput() {



return testOut.toString();



}







@After



public void restoreInputAndOutput() {



System.setIn(originalInput);



System.setOut(originalOutput);



}



}


}


2)-


Description


For this Homework, you will be writing several classes to manage an amusement park.



A list of all the classes you will be submitting is included below:




  • AmusementPark.java




  • Park.java




  • Ride.java




  • Rollercoaster.java




  • SpaceFullException.java




  • WaterPark.java




  • Waterslide.java




  • WrongRideException.java





Note: 5 points of your Challenge grade is based on Coding Style. You will need to follow the standards described on Brightspace. Use the "Run" button to check your Coding Style without using a submission.




Instructions


Reference the Javadochere
for implementation instructions. Be sure to read the requirements carefully.



In this assignment, you will be creating an interface and the necessary classes to include all the information about amusement and water parks such as their names, what rides they have, when they are open, etc.
You may use the program
RunLocalTest.javaprovided in the starter code to test your implementation locally. Be sure to keep in mind that the program only tests AmusementPark.java so it is recommended that you do your own testing beforehand on some of the other classes which must be working as expected (e.g., Ride.java) in order to test the AmusementPark class. Additionally, the test case program includes tests for only some of the methods to be implemented in the AmusementPark class but can be modified to test other methods or even other classes.



Testing




We have included a program that will allow you to create and run output tests automatically in the Starter Code. This will make it easier for you to verify that each possible progression through your solution is correct. Take a look atRunLocalTest.java. There are many utility features and tools that you do not need to worry about at the moment, instead, focus on the test case. It is included in the Starter Code. Read through it.



You can modify the test to test any method you like by following the same format. You can either download the program and run the main method or use the "Run" button on Vocareum to run the test.



Public Test Cases Note


For many homeworks and projects, we will give you test cases that correspond to several of the ways we will be testing your program. But, we will not give you test cases for ALL of the ways we will be testing your program. You should think of other test cases to use that will fully test every aspect of every feature of your program. Just because your program passes all the test cases we give you does not mean that it is fully correct and will receive a score of 100.




Submit


After testing your solution and verifying that it meets the requirements described in this document, you can submit on Vocareum. You have 10 submission attempts to achieve full points.


Starter code-


import org.junit.Test;


import org.junit.After;


import java.lang.reflect.Field;


import org.junit.Assert;


import org.junit.Before;


import org.junit.rules.Timeout;






import org.junit.runner.JUnitCore;


import org.junit.runner.Result;


import org.junit.runner.notification.Failure;






import javax.swing.*;


import java.io.*;


import java.lang.reflect.*;


import java.util.ArrayList;


import java.util.concurrent.ThreadLocalRandom;


import java.lang.reflect.InvocationTargetException;


import java.util.UUID;






import static org.junit.Assert.*;






/**


* A framework to run public test cases.


*


*


Purdue University -- CS18000 -- Fall 2021



*


* @author Purdue CS


* @version August 23, 2021


*/


public class RunLocalTest {



public static void main(String[] args) {



Result result = JUnitCore.runClasses(TestCase.class);



if (result.wasSuccessful()) {



System.out.println("Excellent - Test ran successfully");



} else {



for (Failure failure : result.getFailures()) {



System.out.println(failure.toString());



}



}



}







/**



* A set of public test cases.



*



*


Purdue University -- CS18000 -- Fall 2021




*



* @author Purdue CS



* @version August 23, 2021



*/



public static class TestCase {



private final PrintStream originalOutput = System.out;



private final InputStream originalSysin = System.in;







@SuppressWarnings("FieldCanBeLocal")



private ByteArrayInputStream testIn;







@SuppressWarnings("FieldCanBeLocal")



private ByteArrayOutputStream testOut;







@Before



public void outputStart() {



testOut = new ByteArrayOutputStream();



System.setOut(new PrintStream(testOut));



}







@After



public void restoreInputAndOutput() {



System.setIn(originalSysin);



System.setOut(originalOutput);



}







private String getOutput() {



return testOut.toString();



}







@SuppressWarnings("SameParameterValue")



private void receiveInput(String str) {



testIn = new ByteArrayInputStream(str.getBytes());



System.setIn(testIn);



}







@Test(timeout = 1000)



public void testAmusement() {



try {



ArrayList expectedRides = new ArrayList();



expectedRides.add(new Rollercoaster("Space Dome", "Black", 48, 20, false));



expectedRides.add(new Rollercoaster("Speed Racing", "Black", 48, 16, false));



expectedRides.add(new Rollercoaster("Mover of People", "Blue", 36, 32, true));



AmusementPark testAmusement = new AmusementPark("The Valley Adventure", 10.5, 100,



expectedRides, true, true, false, false, new boolean[]{true, true, true, true});



String actName = testAmusement.getName();



double actAdmissionCost = testAmusement.getAdmissionCost();



double actLand = testAmusement.getLand();



ArrayList actRides = testAmusement.getRides();



boolean actIndoor = testAmusement.isIndoor();



boolean actOutdoor = testAmusement.isOutdoor();



boolean actArcade = testAmusement.isArcade();



boolean actBowling = testAmusement.isBowling();



boolean[] actSeasons = testAmusement.getSeasons();



assertEquals("Ensure your getName() method in AmusementPark.java returns the correct value!", "The Valley Adventure", actName);



assertEquals("Ensure your getAdmissionCost() method in AmusementPark.java returns the correct value!", 10.5, actAdmissionCost, 0.01);



assertEquals("Ensure your getLand() method in AmusementPark.java returns the correct value!", 100, actLand, 0.01);



ArrayList expectedRidesCopy = new ArrayList();



for (Ride r : expectedRides) {



expectedRidesCopy.add(r);



}



assertEquals("Ensure your isIndoor() method in AmusementPark.java returns the correct value!", true, actIndoor);



assertEquals("Ensure your isOutdoor() method in AmusementPark.java returns the correct value!", true, actOutdoor);



assertEquals("Ensure your isArcade() method in AmusementPark.java returns the correct value!", false, actArcade);



assertEquals("Ensure your isBowling() method in AmusementPark.java returns the correct value!", false, actBowling);



for (int i = 0; i



assertEquals("Ensure your getSeasons() method in AmusementPark.java returns the correct value!", true, actSeasons[i]);



}



testAmusement.setName("The Super Valley Adventure");



testAmusement.setAdmissionCost(12.80);



testAmusement.setArcade(true);



testAmusement.setBowling(true);



testAmusement.setSeasons(new boolean[]{true, true, true, false});



assertEquals("Ensure your setName() method in AmusementPark.java sets name to the correct value!", "The Super Valley Adventure", testAmusement.getName());



assertEquals("Ensure your setAdmissionCost() method in AmusementPark.java sets admission cost to the correct value!", 12.80, testAmusement.getAdmissionCost(), 0.01);



assertEquals("Ensure your setArcade() method in AmusementPark.java sets arcade to the correct value!", true, testAmusement.isArcade());



assertEquals("Ensure your setBowling() method in AmusementPark.java sets bowling to the correct value!", true, testAmusement.isBowling());



for (int i = 0; i



boolean expectedSeasons = true;



if (i == 3) {



expectedSeasons = false;



}



assertEquals("Ensure your setSeasons() method in AmusementPark.java sets seasons to the correct value!", expectedSeasons, testAmusement.getSeasons()[i]);



}



Ride newRide = new Rollercoaster("Testing Track", "Orange", 42, 16, false);



expectedRidesCopy.add(newRide);



try {



testAmusement.addRide(newRide);



} catch (WrongRideException e) {



fail("Ensure your addRide() method throws a WrongRideException in the correct situations!");



}



actRides = testAmusement.getRides();



for (int i = 0; i



String expectedName = expectedRidesCopy.get(i).getName();



String expectedColor = expectedRidesCopy.get(i).getColor();



int expectedMinHeight = expectedRidesCopy.get(i).getMinHeight();



int expectedMaxRiders = expectedRidesCopy.get(i).getMaxRiders();



actName = actRides.get(i).getName();



String actColor = actRides.get(i).getColor();



int actMinHeight = actRides.get(i).getMinHeight();



int actMaxRiders = actRides.get(i).getMaxRiders();



assertEquals("Ensure your addRide() method in AmusementPark.java updates the rides instance variable to be a list of rides with the proper names!", expectedName, actName);



assertEquals("Ensure your addRide() method in AmusementPark.java updates the rides instance variable to be a list of rides with the proper colors!", expectedColor, actColor);



assertEquals("Ensure your addRide() method in AmusementPark.java updates the rides instance variable to be a list of rides with the proper minHeight values!", expectedMinHeight, actMinHeight);



assertEquals("Ensure your addRide() method in AmusementPark.java updates the rides instance variable to be a list of rides with the proper maxRiders values!", expectedMaxRiders, actMaxRiders);



}



} catch (Exception e) {



e.printStackTrace();



fail();



}



}



}


}

















Answered 2 days AfterOct 23, 2021

Answer To: Arrays and File I/O Create a program in a class called PlayGame.java that reads data from two files...

Ketaki answered on Oct 26 2021
110 Votes
import java.io.*;
import java.util.*;
public class Main //extends Gamelog
{
public static int numRows = 10;
public static int numCols = 14;
public static int player1Ships;
public static int player2Ships;
publi
c static String[][] grid = new String[numRows][numCols];
public static int[][] missedGuesses = new int[numRows][numCols];
public static void main(String[] args)
    {
System.out.println("**** Welcome to Battle Ships game ****");
System.out.println("Right now, sea is empty\n");
//Step 1 โ€“ Create the ocean map
createOceanMap();
//Step 2 โ€“ Deploy playerโ€™s ships
deployPlayer1Ships();
//Step 3 - Deploy computer's ships
deployPlayer2Ships();
//Step 4 Battle
do
        {
Battle();
}while(Main.player1Ships != 0 && Main.player2Ships != 0);
//Step 5 - Game over
gameOver();
}
public static void createOceanMap()
    {
//First section of Ocean Map
System.out.print(" ");
for(int i = 0; i < numCols; i++)
System.out.print(i);
System.out.println();
//Middle section of Ocean Map
for(int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++)
            {
grid[i][j] = " ";
if (j == 0)
System.out.print(i + "|" + grid[i][j]);
else if (j == grid[i].length - 1)
System.out.print(grid[i][j] + "|" + i);
else
System.out.print(grid[i][j]);
}
System.out.println();
}
//Last section of Ocean Map
System.out.print(" ");
for(int i = 0; i < numCols; i++)
System.out.print(i);
System.out.println();
}
public static void deployPlayer1Ships()
    {
Scanner input = new Scanner(System.in);
System.out.println("\nDeploy Player1 ships:");
//Deploying five ships for player
Main.player1Ships = 5;
        
for (int i = 1; i <= Main.player1Ships; )
        {
System.out.print("Enter X coordinate for your " + i + " ship: ");
int x = input.nextInt();
System.out.print("Enter Y coordinate for your " + i + " ship: ");
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions ยป

Submit New Assignment

Copy and Paste Your Assignment Here