-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added python code for simple battleship game
- Loading branch information
1 parent
6740281
commit 08ecd33
Showing
1 changed file
with
276 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,276 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "view-in-github", | ||
"colab_type": "text" | ||
}, | ||
"source": [ | ||
"<a href=\"https://colab.research.google.com/github/Cherry3939/Battleship/blob/main/battleship.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"# Battleship" | ||
], | ||
"metadata": { | ||
"id": "FsNdSTxVbBUj" | ||
} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"id": "YQmKg1EGloBX" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"import random\n", | ||
"\n", | ||
"def game_setup(): # Set up the game board\n", | ||
" print(\"You have 10 shots\")\n", | ||
" game_board = [[\"0\", \"1\", \"2\", \"3\", \"4\", \"5\"],\n", | ||
" [\"1\", \"O\", \"O\", \"O\", \"O\", \"O\"],\n", | ||
" [\"2\", \"O\", \"O\", \"O\", \"O\", \"O\"],\n", | ||
" [\"3\", \"O\", \"O\", \"O\", \"O\", \"O\"],\n", | ||
" [\"4\", \"O\", \"O\", \"O\", \"O\", \"O\"],\n", | ||
" [\"5\", \"O\", \"O\", \"O\", \"O\", \"O\"]]\n", | ||
" for row in game_board: # print each row on a new line\n", | ||
" print(*row)\n", | ||
" return game_board\n", | ||
"\n", | ||
"\n", | ||
"def generate_ship(game_board):\n", | ||
" ships_placed = 0\n", | ||
" while ships_placed < 3: # stop when 3 ships have been placed\n", | ||
" random_col = random.randint(1, 5)\n", | ||
" random_row = random.randint(1, 5) # randomly generate a coord for a ship to be placed\n", | ||
" # ship will be shown with a Z\n", | ||
" game_board[random_col][random_row] = \"Z\"\n", | ||
" ships_placed += 1\n", | ||
"\n", | ||
" #for row in game_board: # <-- show answers\n", | ||
" #print(*row)\n", | ||
" return game_board\n", | ||
"\n", | ||
"\n", | ||
"def game_start(game_board):\n", | ||
" ammo = 10 # Player will get 10 tries to hit all the ships\n", | ||
" hit_ships = 0\n", | ||
" display_game_board = [[\"0\", \"1\", \"2\", \"3\", \"4\", \"5\"], # original game board without ship locations marked so it won't reveal the locations of the ships whenever the board is updated\n", | ||
" [\"1\", \"O\", \"O\", \"O\", \"O\", \"O\"],\n", | ||
" [\"2\", \"O\", \"O\", \"O\", \"O\", \"O\"],\n", | ||
" [\"3\", \"O\", \"O\", \"O\", \"O\", \"O\"],\n", | ||
" [\"4\", \"O\", \"O\", \"O\", \"O\", \"O\"],\n", | ||
" [\"5\", \"O\", \"O\", \"O\", \"O\", \"O\"]]\n", | ||
" while ammo > 0:\n", | ||
" row = input(\"Pick a column from 1-5: \" ) # printed the wrong names on purpose bc it doesn't look right\n", | ||
" col = input(\"Pick a row from 1-5: \")\n", | ||
"\n", | ||
" # Check validity of entered coord\n", | ||
" if row.isdigit() and col.isdigit():\n", | ||
" row = int(row)\n", | ||
" col = int(col)\n", | ||
" if row in range(1, 6) and col in range(1, 6):\n", | ||
" coord = ((row, col))\n", | ||
" print(coord)\n", | ||
" chosen_coord = game_board[col][row] # locate coord player has chosen to hit\n", | ||
" #print(chosen_coord)\n", | ||
"\n", | ||
" # Check if player hits target\n", | ||
" if chosen_coord == \"Z\":\n", | ||
" print(\"Hit!\")\n", | ||
" hit_ships += 1\n", | ||
" game_board[col][row] = \"H\" # Mark hit ships with H\n", | ||
" display_game_board[col][row] = \"H\" # display board needs to be updated too\n", | ||
" elif chosen_coord == \"O\" or chosen_coord == \"X\":\n", | ||
" print(\"Missed!\")\n", | ||
" game_board[col][row] = \"X\" # Mark missed spots with X\n", | ||
" display_game_board[col][row] = \"X\"\n", | ||
" elif chosen_coord == \"H\":\n", | ||
" print(\"You've already sunk this ship!\")\n", | ||
"\n", | ||
" # Update displayed board so player can keep track\n", | ||
" for line in display_game_board:\n", | ||
" print(*line)\n", | ||
" ammo -= 1\n", | ||
"\n", | ||
" else:\n", | ||
" print(\"Please enter a valid value\")\n", | ||
" else:\n", | ||
" print(\"Please enter a valid value\")\n", | ||
"\n", | ||
"\n", | ||
" if hit_ships == 3:\n", | ||
" print(\"Congrats! You sunk all the ships!\")\n", | ||
" print(f\"You took {10 - ammo} tries\")\n", | ||
" break\n", | ||
"\n", | ||
" if ammo == 0:\n", | ||
" print(\"You ran out of ammo :(\")\n", | ||
" print(f\"You sank {hit_ships} ships\")\n", | ||
"\n", | ||
"\n", | ||
"\n", | ||
"\n", | ||
"\n", | ||
"\n", | ||
"def main():\n", | ||
" print(\"Find and destroy all the ships hidden on the board! Each ship is one block long and there are 3 ships in total\")\n", | ||
" game_board = game_setup()\n", | ||
" game_board = generate_ship(game_board)\n", | ||
" game_start(game_board)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"colab": { | ||
"base_uri": "https://localhost:8080/" | ||
}, | ||
"id": "U_tIVKtamiYH", | ||
"outputId": "71cdee42-d2c3-450d-d780-aa611f04eb03" | ||
}, | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"name": "stdout", | ||
"text": [ | ||
"Find and destroy all the ships hidden on the board! Each ship is one block long and there are 3 ships in total\n", | ||
"You have 10 shots\n", | ||
"0 1 2 3 4 5\n", | ||
"1 O O O O O\n", | ||
"2 O O O O O\n", | ||
"3 O O O O O\n", | ||
"4 O O O O O\n", | ||
"5 O O O O O\n", | ||
"Pick a column from 1-5: 4\n", | ||
"Pick a row from 1-5: 3\n", | ||
"(4, 3)\n", | ||
"Missed!\n", | ||
"0 1 2 3 4 5\n", | ||
"1 O O O O O\n", | ||
"2 O O O O O\n", | ||
"3 O O O X O\n", | ||
"4 O O O O O\n", | ||
"5 O O O O O\n", | ||
"Pick a column from 1-5: 2\n", | ||
"Pick a row from 1-5: 3\n", | ||
"(2, 3)\n", | ||
"Missed!\n", | ||
"0 1 2 3 4 5\n", | ||
"1 O O O O O\n", | ||
"2 O O O O O\n", | ||
"3 O X O X O\n", | ||
"4 O O O O O\n", | ||
"5 O O O O O\n", | ||
"Pick a column from 1-5: 1\n", | ||
"Pick a row from 1-5: 5\n", | ||
"(1, 5)\n", | ||
"Missed!\n", | ||
"0 1 2 3 4 5\n", | ||
"1 O O O O O\n", | ||
"2 O O O O O\n", | ||
"3 O X O X O\n", | ||
"4 O O O O O\n", | ||
"5 X O O O O\n", | ||
"Pick a column from 1-5: 5\n", | ||
"Pick a row from 1-5: 5\n", | ||
"(5, 5)\n", | ||
"Hit!\n", | ||
"0 1 2 3 4 5\n", | ||
"1 O O O O O\n", | ||
"2 O O O O O\n", | ||
"3 O X O X O\n", | ||
"4 O O O O O\n", | ||
"5 X O O O H\n", | ||
"Pick a column from 1-5: 4\n", | ||
"Pick a row from 1-5: 2\n", | ||
"(4, 2)\n", | ||
"Missed!\n", | ||
"0 1 2 3 4 5\n", | ||
"1 O O O O O\n", | ||
"2 O O O X O\n", | ||
"3 O X O X O\n", | ||
"4 O O O O O\n", | ||
"5 X O O O H\n", | ||
"Pick a column from 1-5: 5\n", | ||
"Pick a row from 1-5: 1\n", | ||
"(5, 1)\n", | ||
"Missed!\n", | ||
"0 1 2 3 4 5\n", | ||
"1 O O O O X\n", | ||
"2 O O O X O\n", | ||
"3 O X O X O\n", | ||
"4 O O O O O\n", | ||
"5 X O O O H\n", | ||
"Pick a column from 1-5: 1\n", | ||
"Pick a row from 1-5: 3\n", | ||
"(1, 3)\n", | ||
"Missed!\n", | ||
"0 1 2 3 4 5\n", | ||
"1 O O O O X\n", | ||
"2 O O O X O\n", | ||
"3 X X O X O\n", | ||
"4 O O O O O\n", | ||
"5 X O O O H\n", | ||
"Pick a column from 1-5: 2\n", | ||
"Pick a row from 1-5: 2\n", | ||
"(2, 2)\n", | ||
"Hit!\n", | ||
"0 1 2 3 4 5\n", | ||
"1 O O O O X\n", | ||
"2 O H O X O\n", | ||
"3 X X O X O\n", | ||
"4 O O O O O\n", | ||
"5 X O O O H\n", | ||
"Pick a column from 1-5: 3\n", | ||
"Pick a row from 1-5: 4\n", | ||
"(3, 4)\n", | ||
"Missed!\n", | ||
"0 1 2 3 4 5\n", | ||
"1 O O O O X\n", | ||
"2 O H O X O\n", | ||
"3 X X O X O\n", | ||
"4 O O X O O\n", | ||
"5 X O O O H\n", | ||
"Pick a column from 1-5: 3\n", | ||
"Pick a row from 1-5: 1\n", | ||
"(3, 1)\n", | ||
"Missed!\n", | ||
"0 1 2 3 4 5\n", | ||
"1 O O X O X\n", | ||
"2 O H O X O\n", | ||
"3 X X O X O\n", | ||
"4 O O X O O\n", | ||
"5 X O O O H\n", | ||
"You ran out of ammo :(\n", | ||
"You sank 2 ships\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"main()" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"colab": { | ||
"provenance": [], | ||
"authorship_tag": "ABX9TyP40ccoIMgpqyZpsYa3nLRI", | ||
"include_colab_link": true | ||
}, | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"name": "python" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 0 | ||
} |