Yard Bridge - AI Competition (libcanvas)


    I propose to arrange sports programming - writing AI in Javascript for playing in the yard bridge. I provide a ready-made libcanvas game and an elegant interface for creating AI. All you need is a browser, a little excitement and knowledge of JavaScript. The first step is that you program your AI by playing against it. The second stage - you program your AI and it plays against the best from the first stage. After the second stage, we hold a tournament and announce the results. I am sure that we will get a lot of fun and pleasure. To the winners - glory and honor. If win not registered on Habré - I am ready to provide invites.



    Bridge


    I am sure many have played this game. She has many names and more rules. We will use the following:
    1. The goal is to quickly discard all cards, leaving opponents with the maximum number of cards
    2. Points are awarded for each card at the end of the game - 10 for the top ten, a queen, a king, 15 for an ace, 20 for a jack.
    3. The first who jumped the bar 125 eyes - loses. The one who picks up exactly 125 eyes - they burn up and he starts from scratch (lucky)
    4. The player must lay down a card (or several) of the same value, or the same suit (except for exceptions)
    5. The six must be covered - take cards from the deck until we cover
    6. Seven forces the next one to draw card
    7. Eight - two cards and skip the move (two eights - two, three - three)
    8. Jack - can fall on any suit and you can order any suit
    9. Ace - the next player skips the move

    Conditions of the competition


    We play in two rounds. The first round (one week) - we play against our own AI. Second, I’ll select some of the best AIs, obfuscate them, and play against them. Thus, it will be necessary to make a diverse, adapting to different styles of AI for victory. For the second round I will drive a couple more interesting points in the game (but the interface will remain backward compatible).
    AI should be clearly written. I will conduct a visual inspection of the code. All non-contiguous sites will be rejected. If it turned out to be confusing, it is better to comment either in the code or in a separate field.
    The goal of AI is to earn the minimum number of points per game, while loading cards with enemies.
    Your script should not throw expetions and turn into restricted areas (imagine that AI is a person and think what is available to him).
    Nevertheless, I left access to such zones for debug.
    The AI ​​code must be cross-browser (the latest Chrome, Opera, Fox, IE9).
    You can refer to the methods that MooTools Core provides .
    Light unobtrusive rare humor is allowed in the game console.

    An example of the nuances of the game that are worth processing


    There are many nuances in the Bridge game - for example, the number of players should be taken into account, and it can be any from 2 to 6. If you play together or four, it is advantageous to overwhelm the closest opponent. When playing with the three of us, if we fill up the nearest one, then the next move will be on us, therefore it is better to throw sevens of cards at him.
    In certain stories, you can load the opponent and end the game in one turn.
    When playing with a 2x2 teammate (although this is not provided for by the rules, but often happens) - you can practically not give way to enemies.
    Please note, maybe I will add a network game. Then we will play against our own AI;)

    AI code example


    Each turn, the controller calls the method AI.movement();and waits from that method call this.finishMove(). Below you see an example of the AI ​​that is used in the game now:
    If this is the first move, then it tries to finish (if there is a six, then it will not be able to finish), otherwise it tries if there is something (it is chosen randomly) or take a card. Pay attention to the use of asynchronous calls - before you lay down the next card, you must wait for the previous one to fly.
    /*
     * allRanks : ['a','k','q','j','10','9','8','7','6'],
     * allSuits : ['s','c','h','d'],
     *
     * You can read:
     * <int> this.getPlayers().length
     * <int> this.getPlayers()[index].cards.length
     * <int> this.getPlayers()[index].score
     *
     * <int> card.getValue() - the cost of the card
     * <string> card.getName()  - russian name of the card
     * <Bridge.Card> card.setRequireSuit(<string> suit) - set require suit (one of ['s','c','h','d']) if rank of card is "Jacket"
     *
     * <Bridge.Card|null> this.player.hasPuttable() - return on the puttable cards or null, if no such
     * <int> this.player.sumCards() - the cost of all cards
     */

    window.addEvent('domready', function () {

    Bridge.AI = new Class({
        Extends : Bridge.AIUtils,
        // @Override
        initialize : function (player) {
            this.parent(player);
            // this.debug();
        },
        // @private - part of demo logic
        putCardSmart : function (card) {
            if (card.rank == 'j') {
                card.setRequireSuit(
                    ['s','c','h','d'].getRandom()
                );
            }
            this.putCard(card, this.finishSmart.bind(this));
        },
        // @private - part of demo logic
        getCardSmart : function () {
            this.getCard(function (card) {
                this.canPutCard(card) ?
                    this.putCardSmart(card) :
                    this.finishSmart();
            }.bind(this));
        },
        // @private - part of demo logic
        finishSmart : function () {
            this.canFinishMove() ?
                this.finishMove() :
                this.movement();
        },
        // @Override
        movement : function (first) {
            if (first) {
                this.finishSmart();
            } else {
                var card = this.player.hasPuttable();
                card ? this.putCardSmart(card) : this.getCardSmart();
            }
        }

        // @protected <Bridge.Player[]> getPlayers()
        //        returns array of the players
        // @protected <Bridge.AI> message(<string> msg)
        //        puts text msg to the screen (returns this)
        // @protected <Bridge.Card> lastCard()
        //        returns last opened card
        // @protected <Bridge.AI> debug()
        //        open all cards (just for debug)
        // @protected <Boolean> canhGetCard()
        //        returns true, if you can get card from the deck
        // @protected <Bridge.AI> getCard(<function> onFinish)
        //        gets card from the deck or throw exception. First arg of the function is getted card
        // @protected <Boolean> canPutCard(<Bridge.Card> card)
        //        returns true, if you can put card to the deck
        // @protected <Bridge.AI> putCard(<Bridge.Card> card, <function> onFinish)
        //        puts card to the deck or throw exception
        // @protected <Boolean> canFinishMove()
        //        returns true, if you can finish the move
        // @protected <Bridge.AI> finishMove()
        //        finish the move, or throw exception
    });

    });


    How to take part


    To take part, register at libcanvas.com (you don’t need to confirm your email, just login and password), go to the game and see the “ Edit Artificial Intelligence ” link on top . There we see the text entry code or the ability to specify a link to an external URL (for example, to make it convenient to edit on a localhost, do not forget to go back)

    ps. I answer questions and before the start of the second round I accept proposals for improvement.

    pps Thanks Nutochka for the design)


    ppps I'm interested in the speed and stability of work on your hardware and browsers and the comparison of speed with what was in the scarf

    Also popular now: