Tonight I really got into the Cocoa learning and it did not seem that hard. I got my random d100 dice roller working (I play RoleMaster with friends
) where it keeps track of the last 4 roles. I also added a slider which I could set from values 1 to 5 to pick the highest roll out of X numbers of rolls.
I created 1 class and all the code from my IBOutlet and IBActions went in to. I wrote no code from main except what it came with when the program launched.
Here is what I am wondering:
Should I be writing more of this code in main.m and sending messages to different object methods, or is writing everything in 1 class normal? Pascal class was teaching me to send things out of main into Functions to process and keep my main part clean and easier to read.
Here is my class implementation
It seems a bit cluttered and I was just wondering, how to clean it. Jim showed me a nice wargame code clean up. I am just wondering what I could do to make this look nicer, even though it functions.
Thanks for any guidance.
-Lars
I created 1 class and all the code from my IBOutlet and IBActions went in to. I wrote no code from main except what it came with when the program launched.
Here is what I am wondering:
Should I be writing more of this code in main.m and sending messages to different object methods, or is writing everything in 1 class normal? Pascal class was teaching me to send things out of main into Functions to process and keep my main part clean and easier to read.
Here is my class implementation
Code:
#import "theDice.h"
@implementation theDice
int lastDiceRolled = 0;
int secondDiceRolled = 0;
int thirdDiceRolled = 0;
int forthDiceRolled = 0;
int sliderValue[5];
int bestNumber, numberOnSlider,theRandomRollValue;
-(void)awakeFromNib{
[slider setIntValue:1];
[numRollsToDo setIntValue:[slider intValue]];
}
- (IBAction)theButton:(id)sender {
bestNumber = 0;
for (int i=0; i <= 4; i++) {
sliderValue[i]= 0;
}
numberOnSlider = [slider intValue];
for (int i=0; i < numberOnSlider; i++) {
theRandomRollValue = arc4random() %100 +1;
sliderValue[i] = theRandomRollValue;
if (bestNumber <= theRandomRollValue) {
bestNumber = theRandomRollValue;
}
}
NSString* str = [NSString stringWithFormat:@"%d", bestNumber];
[lable setStringValue:str];
NSString* last = [NSString stringWithFormat:@"%d", lastDiceRolled];
[lastRoll setStringValue:last];
NSString* second = [NSString stringWithFormat:@"%d", secondDiceRolled];
[secondRoll setStringValue:second];
NSString* third = [NSString stringWithFormat:@"%d", thirdDiceRolled];
[thirdRoll setStringValue:third];
NSString* forth = [NSString stringWithFormat:@"%d", forthDiceRolled];
[forthRoll setStringValue:forth];
forthDiceRolled = thirdDiceRolled;
thirdDiceRolled = secondDiceRolled;
secondDiceRolled = lastDiceRolled;
lastDiceRolled = bestNumber;
NSString *sliderTextSpaceOne =[NSString stringWithFormat:@"%d", sliderValue[0]];
[rOne setStringValue:sliderTextSpaceOne];
NSString *sliderTextSpaceTwo =[NSString stringWithFormat:@"%d", sliderValue[1]];
[rTwo setStringValue:sliderTextSpaceTwo];
NSString *sliderTextSpaceThree =[NSString stringWithFormat:@"%d", sliderValue[2]];
[rThree setStringValue:sliderTextSpaceThree];
NSString *sliderTextSpacefour =[NSString stringWithFormat:@"%d", sliderValue[3]];
[rFour setStringValue:sliderTextSpacefour];
NSString *sliderTextSpacefive =[NSString stringWithFormat:@"%d", sliderValue[4]];
[rFive setStringValue:sliderTextSpacefive];
}
- (IBAction)bestOutOfRolls:(id)sender {
[numRollsToDo setIntValue:[slider intValue]];
numberOnSlider = [slider intValue];
}
@end
Thanks for any guidance.
-Lars