Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9309099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:47:08+00:00 2026-06-19T00:47:08+00:00

I am trying to make a board game, since every move has to be

  • 0

I am trying to make a board game, since every move has to be valid, so I am making a copy of board and make a move so I can verify if that move is valid or not.

First I initialize all the positions on the board to be 0 (iterate through the board and set every p to 0

 pair<int, int> p(y, x); 
 board_[p] = 0;

This is the copy board method

void Board::copy(Board & gb) {
for (int y = MIN_Y; y <= MAX_Y; ++y) {
    for (int x = MIN_X; x <= MAX_X; ++x) {
        pair<int, int> p(y, x); 
        if (gb.board_.at(p) != 0) {
            board_[p] = new Pieces(*gb.board_.at(p));  // **where I am confused**
        } else {
            board_[p] = 0;
        }   
    }
}
}

My container in Board is:

map<pair<int,int>, Pieces*> board_;

Now in a play method, I make a copy of the board

unsigned int play(Board & b){
   b.copy(*this);
}

My question: both

board_[p] = new Pieces(*gb.board_.at(p)); //Pieces is a class I defined

and

board_[p] = gb.board_.at(p);

compile without any errors or warnings. Which one should I use though?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T00:47:09+00:00Added an answer on June 19, 2026 at 12:47 am

    Either may be correct, but you probably want the first one. The first will copy each of the Pieces over to the new board – this is known as a deep copy. The second will only copy the pointers to each Pieces over, so both boards point at the same set of Pieces – this is a shallow copy.

    However, there is a bigger issue here. You are defining a copy function, but C++ gives us a language feature for doing this – copy constructors. You should instead define a function like so:

    Board::Board(const Board& other_board) {
      // Copy everything from other_board to this board
    }
    

    And you would use it like so:

    Board board;
    Board newBoard(b);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make a turn-based board game that will be run on
I'm trying to make a board game framework that loads in each game as
I'm trying make an entity with doctrine that has three associations with other entities
I am trying to make a chess game. So I am placing the board,coins
I was trying to make a board game in java and i am stuck
I am trying to make sure that in my memory game, once a person
I'm trying to make a word puzzle game, and for that i'm using a
right now I'm trying to make the battleship board game as practice, and I've
I'm trying to make a game of lights out. I can get the lights
I'm trying to make an application that uses the on-board accelerometer. When I'm setting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.