diff --git a/TicTacToe/TicTacToe.xcodeproj/project.pbxproj b/TicTacToe/TicTacToe.xcodeproj/project.pbxproj index 71ad89c..0a77fdd 100644 --- a/TicTacToe/TicTacToe.xcodeproj/project.pbxproj +++ b/TicTacToe/TicTacToe.xcodeproj/project.pbxproj @@ -8,6 +8,8 @@ /* Begin PBXBuildFile section */ 8D9789E41B3C9A70007CF4CF /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 8D9789E31B3C9A70007CF4CF /* main.m */; }; + C74DA2BD1B43A04500D7ABEE /* TwoD.m in Sources */ = {isa = PBXBuildFile; fileRef = C74DA2BC1B43A04500D7ABEE /* TwoD.m */; }; + C74DA2C01B43A06500D7ABEE /* DecisionMaker.m in Sources */ = {isa = PBXBuildFile; fileRef = C74DA2BF1B43A06500D7ABEE /* DecisionMaker.m */; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -25,6 +27,10 @@ /* Begin PBXFileReference section */ 8D9789E01B3C9A70007CF4CF /* TicTacToe */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = TicTacToe; sourceTree = BUILT_PRODUCTS_DIR; }; 8D9789E31B3C9A70007CF4CF /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; + C74DA2BB1B43A04500D7ABEE /* TwoD.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TwoD.h; sourceTree = ""; }; + C74DA2BC1B43A04500D7ABEE /* TwoD.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TwoD.m; sourceTree = ""; }; + C74DA2BE1B43A06500D7ABEE /* DecisionMaker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DecisionMaker.h; sourceTree = ""; }; + C74DA2BF1B43A06500D7ABEE /* DecisionMaker.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DecisionMaker.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -58,6 +64,10 @@ isa = PBXGroup; children = ( 8D9789E31B3C9A70007CF4CF /* main.m */, + C74DA2BB1B43A04500D7ABEE /* TwoD.h */, + C74DA2BC1B43A04500D7ABEE /* TwoD.m */, + C74DA2BE1B43A06500D7ABEE /* DecisionMaker.h */, + C74DA2BF1B43A06500D7ABEE /* DecisionMaker.m */, ); path = TicTacToe; sourceTree = ""; @@ -119,6 +129,8 @@ buildActionMask = 2147483647; files = ( 8D9789E41B3C9A70007CF4CF /* main.m in Sources */, + C74DA2BD1B43A04500D7ABEE /* TwoD.m in Sources */, + C74DA2C01B43A06500D7ABEE /* DecisionMaker.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -235,6 +247,7 @@ 8D9789E91B3C9A70007CF4CF /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; /* End XCConfigurationList section */ }; diff --git a/TicTacToe/TicTacToe/DecisionMaker.h b/TicTacToe/TicTacToe/DecisionMaker.h new file mode 100644 index 0000000..9141464 --- /dev/null +++ b/TicTacToe/TicTacToe/DecisionMaker.h @@ -0,0 +1,22 @@ +// +// DecisionMaker.h +// TicTacToe +// +// Created by Varindra Hart on 7/1/15. +// Copyright (c) 2015 Mike Kavouras. All rights reserved. +// + +#import +#import "TwoD.h" + +@interface DecisionMaker : NSObject +@property (nonatomic)TwoD *board; +@property (nonatomic) BOOL twoPlayer; +@property (nonatomic) BOOL hardMode_ish; +-(BOOL)checker:(int)winVal; +-(void)makeMove:(int)counter; +-(void)makeBoard; +-(void)printBoard; +-(void)run; +-(BOOL)hasNextMove; +@end diff --git a/TicTacToe/TicTacToe/DecisionMaker.m b/TicTacToe/TicTacToe/DecisionMaker.m new file mode 100644 index 0000000..ff1dbc4 --- /dev/null +++ b/TicTacToe/TicTacToe/DecisionMaker.m @@ -0,0 +1,326 @@ +// +// DecisionMaker.m +// TicTacToe +// +// Created by Varindra Hart on 7/1/15. +// Copyright (c) 2015 Mike Kavouras. All rights reserved. +// + +#import "DecisionMaker.h" +#import "TwoD.h" +@implementation DecisionMaker{ + int win ; + int lose; +} + +-(BOOL)checker:(int)winVal{ + for (int i=0;i<[self.board count];i++ ) { + if(i==0 || i ==[self.board count]-1){ + if ([self.board diagonalSum:i]==winVal){ + return YES; + } + } + if([self.board rowSum:i]==winVal || [self.board columnSum:i]==winVal){ + return YES; + } + } + return NO; +} + +-(void)makeMove:(int)counter{ + char holder[256]; + if(!self.twoPlayer && counter%2==1){ + printf("Your turn. Enter location seperated by a comma with no spaces: "); + + } + int playerNumber = counter%2==1? 1:2; + if(self.twoPlayer){ + printf("Player %d: Choose a position seperated by a comma (no spaces!): ",playerNumber); + } + if((!self.twoPlayer && counter%2==1) || self.twoPlayer){ + while(YES){ + + scanf("%255[^\n]%*c",holder); + fpurge(stdin); + printf("\n"); + NSString *c = [NSString stringWithCString:holder encoding:NSASCIIStringEncoding]; + for(int i = 0; i<[c length]; i++){ + if(!isdigit([c characterAtIndex:i]) && [c characterAtIndex:i]!=','){ + printf("Invalid position\nTry again: "); + + continue; + } + } + NSArray *parts = [c componentsSeparatedByString:@","]; + if([parts count]!=2){ + printf("Invalid position\nTry again: "); + + continue; + } + for(int i =0; i<[parts count]; i++){ + if([parts[i] intValue]<1 || [parts[i] intValue]>[self.board count]){ + printf("Invalid position\nTry again: "); + continue; + } + } + int row = [parts[0] intValue]-1; + int col = [parts[1] intValue]-1; + NSString *xo = counter%2==1? [NSString stringWithFormat:@"1"]:[NSString stringWithFormat:@"-1"]; + if([[self.board objectAtRow:row column:col] isEqualToString:@"0"]){ + [self.board setObject:xo atRow:row column:col]; + break; + } + else{ + printf("Position already taken\nTry again: "); + continue; + } + } + } + + if(!self.twoPlayer && counter%2==0 && self.hardMode_ish==NO){ + while(YES){ + int rowPC = arc4random_uniform([self.board count]); + int colPC = arc4random_uniform([self.board count]); + NSString *pcString = @"-1"; + if([[self.board objectAtRow:rowPC column:colPC] isEqualToString:@"0"]){ + [self.board setObject:pcString atRow:rowPC column:colPC]; + break; + } + else{ + continue; + } + } + } + + if(!self.twoPlayer && counter%2==0 && self.hardMode_ish==YES){ + while(YES){ + // int rowPC = arc4random_uniform([self.board count]); + // int colPC = arc4random_uniform([self.board count]); + int whichRow=-1; + int whichCol=-1; + int whichDiag=-1; + for(int i= 0; i<3; i++){ + if([self.board rowSum:i]==2){ + whichRow =i; + break; + } + if([self.board columnSum:i]==2){ + whichCol =i; + break; + } + if(i==0 || i==2) { + if([self.board rowSum:i]==2){ + whichRow =i; + break; + } + } + } + + if(whichRow!=-1){ + for(int i= 0; i<3; i++){ + if([[self.board objectAtRow:whichRow column:i] isEqualTo:@"0"]){ + [self.board setObject:@"-1" atRow:whichRow column:i]; + } + } + break; + } + + if(whichCol!=-1){ + for(int i= 0; i<3; i++){ + if([[self.board objectAtRow:i column:whichCol] isEqualToString:@"0"]){ + [self.board setObject:@"-1" atRow:i column:whichCol]; + } + } + break; + } + + if(whichDiag!=-1){ + if(whichDiag==0){ + for(int i= 0; i<3; i++){ + if([[self.board objectAtRow:i column:i] isEqualToString:@"0"]){ + [self.board setObject:@"-1" atRow:i column:i]; + } + } + break; + } + + if(whichDiag==2){ + + for (int i= 0; i < 3; i++) { + if([[self.board objectAtRow:i column:whichDiag] isEqualToString:@"0"]){ + [self.board setObject:@"-1" atRow:i column:whichDiag]; + } + whichDiag--; + } + break; + } + } + + int rowPC = arc4random_uniform([self.board count]); + int colPC = arc4random_uniform([self.board count]); + + NSString *pcString = @"-1"; + if([[self.board objectAtRow:rowPC column:colPC] isEqualToString:@"0"]){ + [self.board setObject:pcString atRow:rowPC column:colPC]; + break; + } + else{ + continue; + } + } + } +} +-(void)makeBoard{ + int i; + while(YES){ + printf("Board size: "); + scanf("%d",&i); + fpurge(stdin); + if(3<=i && i<=10){ + break; + } + } + TwoD *gameBoard = [[TwoD alloc] initWithRows:i columns:i]; + self.board = gameBoard; + win = [self.board count]; + lose = -1*win; + char singlePlayer[256]; + printf("Two Player? Answering anything other than YES or NO will default to playing the PC: "); + scanf("%255[^\n]%*c",singlePlayer); + NSString *c = [NSString stringWithCString:singlePlayer encoding:NSASCIIStringEncoding]; + if([c isEqualToString:@"YES"]){ + self.twoPlayer=YES; + } + else{ + self.twoPlayer=NO; + } + self.hardMode_ish=NO; + + if(i==3){ + printf("hardish mode?\n y/n"); + char answer[256]; + memset(answer,'\0',1); + scanf("%255[^\n]%*c",answer); + fpurge(stdin); + if(answer[0]=='y'){ + self.hardMode_ish=YES; + } + + } + + +} + +-(void)printBoard{ + int i = (int)[self.board count]; + NSString *dashed = @""; + for (int l =0; l + +@interface TwoD : NSObject{ +NSMutableArray *Rows; +} +-(instancetype)initWithRows:(int)rows columns:(int)columns; +-(id)objectAtRow:(int)row column:(int)column; +-(void)setObject:(NSString *)object atRow:(int)row column:(int)column; +-(int)count; +-(int)rowSum:(int)rowNumber; +-(int)columnSum:(int)columnNumber; +-(int)diagonalSum:(int)topCorner; + +@end \ No newline at end of file diff --git a/TicTacToe/TicTacToe/TwoD.m b/TicTacToe/TicTacToe/TwoD.m new file mode 100644 index 0000000..9231670 --- /dev/null +++ b/TicTacToe/TicTacToe/TwoD.m @@ -0,0 +1,72 @@ +// +// TwoD.m +// TicTacToe +// +// Created by Varindra Hart on 7/1/15. +// Copyright (c) 2015 Mike Kavouras. All rights reserved. +// + +#import "TwoD.h" + + +@implementation TwoD{ + int count; + +} +-(instancetype)initWithRows:(int)rows columns:(int)columns{ + if(self = [self init]){ + Rows = [[NSMutableArray alloc] initWithCapacity:rows]; + for(int i=0; i < columns; i++){ + NSMutableArray *a = [NSMutableArray arrayWithCapacity:columns]; + [Rows insertObject:a atIndex:i]; + for(int j=0; j +#import "DecisionMaker.h" +#import "TwoD.h" int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... NSLog(@"Hello, World!"); + DecisionMaker *c = [[DecisionMaker alloc] init]; + [c run]; } return 0; } diff --git a/TodoList/TodoList.xcodeproj/project.pbxproj b/TodoList/TodoList.xcodeproj/project.pbxproj index 17baea5..fc8f531 100644 --- a/TodoList/TodoList.xcodeproj/project.pbxproj +++ b/TodoList/TodoList.xcodeproj/project.pbxproj @@ -8,6 +8,9 @@ /* Begin PBXBuildFile section */ 8D9789F71B3C9A7F007CF4CF /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 8D9789F61B3C9A7F007CF4CF /* main.m */; }; + DCEB15821B3F4371004FA257 /* ListManager.m in Sources */ = {isa = PBXBuildFile; fileRef = DCEB15811B3F4371004FA257 /* ListManager.m */; }; + DCEB15851B3F437B004FA257 /* ListItem.m in Sources */ = {isa = PBXBuildFile; fileRef = DCEB15841B3F437B004FA257 /* ListItem.m */; }; + DCEB15881B3F4386004FA257 /* List.m in Sources */ = {isa = PBXBuildFile; fileRef = DCEB15871B3F4386004FA257 /* List.m */; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -25,6 +28,12 @@ /* Begin PBXFileReference section */ 8D9789F31B3C9A7F007CF4CF /* TodoList */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = TodoList; sourceTree = BUILT_PRODUCTS_DIR; }; 8D9789F61B3C9A7F007CF4CF /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; + DCEB15801B3F4371004FA257 /* ListManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ListManager.h; sourceTree = ""; }; + DCEB15811B3F4371004FA257 /* ListManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ListManager.m; sourceTree = ""; }; + DCEB15831B3F437B004FA257 /* ListItem.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ListItem.h; sourceTree = ""; }; + DCEB15841B3F437B004FA257 /* ListItem.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ListItem.m; sourceTree = ""; }; + DCEB15861B3F4386004FA257 /* List.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = List.h; sourceTree = ""; }; + DCEB15871B3F4386004FA257 /* List.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = List.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -58,6 +67,12 @@ isa = PBXGroup; children = ( 8D9789F61B3C9A7F007CF4CF /* main.m */, + DCEB15831B3F437B004FA257 /* ListItem.h */, + DCEB15841B3F437B004FA257 /* ListItem.m */, + DCEB15861B3F4386004FA257 /* List.h */, + DCEB15871B3F4386004FA257 /* List.m */, + DCEB15801B3F4371004FA257 /* ListManager.h */, + DCEB15811B3F4371004FA257 /* ListManager.m */, ); path = TodoList; sourceTree = ""; @@ -119,6 +134,9 @@ buildActionMask = 2147483647; files = ( 8D9789F71B3C9A7F007CF4CF /* main.m in Sources */, + DCEB15881B3F4386004FA257 /* List.m in Sources */, + DCEB15821B3F4371004FA257 /* ListManager.m in Sources */, + DCEB15851B3F437B004FA257 /* ListItem.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -235,6 +253,7 @@ 8D9789FC1B3C9A7F007CF4CF /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; /* End XCConfigurationList section */ }; diff --git a/TodoList/TodoList/List.h b/TodoList/TodoList/List.h new file mode 100644 index 0000000..ea4b6ba --- /dev/null +++ b/TodoList/TodoList/List.h @@ -0,0 +1,41 @@ +// +// List.h +// TodoList +// +// Created by Mesfin Bekele Mekonnen on 6/27/15. +// Copyright (c) 2015 Mike Kavouras. All rights reserved. +// + +#import + + +@class ListItem; + +@interface List : NSObject + +@property (nonatomic) NSString *listName; + +@property (nonatomic) int id; +@property (nonatomic) BOOL viewPriority; +@property (nonatomic) BOOL viewByDueDate; + +-(instancetype)initWithName:(NSString *)name; + +-(void) addItem; +-(void) printItems; +-(void) deleteItem:(int)index; +-(void) editItem:(int)index; +-(void) markCompleted:(int)index; +-(void) setPriority:(int)index; +-(void) active; +-(void) inActive; +-(void) run; +-(void) printCommands; +-(void) printItemsByPriority; +-(void) printByDueDate; +-(void) setDate:(int)index; + +-(BOOL) outOfBounds:(int)index; +-(int) getIndex; + +@end diff --git a/TodoList/TodoList/List.m b/TodoList/TodoList/List.m new file mode 100644 index 0000000..1bd7b06 --- /dev/null +++ b/TodoList/TodoList/List.m @@ -0,0 +1,367 @@ +// +// List.m +// TodoList +// +// Created by Mesfin Bekele Mekonnen on 6/27/15. +// Copyright (c) 2015 Mike Kavouras. All rights reserved. +// + +#import "List.h" +#import "ListItem.h" + +@implementation List { + NSMutableArray *_items; +} + +- (instancetype) initWithName:(NSString *)name{ + + if(self = [super init]){ + self.listName = name; + return self; + } + return nil; +} + +-(void) addItem{ + + if(_items == nil){ + _items = [[NSMutableArray alloc]init]; + } + char temp[256]; + printf("\n Add description: "); + scanf("%255[^\n]%*c",temp); + fpurge(stdin); + NSString *name = [NSString stringWithCString:temp encoding:NSASCIIStringEncoding]; + ListItem *li = [[ListItem alloc] initWithDefaultAndName:name]; + li.idNumber=self.id; + self.id+=1; + [_items addObject:li]; +} + +-(void) deleteItem:(int)index{ + + [_items removeObjectAtIndex:index]; +} + +-(void) markCompleted:(int)index{ + + ListItem *item = [_items objectAtIndex:index]; + + item.completed = YES; +} + +-(void) setDate:(int)index{ + ListItem *item = [_items objectAtIndex:index]; + + + + char array[256]; + printf("Enter date mm/dd/yyyy: "); + scanf("%255[^\n]%*c",array); + fpurge(stdin); + + NSString *info = [NSString stringWithCString:array encoding:NSASCIIStringEncoding]; + for(int i = 0; i < [info length]; i++){ + if((!isdigit([info characterAtIndex:i])&& [info characterAtIndex:i]!='/') ||(ispunct([info characterAtIndex:i]) && [info characterAtIndex:i]!='/')){ + printf("\nInvalid date!\n"); + return; + } + } + NSArray *parts = [info componentsSeparatedByString:@"/"]; + + NSDateComponents *c = [[NSDateComponents alloc]init]; + + int a = [parts[0] intValue]; + int b = [parts[1] intValue]; + int d = [parts[2] intValue]; + + if((a<1 || a>12) || (b<1 || b>31) || (d<0) || (a==2 && b>28 && d%4!=0) || (a==2 && b>29 && d%4==0) || ((a==4||a==6||a==9||a==11) && b>30) || [parts count]!=3){ + printf("\nInvalid Date!\n"); + return; + } + + [c setDay:b]; + [c setMonth:a]; + [c setYear:d]; + + NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; + + NSDate *date = [calendar dateFromComponents:c]; + item.date = date; + item.hasDate = YES; +} + +-(void) printItems{ + NSSortDescriptor *sortingMethod = [[NSSortDescriptor alloc] initWithKey:@"idNumber" ascending:YES]; + + [_items sortUsingDescriptors:@[sortingMethod]]; + int i =1; + for(ListItem *li in _items){ + printf("%d. %s\n",i,[li.itemName UTF8String]); + i++; + } + printf("\n"); +} +-(void) printItemsByPriority{ + int i=1; + char *p1 = " *"; + char *p2 = " **"; + char *p3 = " ***"; + char *p4 = "****"; + + NSSortDescriptor *sortingMethod = [[NSSortDescriptor alloc] initWithKey:@"priority" ascending:NO]; + + [_items sortUsingDescriptors:@[sortingMethod]]; + + for (ListItem *li in _items) { + if(li.priority == 1){ + printf("%d. %s %s\n",i,p1,[li.itemName UTF8String]); + } + if (li.priority == 2) { + printf("%d. %s %s\n",i,p2,[li.itemName UTF8String]); + } + if (li.priority == 3) { + printf("%d. %s %s\n",i,p3,[li.itemName UTF8String]); + } + if (li.priority == 4) { + printf("%d. %s %s\n",i,p4,[li.itemName UTF8String]); + } + i++; + } + printf("\n"); +} + +-(void) printByDueDate{ + int i =1; + NSSortDescriptor *sortingMethod = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:YES]; + + [_items sortUsingDescriptors:@[sortingMethod]]; + for(ListItem *li in _items){ + if(li.hasDate){ + unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay; + NSCalendar *gregorian = [NSCalendar currentCalendar]; + NSDateComponents *component = [gregorian components:unitFlags fromDate:li.date]; + printf("%d. %s | %d/%d/%d\n",i,[li.itemName UTF8String],(int)[component month], (int)[component day], (int)[component year]); + } + else{ + printf("%d. %s\n",i,[li.itemName UTF8String]); + } + i++; + } + printf("\n"); +} + +-(void) setPriority:(int)index{ + int newValue; + + printf("Enter a priority value, 1-4: "); + scanf("%d",&newValue); + fpurge(stdin); + + ListItem *li = [_items objectAtIndex:index]; + li.priority = newValue; +} + +-(void) editItem:(int)index{ + char newName [256]; + printf("New description: "); + scanf("%255[^\n]%*c",newName); + fpurge(stdin); + ListItem *li = [_items objectAtIndex:index]; + li.itemName = [NSString stringWithCString:newName encoding:NSASCIIStringEncoding]; +} + + +-(void) active{ + int i = 1; + printf("\n"); + for(ListItem * li in _items){ + if(!li.completed){ + printf("%d. %s\n",i, [li.itemName UTF8String]); + i++; + } + } + printf("\n"); + while(YES){ + char exitKey[256]; + printf("To exit press q: "); + scanf("%255[^\n]%*c",exitKey); + fpurge(stdin); + NSString *c = [NSString stringWithCString:exitKey encoding:NSASCIIStringEncoding]; + if ([c isEqualToString:@"q"] || [c isEqualToString:@"Q"]){ + printf("\n\n\n"); + break; + } + } +} + +-(void) inActive{ + int i = 1; + printf("\n"); + for(ListItem * li in _items){ + if(li.completed){ + printf("%d. %s\n",i, [li.itemName UTF8String]); + i++; + } + } + printf("\n"); + while(YES){ + char exitKey[1]; + printf("To exit press q: "); + scanf("%1[^\n]%*c",exitKey); + fpurge(stdin); + NSString *c = [NSString stringWithCString:exitKey encoding:NSASCIIStringEncoding]; + if ([c isEqualToString:@"q"] || [c isEqualToString:@"Q"]){ + printf("\n\n\n"); + break; + } + } +} + +-(BOOL) outOfBounds:(int)index{ + NSInteger arraySize = [_items count]; + if(index < 1 || index > arraySize){ + NSString *c = [NSString stringWithFormat:@"%@",index<1? @"Must be greater than or equal to 1\n":@"out of bounds\n"]; + printf("%s",[c UTF8String]); + return YES; + } + return NO; +} + +-(int) getIndex{ + int d; + printf("\nIndex number? "); + scanf("%d",&d); + fpurge(stdin); + printf("\n"); + return d; +} + +-(void) run{ + while(YES){ + char holder[256]; + if (self.viewPriority == NO && self.viewByDueDate == NO){ + [self printItems]; + } + if(self.viewPriority){ + [self printItemsByPriority]; + } + if(self.viewByDueDate){ + [self printByDueDate]; + } + [self printCommands]; + printf("\n:"); + scanf("%255[^\n]%*c",holder); + fpurge(stdin); + printf("\n"); + + NSString *checker = [NSString stringWithCString:holder encoding:NSASCIIStringEncoding]; + + if([checker isEqualToString:@"e"] || [checker isEqualToString:@"E"]){ + int d = [self getIndex]; + + if([self outOfBounds:d]){ + continue; + } + + [self editItem:d-1]; + continue; + } + + if([checker isEqualToString:@"d"] || [checker isEqualToString:@"D"]){ + int d = [self getIndex]; + + if([self outOfBounds:d]){ + continue; + } + [self deleteItem:d-1]; + continue; + } + + if([checker isEqualToString:@"c"] || [checker isEqualToString:@"C"]){ + int d = [self getIndex]; + + if([self outOfBounds:d]){ + continue; + } + [self markCompleted:d-1]; + continue; + } + + if([checker isEqualToString:@"sd"] || [checker isEqualToString:@"SD"]){ + int d = [self getIndex]; + + if([self outOfBounds:d]){ + continue; + } + [self setDate:d-1]; + continue; + } + + if([checker isEqualToString:@"p"] || [checker isEqualToString:@"P"]){ + int d = [self getIndex]; + + if([self outOfBounds:d]){ + continue; + } + [self setPriority:d-1]; + continue; + } + + if([checker isEqualToString:@"a"] || [checker isEqualToString:@"A"]){ + + [self addItem]; + continue; + } + + if([checker isEqualToString:@"t"] || [checker isEqualToString:@"T"]){ + + [self active]; + continue; + } + + + if([checker isEqualToString:@"i"] || [checker isEqualToString:@"I"]){ + + [self inActive]; + continue; + } + + if([checker isEqualToString:@"q"] || [checker isEqualToString:@"Q"]){ + printf("\n\n\n"); + break; + } + if([checker isEqualToString:@"vp"] || [checker isEqualToString:@"VP"]){ + if(self.viewPriority){ + self.viewPriority=NO; + } + else{ + self.viewByDueDate = NO; + self.viewPriority = YES; + } + continue; + } + if([checker isEqualToString:@"vd"] || [checker isEqualToString:@"VD"]){ + if(self.viewByDueDate){ + + self.viewByDueDate=NO; + } + else{ + self.viewByDueDate = YES; + self.viewPriority=NO; + } + continue; + } + + } +} + +-(void) printCommands{ + printf(" a|add e|edit sd|set due date\n"); + printf(" d|delete p|set priority c|mark completed\n"); + printf(" t|active i|inactive q|quit\n"); + printf("vp|View by priority(ON/OFF) vd|View by date(ON/OFF)\n"); + printf("\n\n\n\n\n\n\n\n\n"); + +} +@end diff --git a/TodoList/TodoList/ListItem.h b/TodoList/TodoList/ListItem.h new file mode 100644 index 0000000..35d65f6 --- /dev/null +++ b/TodoList/TodoList/ListItem.h @@ -0,0 +1,21 @@ +// +// ListItem.h +// TodoList +// +// Created by Mesfin Bekele Mekonnen on 6/27/15. +// Copyright (c) 2015 Mike Kavouras. All rights reserved. +// + +#import + +@interface ListItem : NSObject +-(instancetype)initWithDefaultAndName:(NSString *)name; + +@property (nonatomic) int priority; +@property (nonatomic) NSString *itemName; +@property (nonatomic) BOOL completed; +@property (nonatomic) int idNumber; +@property (nonatomic) NSDate* date; +@property (nonatomic) BOOL hasDate; + +@end diff --git a/TodoList/TodoList/ListItem.m b/TodoList/TodoList/ListItem.m new file mode 100644 index 0000000..84df76b --- /dev/null +++ b/TodoList/TodoList/ListItem.m @@ -0,0 +1,40 @@ +// +// ListItem.m +// TodoList +// +// Created by Mesfin Bekele Mekonnen on 6/27/15. +// Copyright (c) 2015 Mike Kavouras. All rights reserved. +// + +#import "ListItem.h" + +@implementation ListItem + +- (instancetype) initWithDefaultAndName:(NSString *)name{ + + if(self = [super init]){ + self.priority = 1; + self.completed = NO; + self.itemName = name; + self.hasDate = NO; + + NSDateComponents *c = [[NSDateComponents alloc]init]; + + [c setDay:30]; + [c setMonth:12]; + [c setYear:3000]; + + NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; + + NSDate *date = [calendar dateFromComponents:c]; + + self.date = date; + + + return self; + } + return nil; +} + + +@end diff --git a/TodoList/TodoList/ListManager.h b/TodoList/TodoList/ListManager.h new file mode 100644 index 0000000..a35e7d7 --- /dev/null +++ b/TodoList/TodoList/ListManager.h @@ -0,0 +1,21 @@ +// +// ListManager.h +// TodoList +// +// Created by Mesfin Bekele Mekonnen on 6/27/15. +// Copyright (c) 2015 Mike Kavouras. All rights reserved. +// + +#import + +@interface ListManager : NSObject + +-(void) run; +-(void) addList; +-(void) deleteList:(int)index; +-(void) printLists; +-(void) printCommands; +-(BOOL) outOfBounds:(int)index; +-(int) getIndex; + +@end diff --git a/TodoList/TodoList/ListManager.m b/TodoList/TodoList/ListManager.m new file mode 100644 index 0000000..3c45918 --- /dev/null +++ b/TodoList/TodoList/ListManager.m @@ -0,0 +1,123 @@ +// +// ListManager.m +// TodoList +// +// Created by Mesfin Bekele Mekonnen on 6/27/15. +// Copyright (c) 2015 Mike Kavouras. All rights reserved. +// + +#import "ListManager.h" +#import "List.h" +#import "ListItem.h" + +@class List; + +@implementation ListManager{ + NSMutableArray *_listArray; +} + +-(void) printCommands{ + + printf("a|Add List v|View List\n"); + printf("d|Delete List q|Quit\n"); + printf("\n\n\n\n\n\n\n"); +} + +-(void)addList{ + if(_listArray == nil){ + _listArray = [[NSMutableArray alloc]init]; + } + char temp[256]; + memset(temp, '\0',256); + printf("\n Add title of Todo-List: "); + scanf("%255[^\n]%*c",temp); + fpurge(stdin); + printf("\n"); + NSString *name = [NSString stringWithCString:temp encoding:NSASCIIStringEncoding]; + List *li = [[List alloc] initWithName:name]; + li.id = 0; + li.viewByDueDate = NO; + li.viewPriority = NO; + [_listArray addObject:li]; +} + +-(void) deleteList:(int)index{ + + [_listArray removeObjectAtIndex:index]; + +} + +-(void) printLists{ + int i =1; + for(List *l in _listArray){ + printf("%d. %s\n",i, [l.listName UTF8String]); + i++; + } + printf("\n"); +} + +-(BOOL) outOfBounds:(int)index{ + NSInteger arraySize = [_listArray count]; + if(index < 1 || index > arraySize){ + NSString *c = [NSString stringWithFormat:@"%@",index<1? @"Must be greater than or equal to 1\n":@"out of bounds\n"]; + printf("%s",[c UTF8String]); + return YES; + } + return NO; +} + +-(int) getIndex{ + int d; + printf("\nIndex number? "); + scanf("%d",&d); + fpurge(stdin); + printf("\n"); + return d; +} + +-(void) run{ + while(YES){ + char userIn[256]; + memset(userIn, '\0', 256); + [self printLists]; + [self printCommands]; + printf("\n:"); + scanf("%255[^\n]%*c",userIn); + fpurge(stdin); + NSString *userString = [NSString stringWithCString:userIn encoding:NSASCIIStringEncoding]; + + if([userString isEqualToString:@"a"] || [userString isEqualToString:@"A"]){ + + [self addList]; + continue; + } + + if([userString isEqualToString:@"d"] || [userString isEqualToString:@"D"]){ + int d = [self getIndex]; + + if([self outOfBounds:d]){ + continue; + } + + [self deleteList:d-1]; + continue; + } + + if([userString isEqualToString:@"v"] || [userString isEqualToString:@"V"]){ + int d = [self getIndex]; + + if([self outOfBounds:d]){ + continue; + } + List* temp = [_listArray objectAtIndex:d-1] ; + [temp run]; + continue; + } + if([userString isEqualToString:@"q"] || [userString isEqualToString:@"Q"]){ + + break; + } + } +} + +@end diff --git a/TodoList/TodoList/main.m b/TodoList/TodoList/main.m index 187be40..9c00d1b 100644 --- a/TodoList/TodoList/main.m +++ b/TodoList/TodoList/main.m @@ -8,10 +8,21 @@ #import +#import "ListManager.h" +#import "List.h" +#import "ListItem.h" + + int main(int argc, const char * argv[]) { @autoreleasepool { - // insert code here... - NSLog(@"Hello, World!"); + + printf("Welcome to your To-do List app!"); + printf("\n\n*******************************\n"); + + + ListManager *lm = [[ListManager alloc]init]; + + [lm run]; } return 0; }