Multiplayer Game Demo Experiment Run Experiment

Multiplayer Game

Jaap Murre

This is a very simple chat ‘game’. Two ‘players’ are randomly matched and can then input text and send it to the other ‘player’ in a turn-taking manner. It is an example of how one could develop a full-fledged (economic) game with two or more players who interact over Internet.


On starting a ‘game’, the subject either has to wait for an ‘opponent’ or if one is already waiting, in which case the subject can start immediately with sending and receiving messages.

This script demonstrates how through use of the store_now() and receive() functions, it is possible to communicate between different sessions. That is, two subjects doing the same experiment can communicate by repeated checking a ‘secret variable’ (we use the timestamp to generate one), writing to it and reading it. Several such variables can be defined for a script. 

The script also shows one way of assigning subjects in order of arrival.

The chat function is very basic and could be replaced by a more involved (economic) game, which could make use of the same communication functions developed in the Game class.

If you are a registered user and signed in, you can here copy this script and its stimuli to your own account, where you can edit it and change it in any way you want.

It is absolutely free to register (no credit card info asked!). You can then instantly copy this experiment with one click and edit it, change its accompanying texts, its landing page, stimuli, etc. Invite your colleagues, friends, or students to check out your experiment. There is no limit on how many people can do your experiment, even with a free account.

The catch? There is no catch! Just keep in mind that with a free account, you cannot collect data. For teaching that is usually not a problem. For research, prepaid data collection (unlimited subjects) starts as low as €10.00 for a 10-day period.

function Game() {
    this.receive_poll_interval = 1000; // ms
    this.opponent_poll_interval = 1000; // ms
}

Game.prototype.wait_for_opponent = function() {
    var opponent_waiting = retrieve('opponent_waiting','script');
    if (!opponent_waiting) {
        this.first = 1; // I was here first
        store_now(true,'opponent_waiting','script');
        this.game_id = 1*(new Date()); // 1* converts to number
        store_now(this.game_id,'game_id','script'); 
        while (true) {
            if(!retrieve('opponent_waiting','script')) {
                this.game_id = retrieve('game_id','script');
                break;
            } else {
                await(this.opponent_poll_interval);
            }
        }
    } else {
        this.game_id = retrieve('game_id','script');
        store_now(false,'opponent_waiting','script'); // Affirm by clearing
        this.first = false; // I was second; someone was already waiting
    }
}

// Sends content over (optional) named channel and waits until it has been received 
Game.prototype.send = function(content,channel) {
    content = {
        data: content
    }
    content = JSON.stringify(content); // Ensure correct JSON-ification on server
    channel = channel || "channel";
    var id = channel + '_' + this.game_id,
        signal;
    store_now(content,id,'script');
    while (true) {
        signal = retrieve(id,'script');
        if(signal) { // Wait until the signal has cleared, i.e., has been received
            await(this.receive_poll_interval);
        } else {
            break;
        }
    }
}

// Receive content over (optionally) named channel. Waits until reception.
Game.prototype.receive = function(channel) {
    channel = channel || "channel";
    var id = channel + '_' + this.game_id,
        signal;
    while (true) {
        signal = retrieve(id,'script');
        if(signal) {
            store_now(false,id,'script'); // clear channel; signals reception
            return signal.data;
        } else {
            await(this.receive_poll_interval); // wait and try again
        }
    }
}

text("Waiting for opponent");
var game = new Game();
game.wait_for_opponent();

var first_turn = true;
while (true) { // Simple turn-taking chat
    if (game.first && first_turn) { // first_turn helps determine who sends first
        first_turn = false;
        input("Send message","message");
        game.send(response.message);
        text("Message received");
    } else {
        text("Waiting for message")
        var msg = game.receive();
        text(msg);
        await(5000);
        input("Send message","message");
        game.send(response.message);
        text("Message received");
    }
}
This experiment has no files of this type
This experiment has no files of this type
This experiment has no files of this type
This experiment has no YouTube links
This experiment has no files of this type