Page 1 of 2

How to do Comparisons Between Variables?

Posted: Fri Jul 11, 2008 1:35 pm
by Nafai
Hi guys,

Still working on the Elect script, and trying to integrate a bit of code as I go along. One of the things I want the game to be able to keep track of is which of the girls likes you best, which of the girls like you second best and so on. I'll be using the point system, and while I can use that to track the girl's individual affection ratings, I don't know of any way to compare and rank their affection ratings. I'd like to do that for certain jealousy-events (Catfights anyone?) so if anyone can clue me in, that'd be much appreciated.

Thanks in advance guys!

Re: How to do Comparisons Between Variables?

Posted: Fri Jul 11, 2008 4:36 pm
by Deji
To sort a number of variables you need to make an iteration inside another iteration.

My basic idea is to make an Array with all the variables.
I made a two dimensional Array, where each element of the main array has two components: the girl's name and a numeric value.

[a,3][b,5][c,6][d,2]

I compare the first element's numeric value with each of the other elements' in order. If I find a higher numeric value, I swap it with the current first element.

at the end of this cycle, my array should look like this:

[c,6][a,3][b,5][d,2]

Now that I know the highest value, I need to know the second, so I leave the first out and compare all the elements with the second value and swap them if I find a higher value, the same way I did with the first one.

Then I repeat with the third value and so on....

In the end, it looks like this:

[c,6][b,5][a,3][d,2]

Then you just need to access the first component of the first element to know the name of the girl with higher affection.

I have no idea how to code this in Ren'Py, thou =X
I tested it in Flash with ActionScript 2.0 and it works... I would post that code, but it'd be confusing ^^;

Hope this helps >_o;

Re: How to do Comparisons Between Variables?

Posted: Fri Jul 11, 2008 5:56 pm
by chronoluminaire
While what Deji says is true, it may be overkill for your needs, depending on how many girls you've got.

ElvenRelations had several lines that went something like:

Code: Select all

if AsilanaOpinion >= TohkoOpinion and AsilanaOpinion >= YurikaOpinion:
  # Asilana section
elif YurikaOpinion >= AsilanaOpinion and YurikaOpinion >= TohkoOpinion
  # Yurika section
else
  # Tohko section

Re: How to do Comparisons Between Variables?

Posted: Fri Jul 11, 2008 7:13 pm
by Sin
You could also try "bubble sort". It's a sorting algorithm that's really easy to program.

1. Compare the first and second value in the list. If the latter is larger, swap them with eachother.
2. Compare the second and third value. If the latter is larger, swap them.
3. Continue doing this for all items in the list.
4. Repeat step 1-3 until you don't have to do any swapping.

It's not the fastest algorithm but it's better than the brute force approach, although I suspect it's still a bit overkill for what you're trying to do. :p (Maybe someone else could find it useful).

It's called bubble sort because the larger values will "bubble" up to the top of the list over each iteration.
Using Deji's example: [a,3][b,5][c,6][d,2]
Iteration 1: [b,5][c,6][a,3][d,2]
Iteration 2: [c,6][b,5][a,3][d,2]

Re: How to do Comparisons Between Variables?

Posted: Fri Jul 11, 2008 9:52 pm
by PyTom
Hi. I'm from the CS police, and I'm here to revoke your CS license for suggesting the use of bubble sort. Heck, insertion sort is better than bubble sort, we'd just fine you for that. :-)

Probably the best thing to use would be python's built-in sort, which uses an algorithm called timsort. (It's a clever variant of mergesort that tries to keep sorted sub-lists together, allowing it to work in O(n) when the list is already sorted.)

I'd probably build a list of (score, label) tuples, sort it using the .sort() method, and then take the second component of the first tuple.

Code: Select all

python:
    scores = [ 
        (eileen_score, "eileen_ending"),
        (lucy_score, "lucy_ending"),
        (mary_score, "mary_ending"),
        ]

     scores.sort()

