Blog

Galton Board in ObjectiveC

by Thomas Spycher on September 12, 2010

Target was to build an application to simulation a “Galton Board“.

Galtonsches_Brett.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#import 
 
@interface Galtonsches_Brett : NSObject {
@private
 
}
 
- (void)Calculate:(double)numBalls field0:(NSProgressIndicator**)field0
                                        field1:(NSProgressIndicator**)field1
                                        field2:(NSProgressIndicator**)field2
                                        field3:(NSProgressIndicator**)field3
                                        field4:(NSProgressIndicator**)field4
                                        field5:(NSProgressIndicator**)field5;
 
- (int)RandomField;
@end

Galtonsches_Brett.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#import "Galtonsches_Brett.h"
 
@implementation Galtonsches_Brett
 
- (void)Calculate:(double)numBalls field0:(NSProgressIndicator**)field0
           field1:(NSProgressIndicator**)field1
           field2:(NSProgressIndicator**)field2
           field3:(NSProgressIndicator**)field3
           field4:(NSProgressIndicator**)field4
           field5:(NSProgressIndicator**)field5 {
    // Set maximul value
    [*field0 setMaxValue:numBalls/2];
    [*field1 setMaxValue:numBalls/2];
    [*field2 setMaxValue:numBalls/2];
    [*field3 setMaxValue:numBalls/2];
    [*field4 setMaxValue:numBalls/2];
    [*field5 setMaxValue:numBalls/2];
 
    // Set current value
    [*field0 setDoubleValue:0];
    [*field1 setDoubleValue:0];
    [*field2 setDoubleValue:0];
    [*field3 setDoubleValue:0];
    [*field4 setDoubleValue:0];
    [*field5 setDoubleValue:0];
 
    // Start the animation and make the progressbar visible
    [*field0 startAnimation:self];
    [*field1 startAnimation:self];
    [*field2 startAnimation:self];
    [*field3 startAnimation:self];
    [*field4 startAnimation:self];
    [*field5 startAnimation:self];
 
    int field;
    for(double balls = 1; balls <= numBalls; balls++) {
        // Get a field
        field = [self RandomField];
        // This whole shit should be done with a @selector
        switch (field) {
            case 0:
                [*field0 setDoubleValue:[*field0 doubleValue]+1];
                break;
            case 1:
                [*field1 setDoubleValue:[*field1 doubleValue]+1];
                break;
            case 2:
                [*field2 setDoubleValue:[*field2 doubleValue]+1];
                break;
            case 3:
                [*field3 setDoubleValue:[*field3 doubleValue]+1];
                break;
            case 4:
                [*field4 setDoubleValue:[*field4 doubleValue]+1];
                break;
            case 5:
                [*field5 setDoubleValue:[*field5 doubleValue]+1];
                break;
        }
        //[self performSelector:@selector(myTestWithAString:) withObject:myString];
    }
}
 
- (int)RandomField {
    int nail = 0;
    int rows = 5;
    for(int row = 0;row <= rows-1;row++) {
        int random = ((int)rand() % 2) + 1;
        if(random == 1) {
            // go left
            nail = nail;
        } else {
            // go right
            nail++;
        }
        //NSLog(@"row: %d and leftRright: %d next nail: %d",row, random, nail);
    }
    return nail;
}

An this is how it gets called:

1
2
3
4
5
Galtonsches_Brett *brett = [[Galtonsches_Brett alloc] init];
 
[brett Calculate:[[numberBalls stringValue] doubleValue] field0:&field0 field1:&field1 field2:&field2 field3:&field3 field4:&field4 field5:&field5];
 
[brett release];

The whole Application is here: XGaltonsches Brett (204)

Wanna say something?