nxtIO::LoaderError nxtIO::read_number( unsigned int &result, bool skip ){ if( skip ) skip_whitespace(); char c = peek(); if( std::isdigit( c ) ){ RETURN_ON_LOADER_ERROR( ReadBytes( &c, 1 ) ); if( c == '0' ){ //It is either in octal or hex and it is not the number "0" result = 0; c = peek(); if( c == 'x' || c == 'X' ){ RETURN_ON_LOADER_ERROR( ReadBytes( &c, 1 ) ); //In hex while( remaining_size() ){ c = peek(); unsigned int hex; if( !hex2number( c, hex ) ) break; //Was valid, add and move to next character result = result * 16 + hex; RETURN_ON_LOADER_ERROR( ReadBytes( &c, 1 ) ); } } else if( std::isdigit( c ) ){ //In octal while( remaining_size() && std::isdigit( peek() ) && c < '8' ){ result *= 8; RETURN_ON_LOADER_ERROR( ReadBytes( &c, 1 ) ); result += c - '0'; } } return LDR_SUCCESS; } else{ //The number is in decimal result = c - '0'; //Add first digit while( remaining_size() && std::isdigit( peek() ) ){ result *= 10; RETURN_ON_LOADER_ERROR( ReadBytes( &c, 1 ) ); result += c - '0'; } return LDR_SUCCESS; } } else return LDR_TEXT_INVALID; return LDR_SUCCESS; }
void parser_base::shrink_stream() { // Skip any leading blanks. skip_blanks(); if (!remaining_size()) return; // Skip any trailing blanks. skip_blanks_reverse(); // Skip leading <!-- if present. const char* com_open = "<!--"; size_t com_open_len = std::strlen(com_open); if (remaining_size() < com_open_len) // Not enough stream left. Bail out. return; const char* p = mp_char; for (size_t i = 0; i < com_open_len; ++i, ++p) { if (*p != com_open[i]) return; next(); } mp_char = p; // Skip leading blanks once again. skip_blanks(); // Skip trailing --> if present. const char* com_close = "-->"; size_t com_close_len = std::strlen(com_close); size_t n = remaining_size(); if (n < com_close_len) // Not enough stream left. Bail out. return; p = mp_char + n; // move to the last char. for (size_t i = com_close_len; i > 0; --i, --p) { if (*p != com_close[i-1]) return; } mp_end -= com_close_len; skip_blanks_reverse(); }
void parser_base::skip_blanks_reverse() { const char* p = mp_char + remaining_size(); for (; p != mp_char; --p, --mp_end) { if (!is_blank(*p)) break; } }
nxtIO::LoaderError nxtIO::skip_whitespace(){ while( remaining_size() ){ char c = peek(); if( std::isspace( c ) ){ RETURN_ON_LOADER_ERROR( ReadBytes( &c, 1 ) ); } else if( c == '/' ){ //TODO: implement comments properly while( remaining_size() ){ RETURN_ON_LOADER_ERROR( ReadBytes( &c, 1 ) ); if( c == '\n' ) break; } } else return LDR_SUCCESS; } return LDR_SUCCESS; }
nxtIO::LoaderError nxtIO::read_text( std::string &text, bool skip ){ if( skip ) skip_whitespace(); text = ""; while( remaining_size() && std::isalpha( peek() ) ){ char c; RETURN_ON_LOADER_ERROR( ReadBytes( &c, 1 ) ); text += c; } return LDR_SUCCESS; }
char *find( const char *filename, size_t &out_size ) { size_t name_hash = hash_value( filename ); size_t bucket = name_hash % table_size_; // start offset of hash chain size_t offset = get_int( base_ + bucket * int_size ); size_t chain_len = 0; // traverse current hash chain while( offset != 0 ) { validate_offset( offset ); const size_t next_offset = get_int( base_ + offset ); offset += int_size; if( strncmp( filename, base_ + offset, remaining_size( offset ) ) == 0 ) { offset += strlen( filename ) + 1; size_t size = get_int( base_ + offset ); offset += int_size; offset = hash_builder::align(offset); // std::cout << "found: " << filename << " chunk size: " << size << " at " << std::hex << offset << std::dec << " chain: " << chain_len << "\n"; if( remaining_size( offset ) < size ) { throw std::runtime_error( "internal error. premature end of hash file." ); } out_size = size; return base_ + offset; } ++chain_len; offset = next_offset; } // fell through: name not found out_size = size_t(-1); return 0; }
bool parser_base::skip_comment() { char c = cur_char(); if (c != '/') return false; if (remaining_size() > 2 && next_char() == '*') { next(); comment(); skip_blanks(); return true; } return false; }
int main(int argc, char* argv[]){ int x, y, result; int ch; while ((ch = getopt(argc, argv, "bpthro:123T?")) != -1) { switch (ch) { case 'b': show_board_status = FALSE; break; case 'p': show_placed_tile = TRUE; break; case 't': show_available_tiles = FALSE; break; case 'h': show_hint = TRUE; break; case 'r': first_player = 2; break; // player 2 moves first case '1': on_serial = 1; break; // player 1 on serial case '2': on_serial = 2; break; // player 2 on serial case '3': on_serial = 3; break; // both player on serial case 'T': use_tcp = TRUE; break; // use TCP case 'o': move_timeout = atoi(optarg); break; // set timeout case '?': default: usage(); return 0; } } init_serial(); printf(">> %s %s vs %s %s \n", team_ids[0], ((first_player==1) ? "*" : ""), team_ids[1], ((first_player==2) ? "*" : "")); // ------------------------------ // clear board & available pieces for(y=0; y<16; y++) for(x=0; x<16; x++) board[y][x] = 0; for(y=0; y<2; y++) for(x=0; x<21; x++) available[y][x] = 1; // setup board: border is already filled for(x=0; x<16; x++){ board[ 0][ x] = BORDER; board[15][ x] = BORDER; board[ x][ 0] = BORDER; board[ x][15] = BORDER; } #ifdef DEBUG_FILL // for test (some grids already occupied on start) for(y=7; y<=14; y++) for(x=12; x<=14; x++) board[y][x] = 2; for(y=12; y<=14; y++) for(x=7; x<=14; x++) board[y][x] = 2; #endif #ifdef DEBUG_LESS_TILES for(x=5; x<21; x++){ available[0][x] = 0; available[1][x] = 0; } #endif switch(result = do_game()){ int a1, a2; case TERMINATE_NORMAL: a1 = remaining_size(1); a2 = remaining_size(2); printf("** Total remaining size: %d / %d. ", a1, a2); if(a1 != a2) printf("Player %d won the game!\n", ( (a1<a2) ? 1 : 2 ) ); else printf("Draw game.\n"); break; case 1: case 2: printf("** Player %d lost the game by violation.\n", result); break; default: case TERMINATE_WRONG: printf("** Game terminated unexpectedly.\n"); break; } close_serial(); return 0; }
int do_game(){ int c, x, y, r, player, turn; char code[6], prev_code[6]; // ------------------------------------------------------------ // start! show_board(); player = first_player; turn = 0; prev_code[0] = 0; while(prompt(player, code, prev_code, !check_possibility(player, turn), turn)){ move m; int e, x_offset, y_offset; if(code[0] == 0) return TERMINATE_WRONG; // ctrl+d // retry if invalid code while(!check_code(code)){ if(interactive(player)){ prompt(player, code, prev_code, FALSE, turn); } else { printf("Invalid move on serial port.\n"); return player; } if(code[0] == 0) return TERMINATE_WRONG; } if(code[0] == 0) return TERMINATE_WRONG; // ctrl+d if(prev_code[0] == 0) strcpy(p1move1, code); if(strcmp(prev_code, "0000") == 0 && strcmp(code, "0000") == 0){ printf("Both pass!\n"); show_hint = FALSE; if(! (check_possibility(player, turn) || check_possibility(next_player(player), turn-1))){ return TERMINATE_NORMAL; } return TERMINATE_NORMAL; // is this OK? } strcpy(prev_code, code); // pass if(strcmp(code, "0000") == 0){ if(turn >= 2){ player = next_player(player); turn++; continue; } else { printf("First move must not be a pass.\n"); return player; } } m = decode_code(code); c = m.piece; r = m.rotate; x_offset = m.x-2; y_offset = m.y-2; if((e = check_move(player, turn, m)) != 0){ show_error(e); return player; } // OK, now place the move for(y=0; y<5; y++){ for(x=0; x<5; x++){ int b; b = pieces[c][0][rotate[r][y][x]]; if (b==1) board[y_offset+y][x_offset+x] = player; } } available[player-1][c] = 0; if(remaining_size(player)==0){ printf("Player %d won the game, since the player has no more pieces.\n", player); return TERMINATE_NORMAL; } // show the board & next player show_board(); player = next_player(player); turn++; } printf("Player %d timed out.\n", player); return player; }