jump expression scores[0][1]
Note that this will use the label, in alphabetical order, as the tiebreaker. (So, Eileen would beat Lucy, leading to another Mugenjohncel game.)

Re: How to do Comparisons Between Variables?

Posted: Fri Jul 11, 2008 10:38 pm
by Deji
PyTom wrote:Hi. I'm from the CS police, and I'm here to revoke your CS license for suggesting the use of bubble sort. Heck, insertion sort is better than bubble sort, we'd just fine you for that. :-)

Probably the best thing to use would be python's built-in sort, which uses an algorithm called timsort. (It's a clever variant of mergesort that tries to keep sorted sub-lists together, allowing it to work in O(n) when the list is already sorted.)

I'd probably build a list of (score, label) tuples, sort it using the .sort() method, and then take the second component of the first tuple.

Code: Select all

python:
    scores = [ 
        (eileen_score, "eileen_ending"),
        (lucy_score, "lucy_ending"),
        (mary_score, "mary_ending"),
        ]

     scores.sort()

jump expression scores[0][1]
Note that this will use the label, in alphabetical order, as the tiebreaker. (So, Eileen would beat Lucy, leading to another Mugenjohncel game.)

I figured there should be a method of sorting in Python, just like in AS!

Thanks for the enlightment!~
*bookmarks topic*

Re: How to do Comparisons Between Variables?

Posted: Fri Jul 11, 2008 11:11 pm
by Nafai
*watches programming language go flying over his head*

Hehe, thanks so much for the help Deji, chrono, Pytom!

Being totally non-versed in the language of programming though, I have a few questions:

* I could use the elif expression more than once right?

* What does "jump expression scores[0][1]" do exactly? How would I be able to integrate this into an "if Girl A is #1 and Girl B is #2 then...; if Girl B is #1 and Girl C is #2 then..." operation?

Re: How to do Comparisons Between Variables?

Posted: Sat Jul 12, 2008 5:40 am
by Sin
Crap! How will I finish Novelty now without my CS license? D:

Re: How to do Comparisons Between Variables?

Posted: Sat Jul 12, 2008 12:25 pm
by Deji
Nafai wrote:
* I could use the elif expression more than once right?

* What does "jump expression scores[0][1]" do exactly? How would I be able to integrate this into an "if Girl A is #1 and Girl B is #2 then...; if Girl B is #1 and Girl C is #2 then..." operation?
1. Yes, you can use elif as much as you want, like in the last example here:
http://www.renpy.org/wiki/renpy/doc/coo ... ed_Endings

2. It basically translates to:
"jump to the second component of the first element in the array", being the second element a label name.
in PyTom's example, since Eileen is the first element (eileen_score, "eileen_ending"), it jumps to the label "eileen ending" (second element of the tuple)

