SHOW:
|
|
- or go back to the newest paste.
| 1 | ||
| 2 | #check and correct the BREAK loop | |
| 3 | ||
| 4 | ||
| 5 | from random import randint | |
| 6 | ||
| 7 | board = [] | |
| 8 | ||
| 9 | for x in range(5): | |
| 10 | board.append(["O"] * 5) | |
| 11 | ||
| 12 | def print_board(board): | |
| 13 | for row in board: | |
| 14 | print " ".join(row) | |
| 15 | ||
| 16 | print "Let's play Battleship!" | |
| 17 | print_board(board) | |
| 18 | ||
| 19 | def random_row(board): | |
| 20 | return randint(0, len(board) - 1) | |
| 21 | ||
| 22 | def random_col(board): | |
| 23 | return randint(0, len(board[0]) - 1) | |
| 24 | ||
| 25 | ship_row = random_row(board) | |
| 26 | ship_col = random_col(board) | |
| 27 | print ship_row | |
| 28 | print ship_col | |
| 29 | ||
| 30 | # Everything from here on should go in your for loop! | |
| 31 | # Be sure to indent four spaces! | |
| 32 | for turn in range(4): | |
| 33 | print "turn", turn + 1 | |
| 34 | guess_row = int(raw_input("Guess Row:"))
| |
| 35 | guess_col = int(raw_input("Guess Col:"))
| |
| 36 | ||
| 37 | if guess_row == ship_row and guess_col == ship_col: | |
| 38 | print "Congratulations! You sunk my battleship!" | |
| 39 | break | |
| 40 | else: | |
| 41 | if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4): | |
| 42 | print "Oops, that's not even in the ocean." | |
| 43 | elif(board[guess_row][guess_col] == "X"): | |
| 44 | print "You guessed that one already." | |
| 45 | else: | |
| 46 | print "You missed my battleship!" | |
| 47 | board[guess_row][guess_col] = "X" | |
| 48 | if turn==3: | |
| 49 | print 'Game Over' | |
| 50 | # Print (turn + 1) here! | |
| 51 | print 'Turn', turn+1 | |
| 52 | print_board(board) |