Aug-10-2021, 10:47 AM
I'm trying to make battle ships in python and for program works for the most part apart from supposedly randomly when the computer is generating coordinates to attack the player and misses instead of creating a coordinate it creates a number.
The coordinate generator works by having a list ["a","b","c","d","e","f","g","h"] then generating a random number for the row (0,7) which is then used to take a letter from the list e.g. if the random number is 2 it would take c from the list. Then it would generate a second number for the column (1,8) then put them together to create a coordinate. e.g. c5. This works a lot of the time but at some points instead of getting c5 it would give 25 and i'm not sure why as it doesn't do it all the time.
The coordinate generator works by having a list ["a","b","c","d","e","f","g","h"] then generating a random number for the row (0,7) which is then used to take a letter from the list e.g. if the random number is 2 it would take c from the list. Then it would generate a second number for the column (1,8) then put them together to create a coordinate. e.g. c5. This works a lot of the time but at some points instead of getting c5 it would give 25 and i'm not sure why as it doesn't do it all the time.
import sys import random import time # GUI print("Welcome to battle boats\n1. New game\n2. Continue game\n3. Quit") startMenuInput = input("Enter 1, 2 or 3 to make your choice: ") ## Subroutines here def printGrid(nums,lines,a,b,c,d,e,f,g,h): print(*nums, sep = '') print(*lines, sep = '') print(*a, sep='') print(*b, sep='') print(*c, sep='') print(*d, sep='') print(*e, sep='') print(*f, sep='') print(*g, sep='') print(*h, sep='') print("\n") def printCOMGrid(COMnums,COMlines,COMa,COMb,COMc,COMd,COMe,COMf,COMg,COMh): print(*COMnums, sep = '') print(*COMlines, sep = '') print(*COMa, sep='') print(*COMb, sep='') print(*COMc, sep='') print(*COMd, sep='') print(*COMe, sep='') print(*COMf, sep='') print(*COMg, sep='') print(*COMh, sep='') print("\n") def printTargetTracker(TTnums,TTlines,TTa,TTb,TTc,TTd,TTe,TTf,TTg,TTh): print(*TTnums, sep = '') print(*TTlines, sep = '') print(*TTa, sep='') print(*TTb, sep='') print(*TTc, sep='') print(*TTd, sep='') print(*TTe, sep='') print(*TTf, sep='') print(*TTg, sep='') print(*TTh, sep='') print("\n") def newGame(): enteredCoordinates = [] COMenteredCoorindates = [] COMattackCoordinates = [] nums = [" "," 1 "," 2 "," 3 "," 4 "," 5 "," 6 "," 7 "," 8 "] lines = [" ---------------------------------"] a = ["a| ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] "] b = ["b| ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] "] c = ["c| ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] "] d = ["d| ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] "] e = ["e| ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] "] f = ["f| ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] "] g = ["g| ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] "] h = ["h| ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] "] COMnums = [" "," 1 "," 2 "," 3 "," 4 "," 5 "," 6 "," 7 "," 8 "] COMlines = [" ---------------------------------"] COMa = ["a| ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] "] COMb = ["b| ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] "] COMc = ["c| ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] "] COMd = ["d| ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] "] COMe = ["e| ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] "] COMf = ["f| ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] "] COMg = ["g| ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] "] COMh = ["h| ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] "] TTnums = [" "," 1 "," 2 "," 3 "," 4 "," 5 "," 6 "," 7 "," 8 "] TTlines = [" ---------------------------------"] TTa = ["a| ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] "] TTb = ["b| ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] "] TTc = ["c| ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] "] TTd = ["d| ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] "] TTe = ["e| ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] "] TTf = ["f| ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] "] TTg = ["g| ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] "] TTh = ["h| ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] ","[ ] "] for i in range (5): alreadyEntered = True placementCoordinate = input("Enter the coordinates of your boat: ") print("\n") while alreadyEntered == True: if placementCoordinate in enteredCoordinates: alreadyEntered = True placementCoordinate = input("Coordinate already entered, enter another coordinate: ") else: alreadyEntered = False if alreadyEntered == False: enteredCoordinates.append(placementCoordinate) row = placementCoordinate[0] column = placementCoordinate[1] column = int(column) if row == "a": a.pop(column) a.insert(column,"[*] ") if row == "b": b.pop(column) b.insert(column,"[*] ") if row == "c": c.pop(column) c.insert(column,"[*] ") if row == "d": d.pop(column) d.insert(column,"[*] ") if row == "e": e.pop(column) e.insert(column,"[*] ") if row == "f": f.pop(column) f.insert(column,"[*] ") if row == "g": g.pop(column) g.insert(column,"[*] ") if row == "h": h.pop(column) h.insert(column,"[*] ") printGrid(nums,lines,a,b,c,d,e,f,g,h) cRowsList = ["a","b","c","d","e","f","g","h"] for i in range (5): alreadyEntered = True randRow = random.randint(0,7) randCol = random.randint(1,8) cRow = cRowsList[randRow] stringCol = str(randCol) COMcoordinate = cRow + stringCol while alreadyEntered == True: if COMcoordinate in COMenteredCoorindates: alreadyEntered = True randRow = random.randint(0,7) randCol = random.randint(1,8) cRow = cRowsList[randRow] stringCol = str(randCol) COMcoordinate = cRow + stringCol else: alreadyEntered = False if alreadyEntered == False: COMenteredCoorindates.append(COMcoordinate) if cRow == "a": COMa.pop(randCol) COMa.insert(randCol,"[*] ") if cRow == "b": COMb.pop(randCol) COMb.insert(randCol,"[*] ") if cRow == "c": COMc.pop(randCol) COMc.insert(randCol,"[*] ") if cRow == "d": COMd.pop(randCol) COMd.insert(randCol,"[*] ") if cRow == "e": COMe.pop(randCol) COMe.insert(randCol,"[*] ") if cRow == "f": COMf.pop(randCol) COMf.insert(randCol,"[*] ") if cRow == "g": COMg.pop(randCol) COMg.insert(randCol,"[*] ") if cRow == "h": COMh.pop(randCol) COMh.insert(randCol,"[*] ") print("All your coordinates and the computers coordinates have been selected. This is your target tracker:\n") printTargetTracker(TTnums,TTlines,TTa,TTb,TTc,TTd,TTe,TTf,TTg,TTh) print("You can use it to track where you have attacked and if the have hit or missed enemy boats.\n") gameWon = False printCOMGrid(COMnums,COMlines,COMa,COMb,COMc,COMd,COMe,COMf,COMg,COMh) while gameWon == False: #Player attack playerScore = 0 comScore = 0 attack = input("Enter the coordinates of where you want to attack:\n ") #Player hit computer if attack in COMenteredCoorindates: print("HIT!\n") row = attack[0] column = attack[1] column = int(column) if row == "a": TTa.pop(column) TTa.insert(column,"[H] ") if row == "b": TTb.pop(column) TTb.insert(column,"[H] ") if row == "c": TTc.pop(column) TTc.insert(column,"[H] ") if row == "d": TTd.pop(column) TTd.insert(column,"[H] ") if row == "e": TTe.pop(column) TTe.insert(column,"[H] ") if row == "f": TTf.pop(column) TTf.insert(column,"[H] ") if row == "g": TTg.pop(column) TTg.insert(column,"[H] ") if row == "h": TTh.pop(column) TTh.insert(column,"[H] ") printTargetTracker(TTnums,TTlines,TTa,TTb,TTc,TTd,TTe,TTf,TTg,TTh) playerScore = playerScore + 1 if playerScore == 5: gameWon = True else: #Player miss computer print("MISS!\n") row = attack[0] column = attack[1] column = int(column) if row == "a": TTa.pop(column) TTa.insert(column,"[M] ") if row == "b": TTb.pop(column) TTb.insert(column,"[M] ") if row == "c": TTc.pop(column) TTc.insert(column,"[M] ") if row == "d": TTd.pop(column) TTd.insert(column,"[M] ") if row == "e": TTe.pop(column) TTe.insert(column,"[M] ") if row == "f": TTf.pop(column) TTf.insert(column,"[M] ") if row == "g": TTg.pop(column) TTg.insert(column,"[M] ") if row == "h": TTh.pop(column) TTh.insert(column,"[M] ") printTargetTracker(TTnums,TTlines,TTa,TTb,TTc,TTd,TTe,TTf,TTg,TTh) #Computer Attack time.sleep(3) alreadyEntered = True randRow = random.randint(0,7) randCol = random.randint(1,8) cRow = cRowsList[randRow] stringCol = str(randCol) COMcoordinate = cRow + stringCol while alreadyEntered == True: if COMcoordinate in COMattackCoordinates: alreadyEntered = True randRow = random.randint(0,7) randCol = random.randint(1,8) cRow = cRowsList[randRow] stringCol = str(randCol) COMcoordinate = randRow + randCol else: alreadyEntered = False if alreadyEntered == False: print("Computer attacks", COMcoordinate) COMattackCoordinates.append(COMcoordinate) #Computer hit player if COMcoordinate in enteredCoordinates: print(COMcoordinate,"\n") print("HIT!\n") row = COMcoordinate[0] column = COMcoordinate[1] column = int(column) if row == "a": a.pop(column) a.insert(column,"[H] ") if row == "b": b.pop(column) b.insert(column,"[H] ") if row == "c": c.pop(column) c.insert(column,"[H] ") if row == "d": d.pop(column) d.insert(column,"[H] ") if row == "e": e.pop(column) e.insert(column,"[H] ") if row == "f": f.pop(column) f.insert(column,"[H] ") if row == "g": g.pop(column) g.insert(column,"[H] ") if row == "h": h.pop(column) h.insert(column,"[H] ") printGrid(nums,lines,a,b,c,d,e,f,g,h) else: #Computer miss player print(COMcoordinate,"\n") print("MISS!\n") row = COMcoordinate[0] column = COMcoordinate[1] column = int(column) if row == "a": a.pop(column) a.insert(column,"[M] ") if row == "b": b.pop(column) b.insert(column,"[M] ") if row == "c": c.pop(column) c.insert(column,"[M] ") if row == "d": d.pop(column) d.insert(column,"[M] ") if row == "e": e.pop(column) e.insert(column,"[M] ") if row == "f": f.pop(column) f.insert(column,"[M] ") if row == "g": g.pop(column) g.insert(column,"[M] ") if row == "h": h.pop(column) h.insert(column,"[M] ") printGrid(nums,lines,a,b,c,d,e,f,g,h) ## Main program from here if startMenuInput == "1": print("\nNew game") newGame() if startMenuInput == "2": print("\nContinue game") if startMenuInput == "3": sys.exit(0)