![]() |
| Forums | Gaming News | Videos | Downloads | Today's Posts | Mark Forums Read | Chat | FAQ | Members List | Contact |
| ||||||
This is a discussion on What is a simpler, more efficient way of writing this... within the PSP Development Forum forums, part of the PSP Development, Hacks, and Homebrew category; Im currently making a lottery game. I have some if statements that look like they can be smaller than they ...
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 |
![]() |
Im currently making a lottery game. I have some if statements that look like they can be smaller than they are: (this is in actionscript, flash 8)
Code:
on (press) {
success = 0;
if (in1 == null || in2 == null || in3 == null || in4 == null || in5 == null || in6 == null || in1 == "" || in2 == "" || in3 == "" || in4 == "" || in5 == "" || in6 == "") {
ready = 0;
messag = "Enter ALL numbers";
} else if (in2>49 || in3>49 || in4>49 || in5>49 || in6>49) {
ready = 0;
messag = "Numbers must range from 0-49";
} else if (in1 == in2 || in1 == in3 || in1 == in4 || in1 == in5 || in1 == in6 || in2 == in3 || in2 == in4 || in2 == in5 || in2 == in6 || in3 == in4 || in3 == in5 || in3 == in6 || in4 == in5 || in4 == in6 || in5 == in6) {
ready = 0;
messag = "Numbers cannot be the same";
} else {
ready = 1;
}
if (ready == 1) {
out1 = Math.round(Math.random()*49);
out2 = Math.round(Math.random()*49);
out3 = Math.round(Math.random()*49);
out4 = Math.round(Math.random()*49);
out5 = Math.round(Math.random()*49);
out6 = Math.round(Math.random()*49);
if (out1 == out2 || out1 == out3 || out1 == out4 || out1 == out5 || out1 == out6) {
out1 = Math.round(Math.random()*49);
}
if (out2 == out3 || out2 == out4 || out2 == out5 || out2 == out6) {
out2 = Math.round(Math.random()*49);
}
if (out3 == out4 || out3 == out5 || out3 == out6) {
out3 = Math.round(Math.random()*49);
}
if (out4 == out5 || out4 == out6) {
out4 = Math.round(Math.random()*49);
}
if (out5 == out6) {
out5 = Math.round(Math.random()*49);
}
if (out1 == 0) {
out1 = Math.round(Math.random()*49);
}
if (out2 == 0) {
out2 = Math.round(Math.random()*49);
}
if (out3 == 0) {
out3 = Math.round(Math.random()*49);
}
if (out4 == 0) {
out4 = Math.round(Math.random()*49);
}
if (out5 == 0) {
out5 = Math.round(Math.random()*49);
}
if (out6 == 0) {
out6 = Math.round(Math.random()*49);
}
if (in1 == out1 || in1 == out2 || in1 == out3 || in1 == out4 || in1 == out5 || in1 == out6) {
success++;
}
if (in2 == out1 || in2 == out2 || in2 == out3 || in2 == out4 || in2 == out5 || in2 == out6) {
success++;
}
if (in3 == out1 || in3 == out2 || in3 == out3 || in3 == out4 || in3 == out5 || in3 == out6) {
success++;
}
if (in4 == out1 || in4 == out2 || in4 == out3 || in4 == out4 || in4 == out5 || in4 == out6) {
success++;
}
if (in5 == out1 || in5 == out2 || in5 == out3 || in5 == out4 || in5 == out5 || in5 == out6) {
success++;
}
if (in6 == out1 || in6 == out2 || in6 == out3 || in6 == out4 || in6 == out5 || in6 == out6) {
success++;
}
messag = "You matched "+success+" numbers";
if (success == 1) {
messag = "You matched "+success+" number";
}
}
}
(highlight the if statements' conditions that go off the screen to reveal their mammouth lengh) -= Double Post =- Come On!
__________________
...Just Returned To The Scene... Last edited by JaSo PsP; 01-17-2007 at 12:21 PM.. Reason: Automerged Doublepost |
|
|
|
|
|
#2 |
![]() ![]() Developer
|
Do you have arrays in actionscript?
__________________
[Blog] [Portfolio] [Homebrew Illuminati - Serious Homebrew Development Forums] [I want to make Homebrew FAQ] [How I broke into the Games Industry] [Programming Book List] [Programming Article List] |
|
|
|
|
|
#3 |
![]() ![]() ...in a dream...
|
__________________
...you'll never know what it's like... spending your whole life in a dream...
Launch a Kitten out of a Cannon and win real cash! Checkout my newly updated site for all my projects (Kitten Cannon, BOXHEAD, Light Cycle 3D) |
|
|
|
|
|
#4 | |
![]() |
Quote:
__________________
...Just Returned To The Scene... |
|
|
|
|
|
|
#5 |
![]() |
You might be able to reduce it by a few lines but it won't be more efficient. Those conditionals will take just as long to run if you were using arrays in some way. I can't see any clear way to cut them down either.
|
|
|
|
|
|
#6 |
![]() |
Use a for loop in places, and then make it easier to read with a switch statement..
Switch statement should be a more elegant solution... Also, dunno if this is true with actionscript, but: if you have an IF statement, with one like of code for the result, you can leave the brackets out.. So just have... if (blah == true) //Result goes here. |
|
|
|
|
|
#7 | |
![]() |
Quote:
__________________
...Just Returned To The Scene... |
|
|
|
|
|
|
#8 |
![]() |
Code:
// number of picks
var picks = 6;
// highest number to pick
var max = 49;
// lotto numbers
var out:Array = new Array();
//
// numbers that you can choose from
var num:Array = new Array();
//
var x, y, success:Number;
//
//initialize the array that holds the numbers you can choose
for (x=0; x<max; x++) {
num[x] = x+1;
}
//
// make the input fields and init to 0
for (x=0; x<picks; x++) {
_root.createTextField("input"+x, x+10, 20, x*20+20, 100, 20);
// dirty evals here because my actionscript is rusty
eval("input"+x).border = 1;
eval("input"+x).backgroundColor = 0x000000;
eval("input"+x).text = "0";
eval("input"+x).restrict = "0-9";
eval("input"+x).type = "input";
}
//
//choose a non-repeating random number from 1-49 five times for the lotto
for (x=0; x<picks; x++) {
// choose a number from the num array
out[x] = num[Math.floor(Math.random()*num.length)];
//delete the array element that was choosen so we don't choose it again
num.splice(x, 1);
}
trace("Winning numbers are: "+out.join(", "));
//
// make a button
_root.createEmptyMovieClip("theButton_mc", 1000);
theButton_mc._x = theButton_mc._y=0;
with (theButton_mc) {
beginFill(0x555555, 100);
moveTo(140, 20);
lineTo(200, 20);
lineTo(200, 40);
lineTo(140, 40);
endFill();
}
// attach actionscript to check winning numbers to the button
theButton_mc.onRelease = function() {
// make an array to recorde the matching numbers
var match:Array = new Array();
var err:Boolean;
// check for dupes
dupes = false;
// probably a better way to do this but...
for (x=0; x<picks; x++) {
for (y=0; y<picks; y++) {
if (int(eval("input"+x).text)<1 or int(eval("input"+x).text)>max or (x != y and (int(eval("input"+x).text) == int(eval("input"+y).text)))) {
dupes = dupes or true;
}
}
}
if (dupes) {
trace("duplicate numbers entered or numbers are too high. try again.");
} else {
// check for matches
success = 0;
for (x=0; x<picks; x++) {
for (y=0; y<out.length; y++) {
if (int(eval("input"+x).text) == out[y]) {
match[success] = out[y];
success++;
}
}
}
trace(success+" numbers matched.");
if (success>0) {
trace("matching numbers are: "+match.join(", "));
}
}
};
Last edited by matriculated; 01-18-2007 at 01:04 PM.. |
|
|
|
|
|
#9 | |
![]() |
Quote:
__________________
...Just Returned To The Scene... |
|
|
|
|
|
|
#11 | |
![]() |
Quote:
PS: The guy in the link IS NOT me
__________________
...Just Returned To The Scene... |
|
|
|
|
![]() |
| Tags |
| efficient , simpler , writing |
| Thread Tools | |
|
|