コード例 #1
0
  private void initGame() {
    // init
    gameState = PLAYER_1_TURN;
    lines = new ArrayList<Line>();
    squares = new ArrayList<Square>();
    // create the drawer
    drawer = new GameDrawer(getApplicationContext(), mGridSize, mGridSize);
    drawer.setOnTouchListener(this);
    points = drawer.getPoints();

    ((LinearLayout) findViewById(R.id.gameArea)).removeAllViews();
    ((LinearLayout) findViewById(R.id.gameArea)).addView(drawer);
    Log.d("dots", "Width = " + ((LinearLayout) findViewById(R.id.gameArea)).getWidth());

    mScores = new Integer[] {0, 0};
  }
コード例 #2
0
  public void undoTurn() {

    this.mScores[gameState] -= addedSquares.size();
    this.gameState = lastGameState;

    this.lines.remove(addedLine);
    drawer.removeLine(addedLine);

    for (Square s : addedSquares) {
      squares.remove(s);
    }
    drawer.removeSquares(addedSquares);
    addedSquares = new ArrayList<Square>();

    // disable the undo button
    ((Button) findViewById(R.id.undoButton)).setEnabled(false);
    updateView();
  }
コード例 #3
0
  private void updateView() {
    TextView p1 = ((TextView) findViewById(R.id.playerOneScore));
    TextView p2 = ((TextView) findViewById(R.id.playerTwoScore));
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

    p1.setText(playerList[0].getName() + ": " + mScores[0]);
    p2.setText(playerList[1].getName() + ": " + mScores[1]);

    // set the color in the view
    int p1Color = Integer.parseInt(prefs.getString(getString(R.string.p1color), "0"));
    int p2Color = Integer.parseInt(prefs.getString(getString(R.string.p2color), "1"));

    p1.setBackgroundColor(getResources().getColor(drawer.COLORS[p1Color]));
    p2.setBackgroundColor(getResources().getColor(drawer.COLORS[p2Color]));

    // set the colors in the drawer
    drawer.setPlayerColor(0, p1Color);
    drawer.setPlayerColor(1, p2Color);

    ((TextView) findViewById(R.id.currentPlayersTurnText))
        .setText(playerList[gameState].getName() + "'s Turn");
    drawer.invalidate();
  }
コード例 #4
0
  private void addLine(Line l) {
    if (inLines(l.getA().ordX, l.getA().ordY, l.getB().ordX, l.getB().ordY) != null) {
      Log.d(
          "DOTS",
          "Line already exists at ("
              + l.getA().ordX
              + ", "
              + l.getB().ordY
              + ") to ("
              + l.getB().ordX
              + ", "
              + l.getB().ordY
              + ")");
      return;
    }
    if (l.getA().ordX > l.getB().ordX || l.getA().ordY > l.getB().ordY) l.swapPoints();
    // get the ordinal location of the points on the line
    int x1 = l.getA().ordX;
    int y1 = l.getA().ordY;
    int x2 = l.getB().ordX;
    int y2 = l.getB().ordY;

    int pointsScored = 0;
    addedSquares = new ArrayList<Square>();

    int xSpace = drawer.getXSpace();
    int ySpace = drawer.getYSpace();

    if (y1 == y2) { // horizontal line
      // top square
      if (y1 > 0) {

        Line a = inLines(x1, y1, x1, y1 - ySpace);
        Line b = inLines(x1, y1 - ySpace, x2, y2 - ySpace);
        Line c = inLines(x2, y2, x2, y2 - ySpace);

        if (a != null && b != null && c != null) {

          Square s = new Square(a, b, c, l, gameState);
          squares.add(s);
          addedSquares.add(s);
          drawer.addSquare(s);
          pointsScored++;
        }
      }
      // bottom square
      Line a = inLines(x1, y1, x1, y1 + ySpace);
      Line b = inLines(x1, y1 + ySpace, x2, y1 + ySpace);
      Line c = inLines(x2, y2, x2, y2 + ySpace);

      if (a != null && b != null && c != null) {
        Square s = new Square(a, b, c, l, gameState);
        squares.add(s);
        addedSquares.add(s);
        drawer.addSquare(s);
        pointsScored++;
      }

    } else { // vertical line

      // left square
      if (x1 > 0) {
        Line a = inLines(x1, y1, x1 - xSpace, y1);
        Line b = inLines(x1 - xSpace, y1, x2 - xSpace, y2);
        Line c = inLines(x2, y2, x2 - xSpace, y2);

        if (a != null && b != null && c != null) {
          Square s = new Square(a, b, c, l, gameState);
          squares.add(s);
          addedSquares.add(s);
          drawer.addSquare(s);
          pointsScored++;
        }
      }
      // right square
      Line a = inLines(x1, y1, x1 + xSpace, y1);
      Line b = inLines(x1 + xSpace, y1, x2 + xSpace, y2);
      Line c = inLines(x2, y2, x2 + xSpace, y2);
      if (a != null && b != null && c != null) {
        Square s = new Square(a, b, c, l, gameState);
        squares.add(s);
        addedSquares.add(s);
        drawer.addSquare(s);
        pointsScored++;
      }
    }
    lines.add(l);
    addedLine = l;
    drawer.addLine(l);
    ((Button) findViewById(R.id.undoButton)).setEnabled(true);

    // add score
    mScores[gameState] += pointsScored;

    // progress player
    if (pointsScored == 0) {
      gameState++;
      if (gameState == mNumPlayers) gameState = 0;
    }

    updateView();
    checkGameOver();
  }