首页 > > 详细

讲解留学生Python语言、辅导留学生Computational and Algorithmic Thinking

CSE 101: Introduction to Computational and Algorithmic Thinking
Assignment Objectives
For this assignment you will be implementing a custom version of the board game Battleship. Throughout the
entire assignment you will be working with objects that represent the player’s game board and a secondary board
that keeps track of where the player has attacked the computer (AI) player.
Extra Credit
The assignment will count as an optional extra credit assignment, with partial credit available. The extra credit
will award a maximum of 4% to a student’s overall course grade after any curve has been applied to course grades.
Submissions will be checked thoroughly for plagiarized content. Any submissions that are similar to any other
submissions will simply be discarded without being graded and a score of zero will be recorded in Blackboard.
Collaboration on this project is strictly forbidden.
Game Overview
CSE 101 Battleship is played between a human player and a computer player. Each player has a M N board
of cells, where M is the number of rows and N is the number of columns, with three battleships per player. Each
battleship takes up exactly one cell on the board. The human player can see his/her ships, but cannot see the
computer player’s ships.
Various Unicode characters are used to represent the contents of a given cell in a board. Each of these characters
has been given a name in all capital letters at the top of the provided battleship.py file. Specifically, we will
use FOG (a black square) to represent a computer player’s cell that the human player has not yet explored, and
EMPTY (a white square) to represent a player’s cell that is yet unattacked by the computer player and does not
contain one of the human player’s ships.
The player chooses location of each individual ship at the beginning of the game. Players in most cases can only
attack one cell at a time. When the human player attacks a cell on the computer player’s board, the cell changes to
EXPLORED (a box containing an X). If the human player attacks a cell that has a ship, the cell on the opponent’s
board changes to BOMB (two concentric circles). A player is only allowed to attack EMPTY, EXPLORED, FOG or
SHIP cells. In addition to the above functionality, the human player can choose to use a special air-strike ability,
which will attack an entire column of the computer player’s board. The human player also has the ability to move
a ship to a new location on map that does not contain another ship (SHIP) or a destroyed ship (BOMB). Each
special ability can be used only once during the game.
Getting Started
CSE 101 – Fall 2017 Course Project Page 1
Visit Blackboard and download the “bare bones” file course project.py. Open the file in PyCharm and fill
in the following information at the top:
1. your first and last name as they appear in Blackboard
2. your Net ID (e.g., jsmith)
3. your Stony Brook ID # (e.g., 111999999)
4. the course number (CSE 101)
5. the assignment name (Course Project)
Do not, under any circumstances, change the names of the functions or their argument lists. The automated
CodeLoad testing system will be looking for exactly those functions provided in course project.py. You
will be able to test your work by uploading your file to CodeLoad.
Upload your final .py file to Blackboard by the due date and time. Late work will not be graded.
Code that crashes and cannot be graded will earn no credit. It is your responsibility to test your code by running
it through CodeLoad and by creating your own test cases.
Preliminaries
For this homework assignment you will be working with the following classes that represent the game itself and
a game board:
class Game:
def __init__(self, ai_board, player_board):
self.ai_board = ai_board
self.player_board = player_board
def __repr__(self):
# see battleship.py for contents
class Board:
def __init__(self, num_rows, num_cols):
self.num_rows = num_rows
self.num_cols = num_cols
self.cells = ... # see course_project.py for contents
self.air_strike = True
self.move_ability = True
def __repr__(self):
# see battleship.py for contents
We will use the following values to represent the state of a cell in the game boards:
EMPTY = u"\u25A1" # A white square
FOG = u"\u25A0" # A black square
EXPLORED = u"\u2612" # A square with an X inside
CSE 101 – Fall 2017 Course Project Page 2
BOMB = u"\u25CE" # Two concentric circles
SHIP = u"\u26F5" # An icon of a sailboat
Note: The top-left cell of a game board is at (1,1), and the bottom-right cell of a game board is at (num rows,
num cols). Your code must properly translate these cell numbers to indexes into Board.cells (e.g., the cell
at position (r,c)) is stored at Board.cells[r-1][c-1]).
Notes on Testing Your Work
The course project.py file includes several test cases for each of the functions below that you are to write.
Expected output for these test cases are given as an appendix to this PDF document.
To actually play a game of Battleship, run the file called course project driver.py. You will not be able
to play a full game until you have written all six functions described below.
Part I: Validate a Cell Position (10 points)
Write the function is valid position(), which takes the following arguments, in this order:
1. board: A Board object.
2. row: An integer value that represents a row on the board. Valid row numbers start with 1.
3. col: An integer value that represents a column on the board. Valid column numbers start with 1.
The function returns True if row is in the range 1 through board.num rows and col is in the range 1 through
board.num cols. Otherwise, the function returns False.
Part II: Place Ship (20 points)
Write the function place ship(), which takes the following arguments, in this order:
1. board: A Board object.
2. row: An integer value that represents a row on the board. Valid row numbers start with 1.
3. col: An integer value that represents a column on the board. Valid column numbers start with 1.
The function replaces the EMPTY cell at position row,col of the board with a SHIP and returns True.
However, there are few exceptions:
If the cell at row,col already has a ship, do not update the board and return False.
If row is less than 1 or greater than board.num rows, do not update the board and return False.
If col is less than 1 or greater than board.num cols, do not update the board and return False.
Part III: Fire in the Hole! (20 points)
Write the function attack(), which takes the following arguments, in this order:
CSE 101 – Fall 2017 Course Project Page 3
1. defender board: Board object that is being attacked.
2. row: An integer value that represents a row on the defender’s board. Row numbers start with 1.
3. col: An integer value that represents a column on the defender’s board. Column numbers start with 1.
The function updates the defender’s board according to the type of cell under attack and returns True or False
appropriately based on the following rules:
If the cell at the given row,col has a SHIP, then update it with BOMB and return True.
If the cell at the given row,col is EMPTY, FOG or EXPLORED, then update it with EXPLORED and return
True.
If the cell at the given row,col contains something other than EXPLORED, SHIP, FOG, or EMPTY, return
False.
If row is less than 1 or greater than defender board.num rows, do not update the board and return
False.
If col is less than 1 or greater than defender board.num cols, do not update the board and return
False.
Part IV: Game Status (10 points)
Write the function game status(), which takes the following arguments, in this order:
1. player board: A Board object that represents the human player’s game board.
2. ai board: A Board object that represents the computer player’s game board.
The function checks if there are SHIPs remaining in either player board or ai board and return values
based on the following rules:
If there are no SHIPs remaining in player board, then return PLAYER LOST, which is a constant
defined at the top of the Python file.
If there are no SHIPs remaining in ai board, then return COMPUTER LOST, which is a constant defined
at the top of the Python file.
If both ai board and player board have one or more SHIPs remaining, then return 0.
Part V: Move Ship (20 points)
Write the function move ship(), which takes the following arguments, in this order:
1. board: A Board object.
2. current location: A tuple consisting of (row,col) coordinates on the board. Valid row and col-
umn numbers start at 1.
3. new location: A tuple consisting of (row,col) coordinates on the board. Valid row and column
numbers start at 1.
CSE 101 – Fall 2017 Course Project Page 4
The function checks if row and col of current location on the board contains a SHIP and if row and
col of new location on the board is an EMPTY or EXPLORED cell. If both these conditions are met, the
function replaces the cell at current location with EMPTY and replaces the cell at new location with
SHIP. The function sets board’s move ability field to False and returns True. If one or more of the
above conditions is not satisfied, the function returns False. For the following error conditions the function
returns False and makes no changes to board:
row of current location is less than 1 or greater than board.num rows
col of current location is less than 1 or greater than board.num cols
row of new location is less than 1 or greater than board.num rows
col of new location is less than 1 or greater than board.num cols
Part VI: Air Strike (20 points)
Write the function air strike(), which takes the following arguments, in this order:
1. attacker board: The Board object of the player who is attacking.
2. defender board: The Board object of the player who is being attacked.
3. col: An integer indicating the column number to be attacked. Valid column numbers start with 1.
The function replaces all EMPTY cells in the given col of the defender board.cells with EXPLORED,
replaces any SHIPs in the column with BOMBs, and leaves any other cells untouched. The function sets the
attacker board’s air strike field to False and then returns True. If col is less than 1 or more than
defender board.num cols, the function returns False and makes no changes to either Board object.
How to Submit Your Work for Grading
To submit your .py file for grading:
1. Login to Blackboard and locate the course account for CSE 101.
2. Click on “Assignments” in the left-hand menu and find the link for this assignment.
3. Click on the link for this assignment.
4. Click the “Browse My Computer” button and locate the .py file you wish to submit. Submit only that one
.py.
5. Click the “Submit” button to submit your work for grading.
Oops, I messed up and I need to resubmit a file!
No worries! Just follow the above directions again. We will grade only your last submission.
CSE 101 – Fall 2017 Course Project Page 5
expected_course_project_output.txt
PART 1:
Testing is_valid_location() with board = 4X6, row = 2, col = 3 True
Testing is_valid_location() with board = 6X7, row = -3, col = 3 False
Testing is_valid_location() with board = 5X5, row = 6, col = 6 False
PART 2:
Testing place_ship() with board = 4X6, row = 3, col = 2 True
□ □ □ □ □ □
□ □ □ □ □ □
g4060 g4060□ □ □ □
g4060□ □ □ □ □
Testing place_ship() with board = 6X7, row = 7, col = 6 False
□ □ □ □ □ □ □
□ □ □ □ □ □ □
□ □ □ □ □ □ □
g4060□ □ □ □ □ □
g4060□ □ □ □ □ □
□ □ □ □ □ □ □
Testing place_ship() with board = 5X5, row = 3, col = 3 False
□ □ □ □ □
□ □ □ □ □
g4060□ □ □ □
g4060□ □ □ □
g4060□ □ □ □
PART 3:
Testing attack() with board = 4X6, row = 3, col = 2 True
Board:
g4060 □ □ □ □ □
□ □ □ □ □ □
g1409 g4060□ □ □ □
g4060□ □ □ □ □
Testing attack() with board = 6X7, row = 7, col = 6 False
Board:
□ □ □ □ □ □ □
□ □ □ □ □ □ □
g4060□ □ □ □ □ □
g4060□ □ □ □ □ □
g4060□ □ □ □ □ □
□ □ □ □ □ □ □
 

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!