I just made an "app" that just prints something up in the command line (outpot) heres an example -
http://img254.imageshack.us/img254/2853/3cc0a810d7564e39b05bade.png
Code:
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
So now i try to make a iPhone app for that, so i made a new iphone project, moved the Card,Deck and Game Classes to the iPhone project and made a High button, a Low button, and a start button to buid a new deck.
now how do i do that when i click the Start Button, it will trigger the method createDeck and CreateFullDeck that are in Deck Class?
First you would make Deck a declared object in your header file of, lets say your UIViewController, make sure you #import Deck.h.
#import "Deck.h"
@interface MainViewController: UIViewController
Deck *deck;
@end
Now, you can initialize your deck in -viewDidLoad method of MainViewController
-(void)viewDidLoad {
[super viewDidLoad];
deck = [[Deck alloc] init];
}
On your button action, you will create the full deck. Assuming this is an IBAction connected to a UIButton in Interface Builder;
-(IBAction)onCreateClicked:(id)sender {
[deck createDeck:deck];
// You now created your deck of cards.
}
Hope this helps !
EDIT: After investigating your code further, There are numerous things wrong with your code, but above example will do what you're asking to do.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With