Generate a 4-by-4 nested list with random floats between 10 and 20 inclusive, and print in grid format.
Use methods from the random module to generate random ships for your Sonar Detector game.
Nested for loops – outer loop for each ship, inner loop for each coordinate
Use either random.randint() or random.randrange() or use random.sample() to get ship coordinates.
Append each ship to ships nested list.
Resources:
Class slides: https://docs.google.com/presentation/d/1KN5-PN8u_sUuDpeVgMvbClVLuG3xPmktJl5sKQtfCUA/edit#slide=id.g17a24fb764f_0_116
Class code: https://replit.com/@ShravyaS/IntroToPython-Class8
Completed game: https://replit.com/@ShravyaS/Sonar-Detector#main.py
Contact Info:
shravyassathish@gmail.com (mentor)
kingericwu@gmail.com (TA)
felixguo41@gmail.com (TA)
import random
grid = []
for i in range(4):
row = []
for j in range(4):
row.append(random.uniform(10, 20))
grid.append(row)
for k in range(4):
for l in range(4):
print(grid[k][l], end = " ")
print()
grid = []
for k in range(10):
row = []
for l in range(10):
row.append(0)
grid.append(row)
def printGrid(grid):
print("\t", end="")
for m in range(10):
print(m, end=" ")
print("\n")
rowNum = 0
for list in grid:
print(str(rowNum), end="\t")
for num in list:
print(num, end=" ")
print()
rowNum += 1
printGrid(grid)
turns = 10
chests = []
for n in range(3):
coords = [random.randint(0, 9), random.randint(0, 9)]
while coords in chests:
for o in range(2):
coords.append(random.randint(0, 9))
chests.append(coords)
print(chests)
chestFound = 0
pastGuesses = []
while turns > 0:
guess = ""
while guess == "" or guess in pastGuesses:
guess = input("Make a guess, in format of x, y please: ").split(", ")
for i in range(2):
guess[i] = int(guess[i])
if guess in pastGuesses:
print("Duplicate guess, try again")
pastGuesses.append(guess)
turns -= 1
if guess in chests:
input("You found a chest! Congratulations!! :D Press any key to continue ")
chestFound += 1
grid[guess[1]][guess[0]] = "!"
else:
chestDistance = []
for chest in chests:
distance = ((chest[0] - guess[0])**2 + (chest[1] - guess[1])**2)**(1/2)
chestDistance.append(distance)
smallestDistance = int(min(chestDistance) + 0.5)
grid[guess[1]][guess[0]] = smallestDistance
printGrid(grid)
if chestFound == 3:
print("You found all the chests!! BD")
break
if turns == 0:
print("You ran out of turns. You lost D:")
print("Locations of the chests:")
for chest in chests:
print(chest)
import random
mylist = []
#print(mylist)
for i in range(4):
mylist.append([])
for j in range(4):
myrand = 10 + 10* random.random()
mylist[i].append(myrand)
print(mylist)
#1
import random
list = []
for y in range(4):
list.append([])
for x in range(4):
list[y].append(random.uniform(10, 20))
for index in range(4):
sublist = list[index]
for item in sublist:
print(item, end = '\t')
print('\n')
#2
import random
ships = []
used = []
for i in range(10):
coords = [random.randint(0, 9), random.randint(0, 9)]
while coords in used:
for j in range(2):
coord = random.randint(0, 9)
coords.append(coord)
used.append(coords)
ships.append(coords)
print(ships)
1.
import random
l = []
for i in range(0, 4):
x = []
for j in range(0, 4):
x.append(random.uniform(0.0, 1.0) * 10 + 10)
l.append(x)
for i in range(0, 4):
for j in range(0, 4):
print(f"{l[i][j]} ", end="")
print("\n")
2.
import random
number = 30 + random.randint(0, 21)
ship_pos = []
for i in range(0, number):
pos = []
for j in range(0, 3):
pos.append(random.randint(-5000, 5000))
ship_pos.append(pos)
i = 1
for ship in ship_pos:
print(f"ship {i} x: {ship[0]}, y: {ship[1]} z: {ship[2]}")
i = i + 1
grid = []
for i in range (5):
row= []
for j in range (5):
row.append(0)
grid.append(row)
def printGrid(grid):
colNum = [i for i in range(5)]
print ("\t\t", end = "")
for i in colNum:
print(i,end = "\t")
print ("\n\n")
rowNum= 0
for list in grid:
print (str(rowNum), end= "\t\t")
for num in list:
print (num, end="\t")
print ("\n")
rowNum += 1
printGrid(grid)
turns = 8
chests = [[0,1],[2,4]]
chestsFound = 0
pastGuesses = []
while turns > 0:
guess =""
while guess== "" or guess in pastGuesses:
guess = input("Make a guess:").split()
for i in range (2):
guess[i] = int(guess[i])
if guess in pastGuesses:
print ("Duplicate guess, try again.")
pastGuesses.append(guess)
turns -= 1
if guess in chests:
input("Chest has been found!")
chestsFound += 1
grid[guess[1]][guess[0]] = "!"
else:
chestDistance = []
for chest in chests:
distance = ((chest[0] - guess[0])**2 + (chest[1]- guess[1])**2)**(1/2)
chestDistance.append(distance)
smallestDistance=min(chestDistance)
smallestDistance= int(smallestDistance + 0.5)
grid[guess[1]][guess[0]] = smallestDistance
print("\033[0;0H\033[2J")
printGrid (grid)
if chestsFound == 2:
print ("All chests have been found. Congratulations, you win!")
break
print("\033[0;0H\033[2J")
printGrid (grid)
if turns == 0:
print("You are out of turns")
print("Location of the chests:")
for chest in chests:
print(chest)