Created
February 24, 2019 15:09
-
-
Save RitamChakraborty/32a91edc69dd3122391d8996131acb01 to your computer and use it in GitHub Desktop.
Command line battleship destroyer game written in python.
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
from random import randint | |
from time import sleep | |
def replay(ans): | |
if ans == "y": | |
battleship(aBoard) | |
else: | |
exit() | |
def print_board(board): | |
for row in board: | |
print(" ".join(row)) | |
def battleship(board): | |
for i in range(5): | |
for j in range(5): | |
board[i][j] = "0" | |
print_board(board) | |
ship_row = randint(0, len(board) - 1) | |
ship_col = randint(0, len(board) - 1) | |
turn = 1 | |
while turn <= 5: | |
print("Turn {}".format(turn)) | |
guess_row = int(input("Guess Row: ")) - 1 | |
guess_col = int(input("Guess Col: ")) - 1 | |
sleep(.5) | |
print("Firing missile...") | |
sleep(1) | |
if guess_row == ship_row and guess_col == ship_col: | |
print("Congratulations! You sank my battleship!") | |
sleep(1) | |
ans = input("Do you want to play again? Enter y for yes or n for no: ") | |
replay(ans) | |
else: | |
if guess_row not in range(5) or \ | |
guess_col not in range(5): | |
print("Oops, that's not even in the ocean.") | |
elif board[guess_row][guess_col] == "X": | |
print("You guessed that one already.") | |
else: | |
print("You missed my battleship!") | |
board[guess_row][guess_col] = "X" | |
print_board(board) | |
turn += 1 | |
if turn == 6: | |
sleep(1) | |
print("Game Over") | |
sleep(1) | |
print("The battleship was at ({},{})".format(ship_row + 1, ship_col + 1)) | |
board[ship_row][ship_col] = "#" | |
print_board(board) | |
sleep(1) | |
ans = input("Do you want to play again? Enter y for yes or n for no: ") | |
replay(ans) | |
aBoard = [] | |
for x in range(0, 5): | |
aBoard.append(["O"] * 5) | |
battleship(aBoard) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment