CIT 590 Homework 10 Battleship

Similar documents
CSci 1113: Introduction to C/C++ Programming for Scientists and Engineers Homework 10 Spring 2018

Project 1 - Battleship Game

CMSC 201 Spring 2018 Project 2 Battleship

5.6.1 The Special Variable this

CS 15 Design Section. Design

Mobile Application Programming: ios

CS201 - Assignment 3, Part 2 Due: Wednesday March 5, at the beginning of class


Assignment 4. Aggregate Objects, Command-Line Arguments, ArrayLists. COMP-202B, Winter 2011, All Sections. Due: Tuesday, March 22, 2011 (13:00)

CS 3360 Design and Implementation of Programming Languages. Exam 1

CS61BL: Data Structures & Programming Methodology Summer Project 1: Dots!

Object Oriented Programming. Week 1 Part 3 Writing Java with Eclipse and JUnit

Com S 227 Spring 2018 Assignment points Due Date: Thursday, September 27, 11:59 pm (midnight) "Late" deadline: Friday, September 28, 11:59 pm

Boaz Kantor Introduction to Computer Science IDC Herzliya ( Reichman )

INTERFACES. 24-Dec-10 INTERFACES VS. INHERITANCE. Boaz Kantor Introduction to Computer Science IDC Herzliya ( Reichman ) Interfaces: Inheritance:

Assignment3 CS206 Intro to Data Structures Fall Part 1 (50 pts) due: October 13, :59pm Part 2 (150 pts) due: October 20, :59pm

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Lab 4 Due April 18 th

n Specifying what each method does q Specify it in a comment before method's header n Precondition q Caller obligation n Postcondition

Logistics. Final Exam on Friday at 3pm in CHEM 102

Java Classes, Inheritance, and Interfaces

Lab Exercise 6: Abstract Classes and Interfaces CS 2334

CS 170 Java Programming 1. Week 13: Classes, Testing, Debugging

ACORN.COM CS 1110 SPRING 2012: ASSIGNMENT A1

CS211 Computers and Programming Matthew Harris and Alexa Sharp July 9, Boggle

CS 170 Java Programming 1. Week 15: Interfaces and Exceptions

Mobile Application Programming: Android

package As7BattleShip;


Chapter 1 Getting Started

CS 315 Software Design Homework 3 Preconditions, Postconditions, Invariants Due: Sept. 29, 11:30 PM

CMPSCI 187 / Spring 2015 Postfix Expression Evaluator

The class Object. Lecture CS1122 Summer 2008

Carrera: Analista de Sistemas/Licenciatura en Sistemas. Asignatura: Programación Orientada a Objetos

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

Midterm Logistics. Midterm tonight, 7PM 10PM Exam location by last name:

Lecture 10. Overriding & Casting About

CS 61B, Spring 1999 MT3 Professor M. Clancy

Programming Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science Indian Institute of Technology, Madras

CMPSCI 187 / Spring 2015 Hangman

COP 3330 Final Exam Review

Rules and syntax for inheritance. The boring stuff

Building a fraction class.

CIS 121 Data Structures and Algorithms with Java Spring 2018

Session 4 Starting the Air Raid Game

CSCI 355 LAB #2 Spring 2004

Super-Classes and sub-classes

CS-202 Introduction to Object Oriented Programming

Creating Java Programs with Greenfoot

CS201 - Assignment 3, Part 1 Due: Friday February 28, at the beginning of class

Background: This programming assignment focuses on implementation and usage of a graph data structure.

Inheritance (IS A Relationship)

Background: This programming assignment focuses on implementation and usage of a graph data structure.

MITOCW watch?v=4dj1oguwtem

Java Programming. Atul Prakash

Java Programming. Computer Science 112

Tips from the experts: How to waste a lot of time on this assignment

CS 315 Software Design Homework 1 First Sip of Java Due: Sept. 10, 11:30 PM

Our second exam is Thursday, November 10. Note that it will not be possible to get all the homework submissions graded before the exam.

CS 2110 Summer 2011: Assignment 2 Boggle

Formal Methods of Software Design, Eric Hehner, segment 1 page 1 out of 5

CIT 590 Homework 6 Fractions

CS 211 Programming Practicum Spring 2017

CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance

