如何 save/load 值从板到文件

How to save/load values from board to file

我正在使用 C++ 11,

我需要 save/load 数组和文件。它是战舰游戏,需要为用户和计算机阵列完成,但我不知道如何开始这个任务。

我想使用 class 方法中的代码将其打印到文件中并放在前面,但我出错了。

任何帮助或线索对我来说都是非常重要的。

#include "Board.h"
#include <iostream>
#include <fstream>



Board::Board()
{
    for (int i=0; i<BOARD_SIZE; i++)
        for (int j=0; j<BOARD_SIZE; j++)
            board[i][j]=WATER;

    for (int i=0; i<SHIPS_NUM; i++)
        ship.push_back(Ship(SHIP_LENGTH[i], SHIP_NAME[i]));
}


Board::Board(const Board &oldBoard)
{
    for (int i=0; i<BOARD_SIZE; i++)
        for (int j=0; j<BOARD_SIZE; j++)
            board[i][j]=oldBoard.board[i][j];
    ship = oldBoard.ship;
}

// operator przypisania (kopiowanie)
Board& Board::operator=(const Board &right)
{
    if (this!=&right)
    {
        for (int i=0; i<BOARD_SIZE; i++)
            for (int j=0; j<BOARD_SIZE; j++)
                board[i][j]=right.board[i][j];
        ship = right.ship;
    }

    return *this;

}


int Board::getNumHits()
{
    int count=0;

    for (int i=0; i<BOARD_SIZE; i++)
        for (int j=0; j<BOARD_SIZE; j++)
            if (board[i][j]==HITTED)
                count++;

    return count;
}

void Board::printPrivateBoard()
{
    std::cout<<"  A B C D E F G H I J\n";
    for (int i=0; i<BOARD_SIZE; i++)
    {
        std::cout<<i<<" ";
        for (int j=0; j<BOARD_SIZE; j++)
        {
            if (board[i][j]==HITTED || board[i][j]==MISSED)
                std::cout<<board[i][j]<<" ";
            else
                std::cout<<UNKNOWN<<" ";
        }
        std::cout<<std::endl;
    }
}


void Board::printPublicBoard()
{
    std::cout<<"  A B C D E F G H I J\n";
    for (int i=0; i<BOARD_SIZE; i++)
    {
        std::cout<<i<<" ";
        for (int j=0; j<BOARD_SIZE; j++)
        {
            std::cout<<board[i][j]<<" ";
        }
        std::cout<<std::endl;
    }

}


char Board::getSpaceValue(int x, int y)
{
    return board[y][x];
}


bool Board::recordHit(int x, int y)
{
    for (int i=0; i<SHIPS_NUM; i++)
    {
        if (ship[i].recordHit(x, y))
        {
            board[y][x]=HITTED; 
            
            if (ship[i].isShipSunk())
                std::cout<<"You sunk the "<<ship[i].getName()<<"!\n";
            return true;
        }
    }
            board[y][x]=MISSED;
    return false;
}


bool Board::placeShip(int shipNum, int x, int y, bool isHorizontal)
{
 
    if (x>=BOARD_SIZE || y>=BOARD_SIZE)
        return false;

    
    if (ship[shipNum].getX()>=0 && ship[shipNum].getY()>=0)
        return false;

   
    for (int i=0; i<ship[shipNum].getSize(); i++)
    {
        
        
        if ((isHorizontal && board[y][x+i]!=WATER) ||
            (!isHorizontal && board[y+i][x]!=WATER))
            return false;
        
        if ((isHorizontal && (x+i)>=BOARD_SIZE) ||
            (!isHorizontal && (y+i)>=BOARD_SIZE))
            return false;
    }

   
    for (int i=0; i<ship[shipNum].getSize(); i++)
    {
        if (isHorizontal)
            board[y][x+i]=SHIP;
        else
            board[y+i][x]=SHIP;
    }

    
    ship[shipNum].setPosition(x, y, isHorizontal);

    return true;
}



void saveBoard(Board &b1,Board &b2) {
    std::fstream file(FILE_NAME.c_str(),std::ios::trunc|std::ios::out);
    


    file.close();

}

和头文件

