Home > Enterprise >  Define a function that reads a position from a file and returns an object representing that particul
Define a function that reads a position from a file and returns an object representing that particul

Time:12-31

I don't know how to create a function that takes as an argument the name of a text file, reads a position from that file and returns an object representing the position where the file must contain one line for each row of the board, using the characters ".", "@", and "O" as above to represent the colours.

def load_board(filename):

and also to create a function save_board(filename, board) that takes as arguments a filename and a Board object and saves the position to a text file in the same format as that accepted by load_board().

so that

b = load_board("l19.txt")
print(b)
save_board("l19b.txt", b)

reads a file consisting of:

v     ....@@@[email protected]
@OO@O@..@O@O...@@.@
@[email protected]@
..@@[email protected]@[email protected].
O.@[email protected]@.OO@@O..O@O
O...OOO@.@@[email protected]@@
..@O@[email protected].@.@..
..@@.@@@[email protected]...@
@O..@.@@@@O..@OOO@O
@..O.@@O@OO@@[email protected]@.
@OOO..@O..@@O@@@.OO
@@[email protected][email protected]@.@@@.@
@[email protected].@O@..@O
@...O@@OO@.@@..O.O.
OO@@..O@@.@[email protected]@@O.
@.O..OO.@O@O@OO.@@.
@@[email protected][email protected]@.@O@O..
...OO@@[email protected].@OO@.
@@.@O.@@..@OOOOO@@@

which gives an output of

   A B C D E F G H I J K L M N O P Q R S
19 . . . . @ @ @ . O O . . @ . O O O . O
18 @ O O @ O @ . . @ O @ O . . . @ @ . @
17 @ O . . @ O . O O O O O . O O O O O @
16 . . @ @ . O O . @ . . O @ O . @ . O .
15 O . @ . @ . O @ . O O @ @ O . . O @ O
14 O . . . O O O @ . @ @ . . . @ . O @ @
13 . . @ O @ . . @ . . O O O . @ . @ . .
12 . . @ @ . @ @ @ . . . @ O . O . . . @
11 @ O . . @ . @ @ @ @ O . . @ O O O @ O
10 @ . . O . @ @ O @ O O @ @ . @ . O @ .
 9 @ O O O . . @ O . . @ @ O @ @ @ . O O
 8 @ @ O @ . O O O . @ . O @ . @ @ @ . @
 7 @ . O . O @ O O . O O . @ O @ . . @ O
 6 @ . . . O @ @ O O @ . @ @ . . O . O .
 5 O O @ @ . . O @ @ . @ . @ . O @ @ O .
 4 @ . O . . O O . @ O @ O @ O O . @ @ .
 3 @ @ O O @ . O . @ . O @ . @ O @ O . .
 2 . . . O O @ @ O . @ O . O . @ O O @ .
 1 @ @ . @ O . @ @ . . @ O O O O O @ @ @

and save_board should create a file identical to this grid which was the file that was used to get the above output

CodePudding user response:

load_board function:

def load_board(filename):
    with open(filename, 'r') as board_file:
        board = board_file.read()
        # You can also use board_file.readlines() to return a list containing each line from the file
    return board

save_board function

def save_board(filename, board):
    with open(filename, 'w') as board_file:
        board_file.write(board)

If you want to know more information on how this works, search up python's built-in "open" function

Edit: Formatting the board will be a little more complicated. To start I would change the load_board function to return the lines of the file instead of the raw text Then make a new function that takes in the lines from load_board and either prints or returns a formatted version of the board.

CodePudding user response:

Give it a try yourself first;

def load_board(filename):
    result = "  "
    with open(filename) as f:
        print(f)
        for index, line in enumerate(f):
            if index == 0:
                result  = ' '.join([chr(alphabets   65) for alphabets in range(len(line) - 1)])   '\n'
            result  = f"{index 1}"
            for characters in line:
                result  = " "   characters
            result  = "\n"
        
        return result

def save_board(filename, data):
    with open(filename, "wt") as f:
        f.write(data)

b = load_board("l19.txt")
print(b)
save_board("l19b.txt", b)
  • Related