Python vraag over battleship

Status
Niet open voor verdere reacties.

Kerkrade

Gebruiker
Lid geworden
14 aug 2011
Berichten
163
Hallo,

Ik heb een battleship gemaakt maar nu wil ik het zo maken dat mijn schepen ook random verticaal worden geplaatst. Nu doet ie t alleen maar horizontaal. Het .py-bestand is ook geupload: http://efshare.com/?s=45KVBE Mocht er iets onduidelijk zijn hoor ik het graag en leg ik het uit


Code:
import string
import time
from random import randint

bord = []
bommen = 0

def maakBord():
    bord = [['.' for x in range(10)] for x in range(10)]
    return bord

def printBord(bord):
    i = 0
    for i in range(len(bord)):
        print(
            string.ascii_uppercase[i], bord[i][0], bord[i][1], bord[i][2], bord[i][3], bord[i][4], bord[i][5], bord[i][6],
            bord[i][7], bord[i][8], bord[i][9])
    print("  1 2 3 4 5 6 7 8 9 10")

def schepenPlaatsenCHEAT(bord):
    x = (randint(1, 10)-1)
    y = (randint(1, 10)-1)
    bord[x][y] = 'S'
    x = x + 1
    if x == 0:
        x = "A"
    elif x == 1:
        x = "B"
    elif x == 3:
        x = "C"
    elif x == 4:
        x = "D"
    elif x == 5:
        x = "E"
    elif x == 6:
        x = "F"
    elif x == 7:
        x = "G"
    elif x == 8:
        x = "H"
    elif x == 9:
        x = "I"
    elif x == 10:
        x = "J"
    print("Het schip ligt op het vakje: ", x,y + 1)
    return bord

def menu(speelbord, bord):
    correct = True
    while correct:
        print("- Menu Zeeslag - \nSingle Player (S) \nTwo Players   (T) \nQuit          (Q)")
        keuze = input("\nKies een optie (S, T of Q): ")
        if keuze.upper() == "S":
            singleplayer(speelbord, bord)
        elif keuze.upper() == "T":
            print("Under construction")
        elif keuze.upper() == "Q":
            correct = False
        elif keuze == "cheat":
            cheatcode(speelbord, bord)
        else:
            print("Je hebt een foute keuze gemaakt")

def singleplayer(speelBord, bord):
    bord = schepen()
    start = time.time()
    printBord(speelBord)
    behaald = False
    while not behaald:
        vindSchip = True
        for row in bord:
            if 'S' in row:
                vindSchip = False
        if vindSchip == True:
            bommenEind = bommen
            end = time.time()
            tijd = round(end - start)
            print ("Je hebt alle schepen geraakt")
            if bommenEind == 1:
                print("Je hebt", bommenEind, "bom gebruikt")
            elif bommenEind > 1:
                print("Je hebt", bommenEind, "bommen gebruikt")
            if round(tijd/60) > 1:
                print("Je hebt hiervoor", round(tijd/60), "minuten nodig gehad")
            if round(tijd/60) == 1:
                print("Je hebt hiervoor", round(tijd/60), "minuut nodig gehad")
            else:
                print("Je hebt hiervoor", round(tijd), "seconden nodig gehad")
            behaald = True
            input("Druk op enter om terug naar het menu te gaan")
        else:
            bom(speelBord, bord)

def bom(speelbord, bord):
    x = 0
    y = 0
    coordinaat = input("Geef de coördinaten voor de bom (vb: A5): ")
    coordinaat = coordinaat.upper()
    x = coordinaat[0]
    y = int(coordinaat[1:]) - 1
    while len(coordinaat) < 2 or len(coordinaat) > 4:
        print("Je hebt een foute invoer ingevoerd")
        coordinaat = input("Geef de coördinaten voor de bom (vb: A5): ")
        x = coordinaat[0]
        y = int(coordinaat[1:]) - 1
    while coordinaat == '':
        print("Je hebt een foute invoer ingevoerd")
        coordinaat = input("Geef de coördinaten voor de bom (vb: A5): ")
        x = coordinaat[0]
        y = int(coordinaat[1:]) - 1                                                                                                            
    while coordinaat[0] != "A" and coordinaat[0].upper() != "B" and coordinaat[0].upper() != "C" and coordinaat[0].upper() != "D" and coordinaat[0].upper() != "E" and coordinaat[0].upper() != "F" and coordinaat[0].upper() != "G" and coordinaat[0].upper() != "H" and coordinaat[0].upper() != "I" and coordinaat[0].upper() != "J":
        print("Je hebt een foute invoer ingevoerd")
        coordinaat = input("Geef de coördinaten voor de bom (vb: A5): ")
        x = coordinaat[0]
        y = int(coordinaat[1:]) - 1
    while int(coordinaat[1:]) > 10 or int(coordinaat[1:]) < 1:
        print("Je hebt een foute invoer ingevoerd")
        coordinaat = input("Geef de coördinaten voor de bom (vb: A5): ")
        x = coordinaat[0]
        y = int(coordinaat[1:]) - 1
    if x.upper() == "A":
        x = 0
    elif x.upper() == "B":
        x = 1
    elif x.upper() == "C":
        x = 2
    elif x.upper() == "D":
        x = 3
    elif x.upper() == "E":
        x = 4
    elif (x.upper() == "F"):
        x = 5
    elif (x.upper() == "G"):
        x = 6
    elif (x.upper() == "H"):
        x = 7
    elif (x.upper() == "I"):
        x = 8
    elif (x.upper() == "J"):
        x = 9
    else:
        print("Je hebt een foute invoer ingevoerd")
    y = int(coordinaat[1:]) - 1
    speelbord[x][y] = 'B'
    printBord(speelbord)
    controle(speelbord, bord, x, y)