JAVA MOCK TEST JAVA MOCK TEST II

Computer Science II. OO Programming Classes Scott C Johnson Rochester Institute of Technology

Supporting Class / C++ Lecture Notes

Slide 1 CS 170 Java Programming 1 Testing Karel

More on inheritance CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2014

Software Testing Prof. Meenakshi D Souza Department of Computer Science and Engineering International Institute of Information Technology, Bangalore

Programming Exercise 14: Inheritance and Polymorphism

CS/ENGRD 2110 FALL Lecture 6: Consequence of type, casting; function equals

Classwork 7: Craps. N. Duong & R. Rodriguez, Java Crash Course January 6, 2015

Week 3 Classes and Objects

CS Exam 1 Review Suggestions

CS 170 Java Programming 1. Week 12: Creating Your Own Types

Programming via Java Subclasses

Spring, 2014 CIT 590. Programming Languages and Techniques Homework 7

Today. Book-keeping. Inheritance. Subscribe to sipb-iap-java-students. Slides and code at Interfaces.

Basic Object-Oriented Concepts. 5-Oct-17

Object-Oriented Design Lecture 23 CS 3500 Fall 2009 (Pucella) Tuesday, Dec 8, 2009

Interlude. Object Oriented Design

BEGINNER PHP Table of Contents

CSCI 355 Lab #2 Spring 2007

Today in CS161. Lecture #12 Arrays. Learning about arrays. Examples. Graphical. Being able to store more than one item using a variable

Object-Oriented Design Lecture 11 CS 3500 Spring 2010 (Pucella) Tuesday, Feb 16, 2010

Chapter 15: Object Oriented Programming

Subclassing for ADTs Implementation

CS 134 Programming Exercise 9:

Assignment #4 Hangman Due: 3:15pm on Monday, November 3rd Your Early Assignment Help (YEAH) hours: 4:15pm-5:15pm, Friday, October 24th in Hewlett 200

Error Handling. public int divide(double a, double b) { if (b==0) return -1; // error double result = a/b; return 0; // success }

Programming Languages and Techniques (CIS120)

Assignment 2 specification Inheritance, Polymorphism and Abstraction

Fleury s Algorithm The Adjacency Matrix and Distances Is There a Path From A to B? What is the Path from A to B? Is There a Path From ANY A to ANY B?

Lab 1: Silver Dollar Game 1 CSCI 2101B Fall 2018

Program development plan

Our second exam is Monday, April 3. Note that it will not be possible to get all the homework submissions graded before the exam.

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

CMPSCI 187: Programming With Data Structures. Lecture #15: Thinking Recursively David Mix Barrington 10 October 2012

Transcription:

CIT 590 Homework 10 Battleship Purposes of this assignment: To give you more experience with classes and inheritance General Idea of the Assignment Once again, this assignment is based on a game, since games are a good source of relatively simple problems. Battleship is usually a two-player game, where each player has a fleet and an ocean (hidden from the other player), and tries to be the first to sink the other player's fleet. We'll just do a solo version, where the computer places the ships, and the human attempts to sink them. How to play The computer places the ten ships on the ocean in such a way that no ships are immediately adjacent to each other, either horizontally, vertically, or diagonally. For example, The human player does not know where the ships are. The initial display of the ocean shows a 10 by 10 array of locations, all the same. The human player tries to hit the ships, by calling out a row and column number. The computer responds with one bit of information hit" or "miss." When a ship is hit but not sunk, the program does not provide any information about what kind of a ship was hit. However, when a

ship is hit and sinks, the program prints out a message "You just sank a ship-type." After each shot, the computer redisplays the ocean with the new information. A ship is "sunk" when every square of the ship has been hit. Thus, it takes four hits (in four different places) to sink a battleship, three to sink a cruiser, two for a destroyer, and one for a submarine. The object is to sink the fleet with as few shots as possible; the best possible score would be 20. (Low scores are better.) When all ships have been sunk, the program prints out a message that the game is over, and tells how many shots were required. Details Name your project Battleship, and your package battleship. The classes Your program should have the following classes: class BattleshipGame This is the "main" class, containing the main method and a variable of type Ocean. class Ocean This contains a 10x10 array of Ships, representing the "ocean," and some methods to manipulate it. abstract class Ship This describes characteristics common to all the ships. It has subclasses: o class Battleship extends Ship Describes a ship of length 4. o class Cruiser extends Ship Describes a ship of length 3. o class Destroyer extends Ship Describes a ship of length 2. o class Submarine extends Ship Describes a ship of length 1. o class EmptySea extends Ship Describes a part of the ocean that doesn't have a ship in it. (It seems silly to have the lack of a ship be a type of ship, but this is a trick that simplifies a lot of things. This way, every location in the ocean contains a "ship" of some kind.) Class BattleshipGame The BattleshipGame class is the "main" class that is, it contains a main method. In this class you will set up the game; accept "shots" from the user; display the results; print final scores; and ask the user if he/she wants to play again. All input/output is done from here (although it uses a print() method in the Ocean class). All computation will be done in the Ocean class and the various Ship classes. To aid the user, row numbers should be displayed along the left edge of the array, and column numbers should be displayed along the top. Numbers should be 0 to 9, not 1 to 10. The top left corner square should be 0, 0. Use different characters (specified below) to indicate locations that contain a hit, locations that contain a miss, and locations that have never been fired upon. Use methods. Don't cram everything into one or two methods, but try to divide up the work into sensible parts with reasonable names.

Class ShipTest Test every non-private method in the Ship class. TDD (Test-Driven Design) is highly recommended. Although the Ship class is abstract, you can still test the methods by creating a ship of some particular type, and using it to test the methods it inherits from Ship. Also test the methods in each subclass of Ship. You can do this here or in separate test classes, as you wish. Class Ship Since we don't really care which end of a ship is the bow and which the stern, we will consider all ships to be facing up or left. Other parts of the ship are in higher-numbered rows or columns. The bowrow and bowcolumn will indicate the bow, and other parts of the ship will be in highernumbered rows or higher-numbered columns. For example, if a Destroyer (length 2) has its bow in (3, 7), then its stern (other square) is in either (4, 7) or (3, 8), but not (2, 7) or (3, 6). You don't need to write a constructor for this class Java will automatically supply one for you (with no arguments). Instance variables int bowrow the row (0 to 9) which contains the bow (front) of the ship. int bowcolumn the column (0 to 9) which contains the bow (front) of the ship. int length the number of squares occupied by the ship. An "empty sea" location has length 1. boolean horizontal true if the ship occupies a single row, false otherwise. boolean [] hit = new boolean[4]; an array of booleans telling whether that part of the ship has been hit. Only battleships use all four locations; cruisers use the first three; destroyers 2; submarines 1; and "empty sea" either one or none. abstract int getlength() Returns the length of this particular ship. (An abstract "ship" doesn't have a fixed length.) Getters: int getbowrow() Returns bowrow int getbowcolumn() Returns bowcolumn boolean ishorizontal() Returns horizontal Setters: void setbowrow(int row) Sets the value of bowrow void setbowcolumn(int column) Sets the value of bowcolumn void sethorizontal(boolean horizontal) Sets the value of the instance variable horizontal. abstract String getshiptype()

Returns the type of this ship. boolean oktoplaceshipat(int row, int column, boolean horizontal, Ocean ocean) Returns true if it is okay to put a ship of this length with its bow in this location, with the given orientation, and returns false otherwise. The ship must not overlap another ship, or touch another ship (vertically, horizontally, or diagonally), and it must not "stick out" beyond the array. Does not actually change either the ship or the Ocean, just says whether it is legal to do so. (Note: The length of this ship is available as an instance variable, so we don't need to supply it as a parameter.) void placeshipat(int row, int column, boolean horizontal, Ocean ocean) "Puts" the ship in the ocean. This involves giving values to the bowrow, bowcolumn, and horizontal instance variables in the ship, and it also involves putting a reference to the ship in each of 1 or more locations (up to 4) in the ships array in the Ocean object. (Note: This will be as many as four identical references; you can't refer to a "part" of a ship, only to the whole ship.) boolean shootat(int row, int column) If a part of the ship occupies the given row and column, and the ship hasn't already been sunk, mark that part of the ship as "hit" (in the hit array, 0 indicates the bow) and return true, otherwise return false. boolean issunk() Return true if every part of the ship has been hit, false otherwise. public String tostring() Returns a single-character String to use in the Ocean's print method (see below). This method should return "x" if the ship has been sunk, "S" if it has not been sunk. This method can be used to print out locations in the ocean that have been shot at; it should not be used to print locations that have not been shot at. class Battleship extends Ship class Cruiser extends Ship class Destroyer extends Ship class Submarine extends Ship Each of these classes has a constructor, the purpose of which is to set the inherited length variable to the correct value, and to initialize the hit array.

String getshiptype() Returns one of the strings "battleship", "cruiser", "destroyer", or "submarine", as appropriate. Class EmptySea extends Ship You may wonder why "EmptySea" is a type of Ship. The answer is that the Ocean contains a Ship array, every location of which is, or can be, a reference to some Ship. If a particular location is empty, the obvious thing to do is to put a null in that location. But this obvious approach has the problem that, every time we look at some location in the array, we have to check if it is null. By putting a non-null value in empty locations, denoting the absence of a ship, we can save all that null checking. EmptySea() This constructor sets the inherited length variable to 1. boolean shootat(int row, int column) This method overrides shootat(int row, int column) that is inherited from Ship, and always returns false to indicate that nothing was hit. boolean issunk() This method overrides issunk() that is inherited from Ship, and always returns false to indicate that you didn't sink anything. public String tostring() Returns a single-character String "-" to use in the Ocean's print method (see below). Class OceanTest This is a JUnit test class for Ocean. Test every required method for Ocean, including the constructor, but not including the print() method. If you create additional methods in the Ocean class, you must either make them private, or write tests for them. Test methods do not need comments, unless they do something non-obvious. Class Ocean Instance variables Ship[][] ships = new Ship[10][10] Used to quickly determine which ship is in any given location.

int shotsfired The total number of shots fired by the user. int hitcount The number of times a shot hit a ship. If the user shoots the same part of a ship more than once, every hit is counted, even though the additional "hits" don't do the user any good. Ocean() The constructor. Creates an "empty" ocean (fills the ships array with EmptySeas). Also initializes any game variables, such as how many shots have been fired. void placeallshipsrandomly() Place all ten ships randomly on the (initially empty) ocean, making certain that all ships are placed legally. Place larger ships before smaller ones. If you place the smaller ships first, you may be unable to place larger ships; but if you place the larger ships first, it will always be possible to find somewhere to put the smaller ships. You will want to use the Random class in the java.util package, so look that up in the Java API. boolean isoccupied(int row, int column) Returns true if the given location contains a ship, false if it does not. boolean shootat(int row, int column) Returns true if the given location contains a "real" ship, still afloat, (not an EmptySea), false if it does not. In addition, this method updates the number of shots that have been fired, and the number of hits. Note: If a location contains a "real" ship, shootat should return true every time the user shoots at that same location. Once a ship has been "sunk", additional shots at its location should return false. int getshotsfired() Returns the number of shots fired (in this game). int gethitcount() Returns the number of hits recorded (in this game). All hits are counted, not just the first time, a given square is hit. (We allow the user to get a worse score by shooting again at the same location.) boolean isgameover() Returns true if all ships have been sunk, otherwise false. Ship[][] getshiparray() Returns the actual 10x10 array of ships, not a copy. The methods in the Ship class that take an Ocean parameter really need to be able to look at the contents of this array;

the placeshipat method even needs to modify it. While it is undesirable to allow methods in one class to directly access instance variables in another class, sometimes there is just no good alternative. void print() Prints the ocean. To aid the user, row numbers should be displayed along the left edge of the array, and column numbers should be displayed along the top. Numbers should be 0 to 9, not 1 to 10. The top left corner square should be 0, 0. Use 'S' to indicate a location that you have fired upon and hit a (real) ship, '-' to indicate a location that you have fired upon and found nothing there, 'x' to indication location containing a sunken ship, and '.' to indicate a location that you have never fired upon. This is the only method in the Ocean class that does any input/output, and it is never called from within the Ocean class (except possibly during debugging), only from the BattleshipGame class. You are welcome to write additional methods of your own. Additional methods should either be tested (if you think they have some usefulness outside this class), or private (if they don't). Submission and Due Date Zip your Eclipse project and turn it in to Canvas before 11:59pm Tuesday, December 5.