📜 ⬆️ ⬇️

Yard bridge - AI competition (libcanvas)

I propose to arrange sports programming - writing AI on Javascript for playing in a courtyard bridge. I provide a ready-made game on libcanvas and an elegant interface for creating AI. All you need is a browser, a bit of excitement and knowledge of JavaScript. The first stage - you program your AI, playing against it. The second stage - you program your AI and it plays against the best of 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. Winners - glory and honor. If the winners are not registered on Habré - ready to provide invites.


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 throw off all the cards quickly, leaving the opponents with the maximum number of cards
2. For each card at the end of the game points are awarded - 10 for the top ten, the lady, the king, 15 for the ace, 20 for the jack.
3. The first who skipped the bar 125 eyes - loses. The one who scores exactly 125 eyes - they burn and he starts from scratch (lucky)
4. The player must place a card (or several) of the same value, or of the same suit (except for exceptions)
5. It is necessary to cover the six - we draw cards from the deck until we cover
6. Seven makes the next take a card
7. Eight - two cards and skip a turn (two eights - two, three - three)
8. Jack - can fit on any suit and you can order any suit
9. Ace - the next player misses the turn.

Conditions of the competition

We play in two rounds. First round (one week) - we play against our own AI. Secondly, I will select some of the best AI, obfustsiruyu them and will play against them. Thus, it will be necessary to make a diverse, adapting to different styles of AI for victory. By the second round, I will drive a couple more interesting points in the game (but the interface will remain backward compatible).
The AI ​​should be clearly written. I will conduct a visual inspection of the code. All non-pockets will be rejected. If it is messy, better 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 downloading maps of enemies.
Your script should not throw away expetions and go into restricted areas (imagine that the AI ​​is a human being and think about what is available to it).
However, I left access to such zones for debugging.
The AI ​​code must be cross-browser (recent Chrome, Opera, Fox, IE9)
You can refer to the methods that MooTools Core provides.
Easy unobtrusive rare humor is allowed in the game console.

An example of the nuances of the game, which is worth processing

In the game Bridge there are many nuances - it is worth considering, for example, the number of players, but it can be any number from 2 to 6. If you play together or four together, then it is advantageous to flood your nearest opponent. When playing three together, if we close the nearest one, then the next move will be on us, therefore it is better to throw him cards with sevens.
In certain situations you can load an opponent and finish the game in one turn.
When playing with a partner 2x2 (although this is not provided for by the rules, but often it happens) - you can practically not make a move for enemies.
Remember, maybe I'll add a network game. We will then play against our own AI;)

AI code sample

Each turn, the controller calls the AI.movement(); method AI.movement(); and waits from that to call this.finishMove() method. Below you see an example of AI that is used in the game now:
If this is the first move, then he tries to finish (if there is a six, he will not be able to finish), otherwise he tries if he has something (chosen randomly) or take a card. Pay attention to the use of asynchronous calls - before you lay down the next card you have to 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 {
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 participate

To take part, we register at libcanvas.com (we don’t need confirmation of the email, just login and password are enough), go into the game and see the " Edit Artificial Intelligence " link above. There we see a textbook for entering a code or the ability to specify a link to an external URL (for example, to be conveniently edited on localhost, do not forget to bring it back)

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

pps. Thank you Nutochka for the design)

ppps interested in the speed and stability of work on your hardware and browsers and the comparison of speed with what was in the kerchief

Source: https://habr.com/ru/post/103614/

All Articles