#include <vector>
#include "Constants.h"
#include "Ship.h"


#ifndef STATKI_BOARD_H
#define STATKI_BOARD_H

class Board{
private:
    char board[BOARD_SIZE][BOARD_SIZE];
    std::vector<Ship> ship;
public:
    Board();
    Board(const Board &boardCopy);
    Board& operator=(const Board &boardRight);
    ~Board() {return;};
    int getNumHits();
    void printPrivateBoard();
    void printPublicBoard();
    char getSpaceValue(int x, int y);
    bool recordHit(int x, int y);
    bool placeShip(int shipNum, int x, int y, bool isHorizontal);
    void saveBoard(Board &b1,Board &b2);
};

#endif

常量

#include <string>

#ifndef STATKI_CONSTANTS_H
#define STATKI_CONSTANTS_H
// game constants
const int SHIPS_NUM = 4;
const std::string SHIP_NAME[SHIPS_NUM] = {"Destroyer","Cruiser","Battleship","Carrier"};
const int SHIP_LENGTH[SHIPS_NUM] = {2,3,4,5};
const int TOTAL_SHIP_SPACES = 17;
const int BOARD_SIZE = 10;
const std::string FILE_NAME = "ships_board.txt";


// board constants
const char MISSED = '@';
const char HITTED = 'X';
const char UNKNOWN = '?';
const char SHIP = '_';
const char WATER = '~';


// to convert char to board position
const int LETTER_CHAR_OFFSET=65;
const int NUMBER_CHAR_OFFSET=48;


#endif //STATKI_CONSTANTS_H

船舶对象

#include "Ship.h"
#include "Constants.h"

// Konstruktor inicjalizuje odrazu statek tylko wtedy
// gdy podana jest nazwa statku oraz jego wielkosc
Ship::Ship(int size, std::string name) {
    shipSize = size;
    shipName = name;
    shipFiledSymbol = new char[shipSize];
    for (int i = 0; i < shipSize ; ++i) {
        shipFiledSymbol[i] = SHIP; // ustawia wysztkie pola statku na nie trafione
    }
    xCord = -1; // Nie jest jeszcze na planszy dlatego -1
    yCord = -1; // Nie jest jeszcze na planszy dlatego -1
    isHorizontal=true;
    isSunk=false;
}

Ship::Ship(int size, std::string name, int x, int y, bool sunk, bool horizontal) {
    shipSize = size;
    shipName = name;
    shipFiledSymbol = new char[shipSize];
    for (int i = 0; i < shipSize ; ++i) {
        shipFiledSymbol[i] = SHIP; // ustawia wysztkie pola statku na nie trafione
    }
    xCord = x;
    yCord = y;
    isSunk = sunk;
    isHorizontal = horizontal;
}

// konstruktor kopiujacy
Ship::Ship(const Ship &shipCopy) {
    shipSize = shipCopy.getSize();
    shipName = shipCopy.getName();
    shipFiledSymbol = new char[shipSize];
    for (int i = 0; i < shipSize ; ++i) {
        shipFiledSymbol[i] = SHIP; // ustawia wysztkie pola statku na nie trafione
    }
    xCord = shipCopy.getX();
    yCord = shipCopy.getY();
    isSunk = shipCopy.isShipSunk();
    isHorizontal = shipCopy.isShipHorizontal();
}
// operator przypisujacy
Ship& Ship::operator=(const Ship &rightShip) {
    // Jeli lewa strona nie rowna sie prawej
    if (this!=&rightShip){
        shipSize = rightShip.getSize();
        shipName = rightShip.getName();
        shipFiledSymbol = new char[shipSize];
        // uswanie wczesniej zaalkowana pamieci przed nowa alokacja
        if (shipSize>0){
            delete [] shipFiledSymbol;
        }
        for (int i = 0; i < shipSize ; ++i) {
            shipFiledSymbol[i] = SHIP; // ustawia wysztkie pola statku na nie trafione
        }
        xCord = rightShip.getX();
        yCord = rightShip.getY();
        isSunk = rightShip.isShipSunk();
        isHorizontal = rightShip.isShipHorizontal();
    }
    return *this;
}


