Stats 102A
Midterm Project
July 2, 2019
General Notes
You will submit a minimum of three files, the core files must conform to the following naming conventions
(including capitalization and underscores). 123456789 is a placeholder, please replace these nine digits
with your nine-digit UID. The files you must submit are:
1. 123456789_stats102a_midterm.R An R script file containing all of the functions you wrote for
the homework. The first line of your .Rmd file, after loading libraries, should be sourcing this
script.
2. 123456789_stats102a_midterm.Rmd Your markdown file which generates the output file of your
submission.
3. 123456789_stats102a_midterm.html/pdf Your output file, either a PDF or an HTML file
depending on the output you choose to generate.
4. included image files You may name these what you choose, but you must include all of the
image files you generated for your structured flowcharts, otherwise your file will not knit.
If you fail to submit any of the required core files you will receive ZERO points for the assignment.
If you submit any files which do not conform to the specified naming convention, you will receive (at
most) half credit for the assignment.
Your coding should adhere to the tidyverse style guide: https://style.tidyverse.org/
All flowcharts should be done on separate sheets of paper, but be included, inline as images, in your
final markdown document.
Any functions you write should be included in a separate functions file.
Your .Rmd file must knit. If your .Rmd file does not knit you will receive (at most) half credit for the
assignment.
The two most common reasons files fail to knit are because of workspace/directory structure issues and
Stats 102A作业代做、代写R语言作业、R编程设计作业调试、代做script file作业
because of missing include files. To remedy the first, ensure all of the file paths in your document are relative
paths pointing at the current working directory. To remedy the second, simply make sure you upload any
and all files you source or include in your .Rmd file.
NOTE: Everything you need to do this assignment is here, in your class notes, or was covered in discussion
or lecture.
DO NOT look for solutions online.
EVERYTHING you submit MUST be 100% your, original, work product. Any student suspected
of plagiarizing, in whole or in part, any portion of this assignment, will be immediately referred to
the Dean of Student’s office without warning, and you will get a zero for all parts of the tainted
homework.
YOU MAY NOT collaborate on this assignment.
– Each person must MUST work INDEPENDENTLY on this assignment in its entirety.
1
Midterm Project Requirements
For each of the following problems you will:
a) Create a structured flowchart of the game and all functions/subroutines.
b) Write pseudocode sufficiently complete, clear, and concise enough to enable a person to accurately
implement the your design in any programming language they are adept with using.
c) Perform a complexity analysis of your AI functions, specifically, of your strongest AI function and the
function you write to place ships.
d) Finally, you must write the functions/subroutines which will allow your game to be played between a
human opponent and your AI player.
Battleship Specification
Rules
From Wikipedia:
Traditionally, the game is played on four grids, two for each player. The grids are typically square, usually
10 × 10, and the individual squares in the grid are identified by letter and number. On a primary grid the
player arranges their own ships and keeps a record of the shots by the opponent. On the secondary grid the
player records their own shots as hits or misses.
Before play begins, each player secretly arranges their ships on their primary grid. Each ship occupies a
number of consecutive squares on the grid, arranged either horizontally or vertically. The number of squares
for each ship is determined by the type of the ship. The ships cannot overlap (i.e., only one ship can occupy
any given square in the grid). The types and numbers of ships allowed are the same for each player. These
may vary depending on the rules.
The default ships we will use come from the 2002 Hasbro edition of the game
Ship Number Name Size
1 Aircraft Carrier 5
2 Battleship 4
3 Destroyer 3
4 Submarine 3
5 Patrol Boat 2
After the ships have been positioned, the game proceeds in a series of rounds. In each round, each player
takes a turn to announce a target square (e.g. “c-7”) in the opponent’s grid which is to be shot at. The
opponent announces whether or not the square is occupied by a ship, and if it is a “miss”, the opponent
player marks their primary grid with a white peg; if a “hit” they mark this on their own primary grid with a
red peg. The attacking player notes the hit or miss on their own “tracking” grid with the appropriate color
peg (red for “hit”, white for “miss”), in order to build up a picture of the opponent’s fleet.
When all of the squares of a ship have been hit, the ship’s owner announce the sinking of either the Carrier,
the Cruiser, the Submarine, the Destroyer, or the titular Battleship. If all of a player’s ships have been sunk,
the game is over and their opponent wins.
Our game will be a variation where the sinking of a ship is not announced.
2
(1) Create several classes of objects.
battleship: Essentially, this is the game object. Everything about the current status of the game is included here.
fleets This is a length-two list of the players’ fleets.
history This is a tibble object with three columns:
from A character vector of the admiral firing the shot.
to A character vector of the admiral who is being fired upon.
target A character vector of the target of the shot. e.g. "f-9", "b-6", etc.
fleet: An object which represents one admiral’s fleet.
admiral A character string representing the player in charge of the fleet.
ocean A length-two numeric vector specifying the size and shape of the ocean the fleet occupies.
ships A list of ship objects which are members of the fleet.
ship: An object representing one ship.
name A character string. The name of the ship.
size An integer. The number of spaces the ship occupies.
position A length-two character vector of the bow (front) and stern (back) position of the ship.
hits A length(ship$size) logical vector indicating which portions of the ship have been hit.
sunk Logical, indicating if the ship is sunk.
(2) Create functions:
Class Constructors:
ship() Creates one ship object
name A character string, the name of the ship, e.g. "Battleship," "Submarine," etc.
size An integer, the number of spaces the ship occupies.
fleet() Creates one fleet object.
admiral A character string to represent the Admiral of the fleet.
ocean Optional. Default: ocean = c(10, 10) A length-two numeric vector representing the dimensions
of the ocean the fleet will occupy. For the sake of this project you may assume ocean dimensions
will be at least 5 × 5 and at most 25 × 25
ships Optional. Default: ships = NULL A variable-length list object containing one or more ship-class
objects.
If ships = NULL, the result of default_ships() should be used.
battleship() Creates a game object.
fleets Optional. Default: fleets = list(). A list of fleet objects in the game.
If length(fleets) < 2 make either 1 or 2 default fleets (standard ships on a standard board). If a
default fleet is generated the admiral should be "Player 1" or "Player 2" as appropriate.
3
Gameplay
play_bs() The workhorse function for playing a single game of Battleship.
players Who is playing the game, this should be the names of functions which return a target string. The
default should be for a one-player game players = c("human", "ai_123456789")
The return value for this function is a list object. The minimal contents of this list should be winner =
the name of the admiral who won the game. You may need to include more data in your return object
to answer all of the questions in this project.
human The function which polls a human player for a target. It should accept a character string indicating a
row (an appropriate letter) and a column (an appropriate number). Accepted responses should be in
the form of "f-3", "J-10", etc. (Hint: The substr() or the strsplit() functions may be helpful for
processing targets.)
ai_123456789() This is your bot. It should take either the following two arguments:
battleship The current game object. Note: Though your bot has access to the entire game object which
notably includes your opponent’s ship placements, you are to limit your bot’s access to the following
items:
history The object detailing the shots and hits in the game.
ocean The objects detailing the size and shape of your opponents ocean, so you know the bounds of
the regions you need to look for ships in.
size The vector of ship sizes so you know how many ships of each size you are looking for.
Any bots which are found to access the opponent’s ship placements will be excluded from the
tournament section and receive a 0 for that portion as well as a 0 for all portions of this project
involving the AI agent you were to write. DON’T BE A CHEATER!
strength Optional. Default: strength = 9. This should be the level of play of your bot. The default
should be strength = 9, this is the best bot you have made at the time of your submission. You
should also have a strength = 0 bot which plays completely randomly, with no strategy. It is
not necessary to have more than these two levels, but it is required to have at least these two.
You may want to explore and experiment with different strength bots for a variety of reasons, but
you are not required to.
No bot shall ever fire upon the same spot twice nor shall any bot fire on a spot outside the confines
of the designated ocean.
The return value of this function should be a character string representing a target square in the
opponent’s ocean grid. E.g. "d-6", "i-7", etc.
or the function should take one argument:
fleet Optional. No default. If a fleet object is passed to your AI function it should return the same fleet
object with updated position values for the ship objects in the fleet. That is, it places the ships.
default_ships() Create a list object containing the five default ships (see rules), with no assigned positions.
position_fleet() A function to assign ships a position in the ocean.
fleet A fleet object.
positions Optional. Default: positions = NULL. A list of the same length as the ship list for the fleet.
Each list item shall comprise a length two character vector indicating the start and end position of
each ship in the ship list.
If no positions list is provided, the ships shall be randomly placed in the ocean. The return value for
this function should be a fleet object with updated ship positions.
4
(3) Create methods
For the following functions create methods for the specified classes. Unless specified, you are free to implement
these however you like, but you should think carefully about what makes sense to do, that is, for example,
what should printing a ship entail? Or a fleet? How would you summarize a game (a battleship object)?
print()
ship Print a meaningful representation of a ship object, something other than simply dumping the
contents with the default printing method.
fleet Print a meaningful representation of a fleet object, something other than simply dumping the
contents with the default printing method.
battleship Print a meaningful representation of a battleship object, something other than simply dumping
the contents with the default printing method.
plot()
fleet This should produce a graphical representation of one player’s game board, with their ships placed
appropriately, including indications where the ship has been hit.
battleship This should produce a graphical representation of the current state of the game. It should show
all the oceans for all players, along with representations of all misses and hits on each ocean, but
should only show the active player’s ships on the active player’s ocean. The plot function should
take an optional argument player, showing only the ships of the specified player. If player = 0
(the default) the ships for all players should be shown.
summary()
fleet Summarize a fleet object in some meaningful way.
battleship Summarize a battleship object in some meaningful way.
(4) Simulate 100,000 games for each of the following conditions:
naive(strength = 0) vs naive(strength = 0)
naive(strength = 0) vs smart(strength = 9)
smart(strength = 9) vs naive(strength = 0)
smart(strength = 9) vs smart(strength = 9)
Questions
A) Standard Game
Answer the following questions for each of your simulations above:
a) What is the minimum number of turns the winning player needed to win the game?
b) What is the maximum number of turns the winning player needed to win the game?
c) What is the distribution of the number of unsunk ships the winning player had remaining?
d) What is the distribution of the number of hits the losing player made?
e) In what proportion of games has the winner lost their Patrol Boat?
f) In what proportion of games is the losing player’s last ship the Patrol Boat?
5
Also answer the following questions:
g) Make a two-way relative frequency table of the proportion of times Player 1 wins (naive/smart vs
naive/smart).
h) Test the hypothesis that order of play is not a statistically significant factor in determining who wins.
Use a 5% significance level, and interpret the p-value.
i) Test the hypothesis that the type of AI player is not a statistically significant factor in determining
who wins. Use a 5% significance level, and interpret the p-value.
B) Handicapped Games
If you wrote your functions as specified in a general enough way, it should be possible to conduct a game
between two different fleets on two different oceans. Experiment with changing the setup of the game to give
one player an advantage over another. Perhaps you simply give one player a 9 × 9 board to hide their ships
in and you give the other player an 11 × 11 board to hide in. Or you trade one player’s Patrol Boat for the
other player’s submarine.
Try at least three different handicaps in favor of the naive AI against your smart AI with the goal of
making it a fair game between the two.
Run 100,000 simulations and provide some summary statistics for each set of simulations.
C) Extensions
a) Describe in as much detail as possible, what you would need to change to implement the Salvo! variant
of the game. (See rules.)
b) Identify at least two (2) ways you could modify or extend the game, other than the Salvo! variant,
and describe in as much detail as possible what you would need to change to implement each of the
extensions you identified.
D) Tournament
Your Battleship bots will be pitted against each other in a series of random, but fair, games (random ocean
size, random fleets, same for both sides). 10% of the points for the Midterm Project will be allocated to your
ranking in this tournament. For anonymity, please add an attribute (in your .R file) to your AI function
called alt which is a character string containing an alternate name to report your function’s results under.
因为专业,所以值得信赖。如有需要,请加QQ:99515681 或邮箱:99515681@qq.com
微信:codehelp