Advice on creating a random card dealer

Hi,

I am looking for some advice on how best to go about creating a random card dealer. An example of what I’m trying to achieve would be the card game “cards against humanity” for anyone who is familiar with that. In that game at the start you are randomly dealt say 5 cards from a deck of 200 (don’t know how many exactly). On your go you use one of your 5 cards. This card is then discarded and you are given a fresh card from the deck.

What I want to do is have a scene which has 5 buttons. There would be say 20 possible options for what these buttons could do. When the game loads the scene I want these buttons to each be given 1 out of the 20 possible options randomly. Each button would then have a label saying what it does. These options for when the button is pressed would either be display some text on screen or load a new scene. If the button just displays some text that button action would then be removed from the button and a new action would be dealt to that button from the possible 20 (minus the ones that are already in use so that there aren’t duplicates).

I am looking for advice on how best to go about doing this in cocos creator.
My thinking was it would be something along the lines of assigning each of the 20 actions a number and creating a function for each. When the game is loaded a number from 1-20 will be randomly picked for each of the 5 buttons in the scene which will also produced the label on each button. When a button is pressed a function will be used to see which number 1-20 is assigned to that button. This will then run the function associated with that number. If necessary the button is then randomly assigned a new number.

I don’t know if this is the best way to go about this or how best to go about coding it but hopefully I’ve managed to explain clearly what I’m trying to do. If anyone can offer any suggestions or advice on how to code something like this that would be great! :smiley:

Thanks

In cocos creator, you can use javacript.
You can add button to scene programmatically.
To create button, you must set handler and you can set funtion name randomly.

let button1 = newButtonNode1.addComponent(cc.Button);
let eventHandler = new cc.Component.EventHandler();
eventHandler.target = targetNode;
eventHandler.component = "MainScript";
eventHandler.handler = "onClick" + randomApiNumber();
button1.clickEvents.push(eventHandler);

And you can implement function like this.
ex: "function onClick1() {}", "function onClick2()".. "function onClick20()"
In each function, you can implement you main logic.

Hi Harry,

Thanks for replying.

I think I understand what your code is doing. You are adding a button to the scene (button1). When that button is pressed it will randomly select one of the 20 (or however many) functions you have created which will do whatever you make the function do. This is interesting to know however I see a problem with how I would be able to label the button. The button needs to have a label on it which says what it does when you press it and so is connected to whatever the function is that it calls. Sorry I know this makes it quite awkward.

Would it be possible to make all the required buttons in CC at the side of the scene (out of the canvas window so they aren’t visible) and then make code which when the scene is loaded randomly picks say 5 of the buttons and moves them into the canvas scene so that they are visible. Then when they are pressed it does the function associated with them, moves them back to out of the scene and then randomly picks another button from the ones out of scene to move into the same position in the scene.

I’m not sure if that is something which makes sense, its just something I thought of as it would allow for the buttons to be easily organised. Would having them out of the scene mean they are still loaded and so would that be wasteful on memory. Alternatively could this process be done programmatically.

Thanks again :sweat_smile: