Skip to content

Instantly share code, notes, and snippets.

@lewtds
Forked from itsjef/battleship.py
Last active December 10, 2015 16:39
Show Gist options
  • Save lewtds/4462756 to your computer and use it in GitHub Desktop.
Save lewtds/4462756 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3.2
"""
Simple Battleship game
TODO
- Networked multiplayer
- GUI (with pygame)
- Sound (with pygame)
- Localization
"""
from random import randint
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)
def main():
board = []
turn = 0
#tạo sân chơi
for x in range(0,5):
board.append(['O']*5)
print("Let's play Battleship!\n")
print_board(board)
print()
#random tọa độ thuyền
ship_row = random_row(board)
ship_col = random_col(board)
#print(ship_row+1 #cheat =)
#print(ship_col+1 #cheat =)
#vào game
for turn in range(4):
guess_row = int(input('Guess Row: '))
guess_col = int(input('Guess Col: '))
if (guess_row-1) == ship_row and (guess_col-1) == ship_col:
print("Congratulations! You sunk le battleship!")
break
else:
if (guess_row < 1 or guess_row > 5) or (guess_col < 1 or guess_col > 5):
print("Bleh, not even in the ocean, brah.")
elif (board[guess_row-1][guess_col-1]) == 'X':
print("You guessed that one already.")
else:
print("You missed dat SOAB!")
board[guess_row-1][guess_col-1] = 'X'
print("Guess left: "+str(3-turn)+'\n')
turn += 1
print_board(board)
if turn == 4:
print('Game Over!')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment