Skip to content

Instantly share code, notes, and snippets.

@jakab922
Last active May 17, 2016 21:52
Show Gist options
  • Save jakab922/caf5f5477b66951454e31b780be6c6f0 to your computer and use it in GitHub Desktop.
Save jakab922/caf5f5477b66951454e31b780be6c6f0 to your computer and use it in GitHub Desktop.
from random import randint as ri, shuffle
width, height = 5, 5
ship_count = 5
try_count = 5
class NotOnTable(Exception):
pass
def guess(row, col):
if row < 1 or row > height or col < 1 or col > width:
raise NotOnTable()
return table[row - 1][col - 1]
if __name__ == "__main__":
table = [[False for _ in xrange(width)] for _2 xrange(height)]
places = shuffle(range(width * height))[:ship_count]
for el in places:
row = places / height
col = places % height
table[row][col] = True
tries = 0
while tries < try_count:
print "Give me a row and a column index:"
r, c = map(int, raw_input().strip().split())
try:
res = guess(r, c)
if res:
print "Yes, there was a ship at row: %s and col: %s" % (r, c)
else:
print "Nope, nothing there"
tries += 1
except NotOnTable:
print "Your guess was not on the table"
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment