rameshvarun / Hnefatafl

Android Hnefatafl game using Google Play Games Services.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Hnefatafl

Overview

Hnefatafl is a family of 2-player Viking board games, where one player tries to guide a King to a refugee square, while attackers try to prevent this. Right now, we're implementing the popular 11x11 variant - Feltar Hnefatafl. To learn more about Hnefatafl + it's history, check out this video - https://www.youtube.com/watch?v=gND2P1sHM3U.

Programming Patterns

Retrolambda

Right now, we're using Retrolambda, which lets you use Java 8 lambda syntax. Essentially, this works in any situation in which you would create an anonymous class from an single-function interface. Careful though, as anything you capture will be kept around by the GC, since it is tied to the life of the lambda.

builder.setTitle("You Have Won The Game.")
  .setPositiveButton("Return to Menu", (DialogInterface sideDialog, int which) -> {
    NavUtils.navigateUpFromSameTask(this);
  }).show();

Stream API Backport

We're also using a backport of the Java 8 Stream API, which lets you do stuff like map, filter, all, etc. Perfect for use with Retrolambda.

return Stream.of(Direction.values()).allMatch((Direction dir) -> {
  // The King is only captured when surrounded on all four sides.
  Position adjacent = defendingPos.getNeighbor(dir);
  return pieces.containsKey(adjacent) && pieces.get(adjacent).hostileTo(piece);
});

Prefer Optional over a Nullable Variable

If you do need to have an extra "undetermined" state for a value, it might be better to use the Optional wrapper (part of the Stream backports). Optional will force you to do an isPresent check, thus preventing easy mistakes where you try to operate on a null object.

Other

  • Generally avoid nulls
  • Try to use final wherever it makes sense.
  • Prefer creating a getter over simply making a variable public (yeah I know, it's verbose, but it communicates that outside classes shouldn't just change the value)
  • Prefer composition over inheritance (Gang of Four / Effective Java advice)

Packages

  • Simulator - The simulator handles all of the rules of Hnefatafl, exporting the Boardclass that represents the state of a game.
  • Hnefatafl AI - This package implements various strategies for controlling the AI player in Player vs. AI games.
  • The Game - This package handles all of the drawing / input, using LibGDX.

Assets Used

About

Android Hnefatafl game using Google Play Games Services.


Languages

Language:Java 97.8%Language:HTML 2.2%