oTree Forum >

Illustrative Game of Nim

#1 by MinFang

Hi there,

I am coding an illustrative version of the game of nim. That is, the current points is shown by corresponding number of blue circles on the page. I trying to realize this via JavaScript.

I have defined a variable current_number representing the number of current points in __init__.py under class Group. I also have used the live_method function to update the value of it in the real-time.

Now I have a problem. In the html file, I cannot directly access the value of current_number in a loop under function display in JavaScript (please check the attachment Nim.html for details. The code in line 42 is not working actually.). I am intended to plot the current number of points everytime the page is refreshed or loaded through function display. How can I pass the value of variable current_number to the JavaScript loop? 

Thank you for any help!

#2 by MinFang

Actually I am just confused about how to access the value of data.current_number in the following loop:

 function display(data){
            let parent = document.getElementById('coinContainer');
            for (let i = 0; i<data.current_number; i++){
                let child = document.createElement('div');
                child.className='coin';
                parent.append(child);
            }
        }
       
i<data.current_number is not working. If I replace data.current_number with an integer i.e., 21, this function can plot 21 circles on the page. But this number is constant and not updated with cuurent number of points left. How to fix this problem?

Thanks again!

#3 by BonnEconLab

Your function “display” takes “data” as the argument. Later, however, you call it with an empty argument:

        window.onload = () => {
                display();
        }

Moreover, I guess that “display(data)” needs to be called somewhere in liveRecv, say, at the end:

        function liveRecv(data) {
            ...
            display(data);
        }

Write a reply

Set forum username