0

iPhoneボードゲームを作りました。ユーザーがマルチプレイヤーまたはシングルプレイヤーをプレイするかどうかを選択する機能があります。

UIButtonボードのピースにを使用しました。

修正したいことが 1 つあります。シングル プレイヤー モードでは、AI のターンを に実装しましUIButtonIBAction。しかし、この場合、プレイヤー 1 の移動とプレイヤー 2 の移動の間に時間はありません。両方が同時に実行されます。

私はこれをしたいと思います:人間の動きを即座に行い、コンピューターが彼の動きを考えます。両方の動きが一緒になって、その後コンピューターが動きを考えたくないのです。

問題は、ゲームが IBAction のコード ブロック全体を読み取り、結果を表示するためであることはわかっていますが、これを修正する方法がわかりません。

なにか提案を?

必要な場合は、コードを表示できますが、私の言いたいことを理解していただけると思います (希望もあり)。

それを要求したコメントのために、ここにコードがあります..

-(void)buttonClickedSinglePlayer:(id)送信者{

[self memorizzastatopartitapassato];
[self volumebottoni];
turno = [self whosTurn];
UIButton *bottoneCampoGioco = sender;
UIButton *bottoneIntelligente;

//Checks if it's Human's turn

if (turno == 0) {
    int valoreBottone = [bottoneCampoGioco.currentTitle intValue];
    if(valoreBottone< 10){

        //if controls are ok, it changes value of human's chosen button

        if((bottoneCampoGioco.currentTitleColor != [UIColor greenColor])){
            [UIView transitionWithView:bottoneCampoGioco duration:0.3 options:UIViewAnimationOptionTransitionFlipFromLeft animations:nil completion:nil];
            [bottoneCampoGioco setTitleColor:[UIColor redColor] forState: UIControlStateNormal];
            [bottoneCampoGioco setTitleColor:[UIColor redColor] forState: UIControlStateHighlighted];
            bottoneCampoGioco.titleLabel.shadowColor = [UIColor blackColor];
            [bottoneCampoGioco.titleLabel setShadowOffset:CGSizeMake(1.0f, 1.0f)];                [bottoneCampoGioco setBackgroundImage: [UIImage imageNamed:@"bottonerosso.png"] forState:UIControlStateNormal];

            [self incrementaTitoloOnClick :bottoneCampoGioco];
        }

        //else alert and loop to first line of the code, until he chooses a valid move
        else {
            [self posizionenonconsentita];
            turno = [self whosTurn];
            goto end;
        }
    }
    else{
        int x =[self mossedisponibilirosse];
        if (x== 1) {

            [self analizzavincitore];
        }

            [self numeroMax];
        turno = [self whosTurn];
        goto end;
    }


    turno = 1;
    [self analizzaBottoni:bottoneCampoGioco];
    [self aggiornapunteggi];
    [self memorizzastatopresente];

pragma mark シングル プレイヤー アクション

    //Azione del giocatore singolo

    //changes turn token's value to allow to AI to play

    turno = [self whosTurn];
    turno = 0;

    ai = [[ArtificialIntelligence alloc]init];

    //send to AI's class the human's pressed button


    [ai setbottonepremuto:bottoneCampoGioco];
    [ai setDimensioneInput:dimensioneInput];

    //send the situation of the board

    [ai setSituazione:arraycampogioco];
    [ai setpunteggiogiocatori:volumerossi giocatoree2:volumeverdi];

    //get back the chosen button from ai's class
    bottoneIntelligente=[ai bottone];
    [mossaEffettuata setText:[self stringaCoordinateDaBottone:bottoneIntelligente]];
    int valoreBottone2 = [bottoneIntelligente.currentTitle intValue];

    //then changes the value of the button chosen from ai's class

    if(valoreBottone2< 10){

        [UIView transitionWithView:bottoneIntelligente duration:0.3 options:UIViewAnimationOptionTransitionFlipFromTop animations:nil completion:nil];
        [bottoneIntelligente setTitleColor:[UIColor greenColor] forState: UIControlStateNormal];
        [bottoneIntelligente setTitleColor:[UIColor greenColor] forState: UIControlStateHighlighted];
        bottoneIntelligente.titleLabel.shadowColor = [UIColor blackColor];
        [bottoneIntelligente.titleLabel setShadowOffset:CGSizeMake(1.0f, 1.0f)];
        [bottoneIntelligente setBackgroundImage: [UIImage imageNamed:@"bottoneverde.png"] forState:UIControlStateNormal];

        [self incrementaTitoloOnClick :bottoneIntelligente];
        [self analizzaBottoni:bottoneIntelligente];
        [self aggiornapunteggi];

    }

end:

    turno = [self whosTurn];
}
turno = [self whosTurn];
numeromosse ++;

}

4

1 に答える 1

0

私の理解が正しければ、遅れてからコードの AI セクションを実行する必要があります。コードを別のメソッドにリファクタリングできる場合は、次のperformSelector:withObject:afterDelay:ように使用できます。

[self performSelector:@selector(aiMove) withObject:nil afterDelay:2.0];

これにより、2 秒後にメソッドが実行されます。または、次のような GCD を使用することもできますdispatch_after(以下)。遅延後に実行するコードをブロックに配置します。

double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

 // 
 //Your code goes here
 //

 });
于 2013-04-09T10:49:21.803 に答える