How to get variables from for loop - java

I'm trying to make a for loop, which makes me 1000 objects and places those to a randomly generated spot (x, y). So here is the code. I have been struggling with this many hours and I have also been searching from the net but haven't found out any way to do that. After that loop I try to add those objects into some kind of radar.
Here's the code (so the problem is that I can't figure out how to take variables from the loop and make it appear outside the loop):
case "look": {
System.out.print("You are at: " +px +", " +py);
System.out.println("");
StringBuilder objects = new StringBuilder(); //That's something i found out form the net..
while (objnum>=0){ objnum--; //Creates randomly 1000objects around the map
int objid = (int)(Math.random() * 11 + 1); //int objnum is 1000, told above
int objx = (int)(Math.random() * 10000 + 1);
int objy = (int)(Math.random() * 10000 + 1);}
board.spawnObject(new BoardObject(objectid, objx, //That's something i found out form the net..
objy, objnum));
for(int x=px-2 ; x< px+3 ; x++ ){ //px=player position
for(int y=py-2 ; y< py+3 ; y++ ){ //this is how radar is created
if(objid==1 && x==objx && y==objy){board[x][y]=1;}
else if(objid==2 && x==objx && y==objy){board[x][y]=2;}
else if(objid==3 && x==objx && y==objy){board[x][y]=3;} //That's where i need info from the loop..
else if(objid==4 && x==objx && y==objy){board[x][y]=4;}
else if(objid==5 && x==objx && y==objy){board[x][y]=5;}
else if(objid==6 && x==objx && y==objy){board[x][y]=-1;}
else if(objid==7 && x==objx && y==objy){board[x][y]=-2;}
else if(objid==8 && x==objx && y==objy){board[x][y]=-3;}
else if(objid==9 && x==objx && y==objy){board[x][y]=-4;}
else if(objid==10 && x==objx && y==objy){board[x][y]=-5;}
if(x==px && y==py){
board[x][y]=6;}//<- this shows players position on radar
if(board[x][y]==-1){
System.out.print("[sto]");
}else if(board[x][y]==0){
System.out.print("[___]");//<- This works well..
}else if(board[x][y]==-2){
System.out.print("[box]");
}
else if(board[x][y]==-3){
System.out.print("[ppl]");
}
else if(board[x][y]==-4){
System.out.print("[pit]");
}
else if(board[x][y]==-5){
System.out.print("[brk]");
} //That's how radar shows dots/objects
else if(board[x][y]==1){
System.out.print("[kid]");
}
else if(board[x][y]==2){
System.out.print("[tre]");
}
else if(board[x][y]==3){
System.out.print("[pet]");
}
else if(board[x][y]==4){
System.out.print("[bus]");
}
else if(board[x][y]==5){
System.out.print("[???]");
}
else if(board[x][y]==6){
System.out.print("[You]");} //<- This works well..
}
System.out.println();
}; }break;

Simply use Collection interface class like Vector for storing objects and then, access them from outside the loop.Follow this link: http://docs.oracle.com/javase/6/docs/api/java/util/Collection.html

