Hello guest, if you read this it means you are not registered. Click here to register in a few simple steps, you will enjoy all features of our Forum.
Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Python BattleShips Random Ship placement FnaticPutin Unladen Swallow Posts: 2 Threads: 2 Joined: Oct 2017 Reputation: 0 Likes received: 0 #1 Oct-12-2017, 07:35 AM (This post was last modified: Oct-12-2017, 07:50 AM by buran. Edited 2 times in total. Edit Reason: no tags ) Hello. I recently started to code a battleship game in python and after debugging, I wanted to see if I could write a piece of code that places the ship in a random location each time I run the game. I shall place the code below, and if anyone could help me it would be much appreciated. If anyone has any ideas on how to improve the general code. I'm following a set of challenges along with this in order to learn how to create games in python because it's part of my coursework for school. The ship seems to spawn at the same location every time which is 2,3. Can someone help? import random store = for x in range(9): store.append(random.randint (0, 8)) for y in range(9): store.append(random.randint (0, 8)) #Setup Global Variables------------------ torpedos = 30 theSea = [["_" for i in range(9)] for j in range(9)] #2D list 9x9 theSea[1][2] = "S" #Place the ship in position x,y ships = 1 #How many ships to find #Define procedures and functions -------------------------------- def displayGrid(): print() print(" 1 2 3 4 5 6 7 8 9 ") #display column headings for y in range(0,9): print((y+1), " " , end="") #print without new line for x in range(0,9): if (theSea[x] [y] =="S"): print("_", " " , end="") #hide the ship else: print(theSea[x][y] , " " , end="") #print contents of array print() #next line print() #begin main thread ------------------------------------------------- name = input("What is your name:") print("Welcome to BATTLESHIPS!" + name) print("An enemy ship is hidden somewhere at sea") print("You only have 10 torpedos to find it!") while ships > 0 and torpedos > 0: displayGrid() print() print("You have " +str(torpedos) + " torpedos left.") print("Target coordinates, Captain?") refx = int(input("Enter the x coordinate: ")) refy = int(input("Enter the y coordinate: ")) refx = refx -1 #computers count from 0 refy = refy -1 #this could be done with fewer lines if (theSea[refx][refy] == "S"): theSea[refx][refy] = "X" print(" DIRECT HIT, CAPTAIN!") ships = ships - 1 else: theSea[refx][refy] = "M" print("You missed...") torpedos = torpedos -1 input() #wait for user to press enter before redrawing grid if ships == 0: print("Huzah! You sunk all the ships.") else: print("You ran out of torpedos" + name) print("GAME OVER") buran wrote Oct-12-2017, 07:50 AM:Please, use proper tags when post code, traceback, output, etc. See BBcode help for more info. Repost with code in code tags. j.crater Posts: 583 Threads: 22 Joined: Sep 2016 Reputation: 26 Likes received: 85 #2 Oct-12-2017, 07:50 AM Hello and welcome! Your project looks nice and I am glad you shared it with us =) However, please, edit your post and put your code in Python code tags. That will make your code readable and make it easier for others to help you. You can find instructions here. Windspar Spam, spam, eggs, and spam Posts: 203 Threads: 11 Joined: Oct 2016 Reputation: 9 Likes received: 27 #3 Oct-13-2017, 11:33 PM (This post was last modified: Oct-13-2017, 11:34 PM by Windspar. Edited 1 time in total.) (Oct-12-2017, 07:35 AM)FnaticPutin Wrote: Hello. I recently started to code a battleship game in python and after debugging, I wanted to see if I could write a piece of code that places the ship in a random location each time I run the game. I shall place the code below, and if anyone could help me it would be much appreciated. If anyone has any ideas on how to improve the general code. I'm following a set of challenges along with this in order to learn how to create games in python because it's part of my coursework for school. The ship seems to spawn at the same location every time which is 2,3. Can someone help? import random store = for x in range(9): store.append(random.randint (0, 8)) for y in range(9): store.append(random.randint (0, 8)) #Setup Global Variables------------------ torpedos = 30 theSea = [["_" for i in range(9)] for j in range(9)] #2D list 9x9 theSea[1][2] = "S" #Place the ship in position x,y ships = 1 #How many ships to find #Define procedures and functions -------------------------------- def displayGrid(): print() print(" 1 2 3 4 5 6 7 8 9 ") #display column headings for y in range(0,9): print((y+1), " " , end="") #print without new line for x in range(0,9): if (theSea[x][y] =="S"): print("_", " " , end="") #hide the ship else: print(theSea[x][y] , " " , end="") #print contents of array print() #next line print() #begin main thread ------------------------------------------------- name = input("What is your name:") print("Welcome to BATTLESHIPS!" + name) print("An enemy ship is hidden somewhere at sea") print("You only have 10 torpedos to find it!") while ships > 0 and torpedos > 0: displayGrid() print() print("You have " +str(torpedos) + " torpedos left.") print("Target coordinates, Captain?") refx = int(input("Enter the x coordinate: ")) refy = int(input("Enter the y coordinate: ")) refx = refx -1 #computers count from 0 refy = refy -1 #this could be done with fewer lines if (theSea[refx][refy] == "S"): theSea[refx][refy] = "X" print(" DIRECT HIT, CAPTAIN!") ships = ships - 1 else: theSea[refx][refy] = "M" print("You missed...") torpedos = torpedos -1 input() #wait for user to press enter before redrawing grid if ships == 0: print("Huzah! You sunk all the ships.") else: print("You ran out of torpedos" + name) print("GAME OVER") This is your code. Not just copy and paste. The reason it always show up is because you told it to. `theSea[1][2] = "S" #Place the ship in position x,y` 99 percent of computer problems exists between chair and keyboard. « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post Battleships Program must display how many ships are left after each turn FnaticPutin 1 268 Oct-24-2017, 03:21 PM Last Post: Windspar

Forum Jump:

Users browsing this thread: 1 Guest(s)