如何将此2d数组的内容保存到文件中 [英] How do I save the contents of this 2d array to a file

查看:96
本文介绍了如何将此2d数组的内容保存到文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助,试图将2d数组的内容保存到文件中。
首先我不知道什么类型的文件应该是.txt或dat。

I Need some help trying to save the contents of the 2d array into a file. First of all im not sure what type the file should be etc .txt or dat.

我已经编辑了这个帖子,以便代码是文本格式不是一个图像。

I have edited the post so that the code is in text format not an image.

这是迄今为止的一切。

program CaptureTheSarum;

{$APPTYPE CONSOLE}

uses
  SysUtils;

Const BoardDimension = 8;

Type
  TBoard = Array[1..BoardDimension, 1..BoardDimension] Of String;

Var
  Board : TBoard;
  GameOver : Boolean;
  StartSquare : Integer;
  FinishSquare : Integer;
  StartRank : Integer;
  StartFile : Integer;
  FinishRank : Integer;
  FinishFile : Integer;
  MoveIsLegal : Boolean;
  PlayAgain : Char;
  SampleGame : Char;
  WhoseTurn : Char;
  savedFile : text;

procedure InitialiseSave;
var
  fileName : string;
begin
  fileName := 'SavedGame.dat';

  assignfile(savedfile,fileName);
  if not fileexists(fileName)
  then
    begin
      rewrite(savedfile);
      closefile(savedfile)
    end
  {endif};
end;

procedure saveGame;
var
 save : string;
 RankNo,FileNo : integer;
begin
  writeln('Would you like to save the Game?');
  readln(save);

  if (save = 'y') or (save = 'Y')
  then
    begin
      reset(SavedFile);
      write(SavedFile,board[fileno,Rankno]);
      closeFile(SavedFile);
    end
  {endif};


推荐答案

似乎你正在尝试做一些板子游戏(大概是国际象棋)

It seems you are trying to make some sort of board game (probably chess).

您所面临的主要问题是您没有将您的主板类型定义为固定大小。你在Delphi中看到的字符串是动态大小的。而在较旧版本的Delphi中,它们在较新版本中限制为255个字符,其大小仅受可用内存的限制。

The main problem you are facing is that you haven't defined your board type as fixed size. You see in Delphi strings are of dynamic size. And while in older versions of Delphi they were limited to 255 characters in newer versions their size is only limited by available memory.

所以您应该更改您的电路板定义(数组)是固定类型。对于大多数棋盘游戏,您可以使用二维数组的Char。

So you should change your board definition (array) to be of fixed type. For most board games you could use 2D array of Char.

TBoard = Array [0..7, 0..7] of Char;

在较旧的非Unicode版本的Delphi Char中将是一个AnsiChar,它允许您存储256个不同的字符或256个不同的数字。

On older non-Unicode versions of Delphi Char will be an AnsiChar which allows you to store 256 different characters or 256 different figures.

在更新的支持Unicode的Delphi版本中,您有更多的可能性。

On newer Delphi versions that support Unicode you have even more possibilities.

无论如何使用固定类型的静态数组的最佳方法是,您可以使用单个命令将整个静态数组保存到文件中。

Anyway the best thing about using static array of fixed type is that you can save the whole static array into a file with a single command

procedure SaveGame;
//When having fixed size types you can use File of Type to quickly get
//ability to store whole type at once. 
//Note this only works for fixed sized records who don't contain any 
//dynamic sized members (strings, dynamic arrays) and static arrays of
//fixed sized type (no strings or other dynamic sized arrays)
//
//With arrays it doesn't even matter whether they are one dimensional 
//or multidimensional. but they need to be static
var Savefile: File of TBoard;
    FileName: String;
begin
  Filename := 'D:\Proba.txt';
  //Assign file
  Assignfile(Savefile,FileName);
  //Check if the file exists if it does open it for editing (reser)
  //else open it in rewrite mode which also automatically creates new 
  //file if the file doesn't exists
  if not Fileexists(Filename) then
    Rewrite(Savefile)
  else
    Reset(SaveFile);

  //Becouse we have a file of fixed sized type we can write the whole 
  //type with just one Write command 
  //your program already know how many bytes it has to write
  //
  //Note if you want to store multiple savegames in a single file you
  //need to use seek to move your current position
  //And because we have file of type the seek moves the current position
  //by N times of the type size
  //So if the size of your type is 64 bytes calling Seek(YourFile,2) 
  //will move current position to the 128th byte
  Write(SaveFile, Board);
  //Close file
  CloseFile(SaveFile);
end;

从文件中读取数据的方式相同。

Reading the data from your file is done in similar way.

Read(Savefile, Board); 

编辑:如果你在旧版本的Delphi和char不允许你有足够的可能性存储您的电路板单元格状态可以随时使用像大多数其他基于网格的游戏一样的整数数组。

If you are on older version of Delphi and the char does not allow you enough possibilities to store the state of your board cell you can always use array of integers like most other grid based games do.

这篇关于如何将此2d数组的内容保存到文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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