コマンドライン(アウトポット)に何かを出力する「アプリ」を作成しました。例を示します-
http://img254.imageshack.us/img254/2853/3cc0a810d7564e39b05bade.png
コード:
Card.h:
#import <Foundation/Foundation.h>
@interface Card : NSObject {
int CardValue;
int CardType;
}
@property int CardValue,CardType;
@end
Card.m:
#import <Foundation/Foundation.h>
@interface Card : NSObject {
int CardValue;
int CardType;
}
@property int CardValue,CardType;
@end
Deck.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
Deck.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
Game.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
Game.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プロジェクトを作成し、カード、デッキ、ゲームクラスをiPhoneプロジェクトに移動し、[高]ボタン、[低]ボタン、[スタート]ボタンを作成して新しいデッキ。
スタートボタンをクリックすると、デッキクラスにあるcreateDeckメソッドとCreateFullDeckメソッドがトリガーされます。