BattleShip Furthered

python

#1

####################################

from random import randint

####################################

def intro():

print "\nWelcome to BattleShips.\nEnemy ships take up one space and are randomly generated.\nYou must try to shoot enemy ships without knowing their location."

####################################

def boardSize():

print ""

sizeBoard = input("Enter size of board (maximum of 9): ")

return sizeBoard

####################################

def shipSize():

print ""

sizeShip = input("Enter number of enemy ships: ")

return sizeShip

####################################

def newBoard(sizeBoard):

print ""

board = []

for i in range(0, sizeBoard):

    board.append(["o"] * sizeBoard)

return board

####################################

def xCoords(sizeShip, sizeBoard):

xBoatCoords = []

for i in range(0,sizeShip):

    xBoatCoords.append(randint(1,sizeBoard))

return xBoatCoords                  

####################################

def yCoords(sizeShip, sizeBoard):

yBoatCoords = []

for i in range(0,sizeShip):
               
    yBoatCoords.append(randint(1,sizeBoard))

return yBoatCoords

####################################

def printMap(boardIn):

xCoordAxis = " "

for i in range(len(boardIn)):

    if (i + 1) <= 9:
        
        xCoordAxis += " " + str(i + 1)

    else:

        print "Cannot create maps bigger than 9, please restart."

        input()

for i in range(len(boardIn)):

    print str((len(boardIn) - i)) + " " + " ".join(boardIn[i])

print xCoordAxis

####################################

def userGuessMap(sizeBoard, oldMap, xHitCoords, yHitCoords):

print ""

xGuess = (input("Enter a x value to shoot: "))

print ""

yGuess = (input("Enter a y value to shoot: "))

print ""

newMap = oldMap

x = xGuess - 1

y = sizeBoard - yGuess

if yGuess not in range(sizeBoard + 1) or xGuess not in range(sizeBoard + 1):

    print "Outside Range Please Restart"
    
    input()

newMap[y][x] = "X"

outcome = hitOutcome(xHitCoords, yHitCoords, xGuess, yGuess)

if outcome == 1:

    winMessage(xGuess, yGuess, cheatMap(sizeBoard, newBoard(sizeBoard), xHitCoords, yHitCoords), newMap)

    return outcome

elif outcome == 0:

    return newMap

####################################

def cheatMap(sizeBoard, firstBoard, xHitCoords, yHitCoords):

for i in range(len(xHitCoords)):

    x = xHitCoords[i] - 1

    y = sizeBoard - yHitCoords[i]

    firstBoard[y][x] = "V"

return firstBoard

####################################

def hitOutcome(xHitCoords, yHitCoords, xShot, yShot):

hit = 0

for i in range(len(xHitCoords)):

    if xHitCoords[i] == xShot and yHitCoords[i] == yShot:

        hit += 1
        
if hit >= 1:

    return 1

if hit == 0:

    return 0

####################################

def gameOver(compMap, newMap):

print "You have let the enemy survive"

print "\nThis was your shot map.\n"

printMap(newMap)

print "\nThis was the enemy map.\n"

printMap(compMap)

print "\nPlease restart to try again."

input()

####################################

def winMessage(x, y, compMap, userMap):

print "Congratulations, you have destroyed the enemy, one of their ships was in the coordinates ", "(", x, ",", y, ")."

print "\nYour map was: \n"

printMap(userMap)

print "\nThe other boats were located as in the following map.\n"

printMap(compMap)

input()

####################################

def turnNumber():

return input("How many turns would you like? ")

#######################################################################

intro()

sizeBoard = boardSize()

sizeShip = shipSize()

firstBoard = newBoard(sizeBoard)

xHitCoords = xCoords(sizeShip, sizeBoard)

yHitCoords = yCoords(sizeShip, sizeBoard)

cheat = input("Cheat = 1, Vanilla = 0: ")

print “”

if cheat == 1:

print "x is", xHitCoords

print "y is", yHitCoords

print ""

number = turnNumber()

for turn in range(number):

print "\nTurn ", turn + 1

if turn == 0:

    print "\nThis is your first blank board.\n"

    printMap(firstBoard)

    newMap = userGuessMap(sizeBoard, firstBoard, xHitCoords, yHitCoords)

else:
    
    print "\nThis is your hit board so far: \n"

    printMap(newMap)

    newMap = userGuessMap(sizeBoard, newMap, xHitCoords, yHitCoords)

if newMap != 1: #I.E No Win

    print "You have missed please try again!"

if newMap == 1:     #I.E - Won

    break

if turn == (number - 1):        #I.E - Turn after last

    gameOver(cheatMap(sizeBoard, newBoard(sizeBoard), xHitCoords, yHitCoords), newMap)

#2

WOW!:open_mouth: This is massive!


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.