// constructor gameBoard::gameBoard() { // set board dimensions (height and width) boardHeight = 4; boardWidth = 4; // randomly place two tiles with a value of 2 (90%) or 4 (90%) newTile(); newTile(); }
// slides tiles up void gameBoard::moveUp() { bool shouldAdd = false; // notes if a change has been made to the board // checks each tile of the board against tiles above it for (int i = 1; i < boardHeight; i++) { for (int j = 0; j < boardWidth; j++) { int row = i; // row number while (board[row][j].getVal() != 0 && // moves through empty tiles board[row-1][j].getVal() == 0 && row > 0) { board[row-1][j].takeVal(board[row][j].getVal()); shouldAdd = board[row][j].makeZero(); // new tile to be added row--; // decrement row } if (row > 0) { // continues if not on the last row if (board[row][j].getVal() != 0 && //if tile meets an equal board[row-1][j].getVal() == board[row][j].getVal()) { board[row-1][j].doubleVal(); shouldAdd = board[row][j].makeZero();//new tile to be added } } } } if (shouldAdd) // if a change has been made newTile(); // adds new tile }
// slides tiles right void gameBoard::moveRight() { bool shouldAdd = false; // notes if a change has been made to the board // checks each tile of the board against tiles to the right of it for (int i = 0; i < boardHeight; i++) { for (int j = boardWidth-2; j > -1; j--) { int col = j; // column number // moves through empty tiles while (board[i][col].getVal() != 0 && // move through empty tiles board[i][col+1].getVal() == 0 && col < boardWidth-1) { board[i][col+1].takeVal(board[i][col].getVal()); shouldAdd = board[i][col].makeZero(); //new tile to be added col++; // increment column } if (col < boardWidth-1) { // continues if not on the last column if (board[i][col].getVal() != 0 && //if tile meets an == tile board[i][col+1].getVal() == board[i][col].getVal()) { board[i][col+1].doubleVal(); shouldAdd = board[i][col].makeZero();//new tile to be added } } } } if (shouldAdd) // if a change has been made newTile(); // adds new tile }
// slides tiles down void gameBoard::moveDown() { bool shouldAdd = false; // notes if a change has been made to the board // checks each tile of the board against tiles below it for (int i = boardHeight-2; i > -1; i--) { for (int j = 0; j < boardWidth; j++) { int row = i; // row number while (board[row][j].getVal() != 0 && // move through empty tiles board[row+1][j].getVal() == 0 && row < boardHeight-1) { board[row+1][j].takeVal(board[row][j].getVal()); shouldAdd = board[row][j].makeZero();//new tile should be added row++; // increment row } if (row < boardHeight-1) { // continues if not on the last row if (board[row][j].getVal() != 0 && //if tile meets an == tile board[row+1][j].getVal() == board[row][j].getVal()) { board[row+1][j].doubleVal(); shouldAdd = board[row][j].makeZero();//new tile to be added } } } } if (shouldAdd) // if a change has been made newTile(); // adds new tile }
void ba(robot *r) { int busIndex = 1; while (1) { r->map->tiles[r->posX][r->posY]->bustrophedonIndex = busIndex; bustrophedon(busIndex++, r); tile *closest = findClosestOpen(r); if (closest == NULL) break; tileSet *path = getAstarRoute(newTile(r->posX, r->posY), closest, r->map); if (path == NULL || path->count == 0){ printf("PATH NOT FOUND\n"); break; } int i; for (i = path->count - 1; i >= 0; i--) { stepToTile(r, path->tiles[i]); r->map->tiles[r->posX][r->posY]->bustrophedonIndex = -1; } } }
PRIVATE void addRandomTile(MAKE_THIS(MainWindow)){ SELFREF_INIT; int x, y, value = rand() % 10 < 9 ? 2 : 4; /* Same condition as it original 2048 */ while (x = rand() % 4, y = rand() % 4, this->tiles[y][x]); this->tiles[y][x] = newTile(this->panel->moduleInstance, value, x, y); $(this->panel)_addChild((GUIObject)(this->tiles[y][x])); displayControl((Control)(this->tiles[y][x])); SendMessageA(this->tiles[y][x]->handle, WM_PAINT, (WPARAM)NULL, (LPARAM)NULL); /* Force repaint after the object has been created */ }
map *newMap(int width, int height) { map *m = (map*) malloc(sizeof(map)); m->width = width; m->height = height; m->tiles = (tile***) malloc(width * sizeof(tile**)); int x, y; for (x = 0; x < width; x++) { m->tiles[x] = (tile**) malloc(height * sizeof(tile*)); for(y = 0; y < height; y++) { m->tiles[x][y] = newTile(x, y); } } return m; }
/* ** If the current tile is full (nCell == nMaxCell), then ** this function is called to get a new, empty tile. */ ILCTILE ilcExtend(ILC ilc) { assert( ilc != NULL ); assert( ilc->tile != NULL ); assert( ilc->first != NULL ); assert( ilc->tile->nCell == ilc->tile->nMaxCell ); ilc->nPrevious += ilc->tile->nCell; /* Use the next tile if it exists, or create a new one */ if ( ilc->tile->next != NULL ) { ilc->tile = ilc->tile->next; ilc->tile->nCell = 0; } else { ilc->tile = ilc->tile->next = newTile(ilc->tile); } return ilc->tile; }
/* ** If the current tile is full (nItems == nMaxItems), then ** this function is called to get a new, empty tile. */ CLTILE clExtend(CL cl) { assert( cl != NULL ); assert( cl->tile != NULL ); assert( cl->first != NULL ); assert( cl->tile->nItems == cl->tile->nMaxItems ); cl->nPrevious += cl->tile->nItems; /* Use the next tile if it exists, or create a new one */ if ( cl->tile->next != NULL ) { cl->tile = cl->tile->next; cl->tile->nItems = 0; } else { cl->tile = cl->tile->next = newTile(cl->tile); } return cl->tile; }
void ilcInitialize(ILC *ilc) { *ilc = malloc(sizeof(struct ilcContext)); assert( *ilc != NULL ); (*ilc)->first = (*ilc)->tile = newTile(NULL); (*ilc)->nPrevious = 0; }
/** * Get the tile id for the OpenStreetMap coordinates */ int FlyerMapBuilder::getTile(int x,int y) { // Load default texture if it has not been loaded yet if(0 == defaultTexture) { defaultTexture = loadTextureRAW("default.bmp"); } // Outside bounds of map if(x < 0 || y < 0 || x > numTiles - 1 || y > numTiles - 1) { return defaultTexture; } { sf::Lock DownloadLock(DownloadMutex); // Find Row OSMTextureMap::iterator firstKey = downloadedTiles.find(x); if(downloadedTiles.end() != firstKey) { // Find Column TextureMap::iterator secondKey = (*firstKey).second.find(y); if((*firstKey).second.end() != secondKey) { // Texture hasn't been loaded yet if(0 == (*secondKey).second) { GLuint result = loadTextureRAW(builderThreadA.getFilenameString(zoom,x,y)); if(result != 0) { (*secondKey).second = result; return (*secondKey).second; } } // Texture is downloading else if(-1 == (*secondKey).second) { return defaultTexture; } // Texture is loaded else { return (*secondKey).second; } } } } // Texture has not been found or is not being downloaded std::pair<int,int> newTile(x,y); { sf::Lock DownloadLock(DownloadMutex); downloadedTiles[x][y] = -1; } // Put texture in queue to be downloaded { sf::Lock QueueLock(QueueMutex); tileQueue.push_back(newTile); } return defaultTexture; }
int isCloser(robot *r, tile *t1, tile *t2) { int est1 = costEstimate(newTile(r->posX, r->posY), t1), est2 = costEstimate(newTile(r->posX, r->posY), t2); return (est1 < est2); }
void clInitialize(CL *cl) { *cl = malloc(sizeof(struct clContext)); assert( *cl != NULL ); (*cl)->first = (*cl)->tile = newTile(NULL); (*cl)->nPrevious = 0; }
// Parse a single background void parseBackground(TiXmlElement *bgXML, FILE *output, int bgNo, uint32_t pal, bool defPal) { // Vector of vectors to store all the ids vector< vector<bgTile> > tiles; // This map corresponds the zbe palette indices with the background palette indices. // It's first-come-first-serve here map<uint32_t, uint16_t> palConv; // Process each row TiXmlElement *bgRowXML = bgXML->FirstChildElement("row"); debug("\tReading background map (tileId, paletteId, hflip, vflip):\n"); unsigned int maxWidth = 0; unsigned int h = 0; uint8_t numPalettes = 0; while (bgRowXML) { ++h; debug("\t\t"); // Vector of ids vector<bgTile> rowTiles; // Process each tile unsigned int w = 0; TiXmlElement *bgTileXML = bgRowXML->FirstChildElement("tile"); while (bgTileXML) { ++w; // Get attributes and fill in the bgTile int tileId, palId = pal, hflip = 0, vflip = 0; // tileId is required if (!getIntAttr(bgTileXML, "id", tileId)) { fprintf(stderr, "ERROR: no tileId defined for tile %d in row %d of background %d\n", w, h, bgNo); exit(EXIT_FAILURE); } // palId is only required if defPal is false if (!getIntAttr(bgTileXML, "palette", palId)) { if (!defPal) { fprintf(stderr, "ERROR: no palette defined for tile %d in row %d of background %d\n", w, h, bgNo); fprintf(stderr, "\t This is requried because no default palette defined in the <background> tag.\n"); exit(EXIT_FAILURE); } } // If this palette is not in the map, add it if (palConv.find(palId) == palConv.end()) { palConv[palId] = numPalettes; ++numPalettes; } // Now change the palId over to reference the appropriate bgPalette id palId = palConv[palId]; // hflip and vflip are optional, defaulting to false getIntAttr(bgTileXML, "hflip", hflip); getIntAttr(bgTileXML, "vflip", vflip); // debug printing made easy debug("(%d %d %d %d) ", tileId, palId, hflip, vflip); // make a new bgTile and add it to the vector bgTile newTile(tileId, palId, hflip == 1, vflip == 1); rowTiles.push_back(newTile); // Get the next tile bgTileXML = bgTileXML->NextSiblingElement("tile"); } // See if that set a new record for maxWidth if (w > maxWidth) maxWidth = w; // Add this row of tiles to the vector of vectors tiles.push_back(rowTiles); debug("\n"); // Get next row bgRowXML = bgRowXML->NextSiblingElement("row"); } // Take the maximum defined width to be the width of the map // (We'll fill in the blanks below) unsigned int w = maxWidth; // Determine the size of the bg debug("\tGot a %d tile x %d tile background\n", w, h); fwrite<uint32_t>(w, output); fwrite<uint32_t>(h, output); // Number of palettes used fwrite<uint8_t>(numPalettes, output); debug("\t%d Palettes used\n", palConv.size()); // Add the palette correspondence ids to the file for (uint16_t i = 0; i < palConv.size(); i++) { // Maps keep things sorted, so we have to actually find the one where it->second == i for ( map<uint32_t, uint16_t>::iterator it = palConv.begin() ; it != palConv.end(); it++ ) { if (it->second == i) { // Just need to write the zbe palette id fwrite<uint32_t>(it->first, output); // echo that debug("\t\tzbe Assets Palette %d = Background Palette %d\n", it->second, it->first); } } } // Write all those uint16_t datas debug("\t"); fpos_t mapLenPos = tempVal<uint32_t>("Background Map Length", output); uint32_t mapLen = 0; debug("\tWriting background map:\n"); for (unsigned int i = 0; i < h; i++) { debug("\t\t"); for (unsigned int j = 0; j < w; j++) { // Make sure to get a value within the range of the vectors uint16_t toWrite = 0; if ( i < tiles.size() && j < tiles[i].size() ) toWrite = tiles[i][j].getTile(); // Write it to the file debug("%x\t", toWrite); fwrite<uint16_t>(toWrite, output); // 16 bits = 2 bytes mapLen += 2; } debug("\n"); } goWrite<uint32_t>(mapLen, output, &mapLenPos); debug("\tBackground map length: %dB\n", mapLen); }