This site requires JavaScript, please enable it in your browser!
Greenfoot back
Don'tCallMeDavie
Don'tCallMeDavie wrote ...

2018/11/29

Help? (Array contains...)

Don'tCallMeDavie Don'tCallMeDavie

2018/11/29

#
So I'm working on a personal project. Basically Bingo. Is there any possible way for you to see if an array contains a value? Here's the code i'm working on.
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class MyWorld here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class MyWorld extends greenfoot.World
{
    private Call call = new Call();
    private Button button = new Button();
    private boolean taken[] = {false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false};
    private Numbers numbers[] = {null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null};
    private String called[] = {null};
    private int arraySize = 0;
    /**
     * Constructor for objects of class MyWorld.
     *
     */
    public MyWorld()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
        initialize();
    }
    public void act()
    {
        update();
        checkWin();
    }
    private void checkWin()
    {
        if (taken[0] == true & taken[1] == true & taken[2] == true & taken[3] == true & taken[4] == true)
        {
            win();
        }
        if (taken[5] == true & taken[6] == true & taken[7] == true & taken[8] == true & taken[9] == true)
        {
            win();
        }
        if (taken[10] == true & taken[11] == true & taken[12] == true & taken[13] == true & taken[14] == true)
        {
            win();
        }
        if (taken[15] == true & taken[16] == true & taken[17] == true & taken[18] == true & taken[19] == true)
        {
            win();
        }
        if (taken[20] == true & taken[21] == true & taken[22] == true & taken[23] == true & taken[24] == true)
        {
            win();
        }
        if (taken[0] == true & taken[5] == true & taken[10] == true & taken[15] == true & taken[20] == true)
        {
            win();
        }
        if (taken[1] == true & taken[6] == true & taken[11] == true & taken[16] == true & taken[21] == true)
        {
            win();
        }
        if (taken[2] == true & taken[7] == true & taken[12] == true & taken[17] == true & taken[22] == true)
        {
            win();
        }
        if (taken[3] == true & taken[8] == true & taken[13] == true & taken[18] == true & taken[23] == true)
        {
            win();
        }
        if (taken[4] == true & taken[9] == true & taken[14] == true & taken[19] == true & taken[24] == true)
        {
            win();
        }
        if (taken[0] == true & taken[6] == true & taken[12] == true & taken[18] == true & taken[24] == true)
        {
            win();
        }
        if (taken[4] == true & taken[8] == true & taken[12] == true & taken[16] == true & taken[20] == true)
        {
            win();
        }
    }
    private void win()
    {
        removeObject(button);
        removeObject(call);
        GreenfootImage img = new GreenfootImage("Bingo!",50,Color.BLACK,Color.WHITE);
        getBackground().drawImage(img,100-img.getWidth()/2,getHeight()/2-img.getHeight()/2);
        Greenfoot.stop();
    }
    private void initialize()
    {
        addObject(new Board(),400,200);
        int y = 99;
        int x = 240;
        int identifier = 0;
        do
        {
            numbers[identifier] = new Numbers('b',identifier);
            addObject(numbers[identifier],x,y);
            y = y + 66;
            identifier++;
        }while(y != 429);
        x = x + 80;
        y = 99;
        do
        {
            numbers[identifier] = new Numbers('i',identifier);
            addObject(numbers[identifier],x,y);
            y = y + 66;
            identifier++;
        }while(y != 429);
        x = x + 80;
        y = 99;
        do
        {
            numbers[identifier] = new Numbers('n',identifier);
            addObject(numbers[identifier],x,y);
            y = y + 66;
            identifier++;
        }while(y != 429);
        x = x + 80;
        y = 99;
        do
        {
            numbers[identifier] = new Numbers('g',identifier);
            addObject(numbers[identifier],x,y);
            y = y + 66;
            identifier++;
        }while(y != 429);
        x = x + 80;
        y = 99;
        do
        {
            numbers[identifier] = new Numbers('o',identifier);
            addObject(numbers[identifier],x,y);
            y = y + 66;
            identifier++;
        }while(y != 429);
         
        int number = 0;
        do
        {
            taken[number] = false;
            number++;
        }while(number != 25);
         
        addObject(call,100,100);
        addObject(button,100,350);
    }
    public void call()
    {
        int callNum = Greenfoot.getRandomNumber(51);
        char callChar[] = {'b','i','n','g','o'};
        int randomNum = Greenfoot.getRandomNumber(5);
        int identifier = 0;
        String totalCall = "" + callChar[randomNum] + callNum;
        do
        {
            callNum = Greenfoot.getRandomNumber(51);
            randomNum = Greenfoot.getRandomNumber(5);
            identifier = 0;
            totalCall = "" + callChar[randomNum] + callNum;
        }while(arrayContains(totalCall));
         
        call.setCall(callChar[randomNum],callNum);
        do
        {
            if (totalCall.equals(numbers[identifier].getCall()))
            {
                numbers[identifier].setClickable(true);
            }
            identifier++;
        }while(identifier != 25);
    }
    private void update()
    {
        int identifier = 0;
        do
        {
            taken[identifier] = numbers[identifier].takenStatus();
            identifier++;
        }while(identifier != 25);
    }
}
These lines are what i'm having trouble with.
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
public void call()
    {
        int callNum = Greenfoot.getRandomNumber(51);
        char callChar[] = {'b','i','n','g','o'};
        int randomNum = Greenfoot.getRandomNumber(5);
        int identifier = 0;
        String totalCall = "" + callChar[randomNum] + callNum;
        do
        {
            callNum = Greenfoot.getRandomNumber(51);
            randomNum = Greenfoot.getRandomNumber(5);
            identifier = 0;
            totalCall = "" + callChar[randomNum] + callNum;
        }while(arrayContains(totalCall));
         
        call.setCall(callChar[randomNum],callNum);
        do
        {
            if (totalCall.equals(numbers[identifier].getCall()))
            {
                numbers[identifier].setClickable(true);
            }
            identifier++;
        }while(identifier != 25);
    }
