Python Language:
In the CIS 41A version of Battleship, the user tries to find thecomputer's battleship that has been randomly put on a grid of rowsand columns (the game board). Here is how the game is played:
- The computer asks the user to choose the size of the board,which is a square of 2, 3, or 4 rows and columns.
- The computer randomly chooses a spot on the board (r, c) wherer = row, c = column, as the location for the battleship
- The computer prints the board with the correct size, and asksthe user for a row and a column.
- If the user row and column matches (r,c), then the computerprints the grid again with an X at the found location. This meansthe user has found the computer's battleship.
Otherwise the computer prints the board again with no 'X' - Repeat steps 3 and 4 until the user has found thebattleship
- Then the computer prints the number tries that the usertook.
Implementation
The program has 2 constants:Â Â MIN which is set to 2,and MAX which is set to 4
and the program has 4 functions:
- getNum: get a number from the user and return it
- prompt the user for the number with an appropriate promptstring
- check that the input number is between a given low and highvalue, inclusive, or print an error message to show the validrange, and then re-prompt until you get a valid number
- printBoard: print the grid based on the size from the user
- start with a row of column numbers going across
- then a row of dashes, which is the top of the board
- each row of the board starts with a row number, then multiplepatterns of:Â Â '|', 3 spaces, and another '|'
which makes up each 'square' in the grid - after each row of squares is a row of dashes
- if the battleship has been sunken by the user, then the string'Â Â XÂ Â 'Â Â (space, X, space) is printedinstead of the 3 spaces
- hideShip: create 2 random numbers between 1 and MAX, to serveas the coordinates for the battleship
- the first 2 random numbers make up the (r, c) location of thebattleship
- you can choose to print the 2 random numbers to screen to maketesting easier
- play: play one game until the user has sunken thebattleship
- call the getNum function to get the size of the board from theuser. Pass input arguments to the function so that it can promptthe user appropriately and print the correct range if the input isnot valid
- call the hideShip function to determine the hidden location ofthe battleship
- call the printBoard function to print the board with no 'X'(since the user hasn't guessed yet)
- call the getNum function twice, once to get the row and asecond time to get the column from the user. Pass input argumentsto the function so that it can prompt the user appropriately andprint the correct range if the input is not valid
- - if the user hasn't guessed the location: print the sameboard, and loop back to step d
- if the user successfully guessed the locations: print theboard with an 'X' at the correct location, and print the number oftrie
- main: let the user keep playing the game until the user choosesto end
- seed the random generator
- call the play function to play the game one time
- ask the user whether to continue, and loop back to step b ifthe answer is 'y' or 'Y'.
Otherwise the program ends.
Important notes (don't miss them in your code):
- There should be 5 functions and each function should work asdescribed.
- The input arguments for the functions are not fully describedin this lab document, it's your job to determine what data need tobe passed to the functions and what (if any) return value thereis
- Don't use global variables (1pt deduction for each globalvariable). Instead, pass input arguments to the functions, andcatch any return value.
- Note that there should be only one printBoardfunction, and it should be able to print the board without the 'X'or with the 'X'. Don't write 2 different printBoardfunctions.
Likewise, there is only one getNum function, and it shouldbe able to prompt the user and print the appropriate error message,depending on whether it's the size of the board, the row number, orthe column number.
This is how a function can make your program shorter: you write 1function and call it 3 times, instead of writing 3 blocks of codeto get the 3 values. - The board game (the rows and columns and row / column numbers)should line up correctly, no matter the size of the board. This isyour practice with using loops.
- Make sure to document your code:
-- A comment at the top of the file with your name and labnumber
-- A docstring (not a comment) for each function, except for themain function