1

私はJavaで蛇と梯子のゲームを開発しています。これは構成可能です。つまり、蛇と梯子の「頭」と「尾」の位置を設定できます。蛇と梯子の画像を読み込んで、適切なサイズに変更してみました。頭と尾の位置の境界にフィットしますが、それは画像の品質に影響を与えるようです。誰かがより良い方法/アルゴリズムを提案できますか?よろしくお願いします。

4

3 に答える 3

1

使用しているアプローチには、品質を低下させる可能性のある2つの「問題」があります。

  1. あなたは(私が思うに)グラフィック画像をアップスケーリングしているため、ブロックが発生します。

  2. x 軸と y 軸の両方のスケールを変更すると (ズームインなど)、長い蛇は短い蛇よりも太く、幅が広くなります。これは、人々が期待するものではありません。

fdのソリューションを多少変更します。次のように進めます。

頭、尾、および中央の1 つのセクションのグラフィックを準備して、任意の数の中央セクションを連結できるようにします。

ヘビを描く必要がある場合は、その長さを計算します。次に、ヘビ全体がこの計算された長さ以上になるために必要な中間セクションの数を確認します。

正しいサイズのビットマップ バッファを作成して、正しい数の中間セクションを持つ水平 (または垂直!) のヘビを保持します。背景を透明に描画してから、このビットマップにヘビを描画します。通常、必要な長さよりもわずかに長くなります。

ビットマップを拡大縮小および回転し、ボード上の正しい位置に配置します。

ある程度スケーリングしますが、スケール ファクターは明らかではないほど小さくする必要があります。たとえば、正確にフィットさせるために中央セクションが 5.5 必要な場合は、中央セクションを 6 つ描画してから、ヘビ全体を約 5.5/6 でスケーリングして、正確な長さにします。これは 10% 未満の変化であるため、明らかではありません。

これにより、上記の問題が解決されます。

  1. ヘビの幅はわずかに異なります
  2. ダウンスケールするだけで、アップスケールすることはありません。

これには、次の実用的な利点があります。

  1. ヘビを水平(または垂直)に配置するだけで、簡単に実行できます
  2. それは非常に効率的でなければなりません。すべての実際の作業は、構築されたヘビを回転、ズーム、および配置する必要があるグラフィック ライブラリによって行われます。ほとんどのプラットフォームでは、これらの操作はハードウェアで計算される変換行列として実装されているため、非常に高速です。私が作成した Android アプリでこのアプローチを広く使用していますが、非常に高速です。

ヘビの「小刻み」が多いほど、スケーリング係数が 1.0 と異なるため、幅の変動が少なくなることに注意してください。

于 2013-02-11T06:44:46.880 に答える
0

public class SankesAndLadder {

static int turn = 0;
static int[] gameBoard = new int[100];
static Map<Integer, Integer> ladderPositionValue;
static Map<Integer, Integer> snakePositionValue;
static int userPosition = 0;
static int computerPosition = 0;


public static void populateBoard() {
    ladderPositionValue = new HashMap<>();
    snakePositionValue = new HashMap<>();
    // SUPPOSE WE HAVE 10 LADDERS AND 10 SNAKES
    for (int i = 0; i < 10; i++) {
        int ladderPositionKey = (int) (Math.random() * 85) + 10;
        if (!ladderPositionValue.keySet().contains(ladderPositionKey)) {
            ladderPositionValue.put((int) (Math.random() * 100) + 1, (int) (Math.random() * 10) + 5);
        } else {
            i--;
        }
        int snakePositionKey = (int) (Math.random() * 95) + 15;
        if (!ladderPositionValue.keySet().contains(ladderPositionKey) &&
                !snakePositionValue.keySet().contains(ladderPositionKey)) {
            snakePositionValue.put((int) (Math.random() * 100) + 1, (int) (Math.random() * 10) + 5);
        } else {
            i--;
        }
    }
}

public static int rollDice(int repeat) {
    System.out.println("ROLLING DICE...PRESS ANY KEY TO CONTINUE:");
    Scanner in = new Scanner(System.in);
    String cont = in.nextLine();
    int rolledNo = (int) (Math.random() * 6) + 1;
    if (rolledNo == 6) {
        System.out.println("NUMBER IS:" + rolledNo + ". ANOTHER CHANCE.");
        rollDice(repeat++);
    } else {
        System.out.println("NUMBER IS:" + rolledNo);
    }
    int finalCount = rolledNo + (repeat * 6);
    return finalCount;
}

public static boolean userTurn() {
    if (turn % 2 == 0) {
        turn++;
        return true;
    }
    turn++;
    return false;
}


public static void newUserPosition(int rolledNo) {
    userPosition = userPosition + rolledNo;
    System.out.println("YOUR NEW POSITION IS:" + userPosition);
    userPosition = checkSnakeOrLadder(userPosition);
}

public static void newComputerPosition(int rolledNo) {
    computerPosition = computerPosition + rolledNo;
    computerPosition = checkSnakeOrLadder(userPosition);
}

private static int checkSnakeOrLadder(int position) {
    if (snakePositionValue.keySet().contains(position)) {
        System.out.println("AAAAAAAAAAAHHHH ... BITTEN BY SNAKE");
        position = position - snakePositionValue.get(position);
    } else if (ladderPositionValue.keySet().contains(position)) {
        System.out.println("YAAAAAAAY.. GOT A LADDER");
        position = position + ladderPositionValue.get(position);
    }
    return position;
}

public static void main(String args[]) {
    System.out.println("WELCOME TO SNAKES & LADDER");
    System.out.println("***************************");
    populateBoard();
    while (userPosition != 100 && computerPosition != 100) {
        if (userTurn()) {
            System.out.println("YOUR TURN:");
            int rolledNo = rollDice(0);
            System.out.println("MOVING YOUR POSITION:");
            newUserPosition(rolledNo);
        } else {
            System.out.println("COMPUTER TURN:");
            int rolledNo = rollDice(0);
            System.out.println("MOVING COMPUTER POSITION:");
            newComputerPosition(rolledNo);
        }
    }
    if (userPosition == 100) {
        System.out.println("YOUR ARE THE WINNER!!!!");
    } else if (computerPosition == 100) {
        System.out.println("COMPUTER WON!!!!");
    }
}

}

于 2015-04-07T19:36:44.807 に答える