-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBattleship.py
More file actions
68 lines (51 loc) · 1.65 KB
/
Battleship.py
File metadata and controls
68 lines (51 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python
from random import randint
# initialise variables
board = []
maxturns = 5
# fill board with 0 representing water
for x in range(5):
board.append(["0"] * 5)
###### FUNC to print the board to the screen
def print_board(board):
for row in board:
print " ".join(row)
# introduction
print "Let's play Battleship!"
print_board(board)
# ----- set battleship position -----
def random_col(board):
return randint(0, len(board) - 1)
def random_row(board):
return randint(0, len(board) - 1)
# set location of battleship
ship_row = random_row(board)
ship_col = random_col(board)
# ---------- GAMEPLAY --------- #
for turn in range(1, maxturns + 1):
# turn title
print "=== Turn %s ===" % turn
# get player input
guess_col = int(raw_input("Guess Col: ")) - 1
guess_row = int(raw_input("Guess Row: ")) - 1
# check the guess
if guess_col == ship_col and guess_row == ship_row:
board[guess_row][guess_col] = "B"
print_board(board)
print "You found the ship!"
break
elif guess_col < 0 or guess_row < 0 or guess_col + 1 > 5 or guess_row + 1 > 5:
print "That's off the board..."
elif board[guess_row][guess_col] == "X":
print "You already guessed that one!"
else:
print "You missed!"
board[guess_row][guess_col] = "X"
# conclude the turn
if maxturns - turn == 0:
print "You lost!"
# make ship visible for fairness
board[ship_row][ship_col] = "B"
else:
print "You have %s turns left." % (maxturns - turn)
print_board(board)