问题描述:

I'm trying to make a game in which two players engage in a fight and press a button on the screen to deal damage to each other. The damage that a character can deal should be a random number between 1 and 10. The damage that the attacked character receives should be the same random number.

The game is to be playable on both Android and iOS (Java and Swift) but how do I ensure that the same random number is generated for damage dealt and damage received? I realize that I could send the damage dealt to the attacked player and have that amount deducted from their health, but I was wondering if there was a way to generate the same sequence of random numbers in Java and Swift so that I wouldn't have to go through a server.

网友答案:

You can probably have each peer be both a client AND a server. This is called peer-to-peer networking. You would have to make sure that the clients are not hacked, however. That could be hard to implement. It is a tough situation and a small server would be the best way.

(P.S. the best language for this would most likely be Haxe. I have used this language before)

...Actually, I thought about it, and you could have a random seed that is shared between the peers. You could count both side's damage on both peers to make sure it is valid.

Edit: Haxe's Math.random() appears to be the same across platforms with a given seed, according to the docs. It can compile to iOS and Android code, among other platforms.

网友答案:

You should write your own class for this. Using the built in random number generators across the two languages will not guarantee this. However, you can write custom logic that does guarantee this.

For example, you might get a list of 100,000 random numbers between 0 and 1. You could save that list as a text file and reference it from both projects.

The above is the closest you could get to solving your problem without making both clients communicate with one another. Since you asked for a solution that did not use a server, that is my answer. However, using a server or some other form of network communication really seems like the right answer here.

相关阅读:
Top