To transform this into what you want for comparision, I think you can do this (taking PyTom's example):

Code: Select all

python:
    scores = [
        (eileen_score, "eileen_ending"),
        (lucy_score, "lucy_ending"),
        (mary_score, "mary_ending"),
        ]

     scores.sort()

if expression scores[0][0] == eileen_score:
    if expression scores[1][0] == lucy_score:
        jump eileen_vs_lucy
    elif expression scores[1][0] == mary_score:
        jump eileen_vs_mary
elif expression scores[0][0] == lucy_score:
    if expression scores[1][0] == elieen_score:
        jump lucy_vs_elieen
    elif expression scores[1][0] == mary_score:
        jump lucy_vs_mary
elif expression scores[0][0] == mary_score:
   if expression scores[1][0] == elieen_score:
        jump mary_vs_elieen
   elif expression scores[1][0] == lucy_score:
        jump mary_vs_lucy
(I'm not sure if you have to keep the "expression" part, thou... I'm just puttting it there to be safe!)

It tells the program to check if the first component of the first tuple is x and the frist component of the second tuple is y, and then jum to the right label accordingly.

Re: How to do Comparisons Between Variables?

Posted: Sat Jul 12, 2008 6:10 pm
by mozillauser
Deji wrote:(I'm not sure if you have to keep the "expression" part, thou... I'm just puttting it there to be safe!)
Well, I'm not very experienced in phyton, but in my opinion the expression is not required for this case. Look at PyToms example:
PyTom wrote:jump expression scores[0][1]
I guess the syntax of the jump statement in phyton is: jump <label>. Therefore the script should contain a statement like: jump eileen_ending (or $ jump eileen_ending if it is located outside a phyton block).
But PyTom wants to jump to a label, that has the same name like the result of: scores[0][1]. Therefore the phyton processor first has to evaluate the result of scores[0][1] and then take this result as an argument for the jump statement.
But how to define that? The solution is the expression. The expression makes the phyton processor to evaluate the expression and embed the result in the jump statement. If the syntax is satisfied, the jump statement is executed as if the programmer coded the statement jump eileen_ending.

Deji, your code snippet needs no expression, because the if-else syntax already expects an expression.
Hmmm, sounds funny but that's just the way it is.

(Maybe there is someone out there, who wants to correct this posting, because he (or she) is either a phyton expert or speaks much better english than me :wink: )

Re: How to do Comparisons Between Variables?

Posted: Sun Jul 13, 2008 1:43 pm
by Gau_Veldt
Python's got nice recursion how about quicksort?

Re: How to do Comparisons Between Variables?

Posted: Sun Jul 13, 2008 2:07 pm
by PyTom
Generally, mergesorts (of which timsort is one) perform better than quicksorts assuming that you have at least n/2 scratch space. Quicksort is only expected O(n lg n) and can get as bad as O(n^2) if you pick the pivot poorly, while Mergesort is guaranteed O(n lg n).

Timsort is an improved mergesort that takes advantage of lists that are already partially sorted, so in the case where the list is already sorted (or you merge 2 sorted lists), it winds up as O(n).

Not to mention, python's built-in sort is written in C, which is much faster than python proper, so you'd win from that as well.

This is 2008. Outside of a CS class, there's no reason to implement your own sorting algorithms anymore.

Re: How to do Comparisons Between Variables?

Posted: Sun Jul 13, 2008 4:53 pm
by Watercolorheart
Wha ... what is this notation?

[letter, number][letter, number] and what does it mean? Should I look up Arrays in the cookbook?

I have never seen this before.

Re: How to do Comparisons Between Variables?

Posted: Sun Jul 13, 2008 4:54 pm
by PyTom
Which notation are you talking about, precisely? Big-O notation is a CS concept, while arrays are discussed in the python tutorial.

Re: How to do Comparisons Between Variables?

Posted: Fri Jul 18, 2008 2:48 am
by cloverfirefly
(Hi, first post.)

I tried PyTom's sample code to make a simple "game" and the following is my code.

Code: Select all

label start:
    
    $ mary_score = 2
    $ eileen_score = 1
    $ lucy_score = 3
    
    "Eileen's score is %(eileen_score)s."
    "Lucy's score is %(lucy_score)s."
    "Mary's score is %(mary_score)s."
    
    python:
        scores = [
            (eileen_score, "eileen_ending"),
            (lucy_score, "lucy_ending"),
            (mary_score, "mary_ending"),
            ]

        scores.sort()

    jump expression scores[0][1]
    
label eileen_ending:
    "Eileen likes you the most!"
    return
    
label lucy_ending:
    "Lucy likes you the most!"
    return
    
label mary_ending:
    "Mary likes you the most!"
    return
Unfortunately, when I run my game, Eileen (with a score of 1) likes me the most! I would imagine the reason for this is that sort is going by ascending order. Is there a way to make sort go by descending order so that the girl with the highest score (in this case Lucy and her score of 3) would be the one that "wins"?