QJ.NET | Videos | Forums | iPhone | MMORPG | Nintendo DS | Wii | PlayStation 3 | PSP | Xbox 360 | PC | Downloads | Contact Us
Forums | Gaming News | Videos | Downloads | Today's Posts | Mark Forums Read | Chat | FAQ | Members List | Contact

QJ.net Game Discussion - PSP, Xbox, Wii, PS3, PSP Homebrew, and PSP Guides

Go Back   QJ.net Game Discussion - PSP, Xbox, Wii, PS3, PSP Homebrew, and PSP Guides > Developers Corner > PSP Development, Hacks, and Homebrew > PSP Development Forum
The above video goes away if you are a member and logged in, so log in now!

What is a simpler, more efficient way of writing this...

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 ...

Reply
 
LinkBack Thread Tools
Old 01-17-2007, 12:21 PM   #1
 
 
My Mood: Bored
Join Date: Apr 2006
Location: England ~¦¦¦|+|¦¦¦~
Posts: 1,112
Trader Feedback: 0
Default What is a simpler, more efficient way of writing this...

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";
		}
			
	}
}
There must be a way to make it more efficient, maybe using arrays?

(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
JaSo PsP is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 01-17-2007, 03:11 PM   #2

Developer
 
yaustar's Avatar
 
Join Date: Jun 2006
Location: UK
Posts: 2,317
Trader Feedback: 0
Default

Do you have arrays in actionscript?
yaustar is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 01-17-2007, 05:40 PM   #3

...in a dream...
 
SG57's Avatar
 
Join Date: Jul 2005
Posts: 4,957
Trader Feedback: 0
Default

Googles first link:

http://www.adobe.com/support/flash/a...ionary059.html
__________________
SG57 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 01-17-2007, 11:35 PM   #4
 
 
My Mood: Bored
Join Date: Apr 2006
Location: England ~¦¦¦|+|¦¦¦~
Posts: 1,112
Trader Feedback: 0
Default

Quote:
Originally Posted by SG57
Hmm, i still dont know how to write about all the elements in an array at once.
__________________
...Just Returned To The Scene...
JaSo PsP is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 01-18-2007, 01:49 AM   #5
 
Join Date: Dec 2006
Location: Wakefield, England
Posts: 60
Trader Feedback: 0
Default

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.
Bytrix is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 01-18-2007, 03:40 AM   #6
MiG
 
MiG's Avatar
 
Join Date: Jun 2006
Location: UK
Posts: 499
Trader Feedback: 0
Default

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.
MiG is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 01-18-2007, 08:40 AM   #7
 
 
My Mood: Bored
Join Date: Apr 2006
Location: England ~¦¦¦|+|¦¦¦~
Posts: 1,112
Trader Feedback: 0
Default

Quote:
Originally Posted by MiG
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.
That last part isnt phesable
__________________
...Just Returned To The Scene...
JaSo PsP is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 01-18-2007, 09:27 AM   #8
 
Join Date: Jan 2006
Posts: 508
Trader Feedback: 0
Default

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(", "));
		}
	}
};
just cut and paste into an empty flash document

Last edited by matriculated; 01-18-2007 at 01:04 PM..
matriculated is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 01-18-2007, 01:20 PM   #9
 
 
My Mood: Bored
Join Date: Apr 2006
Location: England ~¦¦¦|+|¦¦¦~
Posts: 1,112
Trader Feedback: 0
Default

Quote:
Originally Posted by matriculated
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(", "));
		}
	}
};
just cut and paste into an empty flash document
That sort of defeats the point of it being MY game?
__________________
...Just Returned To The Scene...
JaSo PsP is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 01-18-2007, 01:31 PM   #10
 
Join Date: Jan 2006
Posts: 508
Trader Feedback: 0
Default

Quote:
Originally Posted by JaSo PsP
That sort of defeats the point of it being MY game?
I was really bored at work.
matriculated is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 01-19-2007, 03:11 PM   #11
 
 
My Mood: Bored
Join Date: Apr 2006
Location: England ~¦¦¦|+|¦¦¦~
Posts: 1,112
Trader Feedback: 0
Default

Quote:
Originally Posted by matriculated
I was really bored at work.
INTRIGUING

PS: The guy in the link IS NOT me
__________________
...Just Returned To The Scene...
JaSo PsP is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Tags
efficient , simpler , writing

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



All times are GMT -8. The time now is 09:20 AM.



Use of this Web site constitutes acceptance of the TERMS & CONDITIONS and PRIVACY POLICY
Copyright © 2009, QJ.NET. All Rights Reserved.
Contact Us