def controle(speelbord, bord, x, y):
    antwoord = input("Weet je zeker dat je je bom hier wilt plaatsen? J/N: ")
    while antwoord.upper() != "J" and antwoord.upper() != "N":
        print("Je hebt een foute keuze gemaakt")
        antwoord = input("Weet je zeker dat je je bom hier wilt plaatsen? J/N: ")
    if antwoord.upper() == "J":
        global bommen
        print("Bom is geplaatst")
        bommen += 1
        shot = bord[x][y]
        if shot == 'S':
            print("Raak")
            speelbord[x][y] = "#"
            bord[x][y] = "#"
            printBord(speelbord)
        elif shot == '.':
            print("Mis")
            speelbord[x][y] = "-"
            bord[x][y] = '-'
            printBord(speelbord)
        elif shot == '#':
            print("Raak, maar hier heb je al eens geschoten")
            speelbord[x][y] = "#"
            bord[x][y] = "#"
            printBord(speelbord)
        elif shot == '-':
            print("Mis, maar hier heb je al eens geschoten")
            speelbord[x][y] = "-"
            bord[x][y] = "-"
            printBord(speelbord)
    elif antwoord.upper() == "N":
        print("Bom is niet geplaatst")
        speelbord[x][y] = '.'
        printBord(speelbord)   

def cheatcode(speelBord, bord):
    x = schepenPlaatsenCHEAT(bord)
    start = time.time()
    printBord(speelBord)
    behaald = False
    while not behaald:
        vindSchip = True
        for row in bord:
            if 'S' in row:
                vindSchip = False
        if vindSchip == True:
            bommenEind = bommen
            end = time.time()
            tijd = round(end - start)
            print ("Je hebt alle schepen geraakt")
            if bommenEind == 1:
                print("Je hebt", bommenEind, "bom gebruikt")
            elif bommenEind > 1:
                print("Je hebt", bommenEind, "bommen gebruikt")
            if round(tijd/60) > 1:
                print("Je hebt hiervoor", round(tijd/60), "minuten nodig gehad")
            if round(tijd/60) == 1:
                print("Je hebt hiervoor", round(tijd/60), "minuut nodig gehad")
            else:
                print("Je hebt hiervoor", round(tijd), "seconden nodig gehad")
            behaald = True
            input("Druk op enter om terug naar het menu te gaan")
        else:
            bom(speelBord, bord)

def vrijHor(bord, shipsLen, randRij, randKol, verschil):
    ok = True
    i = 0
    while i < shipsLen and ok:
        if bord[randRij][randKol + i * verschil] == '.':
            i = i + 1
        else:
            ok = False
    return ok

def vrijVer(bord, shipsLen, randRij, randKol, verschil):
    ok = True
    i = 0
    while i < shipsLen and ok:
        if bord[randRij][randKol + i * verschil] == '.':
            i = i + 1
        else:
            ok = False
    return ok

ships = [5, 4, 3, 3,2]

def schepen():
    bord = [['.' for x in range(10)] for y in range(10)]
    i = 0
    while i < len (ships):
        randRij = randint(0,9)
        randKol = randint(0,9)
        randRicht = randint(0,1)
        if randRicht == 0:
            verschil = +1
            if randKol + ships[i] > 9:
                verschil = -1
            if vrijHor(bord, ships[i], randRij, randKol,verschil):
                for j in range (0, ships[i]):
                    bord[randRij][randKol + verschil*j] = 'S'
                i = i + 1
        if randRicht == 1:
            verschil = +1
            if randKol + ships[i] > 9:
                verschil = -1
            if vrijVer(bord, ships[i], randRij, randKol, verschil):
                for j in range (0, ships[i]):
                    bord[randRij][randKol + verschil*j] = 'S'
                i = i + 1
    return bord

def main():
    speelbord = maakBord()
    geheimbord = maakBord()
    correct = True
    menu(speelbord, geheimbord)

main()
 
Laatst bewerkt:
Status
Niet open voor verdere reacties.

Nieuwste berichten

Terug
Bovenaan Onderaan