Reputation: 731
I am developing a snake and ladder game in java, that is configurable-that is, you can set the 'head' and 'tail' positions for snakes and ladders in it.I have tried loading snake images and re-sizing them appropriately for fitting in the bounds of the head and tail positions, but that seems to affect the quality of the images. Can anyone suggest a better method/algorithm? Help greatly appreciated.
Upvotes: 1
Views: 2620
Reputation: 464
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!!!!");
}
}
}
Upvotes: 0
Reputation: 681
The approach you are using has two "problems" which may cause poor quality:
You are (I assume) upscaling graphic images, which will cause blockiness.
If you change the scale of both x and y axis (eg zoom in) long snakes will be fatter and wider than short snakes, which isn't what people expect to see.
I would modify fd's solution somewhat. Proceed as follows:
Prepare a graphic for the head, the tail, and a single section of middle so you can chain any number of middle sections together.
When you need to draw a snake, calculate its length. Then see how many middle sections you need for the whole snake to be equal to or greater than this calculated length.
Create a bitmap buffer of the correct size to hold a horizontal (or vertical!) snake with the correct number of middle sections. Draw the background as transparent, then draw the snake into this bitmap. It will typically be slightly longer than you need.
Scale and rotate the bitmap and place it at the correct location on your board.
You will still be scaling to some extent, but the scale factor should be small enough to not be obvious. For example, if you need 5.5 middle sections to make an exact fit, then you would draw 6 middle sections and then scale the whole snake by about 5.5/6 to make it exactly the right length. This is less than 10% change, so shouldn't be obvious.
This solves the problems above:
It has the following practical benefits:
Note that the more "wiggles" in the snake, the less your scaling factor will differ from 1.0 and hence the less variation in width.
Upvotes: 1
Reputation: 10772
I would suggest splitting the snake graphics into sections - a head, a tail and one or more middle body sections. Then, depending on the required length of snake you can construct it from drawing the middle section as long as required.
Upvotes: 4