# needed classes import sys import random from time import gmtime, strftime import pprint # define board columns = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'] rows = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # define difficulty levels diff = {} diff[1] = {'Name': 'Level 1', 'Ships': 8, 'ValidShips': [0, 1, 2, 3], 'Bombs': 90} diff[2] = {'Name': 'Level 2', 'Ships': 5, 'ValidShips': [0, 1, 2, 3], 'Bombs': 70} diff[3] = {'Name': 'Level 3', 'Ships': 3, 'ValidShips': [1, 2, 3], 'Bombs': 50} # define ships (ship(-name), needed block(s) = hits) ships = {} ships[0] = {'Name': 'Slagschip', 'Blocks': 5} ships[1] = {'Name': 'Kruiser', 'Blocks': 4} ships[2] = {'Name': 'Fregatten', 'Blocks': 3} ships[3] = {'Name': 'Mijnenveger', 'Blocks': 2} # define ship positions setPositions = {} hitPositions = {} # function to random set ship def setShip(Id, ship): # roll the dice # 1 = place horizon, 2 = place vertical position = random.randint(1, 2) # pick a (start) random block (= position) row = random.choice(rows) column = random.choice(columns) # pick a random start block, but be sure it is no longer than the board itself firstBlock = random.randint(1, 10 - ship['Blocks']) if firstBlock < 1: # should be in field range return False # place by position if position == 1: # horizon # get (possible) blocks, from left to right (a-j) blocks = columns[firstBlock : firstBlock + ship['Blocks']] elif position == 2: # vertical # get (possible) blocks, from top to bottom (1-10) blocks = rows[firstBlock : firstBlock + ship['Blocks']] row = column # change row to column # check if block are empty and a ship can be set valid = False for block in blocks: default = False blockHorFilled = setPositions.get(str(row) + str(block), default) blockVerFilled = setPositions.get(str(block) + str(row), default) if blockHorFilled or blockVerFilled: return False ship['uuid'] = Id # unique Id for block in blocks: setPositions[str(row) + str(block)] = ship print('Block: ' + str(row) + str(block) + ' = ' + str(setPositions[str(row) + str(block)]) + ' same as ' + str(ship)) return True # function to check if ship(-block) has been hitted def hitShip(row, column): # print what location requested print('Bom afgevuurt op rij: ' + str(row) + ' kolom: ' + str(column)) # check if a ship has been hitted by row, column default = False blockHorFilled = setPositions.get(str(row) + str(column), default) blockVerFilled = setPositions.get(str(column) + str(row), default) if blockHorFilled or blockVerFilled: # if not empty, there is a ship hitPositions[str(row) + str(column)] = 'hit' hitPositions[str( column) + str(row)] = 'hit' if blockHorFilled: print('Je hebt ' + str(blockHorFilled['Name']) + ' geraakt!') elif blockVerFilled: print('Je hebt ' + str(blockVerFilled['Name']) + ' geraakt!') else: hitPositions[str(row) + str(column)] = 'mis' hitPositions[str( column) + str(row)] = 'mis' print('Geen schip geraakt!') return True # exit function # return board and change to user input def checkBoard(): # change 'X' to 'H' (hit) or 'M' (mis) print('*' + "\t",end='') for column in columns: print(str(column) + "\t",end='') print() for row in rows: print(str(row) + "\t",end='') for columnValue in columns: default = False hitBlockHor = hitPositions.get(str(row) + str(columnValue), default) hitBlockVer = hitPositions.get(str(columnValue) + str(row), default) if hitBlockHor == 'hit' or hitBlockVer == 'hit': print("H" + "\t",end='') elif hitBlockHor == 'mis' or hitBlockVer == 'mis': print("M" + "\t",end='') else: print("X" + "\t",end='') print() # return how many ships remaing def checkRemaining(): compareShips = {} hitShips = {} for row in rows: for column in columns: default = False shipBlockHor = setPositions.get(str(row) + str(column), default) shipBlockVer = setPositions.get(str(column) + str(row), default) shipBlockHitHor = hitPositions.get(str(row) + str(column), default) shipBlockHitVer = hitPositions.get(str(column) + str(row), default) if shipBlockHitHor == 'hit' : shipPosition = str(row) + str(column) shipInCompare = compareShips.get(shipPosition, default) if not shipInCompare: compareShips[shipPosition] = 1 hitShip = hitShips.get(shipBlockHor['uuid'], default) if hitShip: hitShip['count'] = hitShip['count'] + 1 else: shipBlockHor['count'] = 1 hitShips[shipBlockHor['uuid']] = shipBlockHor elif shipBlockHitVer == 'hit': shipPosition = str(column) + str(row) shipInCompare = compareShips.get(shipPosition, default) if not shipInCompare: compareShips[shipPosition] = 1 hitShip = hitShips.get( shipBlockVer['uuid'], default) if hitShip: hitShip['count'] = hitShip['count'] + 1 else: shipBlockVer['count'] = 1 hitShips[shipBlockVer['uuid']] = shipBlockVer print(compareShips) print(hitShips) # ask user what level to play userLevel = int(input('Geef een level op (1, 2, 3): ')) if userLevel > 3: sys.exit('Kies alleen een level van 1 t/m 3') # extract them (into pieces) levelName, levelShips, validShips, levelBombs = diff[userLevel]['Name'], diff[userLevel]['Ships'], diff[userLevel]['ValidShips'], diff[userLevel]['Bombs'] # begin placing ships by levelShips count = 0 while True: if count == levelShips: # max. ships released break randomShip = random.choice(validShips) # choose between the ships if (setShip(count, ships[randomShip])): # check if ship can be set count = count + 1 # ship set, +1 # debugging pp = pprint.PrettyPrinter(indent=4) pp.pprint(setPositions) # set start time startTime = strftime("%a, %d %b %Y %H:%M:%S", gmtime()) # print some info print('Start tijd: ' + startTime) print('Je speelt op level: ' + str(levelName)) print('Er zijn ' + str(levelShips) + ' schepen gezet') # user inputs position(s) to place bomb count = 0 while True: if count == levelBombs: # all bombs used sys.exit('Je hebt alle bommen gebruikt!') break checkBoard() checkRemaining() print('Je hebt nog ' + str(levelBombs - count) + ' / ' + str(levelBombs) + ' bommen ter beschikking!') reqHitPosition = input('Geef rij (1 t/m 10) en kolum (a t/m j). Bijv. 1-b: ') reqHitPosition = reqHitPosition.split('-') hitShip(reqHitPosition[0], reqHitPosition[1]) count = count + 1 # used a bomb (+1) print('--------------') print()