如何保存棋盘游戏?Python [英] How to save board game? Python

查看:72
本文介绍了如何保存棋盘游戏?Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 python 对 Connect 4 进行了编程,虽然我不确定如何保存所有用户输入,但我想创建一个保存选项.这将主要是已放置在板上的所有计数器.棋盘是一个多维数组:

I have programmed Connect 4 using python, and I wanted to make a Save option although I'm unsure how I would save all of the users inputs. Which would be mainly all the counters that have been placed on the board. The board is a multidimensional array:

board = []
for row in range(6):
    board.append([])
    for column in range(7):
        board[row].append(0) 

有人知道我将如何存储这些数据吗?我正在考虑将其写入文件,并在打开保存时可以读取它,尽管我不确定如何将文本重新解释为板位置.

Would anyone have any idea how I'd store this data? I was thinking of writing it to a file and when opening the save it can read it, although I'm unsure how to reinterpret the text into board positions.

推荐答案

如果你只关心用 Python 阅读这个文件,那么 腌制吧!

If you only care about reading this file with Python, then pickle it!

import pickle

fp = '/path/to/saved/file.pkl'
with open(fp, 'wb') as f:
    pickle.dump(board, f)

这篇关于如何保存棋盘游戏?Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