Battleship!
This lesson can be found at CodeCademy.com's resource page for Python.
If the links below don't work, the full URL for the lesson is: http://www.codecademy.com/courses/python-beginner-en-4XuFm/0/1?curriculum_id=4f89dab3d788890003000096 |
I would reproduce the entire CodeCademy lesson here, but I'm pretty sure that would be considered a violation of copyright.
Battleship from CodeAcademy
Although I love CodeCademy.com, I don't particularly like it that you don't get to keep your code. Eventually, I would like you to build an AI (a gaming bot) for your Battleship program and have you pit your program against another student's Battleship program.
In order for this to happen, you need to have your code written down somewhere. Portable Python seems like the obvious choice for us.
Go through the lesson at CodeCademy but write down your code in a Portable Python module named "Battleship". You do not have to create a CodeCademy account to use the lesson. To advance to the next lesson at CodeCademy without logging in or writing code in their webpage's IDE (Interactive Development Environment), use the drop down menu on the upper-left portion of the page. It should say 1/19.
IMPORTANT: When you copy and paste your code from CodeCademy into Python you will get several errors.
- CodeCademy does not use parentheses with their print functions. We do. Put parentheses into each of your print() statements.
- CodeCademy uses raw_input() but in Python 3 it changed over to just input(). Delete the raw_ portion of the code.
Run your code in Portable Python to make sure it works!
Making the Game Awesomer.
1. Save a copy of your original Battleship game as Battleship_CodeCademy just in case we mess it up badly and want to start over. From here on out, we will modify our game of Battleship but not touch the Battleship_CodeCademy file.
2. Make your battleship have a length of 3. All the pieces of the ship must touch each other and be in either a vertical or horizontal line (no diagonals!).
4. Make your game into a two-player game. Each player should have a randomly assigned battleship on their own board.
1. Save a copy of your original Battleship game as Battleship_CodeCademy just in case we mess it up badly and want to start over. From here on out, we will modify our game of Battleship but not touch the Battleship_CodeCademy file.
2. Make your battleship have a length of 3. All the pieces of the ship must touch each other and be in either a vertical or horizontal line (no diagonals!).
- Be careful! You don't want your boat going off the side of the board.
- This involves getting the random numbers from the rand_int() function and then automatically placing the other coordinates of the boat next to the random ones.
- You may want to change the either the definition of random_row or random_col in order to incorporate the extra length of the boat.
4. Make your game into a two-player game. Each player should have a randomly assigned battleship on their own board.
Extension (Making the Game Even More Awesomer):
1. Create a version with multiple ships of different sizes. (I suggest starting with ships of length 2 and 3).
2. Create an option where the player gets to determine the size of the board.
3. Have the player determine the number of ships and the size of the board.
4. Build a 1-player version of this game where the user plays against the computer. This means you will need to create a bot (AI) to play against. This bot should be able to make guesses about where to attack; whether your bot attacks with a strategy or not is up to you.
1. Create a version with multiple ships of different sizes. (I suggest starting with ships of length 2 and 3).
2. Create an option where the player gets to determine the size of the board.
3. Have the player determine the number of ships and the size of the board.
4. Build a 1-player version of this game where the user plays against the computer. This means you will need to create a bot (AI) to play against. This bot should be able to make guesses about where to attack; whether your bot attacks with a strategy or not is up to you.