// destructor usuwa dynamicznie zaalokowana pamiec
Ship::~Ship()
{
    if (shipSize > 0)
        delete [] shipFiledSymbol;
}

int Ship::getSize() const {
    return shipSize;
}

std::string Ship::getName() const {
    return shipName;
}

int Ship::getX() const{
    return xCord;
}
int Ship::getY() const{
    return yCord;
}

bool Ship::isShipSunk() const{
    return isSunk;
}

bool Ship::isShipHorizontal() const{
    return isHorizontal;
}

void Ship::setPosition(int x, int y, bool horizontal) {
    xCord = x;
    yCord = y;
    isHorizontal=horizontal;
    return;
}

// funkcja zwraca informacje czy statek zostal trafiony
bool Ship::recordHit(int hitX, int hitY) {
    bool checkHorizontal = isHorizontal && (hitX<xCord || hitX>= xCord+shipSize || yCord != hitY);
    bool checkVertical = !isHorizontal && (hitY<yCord || hitY>= yCord+shipSize || xCord != hitX);
    // Jezli ktorys z tych warunkow jest prawdziy to statek nie zostal trafiony
    if(checkHorizontal || checkVertical)
        return false;
    else{
    // Jesli pierwszy warunek sie nie spelni to statek jest trafiony
        if(isHorizontal){
            shipFiledSymbol[hitX-xCord]=HITTED;
        }else{
            shipFiledSymbol[hitY-yCord]=HITTED;
        }
    }

    // Sprawdzanie czy statek zatonal
    isSunk=true;
    for (int i=0; i<shipSize; i++)
        if (shipFiledSymbol[i]==SHIP)
            isSunk=false;

    return true;
}

船头

#include <string>

#ifndef STATKI_SHIP_H
#define STATKI_SHIP_H

class Ship{
private:
    char* shipFiledSymbol;
    int shipSize;
    int xCord,yCord;
    bool isSunk;
    bool isHorizontal;
    std::string shipName;
public:
    Ship(int size,std::string name);
    Ship(int size,std::string name,int x,int y,bool sunk,bool horizontal);
    Ship(const Ship &shipCopy);
    Ship& operator=(const Ship &rightShip);
    ~Ship();

    int getSize() const;
    std::string getName() const;
    int getX() const;
    int getY() const;
    bool isShipSunk() const;
    bool isShipHorizontal() const;

    void setPosition(int x, int y, bool horizontal);
    bool recordHit(int hitX, int hitY);
};

#endif

主文件仅用于构建(没有出现 winmain 错误)

int main(){
return 0;
}

汇总以上讨论:

class Board 添加一个知道如何编写 Board.

<< 运算符
friend std::ostream & operator<<(std::ostream & out, const Board & b);

然后在 Board.cpp 中,您根据 What are the basic rules and idioms for operator overloading? 中的建议实施 << 和几个 for 循环以遍历二维数组

std::ostream & operator<<(std::ostream & out, const Board & b)
{
    for (int row = 0; row < BOARD_SIZE; row++)
    {
        for (int col = 0; col < BOARD_SIZE; col++)
        {
            out << b.board[row][col] << ' ';
        }
        out << '\n';
    }
    out << '\n';
    return out;
}

现在 saveBoard 非常简单:

bool saveBoard(Board &b1,Board &b2) {
    std::ofstream file(FILE_NAME);
    file << b1 << b2;
    return file.good();
}

我们returnbool因为你总是想让调用者知道操作是否失败。如果调用者不使用 return 值,这是他们必须自己解决的错误。您已尽力而为,但我想您也可以抛出异常。这在这里可能会或可能不会过分。

要读回它,您可以做几乎相同的事情,但使用 >> 运算符:

friend std::istream & operator>>(std::istream & in, Board & b);

然后

std::istream & operator>>(std::istream & in, Board & b)
{
    for (int row = 0; row < BOARD_SIZE; row++)
    {
        for (int col = 0; col < BOARD_SIZE; col++)
        {
            in >> b.board[row][col];
        }
    }
    return in;
}

bool loadBoard(Board &b1,Board &b2) {
    std::ifstream file(FILE_NAME);
    file >> b1 >> b2;
    return file.good();
}