Given the scheme of the fleet of the human player, let the user choose which coordinates attack each turn.
The "computer player" will then attack on its turn following the classic rules of battleship. (The computer player must be able to win, you can't make the computer attack every turn the same cell)
The schemes must contain all the needed ships, this is the list:
[ ][ ] x 4
[ ][ ][ ] x 3
[ ][ ][ ][ ] x 2
[ ][ ][ ][ ][ ][ ] x 1
The scheme must follow the example below, and must be one for each player.
[ ][ ] [ ] [ ][ ][ ][ ]
[ ]
[ ] [ ] [ ][ ][ ] [ ]
[ ] [ ]
[ ]
[ ] [ ] [ ]
[ ] [ ]
[ ][ ] [ ]
[ ][ ][ ][ ][ ][ ]
To input the player scheme you can just hard-code it in your script or let the user insert it in some way (use a specific char to separate rows or let user inser one row per time etc, up to you), the important thing is that your player must be able to easily insert a new scheme (no minified/compressed input so).
The fleet owned by the computer must be randomly generated by the script.
The coordinates are a number(1-10) and a letter(A-L), this is an example battlefield with coordinates:
A B C D E F G H I L
|‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
1 | [ ][ ] [ ] [ ][ ][ ][ ]
2 | [ ]
3 | [ ] [ ] [ ][ ][ ] [ ]
4 | [ ] [ ]
5 | [ ]
6 | [ ] [ ] [ ]
7 | [ ] [ ]
8 | [ ][ ] [ ]
9 |
10 | [ ][ ][ ][ ][ ][ ]
Each turn the script must ask which coordinates hit, and return an indication:
- not hit = the selected coordinates doesn't match a part of a ship
- hit = the selected coordinates does match part of a ship but the ship is not yet sunk
- hit and sunk = the selected coordinates does match part of a ship and the ship is sunk
Then it will have to make the computer hit a cell (bonus points if you can write a smart script that will be challenging) and indicate to user which cell was hit and if one of his ships was hit (same indications of above).
Example "gameplay" log:
Your coordinates: A1
hit
Enemy coordinates: C1
not hit
Your coordinates: B1
hit and sunk
if the user chooses two times the same coordinates consider it as not hit or just ignore it
The expected final outputs (when one of the two players has not ships available) are the two schemes with the coordinates hit by the computer player marked with an x
, example of the first scheme:
[x][x] [x] [x][x][x][ ]
x [x]
[ ] x [x] [ ][ ][x] [x]
[ ] [x]
[ ] x
[x] [x] [ ]
[x] x [ ]
[ ][ ] x [ ]
x [ ][ ][x][ ][ ][ ]
It's probably not the easiest code-golf challenge here but as usual, the shortest code will win.