# 01 Welcome to Battleship
# there is nothing in this lesson
pass
# 02 Getting Our Feet Wet
board = []
# 03 Make a List
# I used the most stupid way
board = [
['O', 'O', 'O', 'O', 'O', ],
['O', 'O', 'O', 'O', 'O', ],
['O', 'O', 'O', 'O', 'O', ],
['O', 'O', 'O', 'O', 'O', ],
['O', 'O', 'O', 'O', 'O', ]
]
# 04 Check it Twice
board = [
['O', 'O', 'O', 'O', 'O', ],
['O', 'O', 'O', 'O', 'O', ],
['O', 'O', 'O', 'O', 'O', ],
['O', 'O', 'O', 'O', 'O', ],
['O', 'O', 'O', 'O', 'O', ]
]
print (board)
[['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O']]
# 05 Custom Print
board = [
['O', 'O', 'O', 'O', 'O', ],
['O', 'O', 'O', 'O', 'O', ],
['O', 'O', 'O', 'O', 'O', ],
['O', 'O', 'O', 'O', 'O', ],
['O', 'O', 'O', 'O', 'O', ]
]
def print_board(board):
for lst in board:
print (lst)
print_board(board)
['O', 'O', 'O', 'O', 'O'] ['O', 'O', 'O', 'O', 'O'] ['O', 'O', 'O', 'O', 'O'] ['O', 'O', 'O', 'O', 'O'] ['O', 'O', 'O', 'O', 'O']
# 06 Printing Pretty
board = [
['O', 'O', 'O', 'O', 'O', ],
['O', 'O', 'O', 'O', 'O', ],
['O', 'O', 'O', 'O', 'O', ],
['O', 'O', 'O', 'O', 'O', ],
['O', 'O', 'O', 'O', 'O', ]
]
def print_board(board):
for lst in board:
print (" ".join(lst))
print_board(board)
O O O O O O O O O O O O O O O O O O O O O O O O O
# 07 Hide
from random import randint
board = []
for x in range(0, 5):
board.append(["O"] * 5)
def print_board(board):
for row in board:
print (" ".join(row))
# Add your code below!
def random_row(board):
return randint(0, len(board) - 1)
def random_col(board):
return randint(0, len(board[0]) - 1)
# 08 and Seek
from random import randint
board = []
for x in range(0,5):
board.append(["O"] * 5)
def print_board(board):
for row in board:
print (" ".join(row))
def random_row(board):
return randint(0, len(board) - 1)
def random_col(board):
return randint(0, len(board[0]) - 1)
ship_row = random_row(board)
ship_col = random_col(board)
# Add your code below!
guess_row = int(input("Guess Row:"))
guess_col = int(input("Guess Col:"))
Guess Row:2 Guess Col:1
# 09 It is Not Cheating It is Debugging
from random import randint
board = []
for x in range(0,5):
board.append(["O"] * 5)
def print_board(board):
for row in board:
print (" ".join(row))
def random_row(board):
return randint(0, len(board) - 1)
def random_col(board):
return randint(0, len(board[0]) - 1)
ship_row = random_row(board)
ship_col = random_col(board)
# Add your code below!
print (ship_col, ship_row)
guess_row = int(input("Guess Row:"))
guess_col = int(input("Guess Col:"))
1 1 Guess Row:2 Guess Col:3
# 10 You win
from random import randint
board = []
for x in range(0, 5):
board.append(["O"] * 5)
def print_board(board):
for row in board:
print (" ".join(row))
print_board(board)
def random_row(board):
return randint(0, len(board) - 1)
def random_col(board):
return randint(0, len(board[0]) - 1)
ship_row = random_row(board)
ship_col = random_col(board)
guess_row = int(input("Guess Row:"))
guess_col = int(input("Guess Col:"))
print (ship_row)
print (ship_col)
# Write your code below!
if guess_row == ship_row and guess_col == ship_col:
print ("Congratulations! You sank my battleship!")
O O O O O O O O O O O O O O O O O O O O O O O O O Guess Row:2 Guess Col:3 2 3 Congratulations! You sank my battleship!
# 11 Danger Will Robinson
from random import randint
board = []
for x in range(0, 5):
board.append(["O"] * 5)
def print_board(board):
for row in board:
print (" ".join(row))
print_board(board)
def random_row(board):
return randint(0, len(board) - 1)
def random_col(board):
return randint(0, len(board[0]) - 1)
ship_row = random_row(board)
ship_col = random_col(board)
guess_row = int(input("Guess Row:"))
guess_col = int(input("Guess Col:"))
print (ship_row)
print (ship_col)
# Write your code below!
if guess_row == ship_row and guess_col == ship_col:
print ("Congratulations! You sank my battleship!")
else:
print ("You missed my battleship!")
board[guess_row][guess_col] = "X"
print_board(board)
O O O O O O O O O O O O O O O O O O O O O O O O O Guess Row:2 Guess Col:3 3 0 You missed my battleship! O O O O O O O O O O O O O X O O O O O O O O O O O
# 12 Bad Aim
from random import randint
board = []
for x in range(0, 5):
board.append(["O"] * 5)
def print_board(board):
for row in board:
print (" ".join(row))
print_board(board)
def random_row(board):
return randint(0, len(board) - 1)
def random_col(board):
return randint(0, len(board[0]) - 1)
ship_row = random_row(board)
ship_col = random_col(board)
guess_row = int(input("Guess Row:"))
guess_col = int(input("Guess Col:"))
print (ship_row)
print (ship_col)
# Write your code below!
if guess_row == ship_row and guess_col == ship_col:
print ("Congratulations! You sank my battleship!")
else:
if (guess_row < 0 or guess_row > len(board)) or (guess_col < 0 or guess_col > len(board[0])):
print ("Oops, that's not even in the ocean.")
else:
print ("You missed my battleship!")
board[guess_row][guess_col] = "X"
print_board(board)
O O O O O O O O O O O O O O O O O O O O O O O O O Guess Row:3 Guess Col:2 0 4 You missed my battleship! O O O O O O O O O O O O O O O O O X O O O O O O O
# 13 Not Again
from random import randint
board = []
for x in range(0, 5):
board.append(["O"] * 5)
def print_board(board):
for row in board:
print (" ".join(row))
print_board(board)
def random_row(board):
return randint(0, len(board) - 1)
def random_col(board):
return randint(0, len(board[0]) - 1)
ship_row = random_row(board)
ship_col = random_col(board)
guess_row = int(input("Guess Row:"))
guess_col = int(input("Guess Col:"))
print (ship_row)
print (ship_col)
# Write your code below!
if guess_row == ship_row and guess_col == ship_col:
print ("Congratulations! You sank my battleship!")
else:
if (guess_row < 0 or guess_row > len(board)) or (guess_col < 0 or guess_col > len(board[0])):
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)
O O O O O O O O O O O O O O O O O O O O O O O O O Guess Row:2 Guess Col:3 3 4 You missed my battleship! O O O O O O O O O O O O O X O O O O O O O O O O O
# 14 Test Run
from random import randint
board = []
for x in range(0, 5):
board.append(["O"] * 5)
def print_board(board):
for row in board:
print (" ".join(row))
print_board(board)
def random_row(board):
return randint(0, len(board))
def random_col(board):
return randint(0, len(board[0]))
ship_row = random_row(board)
ship_col = random_col(board)
guess_row = int(input("Guess Row:"))
guess_col = int(input("Guess Col:"))
print (ship_row)
print (ship_col)
# Write your code below!
if guess_row == ship_row and guess_col == ship_col:
print ("Congratulations! You sank my battleship!")
else:
if (guess_row < 0 or guess_row > len(board)) or (guess_col < 0 or guess_col > len(board[0])):
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)
O O O O O O O O O O O O O O O O O O O O O O O O O Guess Row:2 Guess Col:3 5 3 You missed my battleship! O O O O O O O O O O O O O X O O O O O O O O O O O
# 15 Play It Sam
from random import randint
board = []
for x in range(5):
board.append(["O"] * 5)
def print_board(board):
for row in board:
print (" ".join(row))
print ("Let's play Battleship!")
print_board(board)
def random_row(board):
return randint(0, len(board) - 1)
def random_col(board):
return randint(0, len(board[0]) - 1)
ship_row = random_row(board)
ship_col = random_col(board)
print (ship_row)
print (ship_col)
# Everything from here on should go in your for loop!
# Be sure to indent four spaces!
for turn in range(4):
guess_row = int(input("Guess Row:"))
guess_col = int(input("Guess Col:"))
if guess_row == ship_row and guess_col == ship_col:
print ("Congratulations! You sunk my battleship!")
else:
if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
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 (turn + 1)
print_board(board)
Let's play Battleship! O O O O O O O O O O O O O O O O O O O O O O O O O 1 3 Guess Row:2 Guess Col:3 You missed my battleship! 1 O O O O O O O O O O O O O X O O O O O O O O O O O Guess Row:1 Guess Col:3 Congratulations! You sunk my battleship! 2 O O O O O O O O O O O O O X O O O O O O O O O O O Guess Row:3 Guess Col:4 You missed my battleship! 3 O O O O O O O O O O O O O X O O O O O X O O O O O Guess Row:3 Guess Col:2 You missed my battleship! 4 O O O O O O O O O O O O O X O O O X O X O O O O O
# 16 Game Over
from random import randint
board = []
for x in range(5):
board.append(["O"] * 5)
def print_board(board):
for row in board:
print (" ".join(row))
print ("Let's play Battleship!")
print_board(board)
def random_row(board):
return randint(0, len(board) - 1)
def random_col(board):
return randint(0, len(board[0]) - 1)
ship_row = random_row(board)
ship_col = random_col(board)
print (ship_row)
print (ship_col)
# Everything from here on should go in your for loop!
# Be sure to indent four spaces!
for turn in range(4):
guess_row = int(input("Guess Row:"))
guess_col = int(input("Guess Col:"))
if guess_row == ship_row and guess_col == ship_col:
print ("Congratulations! You sunk my battleship!")
else:
if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
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"
if turn == 3:
print ("Game Over")
print (turn + 1)
print_board(board)
Let's play Battleship! O O O O O O O O O O O O O O O O O O O O O O O O O 2 4 Guess Row:1 Guess Col:2 You missed my battleship! 1 O O O O O O O X O O O O O O O O O O O O O O O O O Guess Row:2 Guess Col:4 Congratulations! You sunk my battleship! 2 O O O O O O O X O O O O O O O O O O O O O O O O O Guess Row:2 Guess Col:4 Congratulations! You sunk my battleship! 3 O O O O O O O X O O O O O O O O O O O O O O O O O Guess Row:1 Guess Col:2 You guessed that one already. Game Over 4 O O O O O O O X O O O O O O O O O O O O O O O O O
# 17 A Real Win
from random import randint
board = []
for x in range(5):
board.append(["O"] * 5)
def print_board(board):
for row in board:
print ("" "".join(row))
print ("Let's play Battleship!")
print_board(board)
def random_row(board):
return randint(0, len(board) - 1)
def random_col(board):
return randint(0, len(board[0]) - 1)
ship_row = random_row(board)
ship_col = random_col(board)
print (ship_row)
print (ship_col)
# Everything from here on should go in your for loop!
# Be sure to indent four spaces!
for turn in range(4):
guess_row = int(input("Guess Row:"))
guess_col = int(input("Guess Col:"))
if guess_row == ship_row and guess_col == ship_col:
print ("Congratulations! You sunk my battleship!")
break
else:
if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
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"
if turn == 3:
print ("Game Over")
print (turn + 1)
print_board(board)
Let's play Battleship! OOOOO OOOOO OOOOO OOOOO OOOOO 1 1 Guess Row:1 Guess Col:3 You missed my battleship! 1 OOOOO OOOXO OOOOO OOOOO OOOOO Guess Row:1 Guess Col:1 Congratulations! You sunk my battleship!
# 18 To Your Battle Stations
from random import randint
board = []
for x in range(5):
board.append(["O"] * 5)
def print_board(board):
for row in board:
print (" ".join(row))
print ("Let's play Battleship!")
print_board(board)
def random_row(board):
return randint(0, len(board) - 1)
def random_col(board):
return randint(0, len(board[0]) - 1)
ship_row = random_row(board)
ship_col = random_col(board)
print (ship_row)
print (ship_col)
# Everything from here on should go in your for loop!
# Be sure to indent four spaces!
for turn in range(4):
guess_row = int(input("Guess Row:"))
guess_col = int(input("Guess Col:"))
if guess_row == ship_row and guess_col == ship_col:
print ("Congratulations! You sunk my battleship!")
break
else:
if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
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"
if turn == 3:
print ("Game Over")
print (turn + 1)
print_board(board)
Let's play Battleship! O O O O O O O O O O O O O O O O O O O O O O O O O 1 2 Guess Row:3 Guess Col:2 You missed my battleship! 1 O O O O O O O O O O O O O O O O O X O O O O O O O Guess Row:1 Guess Col:2 Congratulations! You sunk my battleship!
# 19 Extra Credit
# there is nothing in this lesson
pass