代写 algorithm game GUI Java math AI operating system statistic • skip to content

• skip to content
 cmps11s19
User Tools
• Logged in as: Ziran Zhao (Darren) (zzhao102)
• Update Profile
• Log Out
Site Tools
Search

ToolsShow pagesourceOld revisionsBacklinksMedia ManagerSitemapUpdate ProfileLog Out
• Media Manager
• Sitemap
You are here: Welcome to cmps11s19 » Assignments for cmps11s19 » Assignment 08: AI Battleship Player

assignments:assignment_08
−Table of Contents
• Assignment 08: AI Battleship Player

• Prerequisites

• Background
◦ Game and GUI

• Assignment
◦ Requirements

◦ Extra Credit Opportunity

◦ Compiling/Running/Testing
▪ From the Command Line

▪ From Eclipse

▪ Tip

▪ API Documentation

• Submission

• Due Date and Point Value

Assignment 08: AI Battleship Player
Goals
• Practice with leveraging inheritance-based polymorphism by extending an abstract class.

• Develop an artificial-intelligence algorithm for playing a Battleship-style game.

Prerequisites
This assignment requires knowledge of the material presented in class through week 08.
Background
According to the arbiter of all human knowledge:
A JAR (Java ARchive) is a package file format typically used to aggregate many Java class files and associated metadata and resources (text, images, etc.) into one file for distribution.
For this assignment, there is a JAR file located at /srv/datasets/docs_battleshipplayer/Battleship.jar. This file is also available via HTTP at http://jeff.cis.cabrillo.edu/datasets/docs_battleshipplayer/Battleship.jar.
Battleship.jar is executable. That is, it has a bit of metadata that tells a JVM in which class it should look for a main method. You can run the file from the command line (the following assumes that Battleship.jar is in the current directory):
java -jar Battleship.jar
You will probably want to run this on your own machine, since by default you have a text-only connection to the server. Most operating systems will also let you run a JAR by simply double-clicking the file or right-clicking and choosing a “run” option. If you want to try running on the server itself, you will need to enable X11 forwarding:
• On Mac/Linux: ssh -Y yourusername@jeff.cis.cabrillo.edu


• On Windows: MobaXterm has a built-in capability to do this. You shouldn’t have to do anything special.

• On other devices: This may be problematic on other devices.

Either way, you are starting a JVM that extracts the classes from this JAR and runs a class called BattleshipGame.
Game and GUI
As you can see when running this program, it is a Battleship-style game. This particular game consists only of one board and one player. If the player is able to sink all ships in no more than 60 shots, the player wins.
The players in this game are not human; they are instances of classes that extend an abstract class called BattleshipPlayer.
Run the game. Once you have a player class to use (e.g. RandomShooter below), press the “Select a player class…” button. The other buttons control the rest of the game:
1. Single Game creates a new instance of the player and invokes its run method.

2. Auto automates the playing process as quickly as possible, repeatedly creating a new board and player instance and telling the player to run.

3. Reset clears the statistics shown at the bottom of the window.

Assignment
You shall create a class that extends the abstract class BattleshipPlayer. Include your CruzID in the name of the class, e.g. something like JbergamiPlayer.
In your class, implement at least the following:
• A parameterless constructor, in which you at least assign the inherited String field playerName a value.

• A non-abstract override of the inherited abstract run method, from which your player will play the game.

In order to interact with the game, there are two non-static methods your class may invoke on the inherited game field (a reference to an object of type BattleshipGame):
1. BattleshipGame.inProgress() returns a boolean indicating whether or not the game is still in progress (i.e., not over)

2. BattleshipGame.shoot(int col, int row) fires a shot at the given row and column (range 0–9), and returns a boolean indicating whether the shot was a hit (true) or miss (false). It may also throw an exception if the column/row combination is invalid, or if the game is over. (See the API Documentation for details.)

Here is an example player that shoots randomly (and thus almost always loses):
RandomShooter.java
/**
* This is not a good player, but it is at least a demonstration of the minimal
* structure you need to set up for your player class.
*
* @author Jeffrey Bergamini for CMPS 11, jbergami@ucsc.edu
*/
public class RandomShooter extends BattleshipPlayer {
 
public RandomShooter() {
super(); // Good practice to invoke parent class constructor
this.playerName = “All I do is shoot randomly.”;
}
 
@Override
public void run() {
while (game.inProgress()) {
game.shoot((int) (Math.random() * 10), (int) (Math.random() * 10));
// You can add a delay if you want to be able to watch your player play, move by move:
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.err.println(“This should really never have happened…”);
}
}
}
}
Requirements
1. For full credit your player should win at least 30% of the time, on average.

2. If your player triggers any exceptions via BattleshipGame’s shoot method, your player must catch them.

3. Your player must generate no output on standard output or standard error.

4. Choose meaningful variable/method names and document your code with comments explaining your player’s strategy, so the code is readable.

Extra Credit Opportunity
If your player wins at least 60% of the time, on average, you may be eligible for up to 50% extra credit.
Compiling/Running/Testing

From the Command Line
You will need to include the JAR in your “classpath” in order to compile your player:
javac -cp Battleship.jar MyPlayer.java
If you want to copy Battleship.jar to the current directory:
cp /srv/datasets/docs_battleshipplayer/Battleship.jar .
In order to test your player:
1. Run the JAR: java -jar Battleship.jar


2. Choose your compiled player class file via the “Select a player class…” button. You should then see the name of your player at the bottom of the window.

From Eclipse
If you want to develop and run this in Eclipse, you need to add Battleship.jar to the build path of your project. Save Battleship.jar to your computer somewhere, then do the following:
1. Right-click your project name, and select Properties

2. Select Java Build Path in the panel on the left

3. Select the Libraries tab and the Classpath item

4. Press the Add External JARs… button

5. Browse to where you saved Battleship.jar

6. You can now run the program by pressing the run button (choose to run it as an application)

Tip
If you want to see the progress your player makes as it plays, introduce delay(s) while it plays: See the Thread.sleep method.
API Documentation
Take advantage of the full Javadocs for this game’s platform. BattleshipGame’s shoot method is described in more detail here, including the exceptions that may be thrown.
Submission
Submit only your player’s source code via turnin.
 Feedback Robot
This project has a feedback robot that will run some tests on your submission and provide you with a feedback report via email within roughly one minute.
Please read the feedback carefully.
Due Date and Point Value
Due at 23:59:59 on the date listed on the syllabus.
Assignment 08 is worth 160 points, plus potential extra credit as described above.
Possible point values per category:
——————————————
Player wins at least 30% of the time 160
Extra credit (>60% win rate) 80
Unhandled exceptions -15%
Other poor practices -10–20%
——————————————
assignments/assignment_08.txt · Last modified: 2019-06-03 11:57 by Jeffrey Bergamini

Page Tools
• Show pagesource
• Old revisions
• Backlinks
• Back to top
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 4.0 International