How do I get the computer to choose the highest number?

For problems with creating games or for reporting bugs of the Tool.
Rohan
PaC-DK Newby
Posts: 37
Joined: 23 Apr 2008, 18:45
Location: Mifflin Pa
Contact:

How do I get the computer to choose the highest number?

Post: #75775 Rohan
04 Jun 2010, 15:08

I want the computer to choose the highest number of a set of variables:

Tweek 34
Dogma 25
Snake 11
Fox 16


Obviously the highest number here is Tweek at 35, but how do I get the computer to choose that variable. I have some Idea, but it would take hundreds of lines of code. Im sure there's a simpler way...
When a man fears for his reputation, he will temporarily humble himself.
-Rohan-

User avatar
Baelavay
PaC-DK God
Posts: 1168
Joined: 04 Jun 2006, 19:24
Contact:

Post: #75777 Baelavay
04 Jun 2010, 23:33

This is theoretically speaking but I suppose you want the name of the highest variable saved in a string at the end.

PaCDK allows the usage of < and > which makes the whole issue rather easy. But you can only compare 2 variables at once, so you need a few lines.

Code: Select all

if_num(Tweek;>[Dogma])
  setstring(highest-one-of-tweek-and-dogma;Tweek)
if_num(Tweek;<[Dogma])
  setstring(highest-one-of-tweek-and-dogma;Dogma)

if_num(Snake;>[Fox])
  setstring(highest-one-of-snake-and-fox;Snake)
if_num(Snake;<[Fox])
  setstring(highest-one-of-snake-and-fox;Fox)

if_string(highest-one-of-tweek-and-dogma;>[highest-one-of-snake-and-fox])
  setstring(highest-of-it-all;[highest-one-of-tweek-and-dogma])
if_string(highest-one-of-tweek-and-dogma;<[highest-one-of-snake-and-fox])
  setstring(highest-of-it-all;[highest-one-of-snake-and-fox])


Haven't tested the code though, no guarantee if it's working.

Rohan
PaC-DK Newby
Posts: 37
Joined: 23 Apr 2008, 18:45
Location: Mifflin Pa
Contact:

Thanks

Post: #75798 Rohan
09 Jun 2010, 16:55

Well, thanks for including actual coding as an example, it'll help. That was my original method, but the real game has over 25 different variables, so I was hoping for a single line of code that could calculate the highest set. I’m guessing there isn't so I might just have to dedicate a couple of days to writing all that code. Haha.

User avatar
Benni
Tutorial Reader
Posts: 65
Joined: 09 Jan 2009, 23:38

Post: #75825 Benni
28 Jun 2010, 12:25

A little simpler (and Copy&Paste friendlier) solution would be:

Code: Select all

setnum(MaxNum,0)

if_num(MaxNum; <[Tweek])
{
 setnum(MaxNum; [Tweek])
 setstring(MaxName; Tweek)
}
if_num(MaxNum; <[Dogma])
{
 setnum(MaxNum; [Dogma])
 setstring(MaxName; Dogma)
}
if_num(MaxNum; <[Fox])
{
 setnum(MaxNum; [Fox])
 setstring(MaxName; Fox)
}


In the end, MaxNum contains the value of the largest variable and MaxName contains the name of that variable. It's still a little work, but you can copy the if_num... part with the curly braces once and only have to change the variable name. Unless PaC-DK gets support for arrays, I think this is the easiest way to do this.

User avatar
Baelavay
PaC-DK God
Posts: 1168
Joined: 04 Jun 2006, 19:24
Contact:

Post: #75826 Baelavay
28 Jun 2010, 16:49

You left out "Snake" that makes it look way shorter indeed ;)

I like your solution too. In mine, I compare 3 pairs, twice each time. In yours, you compare all four variables, each time with the previous one. I don't think that one of them is better than the other. You are just using shorter own variable names which is more efficient.

User avatar
Schiman
PaC-DK God
Posts: 1171
Joined: 20 Dec 2006, 21:48
Contact:

Post: #75827 Schiman
28 Jun 2010, 17:21

In PaC-DK something like Arrays does exist!

You can use Variables like this:
setnum(i;0) <--- or whatever index you like
setnum(array[i]; 29) <-- or whatever number you like

you will have a new Variable with the name array0. This is the equivalent to the value at the first index in an array.

User avatar
Benni
Tutorial Reader
Posts: 65
Joined: 09 Jan 2009, 23:38