Help would be appreciated. Thank you in advance!
Don'tCallMeDavie Don'tCallMeDavie

2018/11/29

#
To be more specific, at the end of the DoWhile loop, I want the loop to break once the String value isn't contained in the array.
1
2
3
4
5
6
7
        do
        {
            callNum = Greenfoot.getRandomNumber(51);
            randomNum = Greenfoot.getRandomNumber(5);
            identifier = 0;
            totalCall = "" + callChar[randomNum] + callNum;
        }while(arrayContains(totalCall));
danpost danpost

2018/11/29

#
As an array, you can only iterate through and check each element individually. However, you can save some coding and time by having the array treated as a List object where the contains method would be useful.
Don'tCallMeDavie Don'tCallMeDavie

2018/11/30

#
I've never learned how to use lists. So how could I create a list object to check that?
danpost danpost

2018/11/30

#
Don'tCallMeDavie wrote...
I've never learned how to use lists. So how could I create a list object to check that?
It would be something like this:
1
while (Arrays.asList(arrayName).contains(totalCall))
However, it would be worthwhile to be more direct in your programming. You can avoid the loop while making the choice a simple one. That is, getting one random value that will, by default, point to an uncalled value. You can start with an array of values:
1
2
3
private int[] choices = new int[51*5];
// initialized in constructor with
for (int i=0; i<choices.lengtth; i++) choices[i] = i;
The values range from zero to one less than the number of choices. Now, add a counter for the number of calls made:
1
private int callsMade;
Then the number of choices will always be the initial number of choices minus the number of choices made:
1
2
int chosen = Greenfoot.getRandomNumber(51*5-callsMade);
callsMade++;
To make the random value always point to an unmade choice, one simple movement can be made in the array to keep all the uncalled choices toward the front of the array after grabbing the called one:
1
2
int called = choices[chosen];
choices[chosen] = choices[51*5-callsMade];
The called choice can now be evaluated:
1
2
3
int alphaIndex = called/51;
char callChar = "bingo".charAt(alphaIndex);
int callNum = called%51;
Notice how everything was done absolutely -- no trial and error; so, no loops. Now I will be bold (not) -- you like, Davie?
Don'tCallMeDavie Don'tCallMeDavie

2018/11/30

#
It took me a few moments to fully wrap my head around what you were telling me -- But as far as I can see the system is giving me a random choice of calls with no repeats, which is exactly what I wanted! Thank you so much for the help!
You need to login to post a reply.