You have to create a variable outside the scope and if a condition is true, than change the value of the variable outside the loop, so:
class example {
int number = 0;
.....
for (int i = 0; i < 10; i++) {
if (i == 5) {
number = 5;
}
}
If the condition is true, then number becomes 5, to get this data, you can create getters and setters.
But if you want to store all those 1000 objects in 1 object, i would recommend to use an array(list), so:
class Example {
ArrayList<Integer> object = new ArrayList<Integer>();
case.....
object.add(objx)
object.add(objy)
}

Related

Room placement in a maze using Prim's algorithm

I am attempting to place rooms on an ASCII screen, and then use Prim's algorithm to "fill" the space between rooms with maze, but without actually breaking into the rooms. I have been tinkering for a few hours, and I can't figure out a way to stop my algorithm from breaking into my rooms.
Can anyone help me? I'm pretty lost. I'm practicing map generation techniques, and this is my 5th one I'm on. No, I don't want to do it another way, I simply want to do it this way - but right.
Below is both a photo of my current output with rooms, my current output without rooms, and a link to the relevant source code (AKA the Prim's algorithm section). Thanks again if you can actually help me!
Note: All the opposite method does is figure out which cell the "parent" cell is, and determine direction based off of that. So if parent cell's x value is 7, and the child's x value is 6, then it know that's the new child.
start = new Point(x,y, null);
map[start.x][start.y] = Tile.STAIRS_DOWN;
for(int nx = -1; nx <= 1; nx++){
for(int ny = -1; ny <= 1; ny++){
if((nx == 0 && ny == 0) || (nx != 0 && ny != 0)){
continue;
}
try{
if(map[start.x + nx][start.y + ny] == Tile.FLOOR){
continue;
}
frontier.add(new Point(start.x+nx, start.y + ny, start));
}
catch(Exception e){
continue;
}
}
}
Point last = null;
while(!frontier.isEmpty()){
Point cu = frontier.remove(RandomGen.rand(0, frontier.size() - 1));
Point op = cu.opposite();
try{
if((map[cu.x][cu.y] == Tile.WALL) && (map[op.x][op.y] == Tile.WALL)){
for (int bx = -1; bx <= 1; bx++)
for (int by = -1; by <= 1; by++) {
boolean failed = false;
if (bx == 0 && by == 0 || bx != 0 && by != 0)
continue;
try {
if(map[op.x + bx][op.y + by] == Tile.FLOOR){
break;
}
last = op;
if(!failed){
map[cu.x][cu.y] = Tile.FLOOR;
map[op.x][op.y] = Tile.FLOOR;
frontier.add(new Point(op.x + bx, op.y + by, op));
}
}
catch(Exception e){
continue;
}
}
}
}
catch(Exception e){}
}
Without Rooms
With Rooms
Solved: I needed to check my forward facing corners for open spaces. So if I'm going "east" then I need to check Northeast and Southeast tiles as well, or else I will potentially burrow into the rooms.

Why will my for loop not read my last char of the string?

I am trying to write a basic java program to compress a java string from an input; such as aabbccdddd, into a2b2c2d4. The program does what I ask except it doesn't process the last char, I am getting an output of a2b2c2 instead of the a2b2c2d4. What am I doing wrong?
for(x = 0, y = 1; x<input.length()-1; x++)
{
if (input.charAt(x) != input.charAt(x+1) && count == 1)
{
System.out.print(input.charAt(x));
System.out.print(count);
}
else if (input.charAt(x) == input.charAt(x+y))
{
count++;
}
else if (input.charAt(x) != input.charAt(x+1) && count >= 2)
{
System.out.print(input.charAt(x));
System.out.print(count);
count = 1;
}
else
{
System.out.println("fail");
}
}
You print the count when the next character is not same as the current one. There is no next character for the last character. That is why it is not displayed in the output.
Approach 1
You should add following two lines after the loop:
System.out.print(input.charAt(input.length()-1));
System.out.println(count);
Approach 2
If you do not have problem with modifying the original input. You can add a additional character in the end of the input. This additional character must be a character which will never appear in the original string. Say it is #
Do this before beginning of the loop:
input += "#";
for(...)
The loop is incorrect, you have "-1" after input.length(). Try:
for(x = 0, y = 1; x<input.length(); x++) {
// CODE HERE...
}
Your for loop ends before you hit a condition that forces you to print out what's being buffered, ie. count of 4 for the current (last) character. You need to print out the last character and the current count after the loop.
The following should do what you want
public static void main(String[] args) throws Exception {
String input = "aabbccdddd";
int count= 1;
int x, y;
for (x = 0, y = 1; x < input.length() - 1; x++) {
char charAtX = input.charAt(x);
char charAtXPlus1 = input.charAt(x + 1);
if ( charAtX != charAtXPlus1 && count == 1) {
System.out.print(input.charAt(x));
System.out.print(count);
}
else if (charAtX == input.charAt(x + y)) {
count++;
}
else if (charAtX != charAtXPlus1 && count >= 2) {
System.out.print(input.charAt(x));
System.out.print(count);
count = 1;
} else {
System.out.println("fail");
}
}
System.out.print(input.charAt(x));
System.out.println(count);
}
You should learn how to use a proper debugger and use proper debugging techniques. For example, I've assigned the value returned by input.charAt(x) to a variable because we reuse that value in the various if-else conditions and because it's easier to see it in a debug window.
You are not able to get the desired result because of the condition
else if(input.charAt(x)!=input.charAt(x+1)&&count>=2)
that fails as there is not character at x+1 location.
So, you could add another condition to check if it's the last character and then go ahead with your code
while (input.charAt(x) != input.length()-1)

Can anyone lend a fresh pair of eyes for debugging?

The purpose of this assignment was to create a Field and Robot class and objects of those classes.
The single field object is limited to a square of points from (0, 0) to (50, 50), and contains 3 pieces of gold and 3 bombs.
Three robot objects search the field (one after another) for gold from left to right (0, 0) to (0, 50) and descend through the field (1, 0) to 1, 50) and so on.
The robots are destroyed by bombs that are places by input from the user. Once the gold is collected it cannot be picked up by another robot, and one a bomb explodes it does not do so again.
This is my attempt at solving the problem so far, I am continuing to work on it, but would appreciate a second pair of eyes on it to catch something im missing. The program compiles, but the bombs and gold arent being "found" correctly and the output states that the following robots die on the same bomb as the one before it. Also, there are several pieces of code removed by comments, I did this to test different parts of the program. I think this section is where I'm having trouble. The methods field.alreadyFound() and field.alreadyBombed() return a boolean with the value true. My if statements should be saying if the gold/bomb has already been found, ignore it.
while(x <= 50 && y <= 50 && alive2 == true) {
foundGold1 = robot2.look(field.locateGold1());
foundGold2 = robot2.look(field.locateGold2());
foundGold3 = robot2.look(field.locateGold3());
foundBomb1 = robot2.look(field.locateBomb1());
foundBomb2 = robot2.look(field.locateBomb2());
foundBomb3 = robot2.look(field.locateBomb3());
/*gotBomb1 = field.alreadyBombed1();
gotBomb2 = field.alreadyBombed2();
gotBomb3 = field.alreadyBombed3();
gotGold1 = field.alreadyFound1();
gotGold2 = field.alreadyFound2();
gotGold3 = field.alreadyFound3();*/
if (foundGold1 == true){
if (field.alreadyFound1() == true){}
else {robot2.addGold();
field.foundGold1();}
}
if (foundGold2 == true) {
if (field.alreadyFound2() == true){}
else {robot2.addGold();
field.foundGold2();}
}
if (foundGold3 == true) {
if (field.alreadyFound3() == true){}
else {robot2.addGold();
field.foundGold3();}
}
if (foundBomb1 == true) {
if (field.alreadyBombed1() == true){}
else alive2 = false;
}
if (foundBomb2 == true) {
if (field.alreadyBombed2() == true){}
else alive2 = false;
}
if (foundBomb3 == true) {
if (field.alreadyBombed3() == true){}
else alive2 = false;
}
y = y + 1;
robot2.setLocation(x, y);
//System.out.println(y);
if (y == 50)
{x = x + 1;
y = 0;}
}
I don't see where you set that a bomb has exploded, so from what I see here, you're missing that part.
Below is the code reformatted and slightly restructured: I found the code fairly difficult to work with as-is. It may be easier to work with this shorter, more canonical, and IMO more communicative version.
while (x <= 50 && y <= 50 && alive2 == true) {
foundGold1 = robot2.look(field.locateGold1());
foundGold2 = robot2.look(field.locateGold2());
foundGold3 = robot2.look(field.locateGold3());
foundBomb1 = robot2.look(field.locateBomb1());
foundBomb2 = robot2.look(field.locateBomb2());
foundBomb3 = robot2.look(field.locateBomb3());
/*gotBomb1 = field.alreadyBombed1();
gotBomb2 = field.alreadyBombed2();
gotBomb3 = field.alreadyBombed3();
gotGold1 = field.alreadyFound1();
gotGold2 = field.alreadyFound2();
gotGold3 = field.alreadyFound3();*/
if (foundGold1 && !field.alreadyFound1()) {
robot2.addGold();
field.foundGold1();
}
if (foundGold2 && !field.alreadyFound2()) {
robot2.addGold();
field.foundGold2();
}
if (foundGold3 && !field.alreadyFound3()) {
robot2.addGold();
field.foundGold3();
}
if (foundBomb1 && !field.alreadyBombed1()) {
alive2 = false;
}
if (foundBomb2 && !field.alreadyBombed2()) {
alive2 = false;
}
if (foundBomb3 && !field.alreadyBombed3()) {
alive2 = false;
}
y = y + 1;
robot2.setLocation(x, y);
if (y == 50) {
x = x + 1;
y = 0;
}
}
You may also be hampered by the amount of code it takes to do this work: I assume you have essentially identical code for robot1 and robot2, the only difference being which robot you're currently processing.
Rather than repeat the code, consider passing a currentRobot in to a single method. There are a variety of ways to go about dealing with issues like this, but that fits in well with what you've already done. You're likely want to add an isAlive method/property to the robot class.

Why does my isFullHouse() method also accept a simple three-of-a-kind?

I am having problems with my full house method. I thought it was as simple as checking for three of a kind and a pair. But with my current code i am getting a full house with only a three of a kind. Code for isFullHouse() isThreeOfAKind() and isPair() is below thanks for all the help!
public boolean isPair() {
Pips[] values = new Pips[5];
int count =0;
//Put each cards numeric value into array
for(int i = 0; i < cards.length; i++){
values[i] = cards[i].getPip();
}
//Loop through the values. Compare each value to all values
//If exactly two matches are made - return true
for(int x = 1; x < values.length; x++){
for(int y = 0; y < x; y++){
if(values[x].equals(values[y])) count++;
}
if (count == 1) return true;
count = 0;
}
return false;
}
public boolean isThreeOfAKind() {
Pips[] values = new Pips[5];
int counter = 0;
for(int i = 0; i < cards.length; i++){
values[i] = cards[i].getPip();
}
//Same process as isPair(), except return true for 3 matches
for(int x = 2; x < values.length; x++){
for(int y = 0; y < x; y++){
if(values[x].equals(values[y]))
counter++;
}
if(counter == 2) return true;
counter = 0;
}
return false;
}
public boolean isFullHouse(){
if(isThreeOfAKind() && isPair())
return true;
return false;
}
Check to make sure that the pair is of a different rank than the three of a kind. Otherwise, your isPair() function will find the same cards as the three of a kind. Maybe like this:
public boolean isFullHouse(){
int three = isThreeOfAKind();
int pair = isPair();
if (three != 0 && pair != 0 && three != pair) {
return true;
}
return false;
}
(I used int, but you could change to use your Pips type if you like.)
Can I suggest a way of making your logic dramatically simpler?
Consider a helper method named partitionByRank():
public class RankSet {
private int count;
private Rank rank;
}
/**
* Groups the hand into counts of cards with same rank, sorting first by
* set size and then rank as secondary criteria
*/
public List<RankSet> partitionByRank() {
//input e.g.: {Kh, Qs, 4s, Kd, Qs}
//output e.g.: {[2, K], [2, Q], [1, 4]}
}
Getting the type of hand is really easy then:
public boolean isFullHouse() {
List<RankSet> sets = partitionByRank();
return sets.length() == 2 && sets.get(0).count == 3 && sets.get(1).count() == 2;
}
public boolean isTrips() {
//...
return sets.length() == 3 && sets.get(0).count = 3;
}
This will also help later on when you inevitably need to check whether one pair is greater than another pair, e.g.
You have to remove the three of a kind cards from the five card hand first. Three of a kind is true implies two of a kind is true. The sets need to be disjoint.
You are missing a third condition: the triple needs to be different cards than the pair. Soo... since you have this shared "cards" array, you probably could "mark" the cards as counted, and reset the counted status for each pass:
//Same process as isPair(), except return true for 3 matches
for(int x = 2; x < values.length; x++){
cards[x].setCounted(true); // by default, count the start card
for(int y = 0; y < x; y++){
// make sure the card isn't already counted:
if(!cards[y].isCounted() && values[x].equals(values[y])) {
counter++;
cards[x].setCounted(true); // count it
}
}
if(counter == 2) return true;
counter = 0;
// reset counted cards
for(int z=0, zlen=values.length; z < zlen; z++) { cards[z].setCounted(false); }
}
because three of a kind has a pair as well (actually would probably be 2 pairs in your code)
one way to do this is to sort the hand by rank, then its just conditionals to detect a boat.
if ( ((c1.rank == c2.rank == c3.rank) && (c4.rank == c5.rank)) ||
(c1.rank == c2.rank) && (c3.rank == c4.rank == c5.rank))
ther emight be an extra ( in there but you get the idea...
You need to make sure the pair is a different two cards than the three of a kind. If the hand is A A A 7 8, then both ThreeOfAKind and isPair return true because you have three aces (and a pair of aces).
Your isPair() method will always return true when there are three cards of a kind because your inner loop always tests the y values only up to x.
so with this data AAA78, when x = 1 y = 0 you will get count == 1 in the inner loop and return true although there are three of a kind. It's better to loop over the entire array and count values when
if(values[x].equals(values[y]) && x != y)
Besides - it's better to use one function in the form of isNOfAKind() which gets the amount of cards as a parameter since these two methods essentially do the same.
Just an idea, wouldn't it be easier to do something like this:
int[] count=new int[13];//size of all ranks
for (i=0;i<5;i++)
count[ card[i].rank ] ++;
So you will have for example: 0 0 0 0 0 3 0 0 0 2 0 0 0 0 for a full house. A straight would look like 5 ones in a row: 0 0 0 0 1 1 1 1 1 0 0 0.
Since the methods are public, I would not like the isPair() method to return true if there is a pair. It should only return true if there is nothing better than one pair.
A better general approach to the problem - this is C#, but converting it to Java should be straightforward:
int[] countOfRank = new int[13];
int[] countOfSuit = new int[4];
for(int i = 0; i < cards.length; i++)
{
countOfRank[cards[i].Rank]++;
countOfSuit[cards[i].Suit]++;
}
for (int i=0; i < countOfSuit.length; i++)
{
isFlush = isFlush || countOfSuit[i] == 5;
}
int[] countOfTuple = new int[5];
int runLength=0;
for (int i=0; i < countOfRank.length; i++)
{
if (countOfRank[i] == 1)
{
runLength++;
isStraight = (isStraight || runLength == 5);
}
else
{
runLength=0;
}
countOfTuple[countOfRank[i]]++;
}
isPair = (countOfTuple[2] == 1 && countOfTuple[3] == 0);
isTwoPair = (countOfTuple[2] == 2);
isFullHouse = (countOfTuple[2] == 1 && countOfTuple[3] == 1);
isThreeOfAKind = (countOfTuple[2] == 0 && countOfTuple[3] == 1);
isFourOfAKind = (countOfTuple[4] == 1);
isStraightFlush = (isStraight && isFlush);
isStraight = (isStraight && !isStraightFlush);
isFlush = (isFlush && !isStraightFlush);
isRoyalFlush = (isStraightFlush && countOfRank[12] == 1);
isStraightFlush = (isStraightFlush && !isRoyalFlush);
If you're only dealing with five-card hands, counting the number of pairs should yield one for a pair, two for two-pair, three for three-of-a-kind (e.g. if one has As, Ad, and Ac, the pairs are As-Ad, As-Ac, and Ad-Ac), four for a full house, and six for four-of-a-kind. This logic will not work with seven card hands, since it would count three for e.g. A-A-K-K-Q-Q-J (which should only count as two-pair, not three-of-a-kind), and six for A-A-A-K-K-K-Q (which should count as a full house, not four-of-a-kind).
According to your code inlined comments (exactly two matches words) maybe you are trying to implement isPair method in such a way that it will return false in case of three of a kind combination. If so, you need change your isPair method to iterate over all items in the array, like this:
//Loop through the values. Compare each value to all values
//If exactly two matches are made - return true
for(int x = 0; x < values.length; x++){
for(int y = 0; y < values.length; y++){
if(y != x && values[x].equals(values[y])) count++;
}
if (count == 1) return true;
count = 0;
}

When there is two for() loop, the second one doesn't work

Here are my for() loops :
public void showMovementCase(){
int movePlusAttack = moveAllowed+attackDistance;
int twiceMoveAllowed = (moveAllowed)*2;
for(int i = 0; i <= movePlusAttack*2; i++){
for(int j = 0; j <= movePlusAttack*2;j++){
boolean a = movePlusAttack <= j+i && movePlusAttack >= j-i && i <= movePlusAttack;
boolean b = movePlusAttack <= j+i && movePlusAttack >= i-j && i > movePlusAttack && j <= movePlusAttack;
boolean c = movePlusAttack*3 >= j+i && movePlusAttack >= j-i && i > movePlusAttack && j >= movePlusAttack;
if(a || b || c){
try{
actionSquare[i][j] = new JLabel();
actionSquare[i][j].setIcon(redsquare);
actionSquare[i][j].setBounds(sprite.getX()+(i-movePlusAttack)*16,sprite.getY()+(j-movePlusAttack)*16, 16, 16);
panel.add(actionSquare[i][j], new Integer(1));
}
catch(ArrayIndexOutOfBoundsException e){System.out.println("red :" + e);}
}
}
}
for(int x = 0; x <= twiceMoveAllowed; x++){
for(int y = 0; y <= twiceMoveAllowed;y++){
boolean a = moveAllowed <= y+x && moveAllowed >= y-x && x <= moveAllowed;
boolean b = moveAllowed <= y+x && moveAllowed >= x-y && x > moveAllowed && y <= moveAllowed;
boolean c = moveAllowed*3 >= y+x && moveAllowed >= y-x && x > moveAllowed && y >= moveAllowed;
if(a || b || c){
try{
actionSquare[x][y].setIcon(bluesquare);
System.out.println("Coucou !");
actionSquare[x][y].addMouseListener(mouse);
panel.repaint();
panel.revalidate();
}
catch(ArrayIndexOutOfBoundsException e){System.out.println("blue :" + e); }
}
}
}
}
if this.attackDistance is different of 0, then the second loop doesn't work (it seems to stop at the .setIcon() command).
Do you know a way to fix this ?
Thanks for reading.
Edit:
with :
try{
actionSquare[x][y].setIcon(bluesquare);
System.out.println("Coucou !");
[...]
}
On the second loop, nothing is printed.
but with :
try{
System.out.println("Coucou !");
actionSquare[x][y].setIcon(bluesquare);
[...]
}
"Coucou !" is printed once.
That's why I said that "it seems to stop at the .setIcon() command" I should have said that sooner, sorry.
Here are a few tips:
don't catch exceptions and do nothing with them. That's what you are doing here in both loops, and so it's normal you don't see the error message.
anytime you see long statements like you have, it should be a hint that you could refactor it. For example, create a separate method that validates whether or not you're going to do something in your loop, and then inside the main method you'd call it like if(shouldPerformAction())
consider using less than 8 spaces for indentation. This just eats up your screen real estate.
consider making computations before the loops instead of inside the loop conditions, if the computation is supposed to be fixed (for example this.moveAllowed*2)
imho, no point in prefixing all your methods/fields with this, it just clutters everything. Just call the methods directly.
This is a very, very bad idea:
catch(ArrayIndexOutOfBoundsException e){}
You effectively tell the JVM to ignore any problems with your arrays that it detects. And worse than that: you don't even print anything when that happens.
Put at least a e.printStackTrace() in there to see if a problem occurs and where.
And as a further step: fix your array access to not exceed any limits. Catching an ArrayIndexOutOfBoundsException is a terribly bad idea. Avoid having it thrown at all!
Hmmm... where to begin...
I would first suggest putting something (System.err.println(...)?) inside of your catch blocks. Or just commenting them out entirely so you'd see the full stacktrace. What if you're hitting an exception and just not seeing it?
catch(ArrayIndexOutOfBoundsException e){}
This is a bad practice for two reasons:
You should never catch RuntimeException. It is just a very helpful indicator for errors in code logic (i.e. developer errors) which ought be solved by writing good code.
You should never ignore e unless you know perfectly what you're doing. Add at least an e.printStackTrace() so that you at least know that something failed.
I cleaned up your code for you. Generally, when you have two sections of code that are supposed to be doing the exact same thing, but are not, then rolling them into one method can eliminate that possibility.public void showMovementCase(){
// probably want to remove anything left over from the last invocation
panel.removeAll();
for (JLabel[] array : actionSquare) Arrays.fill(array, null);
colorSquares(moveAllowed + attackDistance, redsquare, null);
colorSquares(moveAllowed * 2, bluesquare, mouse);
for (int x = 0; x < actionSquare.length; x++)
for (int y = 0; y < actionSquare[x].length; y++)
if (actionSquare[x][y] != null) panel.add(actionSquare[x][y], 1);
}
private void colorSquares(int move, Icon color, MouseListener mouse) {
int xMax = Math.min(2 * move, actionSquare.length);
int yMax = Math.min(2 * move, actionSquare[0].length);
for (int x = 0; x < xMax; x++) {
for (int y = 0; y < yMax; y++) {
if (isLegal(x, y, move)) {
if (actionSquare[x][y] == null)
actionSquare[x][y] = new JLabel();
actionSquare[x][y].setIcon(color);
actionSquare[x][y].setBounds(
sprite.getX() + (x - move) * 16,
sprite.getY() + (y - move) * 16, 16, 16 );
if (mouse != null) actionSquare[x][y].addMouseListener(mouse);
}
}
}
}
private static boolean isLegal(int x, int y, int move) {
// informative comment explaining why this mess makes sense
if (move <= y+x && move >= y-x && x <= move) return true;
// informative comment explaining why this mess makes sense
if (move <= y+x && move >= x-y && x > move && y <= move) return true;
// informative comment explaining why this mess makes sense
if (move * 3 >= y+x && move >= y-x && x > move && y >= move) return true;
return false;
}

Categories

Resources