Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Battleship #1

Open
wants to merge 2 commits into
base: Battleship
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions Battleship
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,29 @@ ship_col = random_col(board)

# Allow player to guess where it is
# raw_input asks user for input and returns it as a string > for integers, wrap with int() to convert string

guess_row = int(raw_input("Guess Row: "))
guess_col = int(raw_input("Guess Col: "))
# Added a for loop
for turn in range(4):
print "Turn", turn+1
guess_row = int(raw_input("Guess Row: "))
guess_col = int(raw_input("Guess Col: "))

# Check if guess_row equals ship_row and guess_col equals ship_col
if guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sank my battleship!"
else:
if guess_row not in range(5) or guess_col not in range(5):
print "Oops, that's not even in the ocean."
if guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sank my battleship!"
# Add break to end program after correct guess
break
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 guess_row == guess_row and guess_col == guess_col:
print "You guessed that one already."
else:
# Set list element at guess_row, guess_col to "X"
print "You missed my battleship!"
board[int(guess_row)][int(guess_col)] = "X"
elif board[guess_row][guess_col] == "X":
print( "You guessed that one already." )
else:
print "You missed my battleship!"
board[int(guess_row)][int(guess_col)] = "X"

# See the X on the board
print_board(board)
# Check if number of guesses exceeded and print Game Over
if (turn == 3):
print "Game Over"
print_board(board)