0

Ok so i have a homework assignment and i dont know where to start with this function.

bool placePiece(char** pBoard, int colSize, int rowSize, int columnSelection, char player)

The function is to place a piece represented by char player, on the game board char** pBoard (a 2dArray made from colSize and rowSize) the function is to put the players piece at the bottom of the column selected during that players turn. also if a piece is already at the bottom of the column it puts the piece on top of that one and if the column is full it will return false.

really the biggest issue im having is i really dont understand how im supposed to be using pBoard.

Im not looking for someone to do it for me but just to help me start out on the right path.

  • 3
    It's time for you to [learn about arrays](http://stackoverflow.com/q/4810664/1553090). – paddy Feb 03 '16 at 04:55

1 Answers1

0

To solve this, you need to understand arrays and loops. The first argument in your signature is an array containing your board data (and the next two arguments the dimensions of it) - there you need to access the first element at the columnSelection position and set it to the value of the player argument. The return value should indicate if the operation was successful.

bool placePiece(char** pBoard, int colSize, int rowSize, int columnSelection, char player) {
  if (columnSelection >= colSize) {
    /* invalid column */
    return false;
  }
  for (size_t i = 0; i < rowSize; ++i) {
    /* loop to go over all rows - starting with 0 */
    if (pBoard[columnSelection][i] == 0) {
      /* find first empty row and set it to 'player' value */
      pBoard[columnSelection][i] = player;
      return true;
    }
  }
  /* no free row found -> all rows already set*/
  return false;
}

This code assumes an column-row order in your array and goes from row 0 upwards, but you should get the general idea.

Constantin
  • 8,721
  • 13
  • 75
  • 126