PossibleMove* readMove(char* command, int* exitcode){ char fromTile[6]; char toTile[6]; char promoteToAsString[10]; char promoteTo = 0; //initializing promoteTo to avoid problems with promotion-less move commands strncpy(promoteToAsString, "undefined", 10); *exitcode = 0; if (sscanf(command, "move %5s to %5s %6s", fromTile, toTile, promoteToAsString) < 2){ *exitcode = -1; return NULL; } int fromX, fromY, toX, toY; if (readTile(fromTile, &fromX, &fromY) == -1 || readTile(toTile, &toX, &toY) == -1){ *exitcode = -1; return NULL; } if (!Board_isInRange(fromX, fromY) || !Board_isInRange(toX, toY) ){ *exitcode = -2; return NULL; } if (!str_equals(promoteToAsString, "undefined")){ promoteTo = stringToPiece(promoteToAsString, turn); if (!promoteTo){ *exitcode = -1; // the promotion was not input legally return NULL; } if (promoteTo && (!Board_isFurthestRowForPlayer(turn, toY) || !pieceIsPawn(fromX, fromY))){ *exitcode = -6; // the promotion was input legally, but the move itself is illegal return NULL; } } if(pieceIsPawn(fromX, fromY) && Board_isFurthestRowForPlayer(turn, toY) && promoteTo == 0){ promoteTo = (turn = WHITE)? 'q':'Q'; //default promotion } if (!Board_isInRange(fromX, fromY) || !Board_isInRange(toX, toY)){ *exitcode = -2; return NULL; } if (Board_getColor(&board, fromX, fromY) != turn){ *exitcode = -5; return NULL; } PossibleMove* move = PossibleMove_new(fromX, fromY, toX, toY, promoteTo, &board); if (!move){ *exitcode = 1; return NULL; } return move; }
/*************************************************************************** * readTileimage ***************************************************************************/ void CNDSbgr555::readTileimage(uint8_t * data, tileimage * ti) { int i; for (i=0; i<4; i++) { readTile(&data[i*32], &ti->row1[i]); readTile(&data[i*32+128], &ti->row2[i]); readTile(&data[i*32+256], &ti->row3[i]); readTile(&data[i*32+384], &ti->row4[i]); } }
void Labirinth::setExits(int e) // check exits already placed, than insert new exits up to desired number { std::cout << "at present time there are " << exits << " exits in labirinth" << std::endl; int rand_x; int rand_y; int i; int result; i = 0; while (exits < e) { ++i; std::cout << "placing exit..." << std::endl; rand_x = (rand() + time(0)) % x_side; rand_y = (rand() + time(0)) % y_side; result = readTile(rand_x, rand_y); std::cout << i << " , (" << rand_x << "," << rand_y << ") , " << result << std::endl; if ( result == 0) { setExit(rand_x, rand_y); ++exits; std::cout << "placed exit number " << exits << " at (" << rand_x << "," << rand_y << ")" << std::endl; } else { std::cout << "this is just a wall or exit... another tile will be used" << std::endl; } } std::cout << "at present time there are " << exits << " exits in labirinth" << std::endl; }
void Labirinth::setWalls(int w) // check walls already placed, than insert new wall pieces up to desired number { std::cout << "at present time there are " << walls << " walls in labirinth" << std::endl; int rand_x; int rand_y; int i; int result; i = 0; while (walls < w) { ++i; std::cout << "placing walls..." << std::endl; rand_x = (rand() + time(0)) % x_side; rand_y = (rand() + time(0)) % y_side; result = readTile(rand_x, rand_y); std::cout << i << " , (" << rand_x << "," << rand_y << ") , " << result << std::endl; if ( result == 0) { setWall(rand_x, rand_y); ++walls; std::cout << "placed wall number " << walls << " at (" << rand_x << "," << rand_y << ")" << std::endl; } else { std::cout << "this is just a wall or exit... another tile will be used" << std::endl; } } std::cout << "at present time there are " << walls << " walls in labirinth" << std::endl; }
/* * Main function for handling the "get_moves" command, for printing all possible moves for a given piece on the board during the game stage. * * @return: -5 if the input tile does not is not occupied by one of the current player's pieces * -2 if the tile given is invalid * -1 if the input tile was not formatted legally * 1 if an allocation failure occurred * 0 otherwise */ int printMovesOfPiece(char* command){ char tile[6]; if (sscanf(command, "get_moves %5s", tile) != 1){ return -1; } int x, y; if (readTile(tile, &x, &y) == -1){ return -1; } if (!Board_isInRange(x, y)){ return -2; } if (Board_getColor(&board, x, y) != turn){ return -5; } LinkedList* possibleMoves = Board_getPossibleMovesOfPiece(&board, x, y, 0); if (!possibleMoves){ return 1; } PossibleMoveList_print(possibleMoves); PossibleMoveList_free(possibleMoves); return 0; }
/* * Main function for handling the "set" command for setting pieces on the board during the settings stage. * * @return: -1 if the input tile, color or piece type were not formatted legally * -2 if the input tile is not a possible position on the board * -8 if the specified piece cannot be added there are already * too many pieces of this type on the board * 0 otherwise */ int setPiece(char* command){ int x, y; char tile[6]; char colorString[6]; char pieceString[7]; if (sscanf(command, "set %5s %5s %6s", tile, colorString, pieceString) != 3){ return -1; } if (readTile(tile, &x, &y) == -1){ return -1; } if (!Board_isInRange(x, y)){ return -2; } int color = stringToColor(colorString); char piece = stringToPiece(pieceString, color); if (piece == 0 || color == -1){ return -1; } if (PieceCounter_isAtMax(counter, piece, x, y)){ return -8; } if ((piece == Board_BLACK_PAWN || piece == Board_WHITE_PAWN) && Board_isFurthestRowForPlayer(color, y)){ return -8; } char removedPiece = Board_getPiece(&board, x, y); PieceCounter_update(counter, removedPiece, -1, x, y); PieceCounter_update(counter, piece, 1, x, y); Board_setPiece(&board, x, y, piece); Board_updateKingPosition(&board, x, y); return 0; }
void LiveSocket::receiveTile(BinaryNode* node, Editor& editor, Action* action, const Position* position) { ASSERT(node != nullptr); Tile* tile = readTile(node, editor, position); if(tile) { action->addChange(newd Change(tile)); } }
void OsmAnd::ObfPoiSectionReader_P::readAmenities( const std::unique_ptr<ObfReader_P>& reader, const std::shared_ptr<const ObfPoiSectionInfo>& section, QSet<uint32_t>* desiredCategories, QList< std::shared_ptr<const Model::Amenity> >* amenitiesOut, const ZoomLevel& zoom, uint32_t zoomDepth, const AreaI* bbox31, std::function<bool (const std::shared_ptr<const Model::Amenity>&)> visitor, IQueryController* controller) { auto cis = reader->_codedInputStream.get(); QList< std::shared_ptr<Tile> > tiles; for(;;) { auto tag = cis->ReadTag(); switch(gpb::internal::WireFormatLite::GetTagFieldNumber(tag)) { case 0: return; case OBF::OsmAndPoiIndex::kBoxesFieldNumber: { auto length = ObfReaderUtilities::readBigEndianInt(cis); auto oldLimit = cis->PushLimit(length); readTile(reader, section, tiles, nullptr, desiredCategories, zoom, zoomDepth, bbox31, controller, nullptr); cis->PopLimit(oldLimit); if(controller && controller->isAborted()) return; } break; case OBF::OsmAndPoiIndex::kPoiDataFieldNumber: { // Sort tiles byte data offset, to all cache-friendly with I/O system qSort(tiles.begin(), tiles.end(), [](const std::shared_ptr<Tile>& l, const std::shared_ptr<Tile>& r) -> bool { return l->_hash < r->_hash; }); for(auto itTile = tiles.begin(); itTile != tiles.end(); ++itTile) { const auto& tile = *itTile; cis->Seek(section->_offset + tile->_offset); auto length = ObfReaderUtilities::readBigEndianInt(cis); auto oldLimit = cis->PushLimit(length); readAmenitiesFromTile(reader, section, tile.get(), desiredCategories, amenitiesOut, zoom, zoomDepth, bbox31, visitor, controller, nullptr); cis->PopLimit(oldLimit); if(controller && controller->isAborted()) return; } cis->Skip(cis->BytesUntilLimit()); } return; default: ObfReaderUtilities::skipUnknownField(cis, tag); break; } } }
wlImages wlTilesReadStream(FILE *stream) { wlImages tiles; wlImage tile; wlMsqHeader header; int quantity, i; unsigned char dataByte, dataMask; wlHuffmanNode *rootNode; // Validate parameters assert(stream != NULL); // Read and validate the MSQ header header = wlMsqReadHeader(stream); if (!header) return NULL; if (header->type != COMPRESSED) { wlError("Expected MSQ block of tileset to be compressed"); return NULL; } // Calculate the number of tiles quantity = header->size * 2 / 16 / 16; // Free header structure free(header); // Initialize huffman stream dataByte = 0; dataMask = 0; if (!(rootNode = wlHuffmanReadNode(stream, &dataByte, &dataMask))) return NULL; // Create the images structure which is going to hold the tiles tiles = wlImagesCreate(quantity, 16, 16); // Read the tiles for (i = 0; i < quantity; i++) { tile = tiles->images[i]; readTile(stream, tile, rootNode, &dataByte, &dataMask); } // Free huffman data wlHuffmanFreeNode(rootNode); // Return the tiles return tiles; }
/* * Main function for handling the "rm" command for removing pieces from the board during the settings stage. * * @return: -1 if the input tile was not formatted legally * -2 if the input tile is not a possible position on the board * 0 otherwise */ int removePiece(char* command){ int x, y; char tile[6]; if (sscanf(command, "rm %5s", tile) != 1){ return -1; } if (readTile(tile, &x, &y) == -1){ return -1; } if (!Board_isInRange(x, y)){ return -2; } char piece = Board_removePiece(&board, x, y); PieceCounter_update(counter, piece, -1, x, y); return 0; }
int printMoveValue(char* command){ int exitcode; int depth; int bestOffset = 0; if (command[10] == 'b'){ depth = computeBestDepth(); bestOffset = 3; } else{ if (sscanf(command, "get_score %d", &depth) < 0){ return -1; } } if (!strstr(command, "move") && !strstr(command, "castle")){ return -1; } if (strstr(command, "move")){ PossibleMove* move = readMove(command + 12 + bestOffset, &exitcode); if (exitcode != 0){ // illegal input or illegal move return exitcode; } else{ int score = alphabeta(move, depth, !turn, INT_MIN, INT_MAX); printf("%d\n", score); PossibleMove_free(move); } } // castling move else{ int rookX, rookY; exitcode = readTile(command + 19, &rookX, &rookY); if (exitcode == 0){ PossibleMove* castlingMove = PossibleMove_new(rookX, rookY, 0, 0, 0, &board); int score = alphabeta(castlingMove, depth, !turn, INT_MIN, INT_MAX); printf("%d\n", score); PossibleMove_free(castlingMove); } } return exitcode; }
int castleRook(char* command){ char rookTile[6]; if (sscanf(command, "castle %5s", rookTile) != 1){ return -1; } int rookX, rookY; if (readTile(rookTile, &rookX, &rookY) == -1){ return -1; } if (!Board_isInRange(rookX, rookY)){ return -2; } if (Board_getColor(&board, rookX, rookY) != turn){ return -5; } if(!pieceIsRook(&board, rookX, rookY)){ return -11; } PossibleMove* castlingMove = PossibleMove_new(rookX, rookY, 0, 0, 0, &board); if (!castlingMove){ return 1; } LinkedList* possibleMoves = Board_getPossibleMovesOfPiece(&board, rookX, rookY, 0); if (!possibleMoves){ PossibleMove_free(castlingMove); return 1; } if (!PossibleMoveList_contains(possibleMoves, castlingMove)){ PossibleMove_free(castlingMove); PossibleMoveList_free(possibleMoves); return -12; } Board_copy(&board, castlingMove->board); display(); PossibleMove_free(castlingMove); PossibleMoveList_free(possibleMoves); turn = !turn; return 0; }
QList<jcz::XmlParser::XMLTile> jcz::XmlParser::readTileDefinitions(jcz::Expansion expansion) { QList<XMLTile> tiles; QString file = QString(":/jcz/tile-definitions/%1.xml").arg(Expansions::getName(expansion).toLower()); QFile f(file); f.open(QIODevice::ReadOnly); QXmlStreamReader xml(&f); xml.readNextStartElement(); if (xml.name() != "pack") qWarning() << "unknown element:" << xml.name(); else { int index = 0; while (xml.readNextStartElement()) { Q_ASSERT(xml.tokenType() == QXmlStreamReader::StartElement); if (xml.name() == "tile") { XMLTile tile; tile.expansion = expansion; tile.index = index++; readTile(xml, tile); tiles.append(tile); } else qWarning() << "unknown element:" << xml.name(); } if (xml.hasError()) { qWarning() << xml.errorString(); } } f.close(); return tiles; }
unsigned char* AbstractTileCache::getTile(int tx, int ty) { int key = Tile(tx, ty).key(width / tileSize + 1); Cache::iterator i = tileCache.find(key); if (i == tileCache.end()) { unsigned char *data = readTile(tx, ty); list<Tile*>::iterator li; if ((int) tileCache.size() == capacity) { // evict least recently used tile if cache is full li = tileCacheOrder.begin(); Tile *t = *li; tileCache.erase(t->key(width / tileSize + 1)); tileCacheOrder.erase(li); delete t; } // create tile, put it at the end of tileCacheOrder, and update the map Tile *t = new Tile(tx, ty, data); li = tileCacheOrder.insert(tileCacheOrder.end(), t); tileCache[key] = li; assert(tileCache.find(key) != tileCache.end()); return data; } else { list<Tile*>::iterator li = i->second; list<Tile*>::iterator lj = li; Tile *t = *li; assert(t->tx == tx && t->ty == ty); // put t at the end of tileCacheOrder, and update the map accordingly lj++; if (lj != tileCacheOrder.end()) { tileCacheOrder.erase(li); li = tileCacheOrder.insert(tileCacheOrder.end(), t); tileCache[key] = li; } return t->data; } }
bool OsmAnd::ObfPoiSectionReader_P::readTile( const std::unique_ptr<ObfReader_P>& reader, const std::shared_ptr<const ObfPoiSectionInfo>& section, QList< std::shared_ptr<Tile> >& tiles, Tile* parent, QSet<uint32_t>* desiredCategories, uint32_t zoom, uint32_t zoomDepth, const AreaI* bbox31, IQueryController* controller, QSet< uint64_t >* tilesToSkip) { auto cis = reader->_codedInputStream.get(); const auto zoomToSkip = zoom + zoomDepth; QSet< uint64_t > tilesToSkip_; if(parent == nullptr && !tilesToSkip) tilesToSkip = &tilesToSkip_; std::shared_ptr<Tile> tile(new Tile()); gpb::uint32 lzoom; for(;;) { if(controller && controller->isAborted()) return false; auto tag = cis->ReadTag(); switch(gpb::internal::WireFormatLite::GetTagFieldNumber(tag)) { case 0: tiles.push_back(tile); return true; case OBF::OsmAndPoiBox::kZoomFieldNumber: { cis->ReadVarint32(&lzoom); tile->_zoom = lzoom; if(parent) tile->_zoom += parent->_zoom; } break; case OBF::OsmAndPoiBox::kLeftFieldNumber: { auto x = ObfReaderUtilities::readSInt32(cis); if(parent) tile->_x = x + (parent->_x << lzoom); else tile->_x = x; } break; case OBF::OsmAndPoiBox::kTopFieldNumber: { auto y = ObfReaderUtilities::readSInt32(cis); if(parent) tile->_y = y + (parent->_y << lzoom); else tile->_y = y; // Check that we're inside bounding box, if requested if(bbox31) { AreaI area31; area31.left = tile->_x << (31 - tile->_zoom); area31.right = (tile->_x + 1) << (31 - tile->_zoom); area31.top = tile->_y << (31 - tile->_zoom); area31.bottom = (tile->_y + 1) << (31 - tile->_zoom); if(!bbox31->intersects(area31)) { // This tile is outside of bounding box cis->Skip(cis->BytesUntilLimit()); return false; } } } break; case OBF::OsmAndPoiBox::kCategoriesFieldNumber: { if(!desiredCategories) { ObfReaderUtilities::skipUnknownField(cis, tag); break; } gpb::uint32 length; cis->ReadLittleEndian32(&length); auto oldLimit = cis->PushLimit(length); const auto containsDesired = checkTileCategories(reader, section, desiredCategories); cis->PopLimit(oldLimit); if(!containsDesired) { cis->Skip(cis->BytesUntilLimit()); return false; } } break; case OBF::OsmAndPoiBox::kSubBoxesFieldNumber: { auto length = ObfReaderUtilities::readBigEndianInt(cis); auto oldLimit = cis->PushLimit(length); auto tileOmitted = readTile(reader, section, tiles, tile.get(), desiredCategories, zoom, zoomDepth, bbox31, controller, tilesToSkip); cis->PopLimit(oldLimit); if(tilesToSkip && tile->_zoom >= zoomToSkip && tileOmitted) { auto skipHash = (static_cast<uint64_t>(tile->_x) >> (tile->_zoom - zoomToSkip)) << zoomToSkip; skipHash |= static_cast<uint64_t>(tile->_y) >> (tile->_zoom - zoomToSkip); if(tilesToSkip->contains(skipHash)) { cis->Skip(cis->BytesUntilLimit()); return true; } } } break; case OBF::OsmAndPoiBox::kShiftToDataFieldNumber: { tile->_offset = ObfReaderUtilities::readBigEndianInt(cis); tile->_hash = static_cast<uint64_t>(tile->_x) << tile->_zoom; tile->_hash |= static_cast<uint64_t>(tile->_y); tile->_hash |= tile->_zoom; // skipTiles - these tiles are going to be ignored, since we need only 1 POI object (x;y)@zoom if(tilesToSkip && tile->_zoom >= zoomToSkip) { auto skipHash = (static_cast<uint64_t>(tile->_x) >> (tile->_zoom - zoomToSkip)) << zoomToSkip; skipHash |= static_cast<uint64_t>(tile->_y) >> (tile->_zoom - zoomToSkip); tilesToSkip->insert(skipHash); } } break; default: ObfReaderUtilities::skipUnknownField(cis, tag); break; }