Untitled
unknown
plain_text
a month ago
2.1 kB
2
Indexable
import random computerShips = [] playerShips = [] computerWater = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] playerWater = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] while len(computerShips) < 3: pick = random.randint(0, 9) if pick not in computerShips: computerShips.append(pick) # Put your solution for creating the player's ships while len(playerShips) < 3: pick = int(input("Where would you like to place your ship at: ")) playerShips.append(pick) # Resume with the rest of the starter code while len(computerShips) > 0 and len(playerShips) > 0: print("Remaining player ships:", len(playerShips)) print("Remaining computer ships:", len(computerShips)) print("Computer water: ", computerWater) print("Player water: ", playerWater) while True: playerFire = int(input("Pick a target to shoot 0-9: ")) if playerFire in computerWater: computerWater[playerFire] = "X" break else: print("Invalid target. Try again") while True: computerFire = random.randint(0,9) if computerFire in playerWater: playerWater[computerFire] = "X" break print("You shoot at", playerFire) print("The computer shoots at", computerFire) #Put your if/else for whether a player got a hit/miss while len(computerShips) > 0 and len(playerShips) > 0: computerFire = random.randint(0,9) playerFire = playerFire.append(pick)(0,9) print("The computer shoots at: ", computerFire) print("The player shoots at: ", playerFire) if computerFire in playerShips: print("You've been hit!") playerShips.remove(computerFire) else: print("The computer missed!") if playerFire in computerShips: print("You been hit!") computerShips.remove(playerFire) else: print("The player missed") print("\n\n\n") print("end game") if len(computerShips) == len(playerShips): print("It's a tie!") elif len(playerShips) > len(computerShips): print("You win!") else: print("You lose!")
Editor is loading...
Leave a Comment