示例#1
0
void Grid::update()
{
    if (!m_IsPlaying)
        return;

    // Maps coordinates to its new state
    std::map<std::pair<int,int>, int> stateChanges;

    for (int x = 0; x < NUM_CELLS; ++x)
    {
        for (int y = 0; y < NUM_CELLS; ++y)
        {
            auto numLiveNeighbors = getNumLiveNeighbors(x, y);

            if (m_Cells[x][y] == 1)
            {
                if ((numLiveNeighbors < 2) || (numLiveNeighbors > 3))
                    stateChanges[std::make_pair(x, y)] = 0;
            }
            else
            {
                if (numLiveNeighbors == 3)
                    stateChanges[std::make_pair(x, y)] = 1;
            }
        }
    }

    for (auto& pair : stateChanges)
    {
        auto x = pair.first.first;
        auto y = pair.first.second;

        setCellState(x, y, pair.second);
    }

    ++m_NumGenerations;
}
示例#2
0
void Grid::toggleState(int x, int y)
{
    setCellState(x, y, (m_Cells[x][y] == 0 ? 1 : 0));
}
示例#3
0
void FieldRandomizer::randomizeShips() {
  field.clear();

  unsigned i,j,k,iterator;

  // Set 4block ship
  k = rand(0,1);
  i = rand(0,FieldSize-1);
  j = rand(0,FieldSize-1);
  iterator = 4;
  if (k==0) {
    // then horizontal
    while(iterator > 0) {
      if (m_field[i][j] == EMPTY) {
        setCellState(i,j,SHIP_NORMAL);
        iterator -= 1;
      }
      k = rand(0,1);
      if (k==0) {
        if (j+1 < FieldSize) { j += 1; } else { j -= 1; };
      } else {
        if (j > 0) { j -= 1; } else { j += 1; };
      }
    }
  } else {
    // then vertical
    while(iterator > 0) {
      if (m_field[i][j] == EMPTY) {
        setCellState(i,j,SHIP_NORMAL);
        iterator -= 1;
      }
      k = rand(0,1);
      if (k==0) {
        if (i+1 < FieldSize) { i += 1; } else { i -= 1; }
      } else {
        if (i > 0) { i -= 1; } else { i += 1; }
      }
    }
  }
  // Set 3block ships
  iterator = 2;
  while(iterator > 0) {
    k = rand(0,1);
    i = rand(0,FieldSize-1);
    j = rand(0,FieldSize-1);
    if (k==0) {
      // then horizontal
      if (checkNeighborhoodFor3BlockHorizontal(i,j)&& j<FieldSize-2) {
        setCellState(i,j,SHIP_NORMAL);
        setCellState(i,j+1,SHIP_NORMAL);
        setCellState(i,j+2,SHIP_NORMAL);
        iterator -= 1;
      }
    } else {
      // then vertical
      if (checkNeighborhoodFor3BlockVertical(i,j)&& i<FieldSize-2) {
        setCellState(i,j,SHIP_NORMAL);
        setCellState(i+1,j,SHIP_NORMAL);
        setCellState(i+2,j,SHIP_NORMAL);
        iterator -= 1;
      }
    }
  }
  // Set 2block ships
  iterator = 3;
  while(iterator > 0) {
    k = rand(0,1);
    i = rand(0,FieldSize-1);
    j = rand(0,FieldSize-1);
    if (k==0) {
      // then horizontal
      if (checkNeighborhoodFor2BlockHorizontal(i,j)&& j<FieldSize-1) {
        setCellState(i,j,SHIP_NORMAL);
        setCellState(i,j+1,SHIP_NORMAL);
        iterator -= 1;
      }
    } else {
      // then vertical
      if (checkNeighborhoodFor2BlockVertical(i,j)&& i<FieldSize-1) {
        setCellState(i,j,SHIP_NORMAL);
        setCellState(i+1,j,SHIP_NORMAL);
        iterator -= 1;
      }
    }
  }
  // Set 1block ships
  iterator = 4;
  while(iterator > 0) {
    i = rand(0,FieldSize-1);
    j = rand(0,FieldSize-1);
    if (checkNeighborhoodFor1Block(i,j)) {
      setCellState(i,j,SHIP_NORMAL);
      iterator -= 1;
    }
  }
}