main.vala 2.75 KB
Newer Older
1 2 3 4 5 6 7
/*
 * A sample bot.
 * Currently it is dummy code only to develop the API. It doesn't compile and
 * it probably don't use the better API ever. Let's see.
 */


8
public class Bot
9
{
10 11 12 13 14
  private MainLoop          loop   = null;
  private OVCCClient.Player player = null;
  
  public bool join (OVCCClient.Server server)
    throws Error;
15
  {
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
    uint            player_suffix = 7;
    OVCCClient.Game game = null;
    
    /* connect to the server, choosing an available nickname */
    while (true) {
      try {
        player = new OVCCClient.Player ("Clever bot %3u", player_suffix);
        server.connect (player); /* may throw a rejection */
        break;
      } catch OVCCClient.ServerError.NICKNAME_IN_USE {
      } catch OVCCClient.ServerError.MISSING_AUTHENTICATION {
      }
    }
    /* join an open table */
    if (server.open_tables.length > 0) {
      player.join (server.open_tables[0]); /* may throw a rejection */
    } else {
      throw new OVCCClient.ServerError.NO_TABLE_OPEN ("failed to join a game");
    }
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    
    /* when game is finished, just stop */
    game.finished.connect (() => {
      loop.quit ();
      debug ("We're done, bye!");
    });
    /* we should place a tile? OK, why not */
    game.place_tile.connect ((game, tile) => {
      debug ("Seems we should place a tile...");
      game.board.foreach ((b, p, t) => {
        for (var i = 0; i < 4; i++) {
          var npos = p;
          
          switch (i) {
            case 0: npos.y--; break; /* top */
            case 1: npos.x++; break; /* right */
            case 2: npos.y++; break; /* bottom */
            case 3: npos.x--; break; /* left */
          }
          for (var j = 0; j < 4; j++) {
            /* FIXME: this should probably be asynchronous... */
            if (game.place_tile (player, npos)) {
              return false;
            } else {
              tile.rotate (1);
            }
          }
        }
        return true;
      });
    });
    /* we should place a pawn? let's do so */
    game.place_pawn.connect ((game, tile) => {
      debug ("We're asked to place a pawn!");
      foreach (var o in tile.objects) {
        /* we guess all pawns can go anywhere another can -- which isn't true
         * using some extensions, but it doesn't matter that much. */
        /* FIXME: this should probably also be asynchronous... */
        if (game.place_pawn (player.pawns.nth_data (0), o)) {
          break;
        }
      }
    });
  }
79 80 81 82 83
  
  public Bot (MainLoop l)
  {
    loop = l;
  }
84 85 86 87
}

public void main (string args)
{
88 89 90
  var loop = new MainLoop ();
  var bot = new Bot (loop);
  var server = new OVCCClient.Server ("localhost", 9007);
91
  
92 93 94 95 96 97
  try {
    bot.join (server);
    loop.run ();
  } catch (Error e) {
    warning ("Failed to join on the server: %s", e.message);
  }
98
}