Program Acts Strangely

Discussion in 'Mac Programming' started by bguy, Jun 7, 2009.

  1. macrumors newbie

    Joined:
    May 30, 2009
    #1
    Hello, I am trying to make a text based rpg with an interface, though I am having trouble with the fighting system. When the fight system is activated, instead of displaying what is happening step by step, it does the entire fight (and seems to freeze while doing it) then displays the results. How can I get it to work like I want it to?
    Here is the code I wrote:
    Code:
    #import "FightController.h"
    
    
    @implementation FightController
    - (IBAction)Fight:(id)sender;
    {
    	if ([Status stringValue] == @"Safe")
    	{
    		srand( time(NULL));
    		int Monster;
    		Monster = rand() % ([Level intValue] + 1);
    		int ATKA;
    		int ATKB;
    		int ATKC;
    		NSString *ATKAD;
    		NSString *ATKBD;
    		NSString *ATKCD;
    		bool No;
    		No = FALSE;
    		switch (Monster)
    		{
    			case 0:
    				[Main setStringValue:@"You didn't find a monster."];
    				No = TRUE;
    				break;
    			case 1:
    				[Main setStringValue:@"You got attacked by a giant rat!"];
    				[Name setStringValue:@"Giant Rat"];
    				[MLevel setIntValue:(rand() %3 + 1)];
    				[MMaxHP setIntValue:([MLevel intValue] * 5)];
    				[MHP setIntValue:[MMaxHP intValue]];
    				[MGold setIntValue:([MLevel intValue] * 3)];
    				[MXP setIntValue:([MLevel intValue] + 3)];
    				[Descript setStringValue:@"Giant rats live almost everywhere: forests, deserts, sewers, etc. They offer little danger to anyone but the most inexperianced fighters. There tails are often used as a fashion accessory."];
    				ATKA = 2;
    				ATKAD = [NSString stringWithString:@"The giant rat swiped you with its claw."];
    				ATKB = 3;
    				ATKBD = [NSString stringWithString:@"The giant rat swiped you with its tail."];
    				ATKC = 5;
    				ATKCD = [NSString stringWithString:@"THe giant rat bit you."];
    			default:
    				[Main setStringValue:@"You got attacked by a giant rat!"];
    				[Name setStringValue:@"Giant Rat"];
    				[MLevel setIntValue:(rand() %3 + 1)];
    				[MMaxHP setIntValue:([MLevel intValue] * 5)];
    				[MHP setIntValue:[MMaxHP intValue]];
    				[MGold setIntValue:([MLevel intValue] * 3)];
    				[MXP setIntValue:([MLevel intValue] + 3)];
    				[Descript setStringValue:@"Giant rats live almost everywhere: forests, deserts, sewers, etc. They offer little danger to anyone but the most inexperianced fighters. There tails are often used as a fashion accessory."];
    				ATKA = 2;
    				ATKAD = [NSString stringWithString:@"The giant rat swiped you with its claw."];
    				ATKB = 3;
    				ATKBD = [NSString stringWithString:@"The giant rat swiped you with its tail."];
    				ATKC = 5;
    				ATKCD = [NSString stringWithString:@"THe giant rat bit you."];
    		}
    		sleep(3);
    		if (No)
    		{
    		}
    		else
    		{
    			[Status setStringValue:@"Fighting"];
    			int first;
    			first = rand() % 2 + 1;
    			do
    			{
    				if (first == 1)
    				{
    					int Multiplier;
    					Multiplier = rand() % 5;
    					switch (Multiplier)
    					{
    						case 0:
    						case 1:
    						case 2:
    							[Fight setStringValue:@"You succesfully landed a blow."];
    							[MHP setIntValue:([MHP intValue] - [Atk intValue])];
    							break;
    						case 3:
    							[Fight setStringValue:@"You made a critical hit!"];
    							[MHP setIntValue:([MHP intValue] - ([Atk intValue] * 2))];
    							break;
    						case 4:
    							[Fight setStringValue:@"You missed!"];
    					}
    					sleep(1);
    					if (([HP intValue] <= 0) || ([MHP intValue] <= 0))
    					{
    						break;
    					}
    					int DMG;
    					int Attack;
    					Attack = rand() % 3 + 1;
    					if (Attack == 1)
    					{
    						DMG = rand () % ATKA;
    						[Fight setStringValue:ATKAD];
    					}
    					if (Attack == 2)
    					{
    						DMG = rand () % ATKB;
    						[Fight setStringValue:ATKBD];
    					}
    					if (Attack == 3)
    					{
    						DMG = rand () % ATKC;
    						[Fight setStringValue:ATKCD];
    					}
    					[HP setIntValue:([HP intValue] - DMG)];
    					sleep(1);
    				}
    				else
    				{
    					int DMG;
    					int Attack;
    					Attack = rand() % 3 + 1;
    					if (Attack == 1)
    					{
    						DMG = rand () % ATKA;
    						[Fight setStringValue:ATKAD];
    					}
    					if (Attack == 2)
    					{
    						DMG = rand () % ATKB;
    						[Fight setStringValue:ATKBD];
    					}
    					if (Attack == 3)
    					{
    						DMG = rand () % ATKC;
    						[Fight setStringValue:ATKCD];
    					}
    					[HP setIntValue:([HP intValue] - DMG)];
    					sleep(1);
    					if (([HP intValue] <= 0) || ([MHP intValue] <= 0))
    					{
    						break;
    					}
    					int Multiplier;
    					Multiplier = rand() % 5;
    					switch (Multiplier)
    					{
    						case 0:
    						case 1:
    						case 2:
    							[Fight setStringValue:@"You succesfully landed a blow."];
    							[MHP setIntValue:([MHP intValue] - [Atk intValue])];
    							break;
    						case 3:
    							[Fight setStringValue:@"You made a critical hit!"];
    							[MHP setIntValue:([MHP intValue] - ([Atk intValue] * 2))];
    							break;
    						case 4:
    							[Fight setStringValue:@"You missed!"];
    					}	
    					sleep(1);
    				}
    			}
    			while (([HP intValue] > 0) && ([MHP intValue] > 0));
    			if ([MHP intValue] <= 0)
    			{
    				[Main setStringValue:@"You won the fight."];
    				[XP setIntValue:([XP intValue] + [MXP intValue])];
    				[Gold setIntValue:([Gold intValue] + [MGold intValue])];
    				[Status setStringValue:@"Safe"];
    			}
    			if ([HP intValue] <= 0)
    			{
    				[Main setStringValue:@"You died.\nGame Over."];
    				[Status setStringValue:@"Dead"];
    			}
    		}
    	}
    }
    @end
    
    PS: I don't want commenting on my habits, I just want to know what assumption/error I made and how I would be able to fix it.
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Are you running all that in the main thread? If so then the sleeps etc will cause the main thread to pause, the UI won't update etc. You need to split that out into a separate thread and call back to the main thread to update the UI.
     
  3. thread starter macrumors newbie

    Joined:
    May 30, 2009
    #3
    I'm sorry, I don't know what you mean by thread. I assume yes though. How would you split it?
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Read this. Make sure you understand it. Threading can be a complex issue.
     
  5. macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #5
    Runloops and timers would probably be good enough, without threading.
     
  6. thread starter macrumors newbie

    Joined:
    May 30, 2009
  7. macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #7
    Pull the contents of the loop out into a "fightStep" method, and make an NSTimer that calls that method every certain amount of time.
     

Share This Page