Python Treasure Hunt Project - Coordinates Not Correct


#1



I have no error messages, but I don't think the coordinates for the user's starting positions are correct. If the user asks to stay in the same position on the grid (by inputting 0 and 0), the programme reveals that the spot is out of the ocean, and I have tried various starting numbers but this is still a problem. I would like the user to start on the bottom left "O" on the grid (which should then be replaced with an "X").

Eventually landing on a treasure chest will earn you 10 coins, landing on a bandit steals all your coins etc but I haven't even started that bit yet!
Here's my code for an 8x8 grid:


# Creating the Board #
Move = 0
from random import randint

board = []

for x in range(1, 8):
    board.append(["O"] * 8)

def print_board(board):
    for row in board:
        print( " ".join(row))

print_board(board)

def random_row(board):
    return randint(0, len(board) - 1)

def random_col(board):
    return randint(0, len(board[0]) - 1)

Chest1_Row = random_row(board)
Chest1_Col = random_col(board)
board[Chest1_Row][Chest1_Col] = "T"

Chest2_Row = random_row(board)
Chest2_Col = random_col(board)
board[Chest2_Row][Chest2_Col] = "T"

Chest3_Row = random_row(board)
Chest3_Col = random_col(board)
board[Chest3_Row][Chest3_Col] = "T"

Chest4_Row = random_row(board)
Chest4_Col = random_col(board)
board[Chest4_Row][Chest4_Col] = "T"

Chest5_Row = random_row(board)
Chest5_Col = random_col(board)
board[Chest5_Row][Chest5_Col] = "T"

Chest6_Row = random_row(board)
Chest6_Col = random_col(board)
board[Chest6_Row][Chest6_Col] = "T"

Chest7_Row = random_row(board)
Chest7_Col = random_col(board)
board[Chest7_Row][Chest7_Col] = "T"

Chest8_Row = random_row(board)
Chest8_Col = random_col(board)
board[Chest8_Row][Chest8_Col] = "T"

Chest9_Row = random_row(board)
Chest9_Col = random_col(board)
board[Chest9_Row][Chest9_Col] = "T"

Chest10_Row = random_row(board)
Chest10_Col = random_col(board)
board[Chest10_Row][Chest10_Col] = "T"

Player_Row = 0
Player_Col = 1
while 1:
    board[Player_Row][Player_Col] = "O"

    Player_Row_Add =(int(input("How many squares up do you want to go?")))
    Player_Col_Add = Player_Col + (int(input("How many squares along do you want to go?")))

    if (Player_Row + Player_Row_Add) < 1 or (Player_Row_Add) > 8 or (Player_Col_Add) < 1 or (Player_Col_Add) > 8:
            print ("Oops, that's not even in the ocean.")
    else:
        Player_Row = Player_Row + Player_Row_Add
        Player_Col = Player_Col + Player_Col_Add
        board[Player_Row][Player_Col] = "X"
    Move = Move + 1
    print_board(board)


#2

so here:

for x in range(1, 8):
    board.append(["O"] * 8)

you create a 7 by 8 board, that is what you want?

the stop value of range is not included so currently you have: 1, 2, 3, 4, 5 , 6 , 7. and as you can count, this is 7, not 8

if you have a 8 by 8 board you have the following indexes (both for x and y)

0, 1, 2, 3, 4, 5, 6, 7

depending on what you want, keep in mind that lists are zero indexed based!


#3

Thanks for your response, I've now changed the code to:

for x in range(1, 9):
    board.append(["O"] * 8)

Which has now created an (actual) 8x8 grid :slight_smile:
I also changed the starting player row and column values to

Player_Row = 1 Player_Col = 1

But I'm not sure if that's right because if you ask to stay in your starting position, the X is nowhere near the lefthand corner!


#4

why not:

for x in range(8):
    board.append(["O"] * 8)

so much simpler.

Can you post an updated version of your code? Then i will see what i can find


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.