How to generate random letters as per the word - ios

I’m developing word puzzle game,where one word is given.according to that word the random letters are generated…for that, i applied below logic but some how the random letters not generated as per the given word.
NSMutableArray *ArrOfABCD = [[NSMutableArray alloc]
initWithObjects:#"A",#"B",#"C",#"D",#"E",#"F",#"G",#"H",#"I",#"J",#"K",#"L",#"M",#"N",#"O",#"P",#"Q",#"R",#"S",#"T",#"U",#"V",#"W",#"X",#"Y",#"Z",
nil];
float x = 70;
float y = 100;
float width = 30;
float height = 20;
for(int i =0;i<32;i++)
{
NSUInteger randomIndex = arc4random() %(ArrOfABCD.count);
UIButton *btnLetter = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnLetter setFrame:CGRectMake(x, y, width, height)];
[btnLetter setTitle:[NSString stringWithFormat:#"%#",ArrOfABCD[randomIndex]]
forState:UIControlStateNormal];
[self.view addSubview:btnLetter];
x = x + width + 30;
if((i+1)%4==0)
{
x = 70;
y = y + height + 20;
}
}
can any one suggest me that where i made mistake?

You should generate the unique random number
NSMutableArray* ArrOfWords = [[NSMutableArray alloc] initWithObjects:#"Good",#"Home",#"Beauty",#"Good",nil];
NSMutableArray *ArrOfABCD = [[NSMutableArray alloc] initWithObjects:#"A",#"B",#"C",#"D",#"E",#"F",#"G",#"H",#"I",#"J",#"K",#"L",#"M",#"N",#"O",#"P",#"Q",#"R",#"S",#"T",#"U",#"V",#"W",#"X",#"Y",#"Z", nil];
NSMutableArray *arrDuplicate=[[NSMutableArray alloc]init];
float x = 70;
float y = 100;
float width = 30;
float height = 20;
NSInteger rowIndex=4;
NSString *dataString=[ArrOfWords objectAtIndex:0];
NSMutableArray *arrNumber = [[NSMutableArray alloc]init];
for (int i = 0; i < [dataString length]; i++) {
NSInteger index=arc4random_uniform(rowIndex)+rowIndex;
NSNumber *number=[NSNumber numberWithInt:index];
while ([arrDuplicate containsObject:number] ) {
NSInteger index=arc4random_uniform(rowIndex)+rowIndex;
number=[NSNumber numberWithInt:index];
}
[arrNumber addObject:number];
[arrDuplicate addObject:number];
}
[arrDuplicate removeAllObjects];
NSMutableArray *arrayChar = [NSMutableArray array];
for (int i = 0; i < [dataString length]; i++) {
[arrayChar addObject:[NSString stringWithFormat:#"%C", [dataString characterAtIndex:i]]];
}
for(int i =0;i<32;i++)
{
NSString *str=[ArrOfABCD objectAtIndex:arc4random_uniform(ArrOfABCD.count)];
if ([arrNumber containsObject:[NSNumber numberWithInt:i]] ) {
str=[arrayChar objectAtIndex:arrDuplicate.count];
[arrDuplicate addObject:str];
}
NSLog(#"%#",str);
UIButton *btnLetter = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnLetter setFrame:CGRectMake(x, y, width, height)];
[btnLetter setTitle:str forState:UIControlStateNormal];
x = x + width + 30;
if((i+1)%4==0)
{
x = 70;
y = y + height + 20;
}
}

Related

instantiate an NSArray with integers value

I have a PickerView and I want to initialize it with integer values. For example PickerView with all integers lower than 10.
Thank you
NSArray *array =[NSArray arrayWithObjects:#1,#2,#3,#4,#5,#6,#7,#8,#9,nil];
NSMutableArray *yourArray = [[NSMutableArray alloc] init];
for (int i = 0; i < 10; i++) {
[yourArray addObject:[NSNumber numberWithInt:i]];
}
NSInteger min = 1;
NSInteger max = 10;
NSInteger step = 1;
NSMutableArray<NSNumber *> *options = [NSMutableArray new];
for (NSInteger index = 0; index < ceilf(((float)max)/step); index++)
{
options[index] = #((index + min) * step);
}
NSLog(#"%#",options);
An alternate approach:
NSInteger from = 1;
NSInteger size = 10;
NSInteger step = 1;
NSMutableArray<NSNumber *> *options = [NSMutableArray new];
for (NSInteger index = 0; index < size; index++)
{
options[index] = #((index + from) * step);
}
NSLog(#"%#",options);
Both of the above print the following:
(
1,
2,
3,
4,
5,
6,
7,
8,
9,
10
)
int yourInt = 5;
[myMutableArray addObject:[NSNumber numberWithInt:yourInt]]; have look on this
It will just sample code to add the int value to the array nothing else

SHLineGraphView - Set graph origin value

Using SHLineGraphView
I want to specify the origin of my graph to a different value other than 0.
The issue was already created here Min/Max draw Y axis but the owner seems uninterested in solving it.
This is what I have now
SHLineGraphView.m
#define BOTTOM_MARGIN_TO_LEAVE 30.0
#define TOP_MARGIN_TO_LEAVE 30.0
#define INTERVAL_COUNT 10
//INTERVAL_COUNT - 3 -> Limits the y-axis to 60 but
//not the entire graph.
- (void)drawYLabels:(SHPlot *)plot {
double yRange = [_yAxisRange doubleValue]; // this value will be in dollars
double yIntervalValue = yRange / INTERVAL_COUNT;
double intervalInPx = (self.bounds.size.height - BOTTOM_MARGIN_TO_LEAVE ) / (INTERVAL_COUNT - 3);
NSLog(#"interval px %f",intervalInPx);
NSMutableArray *labelArray = [NSMutableArray array];
float maxWidth = 0;
for(int i= 0; i <= INTERVAL_COUNT - 3 ; i++){
CGPoint currentLinePoint = CGPointMake(_leftMarginToLeave, i * intervalInPx);
CGRect lableFrame = CGRectMake(0, currentLinePoint.y - (intervalInPx / 2), 100, intervalInPx);
NSLog(#"label frame %f", lableFrame.origin.y);
//if(i != 0) {
UILabel *yAxisLabel = [[UILabel alloc] initWithFrame:lableFrame];
yAxisLabel.backgroundColor = [UIColor clearColor];
yAxisLabel.font = (UIFont *)_themeAttributes[kYAxisLabelFontKey];
yAxisLabel.textColor = (UIColor *)_themeAttributes[kYAxisLabelColorKey];
yAxisLabel.textAlignment = NSTextAlignmentCenter;
float val = (yIntervalValue * (10 - i));
NSLog(#"value %f", val);
if(val > 0){
yAxisLabel.text = [NSString stringWithFormat:#"%.1i%#", (int)val, _yAxisSuffix];
} else {
yAxisLabel.text = [NSString stringWithFormat:#"%.0f", val];
}
[yAxisLabel sizeToFit];
CGRect newLabelFrame = CGRectMake(0, currentLinePoint.y - (yAxisLabel.layer.frame.size.height / 2), yAxisLabel.frame.size.width, yAxisLabel.layer.frame.size.height);
yAxisLabel.frame = newLabelFrame;
if(newLabelFrame.size.width > maxWidth) {
maxWidth = newLabelFrame.size.width;
}
[labelArray addObject:yAxisLabel];
[self addSubview:yAxisLabel];
//}
}
viewController.m
_graphView.yAxisRange = #(200);
The y-axis is ok but the graph is still plotted from origin 0.
Create a yAxisMinValue property
#property (nonatomic, strong) NSNumber *yAxisMinValue;
...and update your drawYLabels function as follows:
- (void)drawYLabels:(SHPlot *)plot {
double yRange = [_yAxisRange doubleValue]; // this value will be in dollars
double yRangeMin = [_yAxisMinValue doubleValue];
NSLog(#"yRange: %f", yRange);
NSLog(#"yRangeMin: %f", yRangeMin);
double yIntervalValue = (yRange - yRangeMin) / INTERVAL_COUNT;
NSLog(#"yIntervalValue: %f", yIntervalValue);
double intervalInPx = (self.bounds.size.height - BOTTOM_MARGIN_TO_LEAVE ) / (INTERVAL_COUNT +1);
NSMutableArray *labelArray = [NSMutableArray array];
float maxWidth = 0;
for(int i= INTERVAL_COUNT + 1; i >= 0; i--){
NSLog(#"i = %d",i);
CGPoint currentLinePoint = CGPointMake(_leftMarginToLeave, i * intervalInPx);
CGRect lableFrame = CGRectMake(0, currentLinePoint.y - (intervalInPx / 2), 100, intervalInPx);
if(i != 0) {
UILabel *yAxisLabel = [[UILabel alloc] initWithFrame:lableFrame];
yAxisLabel.backgroundColor = [UIColor clearColor];
yAxisLabel.font = (UIFont *)_themeAttributes[kYAxisLabelFontKey];
yAxisLabel.textColor = (UIColor *)_themeAttributes[kYAxisLabelColorKey];
yAxisLabel.textAlignment = NSTextAlignmentCenter;
float val = (yIntervalValue * (10 - i)) + yRangeMin;
if(val > yRangeMin){
yAxisLabel.text = [NSString stringWithFormat:#"%.1f%#", val, _yAxisSuffix];
NSLog(#"%.1f%#", val, _yAxisSuffix);
} else {
yAxisLabel.text = [NSString stringWithFormat:#"%.1f", val];
NSLog(#"%.1f", val);
}
[yAxisLabel sizeToFit];
CGRect newLabelFrame = CGRectMake(0, currentLinePoint.y - (yAxisLabel.layer.frame.size.height / 2), yAxisLabel.frame.size.width, yAxisLabel.layer.frame.size.height);
yAxisLabel.frame = newLabelFrame;
if(newLabelFrame.size.width > maxWidth) {
maxWidth = newLabelFrame.size.width;
}
[labelArray addObject:yAxisLabel];
[self addSubview:yAxisLabel];
}
}

Creepify text - Combining characters

I want to create creepify text in my app with js code (creepify())
https://github.com/combatwombat/Lunicode.js/blob/master/lunicode.js
website using this js code: http://lunicode.com/creepify
My problem is when i try to combine characters, the result is'nt like in website.
I try to create 3 NSMutableArray
NSMutableArray *diacriticsTop;
NSMutableArray *diacriticsMiddle;
NSMutableArray *diacriticsBottom;
and store value like this
for (int i = 768; i <= 789; i++) {
[diacriticsTop addObject:[NSNumber numberWithInt:i]];
}
for (int i = 790; i <= 819; i++) {
if (i != 794 && i != 795) {
[diacriticsBottom addObject:[NSNumber numberWithInt:i]];
}
}
...
then i convert creepify() in js code to
- (NSString *)creepifyString:(NSString *)inputString
{
BOOL isTop = YES;
BOOL isMiddle = YES;
BOOL isBottom = YES;
NSInteger maxHeight = 15;
NSInteger randomization = 100;
NSMutableString *outputString = [[NSMutableString alloc] init];
unichar output = 0;
for (int i = 0; i < inputString.length; i++) {
unichar c = [inputString characterAtIndex:i];
if (isMiddle) {
NSNumber *temp = diacriticsMiddle[lroundf(drand48()*diacriticsMiddle.count)];
c = c + (unichar) temp;
}
// Top
if (isTop) {
// Put up to this.options.maxHeight random diacritics on top.
// optionally fluctuate the number via the randomization value (0-100%)
// randomization 100%: 0 to maxHeight
// 30%: 70% of maxHeight to maxHeight
// x%: 100-x% of maxHeight to maxHeight
int diacriticsTopLength = diacriticsTop.count - 1;
for (int count = 0,
len = maxHeight - drand48()*((randomization/100)*maxHeight); count < len; count++) {
NSNumber *temp = diacriticsTop[lroundf(drand48()*diacriticsTopLength)];
c = c + (unichar) temp;
}
}
// Bottom
if (isBottom) {
int diacriticsBottomLength = diacriticsBottom.count - 1;
for (int count = 0,
len = maxHeight - drand48()*((randomization/100)*maxHeight); count < len; count++) {
NSNumber *temp =diacriticsBottom[lroundf(drand48()*diacriticsBottomLength)];
c = c + (unichar) temp;
}
}
output = output + c;
}
[outputString appendFormat:#"%C", output];
NSLog(#"%#", outputString);
return outputString;
}

Losing Data in NSMutableArray during Loop

I am trying to create a NSMutableArray so I can populate a map. But immediately after "[row removeAllObjects];" it removes all of the objects in the "layer" array also.
NSMutableArray *row = [[NSMutableArray alloc] init];
NSMutableArray *layer = [[NSMutableArray alloc] init];
NSMutableArray *all = [[NSMutableArray alloc] init];
int blockType = 0;
for (int y = 0; y<10; y++) {
for (int x = 0; x<10; x++) {
for (int z = 0; z<10; z++) {
if (y<5) blockType = 0;
else blockType = arc4random() % 2 + 1;
[row addObject:[NSNumber numberWithInt:blockType]];
}
[layer addObject:row];
[row removeAllObjects];
}
[all addObject:layer];
[layer removeAllObjects];
}
is this because
[layer addObject:row];
[row removeAllObjects];
are being performed at the same time? If so, how do I carry out the actions one after another?
If not, what am I doing wrong?
Try this,
[layer addObject:[row copy]];
You are adding row to layer, but then you're emptying it.
I understand why you are doing this, as you want to re-use both row and layer. However that's not how it works in Objective-C, as variables are just references (C pointers) to objects.
What you need to do is to create all outside the loops, and re-create row and layer inside the inner loops. You can re-use the variables, but the underlying objects need to change.
Try this:
NSMutableArray *all = [[NSMutableArray alloc] init];
NSMutableArray *row;
NSMutableArray *layer;
int blockType = 0;
for (int y = 0; y < 10; y++) {
layer = [[NSMutableArray alloc] init];
for (int x = 0; x < 10; x++) {
row = [[NSMutableArray alloc] init];
for (int z = 0; z < 10; z++) {
if (y < 5)
blockType = 0;
else
blockType = arc4random() % 2 + 1;
[row addObject:[NSNumber numberWithInt:blockType]];
}
[layer addObject: row];
}
[all addObject: layer];
}
Move NSMutableArray *row = [[NSMutableArray alloc] init]; to just after for (int x = 0; x<10; x++) {, and delete [row removeAllObjects]; entirely.
You create a problematic program structure, and it bites you in the behind.
for (int z = 0; z<10; z++) {
if (y<5) blockType = 0;
else blockType = arc4random() % 2 + 1;
[row addObject:[NSNumber numberWithInt:blockType]];
}
[layer addObject:row];
[row removeAllObjects];
This code is supposed to create an array with 10 numbers and add it to the array "layer". But that's not what it does. The array has been created a lot earlier. And add the end, it empties out the array to be ready for the next iteration. That's an awful structure. The correct structure would be:
NSMutableArray* row = [NSMutableArray array];
for (int z = 0; z<10; z++) {
if (y<5) blockType = 0;
else blockType = arc4random() % 2 + 1;
[row addObject:[NSNumber numberWithInt:blockType]];
}
[layer addObject:row];
This does exactly what it is supposed to do: Create an array, fill it with data, add it to layer. No reliance on previous and following code. The complete working code is:
NSMutableArray *all = [[NSMutableArray alloc] init];
for (int y = 0; y<10; y++) {
NSMutableArray *layer = [[NSMutableArray alloc] init];
for (int x = 0; x<10; x++) {
NSMutableArray *row = [[NSMutableArray alloc] init];
for (int z = 0; z<10; z++) {
int blockType = y < 5 ? 0 : arc4random() % 2 + 1;
[row addObject:[NSNumber numberWithInt:blockType]];
}
[layer addObject:row];
}
[all addObject:layer];
}
Well, someone else typed quicker. Which is even better, because you see twice an identical program structure, with almost identical code. That's because the task is clear, and there is exactly one way how any experienced programmer would structure the code.

UISegmentedControl index

I am trying to list out the selected index of many UISegmentControls. I set 5 of them on 1, 2, 3, 4, 5 respectively, and the result after using this code (in the array) is: 2, 2, 0, 0, 0...
for (UISegmentedControl *seg in segmentArray)
{
NSString* xWrapped = [NSString stringWithFormat:#"%d", seg.selectedSegmentIndex+1];
[difficultyH addObject: xWrapped];
}
Why??
EDIT:
This is how the segments are created:
//segment controll
NSArray *itemArray2 = [NSArray arrayWithObjects: #"very easy", #"easy", #"ok", #"hard", #"challenging", nil];
UISegmentedControl *segmentedControl2 = [[UISegmentedControl alloc] initWithItems:itemArray2];
segmentedControl2.frame = CGRectMake(480, -60, 130, 350);
segmentedControl2.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl2.selectedSegmentIndex = val - 1;
[segmentedControl2 addTarget:self action:#selector(segmentedControlHomework:) forControlEvents:UIControlEventValueChanged];
segmentedControl2.transform =
CGAffineTransformRotate(segmentedControl2.transform, degreesToRadians(90));
NSArray *arr = [segmentedControl2 subviews];
for (int i = 0; i < [arr count]; i++) {
UIView *v = (UIView*) [arr objectAtIndex:i];
NSArray *subarr = [v subviews];
for (int j = 0; j < [subarr count]; j++) {
if ([[subarr objectAtIndex:j] isKindOfClass:[UILabel class]]) {
UILabel *l = (UILabel*) [subarr objectAtIndex:j];
l.transform = CGAffineTransformMakeRotation(- M_PI / 2.0); //do the reverse of what Ben did
}
}
}
[image1 addSubview:segmentedControl2];
segmentedControl2.tag = i;
[segmentArray addObject: segmentedControl2];
//segment controll

Resources