Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8331021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:17:47+00:00 2026-06-09T02:17:47+00:00

right now I’m trying to make the battleship board game as practice, and I’ve

  • 0

right now I’m trying to make the battleship board game as practice, and I’ve been using classes for the most part with a few misc standalone functions. Everything below is a standalone. I can’t wrap my mind around it, this works if I attack ships in order from last to first in the list, but if you go in any other order it breaks saying the list index doesn’t exist. Please help.

Basically my system is whenever a ship is “hit” (the move is in the same position as one in the shipList) then it adds a hitList. What these functions is check if any of the items in the hitList against the known positions of the ships…created in a separate list when ship object is made. i been trying to make this work for 2 days

def checkForSunk(shipList, hitList):

    #Lists is something like this [[0,1],[0,2],[0,3]]
    #ShipList[0] is list, [1] is name of ship
    #ShipList is ALL ships.

    print 'SHIPLIST : %s' % (shipList)
    #[[[[0,1],[0,2],[0,3],[0,4],[0,5],[],[]], 'Destroyer'], [[[0,1],[0,2],[0,3],[0,4],[0,5],[],[]], 'Destroyer'], [[[0,1],[0,2],[0,3],[0,4],[0,5],[],[]], 'Destroyer']]

    #[[[0,1],[0,2],[0,3],[0,4],[0,5],[],[]], 'Destroyer']
    #   0                                       1
    print 'HITLIST : %s ' % (hitList)
    for j in range(len(shipList)):
        for i in shipList[j][0]:
            if i in hitList:
                print 'True in ship # %s' % (shipList[j][1])
                del shipList[j][0][shipList[j][0].index(i)] #Delete that part of the ship from the list.
    #Check if there's any empty ships, and delete the ship if there are.
    for j in range(len(shipList)):
        print shipList[j] #Problem around here!!!!!!!!!!!!
        if listIsEmpty(shipList[j][0]):
            print '%s has been sunk!' % (shipList[j][1])
            del shipList[j]


def isGameOver(shiplist):
    if shiplist == []:
        return True
    else:
        return False

def listIsEmpty(list):
    for i in range(len(list)):
        if list[i] != []: #If it finds anything thats not empty, return False. Else true
            return False
        else:
            return True

Am I going about it all wrong? Should I be physically deleting lists?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T02:17:48+00:00Added an answer on June 9, 2026 at 2:17 am

    The answer is the same as for issue Delete item in a list using a for-loop :

    Iterate backwards:

    for j in range(len(shipList) - 1, -1, -1):
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right now I'm learning Opencart, and I'm trying to make my latest product display
Right now I am using sqlite within a ios application, and I want to
Right now I'm using the jQuery selector $('foo+bar') . Since I already have $('foo')
Right now I am only trying to get my getline() function to work. I
Right now I am using learning to program for the iphone and I am
Right now I've been maintaining a second sandbox project where I test concepts and
Right now I'm using the following command to compile a coffeescript file each time
Right now I'm using OleDbDataAdapter objDataReader = new OleDbDataAdapter(); objDataReader.SelectCommand = myCommand; objDataReader.Fill(myDataTable); But
Right now, to be sure I am getting what I want I am using
Right now, I have three models Post, Comment and User (using Devise ) associated

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.