Esempio n. 1
0
void state_t::search(int nb, ostream& os) {
  if (nb == pieces.size()) {
    count += 1;
    if (height <= 4 && width <= 4)
      print(os);
    return;
  }
  int type = pieces[nb];
  int fy = last_place[type].back().first;
  int fx = last_place[type].back().second;

  for (int y = fy; y < height; ++y) {
    if (used_rows[y] == 0)
      for (int x = fx; x < width; ++x) {
        if (used_cells[y + 2][x + 2]) continue;
        if (used_cols[x]) continue;
        if (used_diag_l[y + x] || used_diag_r[y - x + width]) continue;

        if (is_attacking(y, x, type)) continue;

        place_piece(y, x, type, 1);

        board.push_back(item_t(y, x, type));
        last_place[type].push_back(make_pair(y, x));
       
        search(nb + 1, os);

        last_place[type].pop_back();
        board.pop_back();

        place_piece(y, x, type, -1);
      }
    fx = 0;
  }
}
Esempio n. 2
0
		virtual ~ItemGroup()
		{
			for(items_t::iterator it = items.begin(); it != items.end(); ++it)
			{
				delete (*it);
			}
		}
Esempio n. 3
0
void state_t::print(ostream& os) {
  const char names[] = "QRBKN";
  vector<string> b(height, string(width, '.'));
  for (vector<item_t>::iterator i = board.begin(); i != board.end(); ++i) {
    b[i->y][i->x] = names[i->type];
  }
  copy(b.begin(), b.end(), ostream_iterator<string>(os, "\n"));
  os << "\n";
}
Esempio n. 4
0
		virtual std::basic_ofstream<Char> &Serialize(std::basic_ofstream<Char> &to) const
		{
			items_t::size_type size = items.size();
			Item::Serialize(to).write((Char*)&size, sizeof(size)/sizeof(Char));
			for(items_t::const_iterator it = items.begin(); it != items.end(); ++it)
			{
				(*it)->Serialize(to);
			}
			return to;
		}
Esempio n. 5
0
		virtual HANDLE Serialize(HANDLE File) const
		{
			if(!Item::Serialize(File)) return 0;
			unsigned long written;
			items_t::size_type size = items.size();
			if(!WriteFile(File, &size, sizeof(size), &written, 0) || written != sizeof(size)) return 0;
			for(items_t::const_iterator it = items.begin(); it != items.end(); ++it)
			{
				if(!((*it)->Serialize(File))) return 0;
			}
			return File;
		}
Esempio n. 6
0
bool state_t::is_attacking(int y, int x, int type) {
  for (vector<item_t>::iterator i = board.begin(); i != board.end(); ++i) {
    int cy = i->y, cx = i->x;

    if (type == kQueen || type == kRook)
      if (cy == y || cx == x) 
        return true;
    
    if (type == kQueen || type == kBishop)
      if (cy + cx == y + x || cy - cx == y - x) 
        return true;

    if (type == kKing || type == kKnight) {
      const delta_t* delta = type == kKing ? king_rules : knight_rules;
      for (int i = 0; i < 8; ++i)
        if (cy == y + delta[i].dy && cx == x + delta[i].dx) 
          return true;
    }
  }
  return false;
}