Exemple #1
0
void Board2048::runGame(){
	srand(time(0));
try
{
	/* check board */
	if(board.size()==0){
		throw exception();
	}
	/* rand a key to put on the board */
	randKey(2);
	/* Show the initial board */
	assert(getKeyNum()==2);
	showBoard('N');
	while(1){
		/* get input char */
		getInput();
		if(b_quit == 'q')
		  	break;
		else if(b_quit=='n'){
			b_quit = ' ';
			continue;
		}
		moveBoard();
		randKey(1);
		showBoard('N');
		checkOver();
		if(b_quit == 'q')
			break;
	}
	cout<<endl;
}catch(exception e){
	cout<<"Board was NOT initialized"<<endl;
}
}
void TCompGuiDrag::Enabled()
{
	if (checkEnabled()) {
		if (checkOver() && !controller->IsGuiItemDragged()) {
			ChangeState(STRING(Over));
			notifyOver(true);
		}
		else render_state = RSTATE_ENABLED;
	}
}
void TCompGuiButton::Enabled()
{
	//checkOver --> Over
	if (checkEnabled()) {
		if (checkOver()) {
			ChangeState(STRING(Over));
			render_state_target = RSTATE_OVER;
			notifyOver(true);
		}
		else render_state_target = RSTATE_ENABLED;
	}
}
void TCompGuiButton::Clicked()
{
	if (checkEnabled()) {
		if (!checkOver()) {
			ChangeState(STRING(Enabled));
			render_state_target = RSTATE_ENABLED;
			notifyOver(false);
		}
		else if (checkReleased()) {
			setReleased();
			//logic_manager->throwEvent(CLogicManagerModule::EVENT::OnClicked, MY_NAME, MY_OWNER);
		}
	}
}
void TCompGuiButton::Over()
{
	if (checkEnabled()) {
		if (!checkOver()) {
			ChangeState(STRING(Enabled));
			render_state_target = RSTATE_ENABLED;
			notifyOver(false);
		}
		else if (checkClicked()) {
			ChangeState(STRING(Clicked));
			render_state = RSTATE_OVER;
			render_state_target = RSTATE_CLICKED;
			logic_manager->throwEvent(CLogicManagerModule::EVENT::OnButtonPressed, MY_NAME, MY_OWNER);
		}
	}
}
bool action(struct GameBoard board, int columnNum, bool isItX) {
  
        if (columnNum != 1 && columnNum != 2 &&
                columnNum != 3 && columnNum != 4 &&
                columnNum != 5 && columnNum != 6 && columnNum != 7) {

                printf("That's not a valid move. Please enter a column 1-7, or q to quit.\n");
                return false;
        }

        bool changed = false;

        int column = columnNum-1;

        if (board.board[0][column] == 6) {

                printf("That column is full! Try again.\n");

                return changed;

        } // if the invisible top layer says it's full

        int row = 6 - board.board[0][column];

        if (board.isX) {

                board.board[row][column] = 1;
        } // placing the piece!
        else board.board[row][column] = 2;

        board.board[0][column]++;

        switchIsX(board); // DO WE CALL IT ON BOARD?

        changed = true;

        board.over = checkOver(board, row, column, board.board[row][column]); // REMOVE FINAL ARGUMENT PROBABLY
        printBoard(board, changed);

    if (board.over) return changed;
    
    if ((board.aiOn == true) && (changed)) aiTurn(board);
    
        return changed;

}
int maxLine(struct GameBoard board, int row, int col, bool isX) { // moves, calculates, undoes
    
    int player; // X or 0
    
    if (isX) player = 1; else player = 2;
    
    if (board.board[0][col] == 6) return 0;
    
    board.board[row][col] = player;
    
    checkOver(board, row, col, player); // now this will store, temporarily, the max line available
    
    board.board[row][col] = 0; // undo
    
    //cout << "The max for " << row << "," << col << " is " << getMax() << endl;
    
    return getMax(board);
    
}
void TCompGuiDrag::Over()
{
	if (checkEnabled()) {
		if (checkOver()) {
			if (controller->IsGuiItemDragged()) {
				SetDragState(Clicked);
			}
			else render_state = RSTATE_OVER;
			if (controller->IsLeftPressed()) {
				SetValue(value - 0.1f);
				notifyValue();
			}
			else if (controller->IsRightPressed()) {
				SetValue(value + 0.1f);
				notifyValue();
			}
		}
		else {
			ChangeState(STRING(Enabled));
			notifyOver(false);
		}
	}
}
bool aiTurn(struct GameBoard board) {
    
    board.testing = true;
    
    usleep(1000000);
	// this_thread::sleep_for(std::chrono::seconds(1));

    bool changed;
    
    int score;
    int row;
    int max;
    
    // simulate offense and score
    for (int col = 0; col < 7; col++) {
        
        row = 6 - board.board[0][col];
        if (row == 0) {
            
            board.AI[col] = 10;
            continue;
        }
        // if the entire column is filled, tell AI that it can't go there and keep going
        
        // for all seven rows we now have the row,col location of the drop.
        
        board.max = maxLine(board,row,col,true);
        
        //cout << "Max is " << max << endl;
        
        if (board.max == 4) score = 1;
        else if (board.max == 3) score = 3;
        else if (board.max == 2) score = 5;
        else score = 7;
        
        //cout << "score for " << col << " is set to " << score << endl;
        
        board.AI[col] = score;
        
        
    }
    // simulate defense and score
    for (int col = 0; col < 7; col++) {
        
        row = 6 - board.board[0][col];
        
        // for all seven rows we now have the row,col location of the drop.
        
        board.max = maxLine(board,row,col,false); // checking defense
        
        if (board.max == 4) score = 2;
        else if (board.max == 3) score = 4;
        else if (board.max == 2) score = 6;
        else score = 7;
        
        if (score < board.AI[col]) board.AI[col] = score; // if the score is lower, it is overridden
        
    }
    
    // Now we have an array of seven choices.
    int bestCol = 0;
    int bestScore = 10;
    
    for (int kol = 0; kol < 7; kol++) {
        
        //cout << AI[kol] << endl;

        if (board.AI[kol] < bestScore) {
            
            bestScore = board.AI[kol];
            bestCol = kol;
            
        } // if the score is good, this number becomes the best column
        
    }
    srand(time(NULL));
    if (bestScore >= 7) bestCol = rand() % 6; // == 7 IS HARD MODE. 3 IS EASY MODE.
    //cout << "The best column for me to choose is " << bestCol+1 << endl;
    
    // now, finally, we can place the piece.
    int bestRow = 6 - board.board[0][bestCol];
    
    
    board.board[bestRow][bestCol] = 1; // adding in
    
	board.board[0][bestCol]++; // incrementing the count
    
	switchIsX(board);
    
	changed = true;
        board.testing = false;
    
	board.over = checkOver(board, bestRow, bestCol, board.board[bestRow][bestCol]);
	printBoard(board, changed);

    
    if (board.over) printf("The computer has won! ");
    
    board.testing = false;
    
    return changed;
    
}
Exemple #10
0
void BaseScene::update(float dt){
	if (dt <= 0) dt = Director::getInstance()->getAnimationInterval();

	// 入洞检查
	ballInRightHole();

	// 键盘触发
	if (SceneDesigner::balls){
		for (int i = 0; i < SceneDesigner::balls->size(); i++)
			SceneDesigner::balls->at(i)->applyKeepForce(pressed);
	}

	// 控件更新
	score -= dt * 10;
	scoreLabel->setString(MyTool::num2str(int(score)));

	// 电球的引力和斥力作用
	electricBallAreaEffect();

	// physics update
#ifndef NOBOX2D
	world->Step(dt, 8, 3);
#endif

	dealafterContact();

	if (SceneDesigner::balls){
		std::string s = "";
		for (int i = 0; i < int(SceneDesigner::balls->size()); i++){
			auto ball = SceneDesigner::balls->at(i);
			ball->updatePosWithBody();
			ball->getBody()->SetAngularVelocity(ball->getVelocityValue() / RTM_RATIO / 2 /ball->radius);
		
			if (score < 700){
				/*s+= "s : (" + MyTool::num2str(ball->getSprite()->getPosition().x);
				s += " ," + MyTool::num2str(ball->getSprite()->getPosition().y) + ")		";
				s += "b : (" + MyTool::num2str(ball->getBody()->GetPosition().x * 32);
				s += " ," + MyTool::num2str(ball->getBody()->GetPosition().y * 32) + ")";*/
				s += "( e : " + MyTool::num2str(int(ball->enableBox2d)) + ", s : ";
				if(ball->getSprite()) s += "y , b : ";
				else s += "n , b : ";
				if (ball->getBody()) s += "y ";
				else s += "n ";
				s += " )";
			}
		}
		if (score < 700) {
			auto debugger = ((LabelTTF*)getChildByName("debugger"));
			debugger->setString(s);
		}
	}

	if (lock == 0 && checkOver()){ // 检查是否还有球可进
		int targetScore = 0;
		if (score > targetScore) nextScene();
		else gameover();
	}

	// 游戏结束
	if (lock == 0 && score <= 0){
		gameover();
	}
}
Exemple #11
0
void edgeCurve::calcZipper() {
    mZipperPts.clear();
    
    ofPolyline zb;
    
    for (int ind =0; ind <mVerts.size(); ind++) {
        draggableVertex *v = mVerts[ind];
        int nind = ind+1;
        if (nind < mVerts.size()) {
            int nnind = ind-1;
            if (nnind < 0 ) nnind = 0;
            
            draggableVertex *nv = mVerts[nind];
            draggableVertex *nnv = mVerts[nnind];
            
            if (ind == 0) {
                zb.addVertex(v->pos.x,v->pos.y,v->pos.z);
            }
            
            zb.bezierTo((nnv->pos+v->pos)*0.5, v->pos, (v->pos+nv->pos)*0.5);
            
            
        }
        if (ind == mVerts.size()-1) {
            zb.addVertex(v->pos.x,v->pos.y,v->pos.z);
        }
    }
    
    
    vector < ofPoint > pts;
    vector < ofVec3f > ns;
    float os = mZipperWidth;
    int notch_cnt = 40; // must be an even number
    int step = 8; // must be an even number
    float segs = step*notch_cnt;
    mZipperBase = zb.getResampledByCount(segs);
    for (int i=0; i<segs; i++) {
        float at = i/segs;
        if (at > 1.0) at = 1.0;
        pts.push_back( mZipperBase.getPointAtPercent(at));
        ns.push_back(mZipperBase.getNormalAtIndex(mZipperBase.getIndexAtPercent(at)));
    }
    pts.push_back(mVerts[mVerts.size()-1]->pos);
    ns.push_back(mZipperBase.getNormalAtIndex(mZipperBase.getIndexAtPercent(0.999)));
    
    float gperc = 0.1; // how much of a gap?
    for (int i=0; i<pts.size()/step; i ++) {
        if (i % 2 == 0) {
            for (int j=0; j<step;j++) {
                mZipperPts.push_back(pts[j+i*step]);
            }
        } else {
            mZipperPts.push_back(pts[i*step]);
            
            mZipperPts.push_back(checkOver(pts[i*step]+ns[i*step]*os*gperc,os*gperc));
            
            int from = max(0,(int)(i*step-step*0.5)+1);
            int to = min((int)((pts.size()-1)),(int)(i*step+step*1.5-1));
            // reverse
            for (int j=i*step;j>=from;j--) {
                mZipperPts.push_back(checkOver(pts[j]+ns[j]*os*gperc,os*gperc));
            }
            // top
            for (int j= from; j<=to; j++) {
                mZipperPts.push_back(checkOver(pts[j]+ns[j]*os,os));
            }
            // revese back
            for (int j=to;j>=i*step+step;j--) {
                mZipperPts.push_back(checkOver(pts[j]+ns[j]*os*gperc,os*gperc));
            }
            
            mZipperPts.push_back(pts[(i+1)*step]);
            
        }
    }
    
    if (mSlavedCurve) {
        mSlavedCurve->calcZipper();
        
        mZipperPath->clear();
        mZipperPath->moveTo(mZipperPts[0]);
        for (auto pt : mZipperPts) {
            mZipperPath->lineTo(pt);
        }
        
        for (auto pt : mSlavedCurve->mZipperPts){
            mZipperPath->lineTo(pt);
        }
        
        mZipperPath->close();
        
        if (mHole) {
            mHole->addToPath(mZipperPath);
        }
        
        if (mSlaveHole) {
            mSlaveHole->addToPath(mZipperPath);
        }
    }
    
    
}