Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """This is the object file for an implementation of the classic game 'Battleship'."""
- """Coordinate Screen"""
- class Screen(object):
- def __init__(self): #Put in colour option for placement / targetting
- """ROWS"""
- row_y = [' ','A','B','C','D','E','F','G','H','I','J']
- row_1 = ['1','_','_','_','_','_','_','_','_','_','_']
- row_2 = ['2','_','_','_','_','_','_','_','_','_','_']
- row_3 = ['3','_','_','_','_','_','_','_','_','_','_']
- row_4 = ['4','_','_','_','_','_','_','_','_','_','_']
- row_5 = ['5','_','_','_','_','_','_','_','_','_','_']
- row_6 = ['6','_','_','_','_','_','_','_','_','_','_']
- row_7 = ['7','_','_','_','_','_','_','_','_','_','_']
- row_8 = ['8','_','_','_','_','_','_','_','_','_','_']
- row_9 = ['9','_','_','_','_','_','_','_','_','_','_']
- row_0 = ['0','_','_','_','_','_','_','_','_','_','_']
- row_list = [row_y,row_1,row_2,row_3,row_4,row_5,row_6,row_7,row_8,row_9,row_0]
- original_row_list = row_list
- self.preplacement = row_list
- """"""
- self.data = row_list
- self.screentype = False
- def onScreen(self):
- self.screentype = True
- def offScreen(self):
- self.screentype = False
- def updateScreen(self,x_coord,y_coord,value):
- if not 1 <= x_coord <= 10:
- raise Exception("Not valid x coordinate")
- if not 2 <= y_coord + 1 <= 11:
- raise Exception("Not valid y coordinate")
- (self.data[y_coord])[x_coord] = value
- def copyScreen(self):
- return self.data
- def resetScreen(self):
- self.data = original_row_list
- def printScreen(self):
- """Blank Screen"""
- if self.screentype:
- for i in xrange(11):
- print self.data[i]
- def __repr__(self):
- return "Screen"
- """Ship Class:"""
- """Instance creates Ship object of length 2-5 with HP/Status Check and commands"""
- """for vertical or horizontal placement on coordinate screen"""
- class Ship(object):
- def __init__(self,name,value):
- if not str(name).isalpha():
- raise Exception("Not a valid Name!")
- # HP HX HY TX TY STAT
- self.data = [None,None,None,None,None,None]
- self.length = value
- self.name = name[0]
- def reset(self):
- for i in xrange(6):
- self.data[i] = None
- """HP"""
- def setinitialHP(self):
- if 2 <= self.length <= 5:
- self.data[0] = self.length
- else:
- self.reset()
- raise Exception("Not a valid HP value! Ship class reset")
- def changeHP(self,value):
- if 0 <= value <= self.length:
- self.data[0] = value
- else:
- self.reset()
- raise Exception("Not a valid HP value! Ship class reset")
- def remainingHP(self):
- return self.data[0]
- """Status"""
- def setStatus(self):
- if self.remainingHP() == 0:
- self.data[5] = False
- else:
- self.data[5] = True
- def checkStatus(self):
- return self.data[5]
- def printStats(self):
- print self.data
- """Placement"""
- def setHead(self,x_coord,y_coord):
- if not 1 <= x_coord <= 10:
- self.reset()
- raise Exception("Not valid x coordinate! Ship class reset")
- if not 2 <= y_coord + 1 <= 11:
- self.reset()
- raise Exception("Not valid y coordinate! Ship class reset")
- self.data[1] = x_coord
- self.data[2] = y_coord
- def setTail(self,x_coord,y_coord):
- if self.data[1] == None or self.data[2] == None:
- self.reset()
- return Exception("Head not set! Ship class reset")
- elif self.data[1] == x_coord and self.data[2] == y_coord:
- self.reset()
- raise Exception("Ship length is 0! Ship class reset")
- elif self.data[1] != x_coord and self.data[2] != y_coord:
- self.reset()
- raise Exception("Ship not placed vertically or horizontally! Ship class reset")
- elif abs(self.data[1]-x_coord) + abs(self.data[2]-y_coord) + 1 != self.length:
- self.reset()
- raise Exception("Ship not correct length! Ship class reset")
- else:
- self.data[3] = x_coord
- self.data[4] = y_coord
- def place(self,other): #place(Ship,Screen)
- for i in xrange(6):
- if self.data[i] == None:
- raise Exception("Boat is not fully initialized!")
- """Horizontal Placement"""
- if self.data[2] == self.data[4]:
- y_c = self.data[2]
- x_min = min(self.data[1],self.data[3])
- x_max = max(self.data[1],self.data[3])
- if not (1 <= x_min < x_max <= 10):
- self.reset()
- raise Exception("Not a valid placement! Ship class reset")
- elif not 2 <= y_c + 1 <= 11:
- self.reset()
- raise Exception("Not a valid placement! Ship class reset")
- """Copy screen, collision test, placement"""
- copy = other.copyScreen()
- """Collision Test"""
- for i in xrange(x_min,x_max + 1):
- if copy[y_c][i] != '_':
- raise Exception("Ship already located at '(%s,%s)'" % (i,y_c))
- """Placement"""
- other.preplacement = copy #In case of error
- for i in xrange(x_min,x_max + 1):
- other.updateScreen(x,y_c,self.name)
- """Vertical Placement"""
- if self.data[1] == self.data[3]:
- x_c = self.data[1]
- y_min = min(self.data[2],self.data[4])
- y_max = max(self.data[2],self.data[4])
- if not (1 <= x_c <= 10):
- self.reset()
- raise Exception("Not a valid placement! Ship class reset")
- elif not (2 <= y_min + 1 < y_max + 1 <= 11):
- self.reset()
- raise Exception("Not a valid placement! Ship class reset")
- """Copy screen, collision test, placement"""
- copy = other.copyScreen()
- """Collision Test"""
- for i in xrange(y_min,y_max+1):
- if copy[i][x_c] != '_':
- raise Exception("Ship already located at '(%s,%s)'" % (x_c,i))
- """Placement"""
- other.preplacement = copy #In case of error
- for i in xrange(y_min,y_max + 1):
- other.updateScreen(x_c,i,self.name)
- def __repr__(self):
- return "%s : %s" % (self.name,self.length)
- """End of Ship Class"""
- """TESTING"""
- X = Screen()
- submarine = Ship('Sub',3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement