Advertisement
Guest User

Untitled

a guest
Apr 20th, 2014
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.73 KB | None | 0 0
  1. """This is the object file for an implementation of the classic game 'Battleship'."""
  2.  
  3. """Coordinate Screen"""
  4.  
  5. class Screen(object):
  6.  
  7. def __init__(self): #Put in colour option for placement / targetting
  8.  
  9. """ROWS"""
  10.  
  11. row_y = [' ','A','B','C','D','E','F','G','H','I','J']
  12. row_1 = ['1','_','_','_','_','_','_','_','_','_','_']
  13. row_2 = ['2','_','_','_','_','_','_','_','_','_','_']
  14. row_3 = ['3','_','_','_','_','_','_','_','_','_','_']
  15. row_4 = ['4','_','_','_','_','_','_','_','_','_','_']
  16. row_5 = ['5','_','_','_','_','_','_','_','_','_','_']
  17. row_6 = ['6','_','_','_','_','_','_','_','_','_','_']
  18. row_7 = ['7','_','_','_','_','_','_','_','_','_','_']
  19. row_8 = ['8','_','_','_','_','_','_','_','_','_','_']
  20. row_9 = ['9','_','_','_','_','_','_','_','_','_','_']
  21. row_0 = ['0','_','_','_','_','_','_','_','_','_','_']
  22.  
  23.  
  24. row_list = [row_y,row_1,row_2,row_3,row_4,row_5,row_6,row_7,row_8,row_9,row_0]
  25. original_row_list = row_list
  26. self.preplacement = row_list
  27.  
  28. """"""
  29.  
  30. self.data = row_list
  31. self.screentype = False
  32.  
  33. def onScreen(self):
  34. self.screentype = True
  35.  
  36. def offScreen(self):
  37. self.screentype = False
  38.  
  39. def updateScreen(self,x_coord,y_coord,value):
  40.  
  41. if not 1 <= x_coord <= 10:
  42. raise Exception("Not valid x coordinate")
  43.  
  44. if not 2 <= y_coord + 1 <= 11:
  45. raise Exception("Not valid y coordinate")
  46.  
  47. (self.data[y_coord])[x_coord] = value
  48.  
  49. def copyScreen(self):
  50. return self.data
  51.  
  52. def resetScreen(self):
  53. self.data = original_row_list
  54.  
  55. def printScreen(self):
  56.  
  57. """Blank Screen"""
  58. if self.screentype:
  59. for i in xrange(11):
  60. print self.data[i]
  61.  
  62. def __repr__(self):
  63. return "Screen"
  64.  
  65. """Ship Class:"""
  66.  
  67. """Instance creates Ship object of length 2-5 with HP/Status Check and commands"""
  68. """for vertical or horizontal placement on coordinate screen"""
  69.  
  70. class Ship(object):
  71.  
  72. def __init__(self,name,value):
  73.  
  74. if not str(name).isalpha():
  75. raise Exception("Not a valid Name!")
  76.  
  77. # HP HX HY TX TY STAT
  78. self.data = [None,None,None,None,None,None]
  79. self.length = value
  80. self.name = name[0]
  81.  
  82. def reset(self):
  83. for i in xrange(6):
  84. self.data[i] = None
  85.  
  86. """HP"""
  87.  
  88. def setinitialHP(self):
  89.  
  90. if 2 <= self.length <= 5:
  91. self.data[0] = self.length
  92. else:
  93. self.reset()
  94. raise Exception("Not a valid HP value! Ship class reset")
  95.  
  96. def changeHP(self,value):
  97.  
  98. if 0 <= value <= self.length:
  99. self.data[0] = value
  100. else:
  101. self.reset()
  102. raise Exception("Not a valid HP value! Ship class reset")
  103.  
  104. def remainingHP(self):
  105. return self.data[0]
  106.  
  107. """Status"""
  108.  
  109. def setStatus(self):
  110.  
  111. if self.remainingHP() == 0:
  112. self.data[5] = False
  113.  
  114. else:
  115. self.data[5] = True
  116.  
  117. def checkStatus(self):
  118. return self.data[5]
  119.  
  120. def printStats(self):
  121. print self.data
  122.  
  123. """Placement"""
  124.  
  125. def setHead(self,x_coord,y_coord):
  126.  
  127. if not 1 <= x_coord <= 10:
  128. self.reset()
  129. raise Exception("Not valid x coordinate! Ship class reset")
  130.  
  131. if not 2 <= y_coord + 1 <= 11:
  132. self.reset()
  133. raise Exception("Not valid y coordinate! Ship class reset")
  134.  
  135. self.data[1] = x_coord
  136. self.data[2] = y_coord
  137.  
  138. def setTail(self,x_coord,y_coord):
  139.  
  140. if self.data[1] == None or self.data[2] == None:
  141. self.reset()
  142. return Exception("Head not set! Ship class reset")
  143.  
  144. elif self.data[1] == x_coord and self.data[2] == y_coord:
  145. self.reset()
  146. raise Exception("Ship length is 0! Ship class reset")
  147.  
  148. elif self.data[1] != x_coord and self.data[2] != y_coord:
  149. self.reset()
  150. raise Exception("Ship not placed vertically or horizontally! Ship class reset")
  151.  
  152. elif abs(self.data[1]-x_coord) + abs(self.data[2]-y_coord) + 1 != self.length:
  153. self.reset()
  154. raise Exception("Ship not correct length! Ship class reset")
  155. else:
  156. self.data[3] = x_coord
  157. self.data[4] = y_coord
  158.  
  159. def place(self,other): #place(Ship,Screen)
  160.  
  161. for i in xrange(6):
  162. if self.data[i] == None:
  163. raise Exception("Boat is not fully initialized!")
  164.  
  165. """Horizontal Placement"""
  166.  
  167. if self.data[2] == self.data[4]:
  168.  
  169. y_c = self.data[2]
  170. x_min = min(self.data[1],self.data[3])
  171. x_max = max(self.data[1],self.data[3])
  172.  
  173. if not (1 <= x_min < x_max <= 10):
  174. self.reset()
  175. raise Exception("Not a valid placement! Ship class reset")
  176.  
  177. elif not 2 <= y_c + 1 <= 11:
  178. self.reset()
  179. raise Exception("Not a valid placement! Ship class reset")
  180.  
  181. """Copy screen, collision test, placement"""
  182.  
  183. copy = other.copyScreen()
  184.  
  185. """Collision Test"""
  186.  
  187. for i in xrange(x_min,x_max + 1):
  188. if copy[y_c][i] != '_':
  189. raise Exception("Ship already located at '(%s,%s)'" % (i,y_c))
  190.  
  191. """Placement"""
  192.  
  193. other.preplacement = copy #In case of error
  194.  
  195. for i in xrange(x_min,x_max + 1):
  196. other.updateScreen(x,y_c,self.name)
  197.  
  198. """Vertical Placement"""
  199.  
  200. if self.data[1] == self.data[3]:
  201.  
  202. x_c = self.data[1]
  203. y_min = min(self.data[2],self.data[4])
  204. y_max = max(self.data[2],self.data[4])
  205.  
  206. if not (1 <= x_c <= 10):
  207. self.reset()
  208. raise Exception("Not a valid placement! Ship class reset")
  209.  
  210. elif not (2 <= y_min + 1 < y_max + 1 <= 11):
  211. self.reset()
  212. raise Exception("Not a valid placement! Ship class reset")
  213.  
  214. """Copy screen, collision test, placement"""
  215.  
  216. copy = other.copyScreen()
  217.  
  218. """Collision Test"""
  219.  
  220. for i in xrange(y_min,y_max+1):
  221. if copy[i][x_c] != '_':
  222. raise Exception("Ship already located at '(%s,%s)'" % (x_c,i))
  223.  
  224. """Placement"""
  225.  
  226. other.preplacement = copy #In case of error
  227.  
  228. for i in xrange(y_min,y_max + 1):
  229. other.updateScreen(x_c,i,self.name)
  230.  
  231. def __repr__(self):
  232. return "%s : %s" % (self.name,self.length)
  233.  
  234. """End of Ship Class"""
  235.  
  236. """TESTING"""
  237. X = Screen()
  238. submarine = Ship('Sub',3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement