std::bitset<CELLS> PawnState::getCellsToMove(int row, int col, const BoardBase &board) const { std::bitset<CELLS> moves; if(getPieceParty() == PieceParty::BLACK) { if(!isMoved() && board.getPieceTypeAt(INDEX(row + 1, col)) == PieceType::NONE) { setBit(INDEX(row + 2, col), moves); } setBit(INDEX(row + 1, col), moves); } else if(getPieceParty() == PieceParty::WHITE) { if(!isMoved() && board.getPieceTypeAt(INDEX(row - 1, col)) == PieceType::NONE) { setBit(INDEX(row - 2, col), moves); } setBit(INDEX(row - 1, col), moves); } return moves; }
bool Pawn::canMove(int x, int y, int move_x, int move_y) { if(isPromoted()) { return move_x == 0 || move_y == 0 || abs(move_x)==abs(move_y); } int dir = getSide()==0?-1:1; if(move_x == 0) { if(board_->get(x,y+dir) != NULL) { return false; } if(move_y == dir) { return true; } if(!isMoved() && move_y==dir*2) { return board_->get(x,y+move_y)==NULL; } return false; } if(move_y != dir) { return false; } if(abs(move_x) != 1) { return false; } Man *target = board_->get(x+move_x,y+move_y); return target?target->getSide()!=getSide():false; }
bool Pawn::isJump() { if(isPromoted()) { return false; } if(!isMoved()) { return false; } vector<Pos>::reverse_iterator it = move_history_.rbegin(); int y0 = (*it++).second; int y1 = (*it).second; return abs(y0-y1)==2; }
bool Hal::CheckAndAct() { bool sendResult; //Read the state of the microSwitch if (microSwitch.isChanged()) { debugPrint("Switch state: "); debugPrintLn(microSwitch.ReadState()); uint8_t buf[128]; size_t message_length; NodeMessage nodemsg = NodeMessage_init_zero; /* Create a stream that will write to our buffer. */ pb_ostream_t stream = pb_ostream_from_buffer(buf, sizeof(buf)); nodemsg.reading.funcs.encode = &switchsensor_callback; /* Now we are ready to encode the message! */ /* Then just check for any errors.. */ if (!pb_encode(&stream, NodeMessage_fields, &nodemsg)) { debugPrint("Encoding failed: "); debugPrintLn(PB_GET_ERROR(&stream)); } else { message_length = stream.bytes_written; debugPrint("message_length:"); debugPrintLn(message_length); debugPrint("message:<"); for (uint8_t i = 0; i < message_length; i++) { debugPrint(buf[i], HEX); if (i < message_length - 1) debugPrint(" "); } debugPrintLn(">"); } sendResult = HalImpl.sendMessage(buf, message_length); // reset the Time Passed flag alive.setCurrentTime(); } //Read the state of the microSwitch if (isMoved()) { debugPrintLn("Box moved: "); uint8_t buf[128]; size_t message_length; NodeMessage nodemsg = NodeMessage_init_zero; /* Create a stream that will write to our buffer. */ pb_ostream_t stream = pb_ostream_from_buffer(buf, sizeof(buf)); nodemsg.reading.funcs.encode = &movesensor_callback; /* Now we are ready to encode the message! */ /* Then just check for any errors.. */ if (!pb_encode(&stream, NodeMessage_fields, &nodemsg)) { debugPrint("Encoding failed: "); debugPrintLn(PB_GET_ERROR(&stream)); } else { message_length = stream.bytes_written; debugPrint("message_length:"); debugPrintLn(message_length); debugPrint("message:<"); for (uint8_t i = 0; i < message_length; i++) { debugPrint(buf[i], HEX); if (i < message_length - 1) debugPrint(" "); } debugPrintLn(">"); } sendResult = HalImpl.sendMessage(buf, message_length); hasMoved = false; // reset the Time Passed flag alive.setCurrentTime(); } // check alive timer if (alive.isTimePassed()) { // reset the Time Passed flag alive.resetTimePassed(); debugPrint("LTC values: mAh "); debugPrint(ltc.getCharge(), 4); debugPrint(F(" mAh\t")); debugPrint(F("Current ")); debugPrint(ltc.getCurrent(), 4); debugPrint(F(" A\t")); debugPrint(F("Voltage ")); debugPrint(ltc.getVoltage(), 4); debugPrint(F(" V\t")); debugPrint(F("Temperature ")); debugPrint(ltc.getTemp(), 4); debugPrint(F(" C\n")); debugPrint("HTU values: "); debugPrint(F("Temperature ")); debugPrint(htu.getTemp(), 4); debugPrint(F(" C")); debugPrint(F("Humidity ")); debugPrint(htu.getHum(), 4); debugPrint(F(" %\n")); uint8_t buf2[128]; size_t message_length; NodeMessage nodemsg2 = NodeMessage_init_zero; /* Create a stream that will write to our buffer. */ pb_ostream_t stream2 = pb_ostream_from_buffer(buf2, sizeof(buf2)); // add the temperature data to the output buffer nodemsg2.reading.funcs.encode = &tempsensor_callback; /* Now we are ready to encode the message! */ /* Then just check for any errors.. */ if (!pb_encode(&stream2, NodeMessage_fields, &nodemsg2)) { debugPrint("Encoding failed: "); debugPrintLn(PB_GET_ERROR(&stream2)); } else { message_length = stream2.bytes_written; debugPrint("message_length:"); debugPrintLn(message_length); debugPrint("message:<"); for (uint8_t i = 0; i < message_length; i++) { debugPrint(buf2[i], HEX); if (i < message_length - 1) debugPrint(" "); } debugPrintLn(">"); } // add the humidity data to the output buffer nodemsg2.reading.funcs.encode = &humsensor_callback; /* Now we are ready to encode the message! */ /* Then just check for any errors.. */ if (!pb_encode(&stream2, NodeMessage_fields, &nodemsg2)) { debugPrint("Encoding failed: "); debugPrintLn(PB_GET_ERROR(&stream2)); } else { message_length = stream2.bytes_written; debugPrint("message_length:"); debugPrintLn(message_length); debugPrint("message:<"); for (uint8_t i = 0; i < message_length; i++) { debugPrint(buf2[i], HEX); if (i < message_length - 1) debugPrint(" "); } debugPrintLn(">"); } // add the voltage data to the output buffer nodemsg2.reading.funcs.encode = &voltsensor_callback; /* Now we are ready to encode the message! */ /* Then just check for any errors.. */ if (!pb_encode(&stream2, NodeMessage_fields, &nodemsg2)) { debugPrint("Encoding failed: "); debugPrintLn(PB_GET_ERROR(&stream2)); } else { message_length = stream2.bytes_written; debugPrint("message_length:"); debugPrintLn(message_length); debugPrint("message:<"); for (uint8_t i = 0; i < message_length; i++) { debugPrint(buf2[i], HEX); if (i < message_length - 1) debugPrint(" "); } debugPrintLn(">"); } // add the current data to the output buffer nodemsg2.reading.funcs.encode = ¤tsensor_callback; /* Now we are ready to encode the message! */ /* Then just check for any errors.. */ if (!pb_encode(&stream2, NodeMessage_fields, &nodemsg2)) { debugPrint("Encoding failed: "); debugPrintLn(PB_GET_ERROR(&stream2)); } else { message_length = stream2.bytes_written; debugPrint("message_length:"); debugPrintLn(message_length); debugPrint("message:<"); for (uint8_t i = 0; i < message_length; i++) { debugPrint(buf2[i], HEX); if (i < message_length - 1) debugPrint(" "); } debugPrintLn(">"); } // add the charge data to the output buffer nodemsg2.reading.funcs.encode = &chargesensor_callback; /* Now we are ready to encode the message! */ /* Then just check for any errors.. */ if (!pb_encode(&stream2, NodeMessage_fields, &nodemsg2)) { debugPrint("Encoding failed: "); debugPrintLn(PB_GET_ERROR(&stream2)); } else { message_length = stream2.bytes_written; debugPrint("message_length:"); debugPrintLn(message_length); debugPrint("message:<"); for (uint8_t i = 0; i < message_length; i++) { debugPrint(buf2[i], HEX); if (i < message_length - 1) debugPrint(" "); } debugPrintLn(">"); } // only send the number of retries if it is > 0 if (getNumTxRetries() > 0) { // add the retry counter data to the output buffer nodemsg2.reading.funcs.encode = &retriessensor_callback; /* Now we are ready to encode the message! */ /* Then just check for any errors.. */ if (!pb_encode(&stream2, NodeMessage_fields, &nodemsg2)) { debugPrint("Encoding failed: "); debugPrintLn(PB_GET_ERROR(&stream2)); } else { message_length = stream2.bytes_written; debugPrint("message_length:"); debugPrintLn(message_length); debugPrint("message:<"); for (uint8_t i = 0; i < message_length; i++) { debugPrint(buf2[i], HEX); if (i < message_length - 1) debugPrint(" "); } debugPrintLn(">"); } } // send the alive message sendResult = HalImpl.sendMessage(buf2, message_length); // if the send was succesfull then the retry counter can be reset if (sendResult) { setNumTxRetries(0); } } }
void Pawn::updatePossibleMoves() { possible_move_.clear(); int x = move_history_.back().first; int y = move_history_.back().second; if(isPromoted()) { checkLine(x,y, -1,-1); checkLine(x,y, -1, 0); checkLine(x,y, -1, 1); checkLine(x,y, 0,-1); checkLine(x,y, 0, 1); checkLine(x,y, 1,-1); checkLine(x,y, 1, 0); checkLine(x,y, 1, 1); } else { Man *target = NULL; int next_x, next_y; switch(player_) { case 0: next_x = x; next_y = y-1; break; case 1: next_x = x; next_y = y+1; break; } if(board_->isInBounds(next_x, next_y) && !board_->get(next_x, next_y)) { possible_move_.push_back(pair<int,int>(next_x,next_y)); if(!isMoved()) { switch(player_) { case 0: next_x = x; next_y = y-2; break; case 1: next_x = x; next_y = y+2; break; } if(board_->isInBounds(next_x, next_y) && !board_->get(next_x, next_y)) { possible_move_.push_back(pair<int,int>(next_x,next_y)); } } } switch(player_) { case 0: next_x = x+1; next_y = y-1; break; case 1: next_x = x-1; next_y = y+1; break; } if(board_->isInBounds(next_x, next_y)) { Man *target = board_->get(next_x, next_y); if(target) { if(target->getSide() != getSide()) { possible_move_.push_back(pair<int,int>(next_x,next_y)); } } else { switch(player_) { case 0: target = board_->get(next_x, next_y+1); break; case 1: target = board_->get(next_x, next_y-1); break; } /** Pawn *pawn = dynamic_cast<Pawn*>(board_->getLastMoved()); if(pawn && pawn->isJump() && pawn == target) { possible_move_.push_back(pair<int,int>(next_x,next_y)); } /**/ } } switch(player_) { case 0: next_x = x-1; next_y = y-1; break; case 1: next_x = x+1; next_y = y+1; break; } if(board_->isInBounds(next_x, next_y)) { Man *target = board_->get(next_x, next_y); if(target) { if(target->getSide() != getSide()) { possible_move_.push_back(pair<int,int>(next_x,next_y)); } } else { switch(player_) { case 0: target = board_->get(next_x, next_y+1); break; case 1: target = board_->get(next_x, next_y-1); break; } /** Pawn *pawn = dynamic_cast<Pawn*>(board_->getLastMoved()); if(pawn && pawn->isJump() && pawn == target) { possible_move_.push_back(pair<int,int>(next_x,next_y)); } /**/ } } } }