void DisplayNcurses::printMapTile(Map &m, unsigned int row, unsigned int col) const { if ( m.hasObstacle( row, col )) { printTile( row, col, 2, " " ); } else if ( m.isExit( row, col )) { printTile( row, col, 3, "S" ); } else if ( m.isEntry( row, col )) { printTile( row, col, 4, "E" ); } }
void createRandomTile(struct tile ** board, int size, int pos_rel, int multi) { time_t t; srand((unsigned) time(&t)); //randomize the col and row position int row = rand() % size; int col = rand() % size; int value = board[row][col].value; //check if the board is filled up bool filled = fillUpTile(board, size); //that position is blank if (value == 0) { board[row][col].value = random_2_4(); printTile(board[row][col], row, col, pos_rel); printValue(board, size, pos_rel); } else { //the random tile is not empty and the board has not been filled up while (value != 0 && filled == false) { row = rand() % size; col = rand() % size; //the random tile is not empty if (board[row][col].value != 0) { value = board[row][col].value; filled = fillUpTile(board, size); } else { //random between value 2 and 4 board[row][col].value = random_2_4(); printTile(board[row][col], row, col, pos_rel); printValue(board, size, pos_rel); filled = fillUpTile(board, size); break; } } } //check the board has possible moves bool move = canMove(board, size); //the board is filled up and there is no move left for 1 player only if (multi == 0) { if (filled == true && move == false) { //delete win mvprintw(25, 2, " "); //print game over mvprintw(15, 2, "Game over!!"); } } }
bool moveTileLeft(struct tile **board, int size, int pos_rel) { int current_row; int current_col; int new_col = 0; bool moved = false; struct tile node; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { if (board[i][j].value != 0 && j != 0) { current_row = i; current_col = j; //copy the current tile into node node = board[i][j]; board[i][j].value = 0; //previous tile is not empty if (board[i][j - 1].value != 0) { new_col = current_col; } //move left until meet the tile that is not empty or meet the Wall while (board[i][j - 1].value == 0) { new_col = j - 1; moved = true; if (new_col != 0) { j--; } else { break; } } board[current_row][new_col] = node; cleanTile(node, current_row, current_col, pos_rel); printTile(node, current_row, new_col, pos_rel); } } } return moved; }
bool moveTileRight(struct tile **board, int size, int pos_rel) { bool moved = false; int current_row; int current_col; int new_col; struct tile node; //move tile for (int i = 0; i < size; i++) { for (int j = size - 1; j >= 0; j--) { //that tile is not empty and not at the end of the board if (board[i][j].value != 0 && j != size - 1) { current_row = i; current_col = j; //copy the current tile into node. node = board[i][j]; board[i][j].value = 0; //not move if (board[i][j + 1].value != 0) { new_col = current_col; } //check the next tile to be empty or not while (board[i][j + 1].value == 0) { new_col = j + 1; moved = true; if (new_col != size - 1) { j++; } else { break; } } board[current_row][new_col] = node; //clean 4 corners of the old tile cleanTile(node, current_row, current_col, pos_rel); printTile(node, current_row, new_col, pos_rel); } } } return moved; }
bool moveTileUp(struct tile **board, int size, int pos_rel) { int current_col; int current_row; int new_row; bool moved = false; struct tile node; //traverse from col to col for (int col = 0; col < size; col++) { for (int row = 0; row < size; row++) { if (board[row][col].value != 0 && row != 0) { current_col = col; current_row = row; node = board[row][col]; board[row][col].value = 0; //the upper tile is not empty if (board[row - 1][col].value != 0) { new_row = row; } //move up until meet the tile that is not empty while (board[row - 1][col].value == 0) { new_row = row - 1; moved = true; if (new_row != 0) { row = row - 1; } else { break; } } board[new_row][current_col] = node; cleanTile(node, current_row, current_col, pos_rel); printTile(node, new_row, current_col, pos_rel); } } } return moved; }
bool moveTileDown(struct tile **board, int size, int pos_rel) { int current_col; int current_row; int new_row; bool moved = false; struct tile node; for (int col = 0; col < size; col++) { for (int row = size - 1; row >= 0; row--) { //the tile is not empty if (board[row][col].value != 0 && row != size - 1) { current_row = row; current_col = col; node = board[row][col]; board[row][col].value = 0; //lower tile is not empty if (board[row + 1][col].value != 0) { new_row = row; } while (board[row + 1][col].value == 0) { new_row = row + 1; moved = true; if (new_row != size - 1) { row = row + 1; } else { break; } } board[new_row][current_col] = node; cleanTile(node, current_row, current_col, pos_rel); if (node.value != 0) { printTile(node, new_row, current_col, pos_rel); } } } } return moved; }
int main(int argc, char **argv) { if ( argc < 2 ) { err( "usage: gimp_palette_convert <*.map> <output>" ); } unsigned int data_sz = 0; char * data = NULL; if ( !(data=get_gimp_palette( argv[1], &data_sz)) ) { char buf[100]; sprintf( buf, "couldn't get palette: \"%s\"\n", argv[1] ); err( buf ); } // what to call output char outfile[256]; if ( argc > 2 ) { strcpy( outfile, argv[2] ); } else { sprintf( outfile, "%s.map", argv[1] ); } FILE *fp = fopen( outfile, "wb" ); header( fp ); printf( "data size of input file: %u\n", data_sz ); char * p = data; unsigned int i = 0; while ( i < data_sz ) { // trim any white space i += trim_whitespace( &p, i, data_sz ); if ( i >= data_sz ) break; // get length of line to newline int len = linelen( p, i, data_sz ); // check if line is a comment, if so remove it remove_comment( &p, &i, &len, data_sz ); if ( i >= data_sz ) break; // get a line char line[256]; strncpy( line, p, len ); line[len] = '\0'; // translate line values into 1 MapTile definition and print it printTile( line, len, fp ); // advance past end of line, to first character past the '\n' //i += eatline( &data ); p += len; i += len; } fclose( fp ); free( data ); printf( "creating: %s\n", outfile ); return 0; }