3

我刚刚制作了一个“应用程序”,它只是在命令行(outpot)中打印了一些东西,这是一个例子 -

http://img254.imageshack.us/img254/2853/3cc0a810d7564e39b05bade.png

代码:

卡片.h:

#import <Foundation/Foundation.h>
@interface Card : NSObject {
    int CardValue;
    int CardType;
}
@property int CardValue,CardType;
@end

卡.m:

#import <Foundation/Foundation.h>
@interface Card : NSObject {
    int CardValue;
    int CardType;
}
@property int CardValue,CardType;
@end

甲板.h:

#import <Foundation/Foundation.h>
#import "Card.h"
@interface Deck : NSObject {
    NSMutableArray *Deck1, *Deck2, *Deck3, *Deck4, *Deck5;
}
@property NSMutableArray *Deck1, *Deck2, *Deck3, *Deck4, *Deck5;
-(void)createDeck;
-(void)createFullDeck : (Deck *) TheDeck;
-(void)insertToDeck : (Card *) TheCard;
@end

甲板.m:

#import "Deck.h"
#include <stdlib.h> 
@implementation Deck
@synthesize Deck1, Deck2, Deck3, Deck4, Deck5;
-(void)createDeck{
Deck1 = [NSMutableArray arrayWithCapacity:13];
Deck2 = [NSMutableArray arrayWithCapacity:13];
Deck3 = [NSMutableArray arrayWithCapacity:13];
Deck4 = [NSMutableArray arrayWithCapacity:13];
Deck5 = [NSMutableArray arrayWithCapacity:13];
}
-(void)createFullDeck : (Deck *) TheDeck{
    Card *Card1 = [[Card alloc]init];
    [TheDeck createDeck];
    for (int i=1;i<=4;i++){
        for (int j=1;j<=13;j++){
            Card1.CardValue=j;
            Card1.CardType=i;
            [TheDeck insertToDeck:Card1];
        }
    }
}
-(void)insertToDeck : (Card *) TheCard{
if (TheCard.CardType==1)
    [Deck1 addObject:[NSNumber numberWithInteger:TheCard.CardValue]];
if (TheCard.CardType==2)
    [Deck2 addObject:[NSNumber numberWithInteger:TheCard.CardValue]];
if (TheCard.CardType==3)
    [Deck3 addObject:[NSNumber numberWithInteger:TheCard.CardValue]];
if (TheCard.CardType==4)
    [Deck4 addObject:[NSNumber numberWithInteger:TheCard.CardValue]];
}
-(id)DrawCard{
    Card *MethodCard = [[Card alloc]init];
    int randomType = arc4random() % 4;
    if (randomType==0){
        int DeckCount = [Deck1 count];
        int randomValue = arc4random() % DeckCount;
        MethodCard.CardValue = [[Deck1 objectAtIndex:randomValue]integerValue];
        [Deck1 removeObjectAtIndex:randomValue];
    }
    if (randomType==1){
        int DeckCount = [Deck2 count];
        int randomValue = arc4random() % DeckCount;
        MethodCard.CardValue =[[Deck2 objectAtIndex:randomValue]integerValue];
        [Deck2 removeObjectAtIndex:randomValue];
    }
    if (randomType==2){
        int DeckCount = [Deck3 count];
        int randomValue = arc4random() % DeckCount;
        MethodCard.CardValue =[[Deck3 objectAtIndex:randomValue]integerValue];
        [Deck3 removeObjectAtIndex:randomValue];
    }
    if (randomType==3){
        int DeckCount = [Deck4 count];
        int randomValue = arc4random() % DeckCount;
        MethodCard.CardValue =[[Deck4 objectAtIndex:randomValue]integerValue];
        [Deck4 removeObjectAtIndex:randomValue];
    }
    MethodCard.CardType = randomType+1;
    return MethodCard;
}
@end

游戏.h:

#import <Foundation/Foundation.h>
#import "Deck.h"
@interface Game : NSObject

-(void)StartNewGame : (Deck *) TheDeck : (Game *) TheGame;
-(BOOL)GetDecision : (int) TheNumber;
-(void)CheckHiOrLo : (int) FirstNumber : (Deck *) TheDeck : (Game *) TheGame;
@end

游戏.m:

#import "Game.h"
@implementation Game
-(void)StartNewGame : TheDeck : (Game *) TheGame{
    NSLog(@"Welcome to the game High-Low!");
    [TheDeck createDeck];
    [TheDeck createFullDeck:TheDeck];
    Card *TheCard = [[Card alloc]init];
    TheCard = [TheDeck DrawCard];
    [TheGame CheckHiOrLo: TheCard.CardValue : TheDeck : TheGame];
    }
-(BOOL)GetDecision : (int) TheNumber{
    int userNum=0;
    NSLog(@"The Card is %i\nif you think the next Card is going to be higher - type 1.\nif you think the next Card is going to be lower - type 0.", TheNumber);
    scanf("%i", &userNum);
    if(userNum==1)
        return YES;
    if(userNum==0)
        return NO;
    NSLog(@"error in GetDecision in Game.m");
    return NO;
}
-(void)CheckHiOrLo : (int) FirstNumber : (Deck *) TheDeck : (Game *) TheGame{
    Card *TheCard = [[Card alloc]init];
    TheCard = [TheDeck DrawCard];
    BOOL Decision = [TheGame GetDecision:FirstNumber];
    if(FirstNumber>TheCard.CardValue){
        if(Decision==YES){
            NSLog(@"%i", TheCard.CardValue);
            NSLog(@"GameOver");
        }
        if(Decision==NO){
            NSLog(@"%i \nCorrect, Next Card", TheCard.CardValue);
            [TheGame CheckHiOrLo: TheCard.CardValue : TheDeck : TheGame];
        }
    }
    if(FirstNumber<=TheCard.CardValue){
        if(Decision==YES){
            NSLog(@"%i \nCorrect, Next Card", TheCard.CardValue);
            [TheGame CheckHiOrLo: TheCard.CardValue : TheDeck : TheGame];
        }
        if(Decision==NO){
            NSLog(@"%i", TheCard.CardValue);
            NSLog(@"GameOver");
        }
    }
}
@end

所以现在我尝试为此制作一个 iPhone 应用程序,所以我制作了一个新的 iphone 项目,将 Card、Deck 和 Game Classes 移动到 iPhone 项目并制作了一个高按钮、一个低按钮和一个开始按钮来构建一个新的甲板。

现在当我单击开始按钮时,我该怎么做,它会触发 Deck 类中的方法 createDeck 和 CreateFullDeck?

4

1 回答 1

2

首先你会Deck在你的头文件中声明一个对象,比如说你的UIViewController,确保你#import Deck.h的。

#import "Deck.h"

@interface MainViewController: UIViewController

      Deck *deck;
@end

现在,您可以初始化MainViewController的deckin方法-viewDidLoad

-(void)viewDidLoad {

    [super viewDidLoad];

     deck = [[Deck alloc] init];
 }

在您的按钮操作中,您将创建完整的牌组。假设这是一个IBAction连接到UIButtonInterface Builder 的;

 -(IBAction)onCreateClicked:(id)sender {

      [deck createDeck:deck];
      // You now created your deck of cards.
   }

希望这可以帮助 !

编辑:在进一步调查您的代码后,您的代码有很多问题,但上面的示例将按照您的要求执行。

于 2012-06-08T14:53:50.483 回答