In this lab , you will build the “ The Battle Ship s Game ”. The Game has a n x
ID: 3864638 • Letter: I
Question
In this lab , you will build the “ The Battle Ship s Game ”. The Game has a n x n grid and m ships. Each ship takes up exactly three cells. In the game, it’s you against the computer, but unlike the real battleship game, you don’t place any ships on your own. Instead, your job is to sink the computer’s ship in the fewest number of guess.
Goal:Sink all the computer’s ships(0
Setup:When the game program is launched, user is again asked to enter whatwould be the size of the grid, that is the value ofn. The value ofnmust bebetween 3and10. Then, the computer placesmships on an x ngrid (for an example, see thefigure below).When that’scomplete the game asks for your first guess.
How to play:We haven’t learned to build GUI (Graphical User Interface) yet, so thisversion works at the command-line. The computer will prompt you to enter a guess(a cell), that you willtype at the command-line as “1 3”(where “1 3” means 3rd cell of the 1strow), “45”, etc.. In response to your guess, you will see a result at thecommand line, either “Hit”, “Miss”, “Kill” (or whatever the lucky battleship of the dayis!). When you have sunk all m battleships, the game ends by printing out your rating.
Here is a sample run of the game:
Welcome to the Battle Ships game!
Please enter the size of the grid: 8
Please enter number of ships: 3
Your goal is to sink 3 ships.
Try to sink them all in the fewest number of guesses
Enter a guess [row (1 to 8)] [col (1 to 8)]: 1 3 miss
Enter a guess [row (1 to 8)] [col (1 to 8)]: 2 1 hit
Enter a guess [row(1 to 8)] [col (1 to 8)]: 2 2 miss
Enter a guess [row (1 to 8)] [col (1 to 8)]: 3 1 hit
Enter a guess [row (1 to 8)] [col (1 to 8)]: 4 1 kill
Enter a guess [row (1 to 8)] [col (1 to 8)]: 3 3 miss
Enter a guess [row (1 to 8)] [col (1 to 8)]: 1 7 miss
Enter a guess [row (1 to 8)] [col (1 to 8)]: 4 3 hit
Enter a guess [row (1 to 8)] [col (1 to 8)]: 4 4 hit
Enter a guess [row (1 to 8)] [col (1 to 8)]: 4 5 kill
Enter a guess [row (1 to 8)] [col (1 to 8)]: 5 1 miss
Enter a guess [row (1 to 8)] [col (1 to 8)]: 3 1 miss
Enter a guess [row (1 to 8)] [col (1 to 8)]: 6 1 miss
Enter a guess [row (1 to 8)] [col (1 to 8)]: 61 1 miss
Enter a guess [row (1 to 8)] [col (1 to 8)]: 6 1 miss
Enter a guess [row (1 to 8)] [col (1 to 8)]: 8 4 hit
Enter a guess [row (1 to 8)] [col (1 to 8)]: 8 5 hit
Enter a guess [row (1 to 8)] [col (1 to 8)]: 8 6 kill
All ships are sank!
It only took you18 guesses.
You got out before your options sank.
When player can finish the game within 20 guesses,print the following message (where xx is the number of guesses):
All ships are sank! It only took you XX guesses.
You got out before your options sank.
If it takes more than 20 guesses, print the following message All ships are sank! Took you long enough.XX guesses.Fish are dancing with your options.
Your program should term
inate after 25 guesses and
reveal the positions of the
ships.
NOTE: While designing the classes for this lab, you must follow OOP design concepts (Abstraction,Modularity,Inheritance and API design)
i need a UML diagram for the program
and a code for the program in java application
and proper comments for the program.
Explanation / Answer
# -*- coding: utf-8 -*-from __future__ import divisionimport numpy import operator import commands importrandom import sys import os from time import sleepfrom grid import grid from fleet import fleet #setupNavy#Purpos: To place our ships on the grid#Receives: setupSelection- either manual or random# gridClass# sortedShipList#Return: def setupNavy(setupSelection,gridClass,sortedShipList): used="no" shipCoordList=[] for shipData in sortedShipList: shipName=shipData[0] shipSize=shipData[1] if setupSelection=="manual": header= "Ship placement: %s size is: %s "%(shipName,shipSize) sys.stdout.write(header) con="yes"while con=="yes": if setupSelection=="manual": start=raw_input("Enter start point: ") else: randomX=random.randrange(0,gridClass.xGridSize) randomY=random.randrange(0,gridClass.yGridSize) xLetter=gridClass.alphDict[randomX] start="%s%d"%(xLetter,randomY) coordStatus=gridClass.checkDataPointValue(start) if coordStatus=="E": con="no" if setupSelection=="manual": placement=raw_input("Place Vertical (V) or Horizontal (H): ") else: placement=random.choice("VH") end=gridClass.determineEndPoint(start,shipSize,placement) if end=="F": if setupSelection=="manual": error="Datapoint: %s will place %s off the grid "%(start,shipName) sys.stdout.write(error) used="yes" else: shipCoordList=gridClass.determineFullLocation(start,end) gridClass.shipLocationDict[shipName]=shipCoordList for coord in shipCoordList: coordList=coord.split(',') dataPoint="%s%s"%(gridClass.alphDict[int(coordList[0])],coordList[1]) coordStatus=gridClass.checkDataPointValue(dataPoint)if coordStatus=='T': if setupSelection=="manual": error="Datapoint: %s is already used "% dataPoint sys.stdout.write(error) used="yes" if used=="no": gridClass.gridValuesUsed+=shipCoordList else: if setupSelection=="manual": error= "Datapoint: %s is already used "% dataPoint sys.stdout.write(error) con="yes" if used=="yes": con="yes" used="no" os.system('clear; history -c') gridClass.shipPlacement(start,end,shipCoordList) gridDict=gridClass.populateGrid() if setupSelection=="manual": gridClass.displayGrid() sleep(0.25) os.system('clear; history -c') return def determineNextAttackInSeq (attackerGridClass): startLocation="";xHitList=[];yHitList=[] for hit in attackerGridClass.hitList: hitCoordList=hit.split(',') xHitList.append(hitCoordList[0]) yHitList.append(hitCoordList[1]) if len(set(xHitList))>1: nextY=yHitList[0] xHitList=sorted(xHitList) nextBegX=int(xHitList[0])-1 nextEndX=int(xHitList[len(xHitList)-1])+1 begCoord="%s,%s"%(nextBegX,nextY) lastCoord="%s,%s"%(nextEndX,nextY) if begCoord in attackerGridClass.attackList: startLocation=begCoord elif lastCoord in attackerGridClass.attackList: startLocation=lastCoord #Vertical order elif len(set(yHitList))>1: nextX=xHitList[0] yHitList=sorted(yHitList) nextBegY=int(yHitList[0])-1 nextEndY=int(yHitList[len(yHitList)-1])+1 begCoord="%s,%s"%(nextX,nextBegY) lastCoord="%s,%s"%(nextX,nextEndY) if begCoord in attackerGridClass.attackList: startLocation=begCoord elif lastCoord in attackerGridClass.attackList: startLocation=lastCoord return startLocation def checkRoadBlocks(attackerGridClass,defenderFleetClass,startLocation): startList=startLocation.split(',') xValue=int(startList[0]) yValue=int(startList[1]) #Convert to alpha numeric for display purposes #ie. 2,13 is C13 start="%s%s"%(attackerGridClass.alphDict[xValue],yValue) possVertLocList=[] possHorzLocList=[] maxGridSize=attackerGridClass.xGridSize minShipSize=defenderFleetClass.minShipSize possVertLocList.append(startLocation) possHorzLocList.append(startLocation) iteratorSeq=0 startX=xValue startY=yValue while iteratorSeq < minShipSize: iteratorSeq=iteratorSeq+1 #Veritcal locations possY=int(startY)+iteratorSeq if possY<maxGridSize and possY>=0: possVertLocBelow="%s,%s"%(startX,possY) possVertLocList.append(possVertLocBelow) possY=int(startY)-iteratorSeq if possY<maxGridSize and possY>=0: possVertLocAbove="%s,%s"%(startX,possY) possVertLocList.append(possVertLocAbove)#Horziontal locations possX=int(startX)+iteratorSeq if possX<maxGridSize and possX>=0: possHorzLocRight="%s,%s"%(possX,startY) possHorzLocList.append(possHorzLocRight) possX=int(startX)-iteratorSeq if possX<maxGridSize and possX>=0: possHorzLocLeft="%s,%s"%(possX,startY) possHorzLocList.append(possHorzLocLeft) for coord in attackerGridClass.missedList: if coord in possVertLocList: possVertLocList.remove(coord) elif coord in possHorzLocList: possHorzLocList.remove(coord) possVertLocList= sorted(possVertLocList) ySList=[] for coordS in possVertLocList: xySlist=coordS.split(',') ySList.append(xySlist[1]) ySList.sort(key=int) numSeq=0 prev=-1 count=0 ySeqList=[] for ySValue in ySList: ySValue=int(ySValue) if prev==ySValue: numSeq=numSeq+1 prev=prev+1 else: prev=int(ySValue)+1 numSeq=1 ySeqList.append(numSeq) possHorzLocList=sorted(possHorzLocList) xSList=[] for coordS in possHorzLocList: xySlist=coordS.split(',') xSList.append(xySlist[0]) xSList.sort(key=int) numSeq=0 prev=-1 count=0 xSeqList=[] for xSValue in xSList: xSValue=int(xSValue) if prev==xSValue: numSeq=numSeq+1 prev=prev+1 else: prev=int(xSValue)+1 numSeq=1 xSeqList.append(numSeq) if minShipSize not in xSeqList and minShipSize not in ySeqList: attackerGridClass.blockedCoordList.append(startLocation) return attackerGridClass.blockedCoordList def findAroundCoords(attackerGridClass,startLocation): startList=startLocation.split(',') xValue=int(startList[0]) yValue=int(startList[1]) left=xValue-1 below=yValue-1 right=xValue+1 above=yValue+1 if left>=0: leftValue="%s,%s"%(left,yValue) leftLetter="%s%s"%(attackerGridClass.alphDict[left],yValue) if leftValue notin attackerGridClass.attackList and leftValue not in attackerGridClass.hitList and leftValue not in attackerGridClass.missedList: attackerGridClass.attackList.append(leftValue) if below>=0: belowValue="%s,%s"%(xValue,below) belowLetter="%s%s"%(attackerGridClass.alphDict[xValue],below) if belowValue not in attackerGridClass.attackList and belowValue notin attackerGridClass.hitList and belowValue not in attackerGridClass.missedList: attackerGridClass.attackList.append(belowValue) if right<=(attackerGridClass.xGridSize-1): rightValue="%s,%s"%(right,yValue) rightLetter="%s%s"%(attackerGridClass.alphDict[right],yValue) if rightValue not in attackerGridClass.attackList and rightValue not in attackerGridClass.hitList and rightValue not in attackerGridClass.missedList: attackerGridClass.attackList.append(rightValue) if above<=(attackerGridClass.yGridSize-1) or above==attackerGridClass.yGridSize-1: aboveValue="%s,%s"%(xValue,above) aboveLetter="%s%s"%(attackerGridClass.alphDict[xValue],above) if aboveValue not in attackerGridClass.attackList and aboveValue notin attackerGridClass.hitList and aboveValue not in attackerGridClass.missedList: attackerGridClass.attackList.append(aboveValue) return attackerGridClass.attackList def checkShipStatus(attackerGridClass,defenderGridClass,defenderFleetClass): #Determine if the attacked ship is sunk. for ship, locationList in defenderGridClass.shipLocationDict.iteritems(): hitsTaken=0 shipSize=defenderFleetClass.shipFleetDict[ship] if defenderFleetClass.shipStatusDict[ship]=="active": for location in locationList: if location in attackerGridClass.attackedCoordList: hitsTaken+=1 if hitsTaken==shipSize: print "%s sunk a %s"%(whosTurn,ship) sleep(1.25) attackerGridClass.blockedCoordList=[] defenderFleetClass.shipStatusDict[ship]="sunk" defenderFleetClass.numberSunkShips+=1 attackerGridClass.attackNumber=0 defenderFleetClass.minShipSize=defenderFleetClass.determineMinShipSize() if len(attackerGridClass.hitList)>shipSize: attackerGridClass.attackList=[] tmpHitList=sorted(attackerGridClass.hitList) firstCoord=tmpHitList[0] findAroundCoords(attackerGridClass,firstCoord) lastCoord=tmpHitList[len(tmpHitList)-1] findAroundCoords(attackerGridClass,lastCoord) attackerGridClass.hitList=[] else: attackerGridClass.attackList=[] attackerGridClass.hitList=[] #Empty attackList... Possiable problem!!! #print "*************************"#for extraCoord in attackerGridClass.attackList:#goodCoord=determineNextAttackInSeq(attackerGridClass) #print goodCoord #print attackerGridClass.attackList returndef attackShip(whosTurn,attackerGridClass,defenderGridClass,defenderFleetClass): newAttack="Y" numShipsInFleet=len(defenderFleetClass.shipStatusDict) while newAttack=="Y": #Person vs computer. We prompt for location to attack if whosTurn=="player": attackCoords=raw_input("Input attack coordinates (ex B2): ") xLetter=attackCoords[:1] xValue=defenderGridClass.alphList.index(xLetter) yValue=int(attackCoords[1:]) startLocation="%s,%s"%(xValue,yValue) else: #If we already have a planned attacked list then get the next one #The attackList is a educated guess where we should attack next #based on previous attacks. print attackerGridClass.attackList if len(attackerGridClass.attackList)!=0: attackerGridClass.attackList=sorted(attackerGridClass.attackList)################################################################ # If we have a hit we want to check what the next sequitnal order would be startLocation=determineNextAttackInSeq (attackerGridClass) if startLocation=="": #Get first location in attack list print "nexxxxt" startLocation=attackerGridClass.attackList[0] print startLocation ################################################################ end='' else: searchListEmpty="T" while searchListEmpty=="T": #If we dont have a searchList then lets build one. #The searchList is some logical search. We built that in defineSearchList if len(attackerGridClass.searchList)==0:#Get the max size of active ships. #The maxShipSize is used in our search algorithm defenderFleetClass.maxShipSize=defenderFleetClass.determineMaxShipSize() #defineSearchList is a digonal search attackerGridClass.searchList=attackerGridClass.defineSearchList(defenderFleetClass.maxShipSize,attackerGridClass.attackNumber) attackerGridClass.attackNumber+=1 #Remove location if we have already missed/hit that spot attackerGridClass.searchList
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.