//新規ボール作成 BallSprite* GameLayer::newBalls(BallSprite::PositionIndex positionIndex, bool visible) { //現在のタグを取得 int currentTag = BallSprite::generateTag(positionIndex); //乱数を元に、ランダムでタイプを取得 int ballType; while (true) { ballType = _distForBall(_engine); if (!visible) break; //妥当性のチェック(ボールが隣り合わせにならないようにする) //左隣のボール auto ballX1Tag = currentTag - 10; //1つ左隣は10引いた値 auto ballX2Tag = currentTag - 20; //2つ左隣は20引いた値 auto ballX1 = (BallSprite*)(getChildByTag(ballX1Tag)); auto ballX2 = (BallSprite*)(getChildByTag(ballX2Tag)); //現在のボールが、1つ左隣と2つ左隣のボールと同じだとNG if (!(ballX1 && ballType == (int)ballX1->getBallType()) || !(ballX2 && ballType == (int)ballX2->getBallType())) { //下隣のボール auto ballY1Tag = currentTag - 1; //1つ下隣は1引いた値 auto ballY2Tag = currentTag - 2; //2つ下隣は2引いた値 auto ballY1 = (BallSprite*)(getChildByTag(ballY1Tag)); auto ballY2 = (BallSprite*)(getChildByTag(ballY2Tag)); //現在のボールが、1つ下隣と2つ下隣のボールと同じだとNG if (!(ballY1 && ballType == (int)ballY1->getBallType()) || !(ballY2 && ballType == (int)ballY2->getBallType())) { //左隣と下隣が揃わない場合は、ループを抜ける break; } } } //ボールの表示 auto ball = BallSprite::create((BallSprite::BallType)ballType, visible); ball->setPositionIndexAndChangePosition(positionIndex); addChild(ball, ZOrder::Ball); return ball; }
//指定方向のボールと同じ色かチェックする bool GameLayer::isSameBallType(BallSprite::PositionIndex current, Direction direction) { //全てのボールのBallTypeを取得 auto allBalls = getAllBalls(); if (direction == Direction::x) { if (current.x + 1 > BALL_NUM_X) //列が存在しない場合は抜ける return false; } else { if (current.y + 1 > BALL_NUM_Y) //行が存在しない場合は抜ける return false; } //現在のボールを取得 int currentTag = BallSprite::generateTag(BallSprite::PositionIndex(current.x, current.y)); BallSprite* currentBall = allBalls.at(currentTag); //次のボールを取得 int nextTag; if (direction == Direction::x) nextTag = BallSprite::generateTag(BallSprite::PositionIndex(current.x + 1, current.y)); else nextTag = BallSprite::generateTag(BallSprite::PositionIndex(current.x, current.y + 1)); auto nextBall = allBalls.at(nextTag); if (currentBall->getBallType() == nextBall->getBallType()) //次のボールが同じBallTypeである return true; return false; }
WORD Player::checkScored(ProgramManager &rProgramManager, GameInfo &rGameInfo) { WORD ret_val = 0; for(size_t i = 0; i < NUMBER_OF_BALLS; i++) if(tmpBallsStatus_[i] != rProgramManager.getScored()[i]) { BallType ballType = getBallType(static_cast<Balls::Ball>(i)); if(!rGameInfo.firstScore && ballType != BallType::Zero) { rGameInfo.firstScore = !rGameInfo.firstScore; ballType_ = ballType; rGameInfo.ballType2 = (ballType_ == BallType::Solid)? BallType::Striped : BallType::Solid; } else if(ballType_ == BallType::NoType && rGameInfo.firstScore) ballType_ = rGameInfo.ballType2; switch(ballType) { case BallType::Zero: rProgramManager.updateZero(); rGameInfo.scoredZero = TRUE; if(score_ != 0) score_--; break; case BallType::Solid: if(i == Balls::Ball::eighth) { rGameInfo.scoredEight = TRUE; score_ = 0; return FALSE; } else { if(ballType_ == BallType::Solid) { rGameInfo.scored = TRUE; ret_val++; } else { rGameInfo.scoredWrong = TRUE; rGameInfo.wrongBall = static_cast<Balls::Ball>(i); if(score_ != 0) score_--; } } break; case BallType::Striped: if(ballType_ == BallType::Striped) ret_val++; else { rGameInfo.scoredWrong = TRUE; rGameInfo.wrongBall = static_cast<Balls::Ball>(i); if(score_ != 0) score_--; } break; default: PostQuitMessage(-999); } } return ret_val; }