Sinisterly
[Objective-C] Command line game - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: C, C++, & Obj-C (https://sinister.ly/Forum-C-C-Obj-C)
+--- Thread: [Objective-C] Command line game (/Thread-Objective-C-Command-line-game)

Pages: 1 2


[Objective-C] Command line game - Anima Templi - 02-12-2013

Okay i promised to come of with my version in Objective-C. It's been some days since i promised that, but i had some troubles which was a pain in the ass to solve. I finally came across it, so here's my version. Also I didn't came up with the story. But it was quite some challenges to implement the story into code. Also the code is poorly commendted, i did some overall commenting with the story in the days to help myself. I can comment on it if you want me to, but at this point i see no need for it since no one in here are programming in it, and are properly not planning on learning it.

main.m
Code:
#import <Foundation/Foundation.h> #import "Game.h" void waitOnCR (void) { while ( getchar() != '\n' ) { }; } int response; int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... Game* myGame = [[Game alloc] init]; myGame.health=100; bool gameIsRunning = true; while (gameIsRunning){ [myGame instructions]; [myGame day1]; [myGame day2]; [myGame day3]; if (myGame.health<=0) { NSLog (@"\n\nYou have died. \nPress enter for the next dialogue."); waitOnCR(); } [myGame printScore:myGame.health]; NSLog(@"\n\nRestart?\n1. Yes\n2. No"); scanf("%i", &response); if (response == 2){ break; } } } return 0; }

Game.h
Code:
#import <Foundation/Foundation.h> void waitOnCR (void); int health; int answ; int score; @interface Game : NSObject { BOOL userIsAlive; } @property (strong) NSString* characterName; @property BOOL userIsAlive; @property int health; @property int answ; @property int score; - (void) instructions; - (void) day1; - (void) day2; - (void) day3; - (void) printHealth; - (void) printScore: (float) x; @end

Game.m
Code:
#import "Game.h" @implementation Game @synthesize characterName; @synthesize userIsAlive; @synthesize health; @synthesize answ; @synthesize score; - (void) instructions { NSLog(@"\n\nYour plane crashed on an island. Help is coming three days. Try to survive. \nType your first name and press enter to continue.\n"); char firstname[20]; scanf ("%s", firstname); NSString* characterName; characterName = [NSString stringWithCString:firstname encoding:NSASCIIStringEncoding]; NSLog (@"Your name is: %@. Press enter to continue.\n", characterName); waitOnCR(); } - (void) day1 { userIsAlive = true; while (userIsAlive){ // 2 - Scenario1 NSLog (@"\n\nDAY:01\n\nWith blurred vision, you look around to gather your surroundings. \nThe beach is rough, and the salty water is irritating your skin. You recall the place crash. \n1. Walk into the jungle. \n2. Sleep and regain strength before walking into jungle."); scanf("%i", &answ); waitOnCR(); // 3 - Answer1 if (answ==2) { NSLog(@"\n\nYou feel well rested. However, you are hungry and freezing without shelter. It is too dark to build a shelter. You try to sleep in a tree, but you are attacked by monkeys. \nPress enter to continue."); waitOnCR(); health = 0; userIsAlive = false; break; } // 4 - Scenario2 NSLog (@"\n\nInside the jungle, you find a bush with little black fruit, at its base are white spekled mushrooms. \n1.Eat mushrooms. \n2.Eat berries."); scanf("%i", &answ); waitOnCR(); // 5 - Answer2 if (answ==1) { NSLog(@"\n\nYou are left with a bitter taste in your mouth and a gurgling in your stomach. \nHealth decreassed. \n press enter to continue."); health= health - 10; waitOnCR(); }else { NSLog(@"\n\nThe sweetness of the berries is quite savory considering all the seawater you consumed. \nPress enter to continue."); waitOnCR(); health= health + 10; } // 6 - Day Completion Message NSLog(@"\n\nYou fall asleep beneath the tree. DAY:01 was successfully completed. \nPress enter to continue"); waitOnCR(); break; } } - (void) day2 { while (userIsAlive){ // 2 - Scenario1 NSLog(@"\n\nDAY:02\n\nUpon being nudged be a large animal, you awake. \nIt is a large gorilla. \n1.Run. \n2.Embrace your fate, and climb into its mouth."); scanf("%i", &answ); waitOnCR(); // 3 - Answer1 if (answ==1){ NSLog(@"\n\nThe gorilla watches you run in amusement. It trails behind you, until you drop from exhaustion. \nHe takes a bite out of your screwny arm. \nPress enter to continue."); waitOnCR(); NSLog(@"\n\nOUCH!\nThe gorilla shakes its head in disapproval and proceeds and proceeds to spit out the remaining flesh. The gorilla rins back into the jungle. \nYour gaping wound results in a significant health decrease. \nPress enter to continue."); waitOnCR(); health= health - 60; }else { NSLog(@"\n\nBy peacefully approaching the gorilla, it does not feel threatened. It takes you on its back to explore the island. \nPress enter to continue."); waitOnCR(); NSLog(@"\n\nAs you pass through the jungle, you see that snakes inhabit the taller trees. You pass through a grand meadow of grass. \nThe ape slows, as you look to the east coast of the island, you make out a tribe of worriors. The ape turns and brings you back to where he found you. \nYou take a mental note of what you saw. \nPress enter to continue."); waitOnCR(); } // 4 - Scenario2 NSLog(@"\n\nYou feel slightly chilled. You know the night will be really cold if you don't build a fire and retrieve. \n1. Search the beach. \n2. Search the jungle."); scanf("%i", &answ); waitOnCR(); // 5 - Answer2 if (answ==1) { while (answ==1) { NSLog(@"\n\nYou feel the warm sand under your feet. \n1. Search the west coach. \n2.Search the southern coast."); scanf("%i", &answ); if (answ==1){ NSLog(@"\n\nYou walk for for a mile, and come upon some sun dried timber. You create a tee-pee structure out of the wood. \nYou manage to make the kindle catch fire by scratching rocks togehter. \nPress enter to continue."); waitOnCR(); break; } if (answ==2) { NSLog(@"\n\nYou walk in circles for another mile, this part of the beach does not have timber. \nPress enter to continue."); answ=1; waitOnCR(); }else { NSLog(@"\n\nYou find plenty of wood, and drag it on to the beach for your fire. \nPress enter to continue."); waitOnCR(); NSLog(@"As night falls you have failed to ligt your shelter under another tree, but you are exceptionally cold and week. Health decreasses. \nPress enter to continue."); health= health - 10; waitOnCR(); } } } // 6 - Day Completion Message NSLog(@"\n\nYou fall asleep. DAY:02 was successfully completed. \nPress enter to continue."); waitOnCR(); break; } } - (void) day3 { while (userIsAlive) { BOOL ate_granola; // 2 - Scenario1 NSLog(@"\n\nDAY:03\n\nThe sun bears down on your face and the sound of seagulls wake you. As you attempt to stand up, you are reminded of your revaging hunger. \nPress enter to continue."); waitOnCR(); NSLog(@"\n\nTo your surprise, you discover a granola bar, you stashed in teh lower pocket of your cargo pants before your flight. \n1. Eat it. \n2. Save it for later."); scanf("%i", &answ); waitOnCR(); // 3 - Answer1 if (answ==1){ ate_granola = true; NSLog(@"\n\nThe granola bar effectively curbs your hunger. Health is increased. \nPress enter to continue."); health= health + 10; waitOnCR(); }else { ate_granola = false; NSLog(@"\n\nPerhaps you will find a greater use for the bar later. \nPress enter to continue."); waitOnCR(); } // 4 - Scenario2 NSLog(@"\n\nYou recall that today is the day the dispatchers promised a rescue. You need an aerial view of the island, to see where the rescue team will come. \n1. Climb a tall tree. \n2. Climb a rock face."); scanf("%i", &answ); waitOnCR(); // 5 - Answer2 if (answ==1) { NSLog(@"\n\nYou climb to the top of a mossed over tree. The view is great and you can see a meadowt that would be ideal for a helicopter landing. \nHowever, you also see a perfect area for a boat rescue on the east coast. \nAs you take in the view, you feel a jab and a following stinging sensation. \nYou have been bit by a snake. \nPress enter to continue."); waitOnCR(); NSLog(@"\n\nAfter you climb down the tree, you faint at its base. Health is decreased significantly."); waitOnCR(); health= health - 50; if (health<=0){ break; } }else { NSLog(@"\n\nYou climb to the top of a mossed over tree. The view is great and you can see a meadowt that would be ideal for a helicopter landing. \nHowever, you also see a perfect area for a boat rescue on the east coast. \nPress enter to continue."); } NSLog(@"\n\nYou decide to talk to the east coast as it seems to be the most likely rescue point. \nPress enter to continue."); waitOnCR(); NSLog(@"\n\nAs you walk through the jungle, you trip over a net. You are instantly flung upside down. \nPress enter to continue."); waitOnCR(); NSLog(@"As you look around, all you can make out are dark forms with bright painted faces. You are surrounded by natives. \nPress enter to continue."); waitOnCR(); NSLog(@"\n\nThey take you to the east beach, and set you next to a grand fire. When they are not looking, you reach into your pockets for something useful. \nPress enter to continue."); waitOnCR(); if (ate_granola==true) { NSLog(@"\n\nYou search to find your granola bar wrapper, but you seem to have left it under a tree. you imagine that the shine foil wrapper would have deeply impressed the natives. \nThe natives knock you out."); health =0; break; } else { NSLog(@"You pull out your granola bar and draw their attention. They are shocked by the foil wrapper. They think it is a new metal. \nThe chief accepts your invitation to the honey granola bar. He is deeply impressed. \nPress enter to continue."); waitOnCR(); NSLog(@"\n\nIn this same moment, a large steel rescue boat approaches. The natives are terrified and leave you standing on the beach. \nYou are rescued. \nPress enter to continue."); waitOnCR(); NSLog(@"\n\nYOU WIN\nPress enter to continue."); } // 6 - Day Completion Message } } - (void) printHealth{ if (health>0) { NSLog(@"\n\n%@ managed to finish the dat with a total health of: %i\nPress enter to continue.", characterName, health); waitOnCR(); } } -(void) printScore:(float) x { NSLog(@"%@ based on your health you received %i percent of the available points.", characterName, score); waitOnCR(); } @end



[Objective-C] Command line game - Anima Templi - 02-12-2013

Okay i promised to come of with my version in Objective-C. It's been some days since i promised that, but i had some troubles which was a pain in the ass to solve. I finally came across it, so here's my version. Also I didn't came up with the story. But it was quite some challenges to implement the story into code. Also the code is poorly commendted, i did some overall commenting with the story in the days to help myself. I can comment on it if you want me to, but at this point i see no need for it since no one in here are programming in it, and are properly not planning on learning it.

main.m
Code:
#import <Foundation/Foundation.h> #import "Game.h" void waitOnCR (void) { while ( getchar() != '\n' ) { }; } int response; int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... Game* myGame = [[Game alloc] init]; myGame.health=100; bool gameIsRunning = true; while (gameIsRunning){ [myGame instructions]; [myGame day1]; [myGame day2]; [myGame day3]; if (myGame.health<=0) { NSLog (@"\n\nYou have died. \nPress enter for the next dialogue."); waitOnCR(); } [myGame printScore:myGame.health]; NSLog(@"\n\nRestart?\n1. Yes\n2. No"); scanf("%i", &response); if (response == 2){ break; } } } return 0; }

Game.h
Code:
#import <Foundation/Foundation.h> void waitOnCR (void); int health; int answ; int score; @interface Game : NSObject { BOOL userIsAlive; } @property (strong) NSString* characterName; @property BOOL userIsAlive; @property int health; @property int answ; @property int score; - (void) instructions; - (void) day1; - (void) day2; - (void) day3; - (void) printHealth; - (void) printScore: (float) x; @end

Game.m
Code:
#import "Game.h" @implementation Game @synthesize characterName; @synthesize userIsAlive; @synthesize health; @synthesize answ; @synthesize score; - (void) instructions { NSLog(@"\n\nYour plane crashed on an island. Help is coming three days. Try to survive. \nType your first name and press enter to continue.\n"); char firstname[20]; scanf ("%s", firstname); NSString* characterName; characterName = [NSString stringWithCString:firstname encoding:NSASCIIStringEncoding]; NSLog (@"Your name is: %@. Press enter to continue.\n", characterName); waitOnCR(); } - (void) day1 { userIsAlive = true; while (userIsAlive){ // 2 - Scenario1 NSLog (@"\n\nDAY:01\n\nWith blurred vision, you look around to gather your surroundings. \nThe beach is rough, and the salty water is irritating your skin. You recall the place crash. \n1. Walk into the jungle. \n2. Sleep and regain strength before walking into jungle."); scanf("%i", &answ); waitOnCR(); // 3 - Answer1 if (answ==2) { NSLog(@"\n\nYou feel well rested. However, you are hungry and freezing without shelter. It is too dark to build a shelter. You try to sleep in a tree, but you are attacked by monkeys. \nPress enter to continue."); waitOnCR(); health = 0; userIsAlive = false; break; } // 4 - Scenario2 NSLog (@"\n\nInside the jungle, you find a bush with little black fruit, at its base are white spekled mushrooms. \n1.Eat mushrooms. \n2.Eat berries."); scanf("%i", &answ); waitOnCR(); // 5 - Answer2 if (answ==1) { NSLog(@"\n\nYou are left with a bitter taste in your mouth and a gurgling in your stomach. \nHealth decreassed. \n press enter to continue."); health= health - 10; waitOnCR(); }else { NSLog(@"\n\nThe sweetness of the berries is quite savory considering all the seawater you consumed. \nPress enter to continue."); waitOnCR(); health= health + 10; } // 6 - Day Completion Message NSLog(@"\n\nYou fall asleep beneath the tree. DAY:01 was successfully completed. \nPress enter to continue"); waitOnCR(); break; } } - (void) day2 { while (userIsAlive){ // 2 - Scenario1 NSLog(@"\n\nDAY:02\n\nUpon being nudged be a large animal, you awake. \nIt is a large gorilla. \n1.Run. \n2.Embrace your fate, and climb into its mouth."); scanf("%i", &answ); waitOnCR(); // 3 - Answer1 if (answ==1){ NSLog(@"\n\nThe gorilla watches you run in amusement. It trails behind you, until you drop from exhaustion. \nHe takes a bite out of your screwny arm. \nPress enter to continue."); waitOnCR(); NSLog(@"\n\nOUCH!\nThe gorilla shakes its head in disapproval and proceeds and proceeds to spit out the remaining flesh. The gorilla rins back into the jungle. \nYour gaping wound results in a significant health decrease. \nPress enter to continue."); waitOnCR(); health= health - 60; }else { NSLog(@"\n\nBy peacefully approaching the gorilla, it does not feel threatened. It takes you on its back to explore the island. \nPress enter to continue."); waitOnCR(); NSLog(@"\n\nAs you pass through the jungle, you see that snakes inhabit the taller trees. You pass through a grand meadow of grass. \nThe ape slows, as you look to the east coast of the island, you make out a tribe of worriors. The ape turns and brings you back to where he found you. \nYou take a mental note of what you saw. \nPress enter to continue."); waitOnCR(); } // 4 - Scenario2 NSLog(@"\n\nYou feel slightly chilled. You know the night will be really cold if you don't build a fire and retrieve. \n1. Search the beach. \n2. Search the jungle."); scanf("%i", &answ); waitOnCR(); // 5 - Answer2 if (answ==1) { while (answ==1) { NSLog(@"\n\nYou feel the warm sand under your feet. \n1. Search the west coach. \n2.Search the southern coast."); scanf("%i", &answ); if (answ==1){ NSLog(@"\n\nYou walk for for a mile, and come upon some sun dried timber. You create a tee-pee structure out of the wood. \nYou manage to make the kindle catch fire by scratching rocks togehter. \nPress enter to continue."); waitOnCR(); break; } if (answ==2) { NSLog(@"\n\nYou walk in circles for another mile, this part of the beach does not have timber. \nPress enter to continue."); answ=1; waitOnCR(); }else { NSLog(@"\n\nYou find plenty of wood, and drag it on to the beach for your fire. \nPress enter to continue."); waitOnCR(); NSLog(@"As night falls you have failed to ligt your shelter under another tree, but you are exceptionally cold and week. Health decreasses. \nPress enter to continue."); health= health - 10; waitOnCR(); } } } // 6 - Day Completion Message NSLog(@"\n\nYou fall asleep. DAY:02 was successfully completed. \nPress enter to continue."); waitOnCR(); break; } } - (void) day3 { while (userIsAlive) { BOOL ate_granola; // 2 - Scenario1 NSLog(@"\n\nDAY:03\n\nThe sun bears down on your face and the sound of seagulls wake you. As you attempt to stand up, you are reminded of your revaging hunger. \nPress enter to continue."); waitOnCR(); NSLog(@"\n\nTo your surprise, you discover a granola bar, you stashed in teh lower pocket of your cargo pants before your flight. \n1. Eat it. \n2. Save it for later."); scanf("%i", &answ); waitOnCR(); // 3 - Answer1 if (answ==1){ ate_granola = true; NSLog(@"\n\nThe granola bar effectively curbs your hunger. Health is increased. \nPress enter to continue."); health= health + 10; waitOnCR(); }else { ate_granola = false; NSLog(@"\n\nPerhaps you will find a greater use for the bar later. \nPress enter to continue."); waitOnCR(); } // 4 - Scenario2 NSLog(@"\n\nYou recall that today is the day the dispatchers promised a rescue. You need an aerial view of the island, to see where the rescue team will come. \n1. Climb a tall tree. \n2. Climb a rock face."); scanf("%i", &answ); waitOnCR(); // 5 - Answer2 if (answ==1) { NSLog(@"\n\nYou climb to the top of a mossed over tree. The view is great and you can see a meadowt that would be ideal for a helicopter landing. \nHowever, you also see a perfect area for a boat rescue on the east coast. \nAs you take in the view, you feel a jab and a following stinging sensation. \nYou have been bit by a snake. \nPress enter to continue."); waitOnCR(); NSLog(@"\n\nAfter you climb down the tree, you faint at its base. Health is decreased significantly."); waitOnCR(); health= health - 50; if (health<=0){ break; } }else { NSLog(@"\n\nYou climb to the top of a mossed over tree. The view is great and you can see a meadowt that would be ideal for a helicopter landing. \nHowever, you also see a perfect area for a boat rescue on the east coast. \nPress enter to continue."); } NSLog(@"\n\nYou decide to talk to the east coast as it seems to be the most likely rescue point. \nPress enter to continue."); waitOnCR(); NSLog(@"\n\nAs you walk through the jungle, you trip over a net. You are instantly flung upside down. \nPress enter to continue."); waitOnCR(); NSLog(@"As you look around, all you can make out are dark forms with bright painted faces. You are surrounded by natives. \nPress enter to continue."); waitOnCR(); NSLog(@"\n\nThey take you to the east beach, and set you next to a grand fire. When they are not looking, you reach into your pockets for something useful. \nPress enter to continue."); waitOnCR(); if (ate_granola==true) { NSLog(@"\n\nYou search to find your granola bar wrapper, but you seem to have left it under a tree. you imagine that the shine foil wrapper would have deeply impressed the natives. \nThe natives knock you out."); health =0; break; } else { NSLog(@"You pull out your granola bar and draw their attention. They are shocked by the foil wrapper. They think it is a new metal. \nThe chief accepts your invitation to the honey granola bar. He is deeply impressed. \nPress enter to continue."); waitOnCR(); NSLog(@"\n\nIn this same moment, a large steel rescue boat approaches. The natives are terrified and leave you standing on the beach. \nYou are rescued. \nPress enter to continue."); waitOnCR(); NSLog(@"\n\nYOU WIN\nPress enter to continue."); } // 6 - Day Completion Message } } - (void) printHealth{ if (health>0) { NSLog(@"\n\n%@ managed to finish the dat with a total health of: %i\nPress enter to continue.", characterName, health); waitOnCR(); } } -(void) printScore:(float) x { NSLog(@"%@ based on your health you received %i percent of the available points.", characterName, score); waitOnCR(); } @end



RE: [Objective-C] Command line game - Deque - 02-12-2013

Looks like a cool game.
Compiling obj-c on Linux is a bit messy as I just discovered.

I did this:
sudo apt-get install gobjc gnustep gnustep-make gnustep-common

Added this to .bashrc
. /usr/share/GNUstep/Makefiles/GNUstep.sh

Created this GNUmakefile:

Quote:include $(GNUSTEP_MAKEFILES)/common.make

APP_NAME = Game
Game_OBJC_FILES = main.m Game.m

include $(GNUSTEP_MAKEFILES)/application.make

Still get this error:
Quote:deque@decra:~/objc$ make all
This is gnustep-make 2.6.1. Type 'make print-gnustep-make-help' for help.
Making all for app Game...
Compiling file main.m ...
main.m:1:34: fatal error: Foundation/Foundation.h: file or directory not found
Compiling finished.
make[3]: *** [obj/Game.obj/main.m.o] error 1
make[2]: *** [internal-app-run-compile-submake] error 2
make[1]: *** [Game.all.app.variables] error 2
make: *** [internal-all] error 2
(not original, I translated the error message to english)

Any ideas?


RE: [Objective-C] Command line game - Deque - 02-12-2013

Looks like a cool game.
Compiling obj-c on Linux is a bit messy as I just discovered.

I did this:
sudo apt-get install gobjc gnustep gnustep-make gnustep-common

Added this to .bashrc
. /usr/share/GNUstep/Makefiles/GNUstep.sh

Created this GNUmakefile:

Quote:include $(GNUSTEP_MAKEFILES)/common.make

APP_NAME = Game
Game_OBJC_FILES = main.m Game.m

include $(GNUSTEP_MAKEFILES)/application.make

Still get this error:
Quote:deque@decra:~/objc$ make all
This is gnustep-make 2.6.1. Type 'make print-gnustep-make-help' for help.
Making all for app Game...
Compiling file main.m ...
main.m:1:34: fatal error: Foundation/Foundation.h: file or directory not found
Compiling finished.
make[3]: *** [obj/Game.obj/main.m.o] error 1
make[2]: *** [internal-app-run-compile-submake] error 2
make[1]: *** [Game.all.app.variables] error 2
make: *** [internal-all] error 2
(not original, I translated the error message to english)

Any ideas?


RE: [Objective-C] Command line game - Anima Templi - 02-12-2013

I have no ideas? I'm developing from a MacBook and it works just fine. I know that objective-c is a douchebag when it comes to other OS's than OS X and iOS. The only thing i can do to help you now is giving you this link:

http://www.techotopia.com/index.php/Installing_and_Using_GNUstep_and_Objective-C_on_Linux

It should insure that you have installed GNUstep properly.


RE: [Objective-C] Command line game - Anima Templi - 02-12-2013

I have no ideas? I'm developing from a MacBook and it works just fine. I know that objective-c is a douchebag when it comes to other OS's than OS X and iOS. The only thing i can do to help you now is giving you this link:

http://www.techotopia.com/index.php/Installing_and_Using_GNUstep_and_Objective-C_on_Linux

It should insure that you have installed GNUstep properly.


RE: [Objective-C] Command line game - Deque - 03-24-2013

Hi [twu=26232]Anima Templi[/twu].

I am compiling the HC CLI Game Collection.
Could you provide a binary for Mac of your game?
I am not able to compile it.

Regards
Deque


RE: [Objective-C] Command line game - Deque - 03-24-2013

Hi [twu=26232]Anima Templi[/twu].

I am compiling the HC CLI Game Collection.
Could you provide a binary for Mac of your game?
I am not able to compile it.

Regards
Deque


RE: [Objective-C] Command line game - Anima Templi - 03-24-2013

I will take a look at it, when I get the time. Hopefully soon.


RE: [Objective-C] Command line game - Anima Templi - 03-24-2013

I will take a look at it, when I get the time. Hopefully soon.