-1

I know there are a lot of these but i've been searching for an hour and nothing is working.

          ship2X=eg.passwordbox("Player " + str(playerNumber) + " input the x co-ordinate for your SECOND ship ")
          ship2Y=eg.passwordbox("Player " + str(playerNumber) + " input the y co-ordinate for your SECOND ship ")

          return[ship2X, ship2Y]

The above code is in a function.

def haveShot(playerNumber, ship, ship2, board):

    global ship2

    eg.msgbox("Player " + str(playerNumber) + " your shot")

    hit=False

    shotX=eg.enterbox("Enter the x-coordinate for your shot: ")
    shotY=eg.enterbox("Enter the y-coordinate for your shot: ")

.... error checking here....

if int(shotX) == int(ship[0]) and int(shotY) == int(ship[1]):
        board[5 - int(shotY)][int(shotX) - 1] = "X"
        eg.msgbox("Nice shot! You hit ship 1")
        hit = True

    elif int(shotX) == int(ship2[0]) and int(shotY) == int(ship2[1]):
        board[5 - int(shotY)][int(shotX) - 1] = "X"
        eg.msgbox("Nice shot! You hit ship 2")
        hit = True


    elif board[5 - int(shotY)][int(shotX) - 1] == "o":
        eg.msgbox("You already tried that shot! D'oh!")

    else:
        board[5 - int(shotY)][int(shotX) - 1] = "o"
        eg.msgbox("Unlucky - you missed!")

Ye, I have an if before that.

Then I have this near the end:

hit = False
winner = "0"
p1 = 0
p2 = 0

while hit == False:
    hit = haveShot("1", player2Ship, player2Ship, player1Board)

    if hit:
        p1 = p1+1

    hit = haveShot("2", player1Ship, player1Ship, player2Board)

    if hit:
        p2 = p2+2

I copied it from the first enter ship thing so I'm super confused as to why that's happening...

Any ideas?

If you want to see the full code you can see it at: http://pastebin.com/TAyHtnTs

The error I have is if I do enter the correct co-ordinate for the second ship it says I missed it, however if I enter the correct co-ordinates for the first ship it says I hit it like it shoul.

Thanks for the help you can provide :)

Shivam Paw
  • 11
  • 1
  • 4
  • 1
    Where have you defined `lol`? – khelwood Oct 26 '14 at 19:51
  • Doesn't the first code bit I posted define it or do I need to add something like `lol = ""`? – Shivam Paw Oct 26 '14 at 19:52
  • That link doesn't contain any of this code. You're going to need to show more if you want help. `lol` isn't defined anywhere in the stuff you've posted so far. – James Jenkinson Oct 26 '14 at 19:57
  • If you want to access the global variable `lol` from a function, you need to write `global lol` inside the function. – kyflare Oct 26 '14 at 19:59
  • Also just initialising with an empty string won't fix it, because it won't have a `0` or `1` index, and you'll receive an `IndexError`. – James Jenkinson Oct 26 '14 at 19:59
  • In the link I changed lol to ship2 – Shivam Paw Oct 26 '14 at 19:59
  • Please make minimal, complete example of your problem and edit your question (you probably figure out your issue doing this anyway). – m.wasowski Oct 26 '14 at 20:01
  • I have updated it with a new error after defining it. also new full code – Shivam Paw Oct 26 '14 at 20:09
  • Get rid of that global declaration, it's not needed if you've passed it in through the function. The global keyword lets you overwrite values stored in the global scope. This might be helpful for understanding scope rules: http://nbviewer.ipython.org/github/rasbt/python_reference/blob/master/tutorials/scope_resolution_legb_rule.ipynb – James Jenkinson Oct 26 '14 at 20:29
  • I posted my first answer before your edits, the new one matches that code you've posted. – James Jenkinson Oct 26 '14 at 20:55
  • Thanks james, I'll check this out when I get up tomorrow and vote on it ;) – Shivam Paw Oct 26 '14 at 21:54

2 Answers2

0

I don't have enough reputation to comment, or I would, but...

You haven't defined "lol", you have defined "lolX" and "lolY", which are separate variables.

If you want to define a list or a dictionary with values inside, you cannot just write lolX or lolY, you would have to do (I use a dictionary here since it seems to be what you want):

lol = {}

lol[X]=eg.passwordbox("Player " + str(playerNumber) + " input the x co-ordinate for your SECOND ship ")

Then you can access the value in lol by lol[X].

You have defined separate variables, rather than define a specific value of that variable.

Alex Huszagh
  • 13,272
  • 3
  • 39
  • 67
  • Hi @ShivamPaw, you are still defining new variables than adding values to a dictionary or list. Ship2X and Ship2Y are separate variables, you want something called Ship2 that stored values for [X] and [Y]. – Alex Huszagh Oct 26 '14 at 20:17
0

Couple of things:

Line 81

You can't have two subsequent return statements. The first one will exit the function. If you want to return two sets of coordinates return a nested list:

return [[x1, y1], [x2, y2]]

Line 161

Then use unpacking to get these:

p1ship1, p1ship2 = inputCoords("1")
p2ship1, p2ship2 = inputCoords("2")

Line 171/176

Make sure the two ships you're passing in are different (currently they're the same):

hit = haveShot("1", player2Ship, player2Ship, player1Board)

to

hit = haveShot("1", p2ship1, p2ship2, player1Board)

Line 170

The while hit == False condition means that the game will exit as soon as player 1's ship is hit. Use another variable to check if the game is over, such as:

while player1ShipCount > 0 and player2ShipCount > 0:
    #play game

And keep track of each players' available ships.

James Jenkinson
  • 1,563
  • 2
  • 16
  • 33
  • Seems valid so super thanks! In the script ship1 is just ship so I just need to replace that with your code ;) I'll test when i get up tomorrow and get back to ya :D Cheers! Maybe I could get some upvotes now that the question is clearer :) Also I'm not sure about your last point. Could I just do it by keeping count of the points and have an if statement outside of the loop to check if any of the players points is equal to 2 and then if it is tell them they won? – Shivam Paw Oct 26 '14 at 21:56
  • You don't _need_ to change it to ship. The variable `ship` is named by the function header, as soon as it enters the function it ceases to be called whatever it was outside of the function. You could pass in a variable named `badger` if you like. That said it helps to keep namings fairly consistent. – James Jenkinson Oct 27 '14 at 10:12
  • And yeah, that would be a valid way to exit the game loop. – James Jenkinson Oct 27 '14 at 10:25