Hey,
I try of saving my board to a text file and then trying to load my board from the text file but actually does not works.

If anyone knows how this can be solve let me know !

Thanks

Dani AI

Generated

Two practical, low‑risk ways to save/load a Connect Four board, plus a short debug checklist you can apply right away. This builds on ’s plain‑text idea and ’s pickling suggestion but adds safer, more portable options and a few common gotchas.

Use JSON (recommended for portability and readability)

import json

def save_board_json(board, filename):
    payload = {'rows': len(board), 'cols': len(board[0]) if board else 0, 'board': board}
    with open(filename, 'w', encoding='utf-8') as f:
        json.dump(payload, f)

def load_board_json(filename):
    with open(filename, 'r', encoding='utf-8') as f:
        return json.load(f)['board']

JSON preserves a list-of-lists (rows) structure, is language-agnostic, and easy to inspect if something goes wrong.

Plain text with an explicit header (minimal, human readable)

def save_board_text(board, filename):
    rows = len(board)
    cols = len(board[0]) if rows else 0
    with open(filename, 'w', encoding='utf-8') as f:
        f.write(f"{rows} {cols}\n")
        for row in board:
            f.write(''.join(row) + "\n")

def load_board_text(filename):
    with open(filename, 'r', encoding='utf-8') as f:
        rows, cols = map(int, f.readline().split())
        return [list(f.readline().strip()) for _ in range(rows)]

The header eliminates ambiguity about orientation (rows vs columns). If your internal representation stores columns as stacks, serialize columns instead and document that in the file.

Quick checklist and cautions

  • Validate after load (check dims, allowed symbols like 'r','y','s') and run a round‑trip test (save then load and assert equality).
  • If you use pickle: open files in binary mode on modern Python (and never unpickle data from untrusted sources).
  • If “it does not work” for you: print the saved file to confirm its contents, confirm the board shape you expect, and catch exceptions around load to see parsing errors.

These steps should make save/load deterministic and help find the specific mismatch causing failures for .

Recommended Answers

All 2 Replies

just write it to a text file so that the text file looks something like this:

sssssssss
sssssssss
rryrssyry
yyyryryyr
yryrryryr

where s is an unoccupied slot and r represents a red and y represents a yellow..

this way you can reconstruct the board giving each slot of the board a specific state or occupier.

It is quite easy to parse the string(s) using basic string manipulation.

Hi eleonora,

It's also easy to use the pickle module. Pickle dumps objects into a data file and reloads them without you having to worry about parsing text. So, if you have a variable named "board" and you want to dump it to a file called "board.dat" you can just do:

import pickle
board = dict()
f = open("board.dat", "w")
pickle.dump(board, f)
f.close()

And if you want to retrieve it, you can do:

import pickle
f = open("board.dat", "r")
board = pickle.load(f)
f.close()

Hope this helps!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.