Programming problem - just psuedo code

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
Post Reply
User avatar
I.M. Weasel
Iron Muskateer Weasel
Posts: 2780
https://www.artistsworkshop.eu/meble-kuchenne-na-wymiar-warszawa-gdzie-zamowic/
Joined: Fri Jan 04, 2002 4:45 am
Location: The city of the future, Los Braingeles
Has thanked: 1 time
Been thanked: 3 times
Contact:

Programming problem - just psuedo code

Post by I.M. Weasel »

I am actually having trouble with arrays right now in a home project. Its hard to find exactly what i'm looking for online.. its in gml, which is very similar to C/C++. I just can't wrap my head around the best way to work this out. Sorry if this is not explained well, sometimes its hard to get out exactly what you mean.

The issue is - when a fight starts, i want to take the array of enemies and combine any names that match and show the number of them...

Code: Select all

Bill
Ted 3
Rufus
Napoleon 2
It would be easy if I new exactly what the names would be, but since they could be whatever enemy I have to do the matching myself. The enemy_slot array has a max size of 6, but with 2 caveats: the size may be less than 6, and if any of the spot have the string "null" they get skipped.

Below is the code idea that ive been throwing around trying to figure this out, but I just dont know what to do

Code: Select all

string names[6];

loop into names array
skip any names == "null";
if string at names[LOOP_NUMBER] matches the same string at another spot in the array
     print names[LOOP_NUMBER] HOW_MANY_TIMES_THAT_NAME_APPEARS_IN_ARRAY
else if (no matches)
   print names[LOOP_NUMBER]
:arrow: http://tofuheavyindustries.com
Mac Dream Tool / Mac Dream Tool Services (released Sept. 2019)
Creator of Various awesome Video Games

"You don't have to be forgiven. Clint Eastwood taught us that."
User avatar
bogglez
Moderator
Moderator
Posts: 578
Joined: Sun Apr 20, 2014 9:45 am
Has thanked: 0
Been thanked: 0

Re: Programming problem - just psuedo code

Post by bogglez »

I'm not entirely sure I understood your problem..

You have an array

Code: Select all

values = ["banana", "apple", "banana", "orange"]
and want the output

Code: Select all

Apple
Banana 2
Orange
I assume?


There are many ways to do this. I'll use Python without Pythonisms, because I don't know your programming language.

Code: Select all

foo = ["banana", "apple", "banana", "orange"]

def summary_print(value, count):
	if count == 1:
		print(value)
	else:
		print(value + ": " + str(count))

def summary(values):
	n = len(values)

	if n == 0:
		return

	# by sorting we can just count repetitions
	# e.g. apple orange apple -> apple apple orange
	# just keep a counter while counting each item and reset on new item        
	sorted_values = sorted(values)
	count = 1 # there is at least one element, so its count must be at least 1

	for i in range(n):
		# if this is not the last element and it has the same value as the next item
		if i < n-1 and sorted_values[i] == sorted_values[i+1]:
			count += 1
		# this is the last element or it has a different value than the next item
		else:
			summary_print(sorted_values[i], count)
			count = 1

summary(foo)
Here I create a copy of the array when sorting.. if you don't care about the order, you can just sort it in-place. There are also algorithms in which you avoid memory allocation entirely by scanning over the array many times to find the position of the current element in the order to establish whether it was already handled, etc.
Wiki & tutorials: http://dcemulation.org/?title=Development
Wiki feedback: viewtopic.php?f=29&t=103940
My libgl playground (not for production): https://bitbucket.org/bogglez/libgl15
My lxdream fork (with small fixes): https://bitbucket.org/bogglez/lxdream
User avatar
I.M. Weasel
Iron Muskateer Weasel
Posts: 2780
Joined: Fri Jan 04, 2002 4:45 am
Location: The city of the future, Los Braingeles
Has thanked: 1 time
Been thanked: 3 times
Contact:

Re: Programming problem - just psuedo code

Post by I.M. Weasel »

correct bogglez. thank you for your psuedocode. I came upon the answer late last night, but if its a little spaghetti-fied, its interesting to see other solutions.

Heres what I ended up coming up with. Thankfully what im using doesnt worry about memory allocation:

Code: Select all

    names[5] = 0;
    
    for (jjj = 0;jjj < 6;jjj+=1){
        if (instance_exists(enemy_slot[jjj])) {
            for (hhh = 0; hhh < 6; hhh+=1){
                if (instance_exists(enemy_slot[hhh])) {
                    if (enemy_slot[jjj].en_name == enemy_slot[hhh].en_name){                   
                        if (jjj > hhh ) { names[jjj] = 300; break;}
                        if (jjj <= hhh) names[jjj] +=1;
                    }
                }
            }
        }
    }
                
    for (jjj = 0;jjj < 6;jjj+=1){
        if (names[jjj] != 300){
            if (instance_exists(enemy_slot[jjj])) {
                draw_text(20,(230+YYY),enemy_slot[jjj].en_name + " " + string(names[jjj])); //330
                YYY+=16;
            }
            //YYY+=16;
        }    
    }

:arrow: http://tofuheavyindustries.com
Mac Dream Tool / Mac Dream Tool Services (released Sept. 2019)
Creator of Various awesome Video Games

"You don't have to be forgiven. Clint Eastwood taught us that."
User avatar
bogglez
Moderator
Moderator
Posts: 578
Joined: Sun Apr 20, 2014 9:45 am
Has thanked: 0
Been thanked: 0

Re: Programming problem - just psuedo code

Post by bogglez »

If you would like some code review :
Don't name your variables jjj etc. If you have abstract loops (sorting something, matrix calculation), using i,j,k should be enough. If you need any more or do something concrete you should start using descriptive names.

Your algorithm is O(n^2), mine is O(n*log(n)). That means for 100 items your algorithm would execute the inner loop 10000 times, mine 200 times (in the sort function). So be aware that your algorithm will become very slow very quickly as you add more items.
Also because you hard-coded 300 that's your limit anyway.

Your algorithm performs a lot of checks whether an enemy instance exists to begin with. Instead of looping over all possible enemy types which might not even be in use, it would be better to loop over existing enemy instances only. You may have to rearrange some data structures but it's very beneficial.

Your inner loop doesn't have to start from 0, you throw those results away in the inner loop's if branch anyway. It should start from jjj+1 to avoid self-comparisons and comparisons that have already been made.
There's also no need for the 300. Just set the value to 0 and skip elements with count of 0 in your loop.

If you're not very interested in optimizing this I suggest you go with my algorithm because your language's standard library is bound to include an optimized sort function that will do the heavy lifting for you.
Wiki & tutorials: http://dcemulation.org/?title=Development
Wiki feedback: viewtopic.php?f=29&t=103940
My libgl playground (not for production): https://bitbucket.org/bogglez/libgl15
My lxdream fork (with small fixes): https://bitbucket.org/bogglez/lxdream
User avatar
mankrip
DCEmu Ex-Mod
DCEmu Ex-Mod
Posts: 3712
Joined: Sun Nov 04, 2001 5:12 pm
Has thanked: 0
Been thanked: 0
Contact:

Re: Programming problem - just psuedo code

Post by mankrip »

"Psuedo" sounds like a Hispanic lady name.
Ph'nglui mglw'nafh mankrip Hell's end wgah'nagl fhtagn.
==-=-=-=-=-=-=-=-=-=-==
Dev blog / Twitter / YouTube
Image
Post Reply