Post: #75828 Benni
28 Jun 2010, 18:39

Baelavay wrote:You left out "Snake" that makes it look way shorter indeed ;)

I like your solution too. In mine, I compare 3 pairs, twice each time. In yours, you compare all four variables, each time with the previous one. I don't think that one of them is better than the other. You are just using shorter own variable names which is more efficient.


It's not just the variable names. Think about the amount of work for 25 different variables. In my solution, it is mostly copy&paste. Use the same code snippet 25 times and just replace the variable name. In yours, you will have to introduce 12+6+3+1=22 additional variables and comparisons, each with their own name and make sure that you always compare the right ones with each other and don't forget any. That will get very complicated very quickly. And just think about what happens if you need to add or remove one of the variables.

@Schiman
Thanks, I didn't know that (or I forgot). That way you could even sort the whole list instead of just finding the topmost one.

User avatar
Baelavay
PaC-DK God
Posts: 1168
Joined: 04 Jun 2006, 19:24
Contact:

Post: #75829 Baelavay
28 Jun 2010, 22:53

@Benni: Yeah you're right, I got the point.

@Schiman: But I don't get at all what you're talking about there xD Can you explain it a little more in depth for me please?

User avatar
Schiman
PaC-DK God
Posts: 1171
Joined: 20 Dec 2006, 21:48
Contact:

Post: #75830 Schiman
29 Jun 2010, 09:15

I'm sorry, but I'm going to explain the array-thing in German ^^.

Was Arrays sind weißte ja, gehe ich mal von aus.
Im PaC-DK ist es möglich Variablen dynamisch zu erzeugen.
Du kannst einen Variablennamen bestehend aus anderen Variablen beliebig zusammenbasteln.

Beispiel:

Code: Select all

setstring(VarName;Array)
setnum(index;1)
setnum([VarName][index];29)

Du würdest nun eine Variable besitzen, die den Namen "Array1" und den Wert "29" besitzt.
Du kannst damit perfekt mit Arrays und Schleifen arbeiten.

Nehmen wir an, du willst ein Array haben, wo die Werte von 0 bis 100 eingespeichert sind (also Wert == Index)
Du baust also eine function, die folgendemaßen aufgerufen wird:

Code: Select all

setnum(index;0)
function(nullbishundert;1)


Die function "nullbishundert" enthält nun folgenden Code:

Code: Select all

setnum(array[index];[index])
setnum(index;+1)
if_num(index;>100)
   stopfunction(nullbishundert)


Du hast also faktisch eine do-while-Schleife mit Endbedingung.

Nachdem die Function durchgelaufen ist, hast du folgende Variablentabelle:

Code: Select all

Name      Wert
array0     0
array1     1
array2     2
.
.
.
array99   99
array100 100

Du hast also dynamisch 101 Variablen erstellt und damit eine Art 101-elementiges eindimensionales Array erstellt.
Mit mehreren Indizes sind multidimensionale Arrays auch kein Problem. Dabei muss man aber beachten, dass die einzelnen Indizes mit einem Platzhalter getrennt werden.

Ungefähr verstanden, was ich meine?

Dieses Konstrukt ist unabkömmlich, wenn du so Sachen wie den Stuboter oder mein Kampfsystem scripten willst ^^.

Das einzige Problem ist(und das sehe ich auch als großes Manko im PaC-DK), dass die Funktionen nur mit 50 Durchläufen pro Sekunde arbeiten. Das ist zu langsam. Aber das lässt sich nicht ändern. Man muss sich eben so behelfen, dass man den Code der Funktion einfach zweimal hintereinanderkopiert, dann hat man schon 100Hz ^^.

User avatar
Schiman
PaC-DK God
Posts: 1171
Joined: 20 Dec 2006, 21:48
Contact:

Post: #75831 Schiman
30 Jun 2010, 08:28

Oh, kleiner Fehler^^.

Beim Aufruf der Funktion "nullbishundert" muss stehen:

Code: Select all

setnum(index;0)
function(nullbishundert;infinit)

User avatar
Baelavay
PaC-DK God
Posts: 1168
Joined: 04 Jun 2006, 19:24
Contact:

Post: #75832 Baelavay
30 Jun 2010, 18:37

That's why I'm rather graphic artist than coder :P


Return to “Problems and Bugs”

Who is online

Users browsing this forum: No registered users and 2 guests