How can I simplify this code? (Chess game obstruction testing) - java

I'm making a chess game in Java, and testing to make sure there are no pieces blocking the path of the piece being moved. The piece moves from (srcX,srcY) to (dstX,dstY).
I've written this code which checks if there are any obstructions for a rook:
if(dstY == srcY) {
// No change on Y axis, so moving east or west
if(dstX > srcX) {
// Moving east
// Test every cell the piece will pass over
for(int x = srcX+1; x < dstX; x++) {
// Is the cell set?
if(isPiece(x, srcY)) {
return true;
}
}
} else {
// Must be moving west
// Test every cell the piece will pass over
for(int x = srcX-1; x > dstX; x--) {
// Is the cell set?
if(isPiece(x, srcY)) {
return true;
}
}
}
} else if(dstX == srcX) {
// No change on X axis, so moving north or south
if(dstY > srcY) {
// Moving north
// Test every cell the piece will pass over
for(int y = srcY+1; y < dstY; y++) {
// Is the cell set?
if(isPiece(srcX, y)) {
return true;
}
}
} else {
// Must be moving south
// Test every cell the piece will pass over
for(int y = srcY-1; y > dstY; y--) {
// Is the cell set?
if(isPiece(srcX, y)) {
return true;
}
}
}
}
but it's a bit big and I'm sure it can be simplied.. any ideas?
ps, this is ONLY obstruction testing. I've already validated everything else.

Once you've tested for direction, you can set dx, dy values (e.g. dx=1, dy=0 for east). Then you can have a single for loop for all cases and just increment x and y by dx and dy respectively at each iteration.
You can then simplify the direction checking into the following:
if dstY == srcY: dy = 0
else: dy = (dstY - srcY) / abs(dstY - srcY)
if dstX == srcX: dx = 0
else: dx = (dstX - srcX) / abs(dstX - srcX)
Code:
int dx, dy;
if (dstY == srcY) dy = 0;
else dy = (dstY - srcY) / Math.abs(dstY - srcY);
if (dstX == srcX) dx = 0;
else dx = (dstX - srcX) / Math.abs(dstX - srcX);
while (srcX != dstX || srcY != dstY) {
srcX += dx; srcY += dy;
if (isPiece(srcX, srcY))
return true;
}
return false;
Also beware that this code (and yours) will fail if the move is not horizontal, vertical or diagonal.

You could do something along these lines (untested as I don't have a compiler to hand):
int dx = 0;
int dy = 0;
if (dstX != srcX) {
dx = (dstX > srcX) ? 1 : -1;
} else if (dstY != srcY) {
dy = (dstY > srcY) ? 1 : -1;
}
int x = srcX + dx;
int y = srcY + dy;
while (x != dstX || y != dstY) {
if (isPiece(x, y)) return true;
x += dx;
y += dy;
}

First, write tests. Lots and lots of tests. That way you can be confident that you're simplifying without changing the meaning of the code.
Refactoring without unit tests is like walking a high wire without a safety net.

Nearly the same, but with for loops:
// move along x axis
for (int x = 1; x < Math.abs(srcX - dstX); x++) {
int curX = (srcX - dstX) < 0 ? srcX - x : srcX + x;
if (isPiece(curX, srcY))
return true;
}
// move along y axis
for (int y = 1; y <= Math.abs(srcY - dstY); y++) {
int curY = (srcY - dstY) < 0 ? srcY - y : srcY + y;
if (isPiece(srcX, curY))
return true;
}

My soultion would be: introduce a direction class, and then do the check in this style:
isBlocked(startPossition, direction, numberOfFields)
I have done a little example, using 3 Classes.
Direction - an enum to represent the 8 directions (2 horizontal, 2 vertical, 4 diagonal)
Position - the x and y value of an position
LinarMove - represent one linear Move(startPossition, direction, numberOfFields) and contains the isBlockedMethod
The Enum:
public enum Direction {
UP(0, 1),
DOWN(0, -1),
LEFT(1, 0),
RIGHT(-1, 0),
UP_LEFT(UP, LEFT),
UP_RIGTH(UP, RIGHT),
DOWN_LEFT(DOWN, LEFT),
DOWN_RIGHT(
DOWN, RIGHT);
private final int incrementX;
private final int incrementY;
private Direction(int incrementX, int incrementY) {
this.incrementX = incrementX;
this.incrementY = incrementY;
}
private Direction(Direction sub1, Direction sub2) {
this.incrementX = sub1.incrementX + sub2.incrementX;
this.incrementY = sub1.incrementY + sub2.incrementY;
}
public Position oneField(Position start) {
return new Position(start.getX() + this.incrementX, start.getY()
+ this.incrementY);
}
}
The purpuse of second constructor is only that it alowes to write the diagonal moves in a more readable way.
public class Position {
private final int x;
private final int y;
public Position(int x, int y) {
super();
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
#Override
public String toString() {
return "x:" + x + ", y:" + y;
}
}
The Move contains the isBlocked Method -- you can see how small it get, and how readable it become. At least there is no single direction related if statement left.
The name LinareMove sugget that there is possible an other kind of move for the knight.
public class LinearMove {
private final Position start;
private final Direction direction;
/** Length of the move. */
private final int numberOfFields;
public LinearMove(Position start, Direction direction, int numberOfFields) {
super();
this.start = start;
this.direction = direction;
this.numberOfFields = numberOfFields;
}
boolean isBlocked() {
Position pos = this.start;
for (int i = 0; i < (this.numberOfFields - 1); i++) {
pos = this.direction.oneField(pos);
if (isPiece(pos)) {
return true;
}
}
return false;
}
boolean isPiece(Position pos) {
//Dummy;
System.out.println(pos);
return false;
}
}
And this is how it works:
public static void main(String[] args) {
new LinearMove(new Position(1, 1), Direction.RIGHT, 3).isBlocked();
}
You maybe noticed, that the knights move is some kind of probem. With this soultion you could model it in two ways:
- 4 special Directions
- an other kind of move class (this is the more cleaner way, because you could always return true, in the isBockedMethod)

Related

Chess: Duplicate code in movement validations - Java

I am currently learning Java and working on a class assignment. We're supposed to make a "weird version" of Chess.
Like in original chess, pieces can't move if there is a piece in their way, the obvious exception being the Horse, which can hop over all pieces except Kings.
I have an abstract class Piece that is inherited by all the piece types, each with their own rules of movement. Most of them have this movement restriction, and so I have defined the methods in this class:
public boolean freeWayHorizontally(int xO, int yO, int xD) {
//RIGHT
if (xO < xD) {
for (int x = xO + 1; x < xD; x++) {
CrazyPiece thereIsPiece = Simulador.checkIfTheresPiece(x, yO);
if (thereIsPiece != null){
return false;
}
}
//LEFT
} else if (xO > xD) {
for (int x = xO - 1; x > xD; x--) {
CrazyPiece thereIsPiece = Simulador.checkIfTheresPiece(x, yO);
if (thereIsPiece != null){
return false;
}
}
}
return true;
}
public boolean freeWayVertically(int xO, int yO, int yD) {
//UP
if (yO < yD) {
for (int y = yO + 1; y < yD; y++) {
CrazyPiece thereIsPiece = Simulador.checkIfTheresPiece(xO, y);
if (thereIsPiece != null){
return false;
}
}
//DOWN
} else if (yO > yD) {
for (int y = yO - 1; y > yD; y--) {
CrazyPiece thereIsPiece = Simulador.checkIfThereIsPiece(xO, y);
if (thereIsPiece != null){
return false;
}
}
}
return true;
}
thereIsPiece(int x, int y) is a function from the Chess Simulator class that, given a position on the board, returns the piece in that position.
As is obvious, these two receive the same parameters (origin coordinates and a destination coordinate, where one of the destination coordinates is one of the piece's origin coordinates), so the only thing that really changes is the way thereIsPiece() is called. EDIT: And because of this, they're marked as duplicates, and from what I've been told, that's very bad!
However, I can't seem to figure out a way to solve this problem using only one of these methods; ALSO AN EDIT: I've tried overloading it, but then it'd work only vertically or horizontally (may have done it wrong).
The thing is I need these to be done separately to implement the Horse's movement, that overrides these methods:
public boolean freeWayHorizontally(int xO, int yO, int xD) { //Overriden by the Horse class
//RIGHT
if (xO < xD) {
for (int x = xO + 1; x <= xD; x++) {
CrazyPiece thereIsPiece = Simulador.checkIfTheresPiece(x, yO);
if (thereIsPiece != null && thereIsPiece.isKing){
return false;
}
}
//LEFT
} else if (xO > xD) {
for (int x = xO - 1; x >= xD; x--) {
CrazyPiece thereIsPiece = Simulador.checkIfTheresPiece(x, yO);
if (thereIsPiece != null && thereIsPiece.isKing){
return false;
}
}
}
return true;
}
public boolean freeWayVertically(int xO, int yO, int yD) { //Overriden by the Horse class
//UP
if (yO < yD) {
for (int y = yO + 1; y <= yD; y++) {
CrazyPiece thereIsPiece = Simulador.checkIfTheresPiece(xO, y);
if (thereIsPiece != null && thereIsPiece.isKing){
return false;
}
}
//DOWN
} else if (yO > yD) {
for (int y = yO - 1; y >= yD; y--) {
CrazyPiece thereIsPiece = Simulador.checkIfThereIsPiece(xO, y);
if (thereIsPiece != null && thereIsPiece.isKing){
return false;
}
}
}
return true;
}
And then calls its own type of movement check, also defined in the Piece class:
public boolean freeWayL(int xO, int yO, int xD, int yD) {
boolean fH, fV;
//Horizontal -> Vertical
fH = this.freeWayHorizontally(xO, yO, xD);
if (fH) {
fV = this.freeWayVertically(xD, yO, yD);
if (fV) {
return true;
}
}
//Vertical -> Horizontal
fV = this.freeWayVertically(xO, yO, yD);
if (dV) {
fH = this.freeWayHorizontally(xO, yD, xD);
if (fH) {
return true;
}
}
return false;
}
What can I do to avoid all this duplication, or even to make these validations better?
The first issue with your code is that you have different method on Simulador class to check positions:
// When is checking horizontally
CrazyPiece thereIsPiece = Simulador.pegaPecaPorCoordenada(x, yO);
// When is checking vertically
CrazyPiece thereIsPiece = Simulador.checkIfThereIsPiece(xO, y);
I don't see a reason why this should not be a single method.
I can see 2 different point of improvement here:
rewrite the loop changing start and end in a way to have only one loop.
As an example:
public boolean freeWayHorizontally(int xO, int yO, int xD) {
int destination = xD;
int origin = xO + 1;
if (xO > xD) {
destination = xO - 1;
origin = xD;
}
for (int x = origin; x < destination; x++) {
CrazyPiece thereIsPiece = Simulador.pegaPecaPorCoordenada(x, yO);
if (thereIsPiece != null){
return false;
}
}
return true;
}
When you have the origin is before the destination, you move from origin to destination stight forward.
When the destination is before the origin, you just swap and move from destination to origin.
the second point is the check condition.
I think you should just add a method:
private boolean checkPositionIsFree(int x, int y) {
return Simulador.pegaPecaPorCoordenada(x, y) != null;
}
Now you need to have one for the horizontal and one for the vertical, until you can't merge the 2 methods.
And then you can rewrite your method like so:
public boolean freeWayHorizontally(int xO, int yO, int xD) {
int destination = xD;
int origin = xO + 1;
if (xO > xD) {
destination = xO - 1;
origin = xD;
}
for (int x = origin; x < destination; x++) {
if (checkPositionIsFree(x, yO)){
return false;
}
}
return true;
}
For the Horse, you just #Override the checkPositionIsFree() method with the proper condition (adding the check on the king), and everything should work.
Update
To cover completely the horse case you can have a method that work on the input data:
#Override
public boolean freeWayHorizontally(int xO, int yO, int xD) {
return super.freeWayHorizontally(xO, yO, xD + 1);
}
In such a way you can avoid to rewrite all the code.
By the way, your code here have some typos, maybe you rewrite it. It's better to check those kind of things, and have working code (in your case) or the exact code is failing, to avoid lost vouluntiers time following the wrong bug.

How do I print original point coordinates if I translate a point outside of a certain square?

I'm trying to print the coordinates of a translated point. If the point is translated outside of the 10, -10 range on both the x and y axes, then the original point should be returned and not the translated one. So, for p1 (5,4), if I were to translate it by (7,8), it should return (5,4). I'm having trouble figuring out how exactly to tell Java to print the original point in this case. Do I need to include a Boolean of some sort? Here's what I have so far. (The code is written in German, but "verschiebe" means translate.) Any help would be greatly appreciated! Thanks y'all in advance :)
public class Punkt2 {
private int x;
private int y;
public void setX (int i) {
x = i;
}
public void setY (int i) {
y = i;
}
public void verschiebe(int deltaX, int deltaY){
x = x + deltaX;
y = y + deltaY;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public static void main (String[] args){
Punkt p1 = new Punkt();
p1.setX(5); p1.setY(4);
Punkt p2 = new Punkt();
p2.setX(2);p2.setY(1);
p1.verschiebe(7,8);
if (p1.getX() > 10 || p1.getY() > 10 || p1.getX() < -10 || p1.getY() < -10){
System.out.println(new Punkt());
}
else {
System.out.println(p1.getX() + "," + p1.getY());
}
p2.verschiebe(3, 2);
if (p2.getX() > 10 || p2.getY() > 10 || p2.getX() < -10 || p2.getY() < -10){
System.out.println(new Punkt());
}
else {
System.out.println(p2.getX() + "," + p2.getY());
}
}
}
If you mean that you don't want the point's x or y value to be over 10 or below -10, you can just simply not change the values in the verschiebe method.
Like this:
public void verschiebe(int deltaX, int deltaY){
if(x + deltaX <= 10 && x + deltaX >= -10){
x = x + deltaX;
}
if(y + deltaY <= 10 && y + deltaY >= -10){
y = y + deltaY;
}
}
Then the point's x and y value will stay the same if they are about to get greater than 10 or lesser than -10. So now in the main class you can just print the point you want to and your verschiebe method will take care that your x and y will always be between -10 and 10.
The usual way of doing this would be to check the bounds inside of the translate() method.
For example:
private boolean outOfBounds(point, bounds) {
return (abs(point) > bounds);
}
public void translate(int deltaX, int deltaY) {
int newX = x + deltaX;
int newY = y + deltaY;
if(outOfBounds(newX, 10) || outOfBounds(newY, 10)) {
return;
}
x = newX;
y = newY;
}
Have a Punkt object be responsible for translating points by returning a "translated" point. It can return self when the translation results in point that is out of bounds.
class Punkt {
private int x;
private int y;
public Punkt(int x, int y) {
this.x = x;
this.y = y;
}
public Punkt verschiebe(int dX, int dY) {
int newX = x+dX;
int newY = y+dY;
if (abs(newX) > 10 || abs(newY) > 10) {
return this;
}
return new Punkt(newX, newY);
}
public void print() {
System.out.println(x + "," + y);
}
}
Would also advise that you have Punk print itself, so your clients can look as:
public static void main (String[] args){
Punkt p1 = new Punkt(5, 4);
Punkt p2 = new Punkt(2, 1);
p1.verschiebe(7,8).print();
p2.verschiebe(3,2).print();
}
Better still, if the client actually needs to print translated points the above code can be further improved to:
public static void main (String[] args){
Punkt p1 = new Punkt(5, 4);
Punkt p2 = new Punkt(2, 1);
p1.printTranslated(7,8);
p2.printTranslated(3,2);
}
It's not hard to have Punkt comply to client's needs.

Collision method gone crazy;

I am building a 2d tile based game, similiar to a Legend of Zelda game, and my collision method is not working properly. The method will return collisions when the player is far below an impassable tile. There must be a logic error I do not see here. This is in Java.
From static collision class:
public static boolean collisionAbovePlayer(TileGrid grid, float x, float y, int width, int height) {
boolean collision = false;
for (Tile[] row : grid.map) {
for (Tile t : row) {
// Each Tile t in map:
if (t.getType().getPassable() == false) {
// Each impassable tile:
if ( (x-t.getX() > 0 && x - t.getX() < t.getWidth()) || (x - t.getX() < 0 && Math.abs(x - t.getX()) < width )) {
// Tile is within collision x range
if ((t.getY() + t.getHeight() + 1) - y < Math.abs(5)) {
collision = true;
} else {
collision = false;
}
} else {
collision = false;
}
}
}
}
return collision;
}
This is being called by this method in my Player class (I haven't implemented collision methods for the other directions yet):
public void Update() {
if (first)
first = false;
else {
if ((Keyboard.isKeyDown(Keyboard.KEY_W)) && (!collisionAbovePlayer(grid, x, y, width, height))) {
y -= Delta() * speed;
}
if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
x -= Delta() * speed;
}
if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
y += Delta() * speed;
}
if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
x += Delta() * speed;
}
}
}
Let me know if you need to see more of the code, thanks for your time.
Fixed:
public static boolean collisionAbovePlayer(TileGrid grid, float x, float y, int width, int height) {
for (Tile[] row : grid.map) {
for (Tile t : row) {
// Each Tile t in map:
if (!t.getType().getPassable()) {
// Each impassable tile:
if ((x - t.getX() > 0 && x - t.getX() < t.getWidth()) || (x - t.getX() < 0 && Math.abs(x - t.getX()) < width )) {
// Tile is within collision x range
System.out.println("IN X RANGE");
if (Math.abs((t.getY() + t.getHeight() + 1) - y) < 2) {
return true;
}
}
}
}
}
return false;
}
In the original code it only returned the last tile that set 'boolean collision' to true, and the final statement checking the Y required Math.abs() around the first value.

Move tile by tile with dynamic speed

I'm using java with slick2d library and trying to move tile by tile with dynamic speed. I have tried a couple methods but none of them can move with dynamic speed between the tiles. Can someone help me with that and give some examples?
edit:
this two methods have I tried
move with out delta
movementSpeed = 2;
//decide direction
if(targetX != x)
{
animation.update(delta);
if(originalX < targetX)
x += movementSpeed;
else if(originalX > targetX)
x -= movementSpeed;
}
if(targetY != y)
{
animation.update(delta);
if(originalY < targetY)
y += movementSpeed;
else if(originalY > targetY)
y -= movementSpeed;
}
lerp
public static float lerp(float start, float stop, float t)
{
if (t < 0)
return start;
return start + t * (stop - start);
}
public void move(long delta)
{
if (procentMoved == 0)
{
if (getSpeed(targetX, targetY) != 0)
{
movementSpeed = getSpeed(targetX, targetY);
} else
{
targetX = originalX;
targetY = originalY;
}
}
if (procentMoved < 1)
{
animation.update(delta);
// movementSpeed = getSpeed(targetX, targetY);
procentMoved += movementSpeed;
} else if (procentMoved > 1)
{
animation.update(delta);
//TODO fix bouncing bug
procentMoved = 1;
}
+ movementSpeed);
x = lerp(originalX, targetX, procentMoved);
y = lerp(originalY, targetY, procentMoved);
if (x == targetX)
;
originalY = x;
if (y == targetY)
;
originalY = y;
}
It seems as if this could be your issue. Your if statements are just closing and not really doing its part. Also, you're variables are mixed up as well.
if (x == targetX)
; // This will skip the If statement
originalY = x;
if (y == targetY)
; // This will skip the If statement
originalY = y;
}
In all reality you're saying
orginalY = x; // Y = X?
orginalY = y; // Y = Y
Please do not take this to heart. I'm still having this issue as well, however I'm having to do some corrections and auto placements in order for this to work correctly.

Android/Java how to check if a Rectangle and a line segment intersect without line2d [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
in my game for Android I need to check for intersection between a rectangle and a line segment. I cannot use line2d as android does not support that. I have looked at similar questions dealing with lines and tried to modify them and failed. I also realize this Q/A which is basically what I want, however I have failed. For an example here is my code for a Line class that includes my attempt at intersection. The results have been some non-intersections returning true and some intersections returning false. EDIT: Oli Charlesworth helped me and here is working code for any googlers out there.
package com.example.HelloAndroid;
import android.graphics.Rect;
public class Segment {
int x1;
int y1;
int x2;
int y2;
double m;
double b;
boolean ishoriz;
boolean isvert;
public Segment(int x1s, int y1s, int x2s, int y2s) {
if (x1s > x2s) {
this.x1 = x2s;
this.x2 = x1s;
} else {
this.x1 = x1s;
this.x2 = x2s;
}
if (y1s > y2s) {
this.y1 = y2s;
this.y2 = y1s;
} else {
this.y1 = y1s;
this.y2 = y2s;
}
int ydif = y2s - y1s;
int xdif = x2s - x1s;
if (ydif == 0) {
this.ishoriz = true;
this.m = 0;
this.b = x1s;
} else if (xdif == 0) {
this.isvert = true;
} else {
this.m = (double) ydif / xdif;
double r = (double) ydif / xdif;
this.b = y1s - (r * x1s);
this.isvert = false;
this.ishoriz = false;
}
}
public final boolean intersected(Segment s, Segment s2) {
if (s.ishoriz && s2.ishoriz) {
//parallel
return false;
}
if (s.isvert && s2.isvert) {
//parallel
return false;
}
if (s.isvert) {
//x is constant see if the x is on the other line
int x = s.x1;
//add 2 for round-off error
if (s2.x1 <= x + 2 && s2.x2 + 2 >= x) {
//solve and check if y is on both segments
int y = (int) ((s.m * x) + s.b);
if(s.y1<=y+2&&s.y2+2>=y)
{
if(s2.y1<=y+2&&s2.y2+2>=y)
{
return true;}
}
}
return false;
}
if (s2.isvert) {
//x is constant see if the x is on the other line
int x = s2.x1;
//add 2 for round-off error
if (s.x1 <= x + 2 && s.x2 + 2 >= x) {
//solve and check if y is on both segments
int y = (int) ((s.m * x) + s.b);
if(s.y1<=y+2&&s.y2+2>=y)
{
if(s2.y1<=y+2&&s2.y2+2>=y)
{
return true;}
}
}
return false;
}
if (s.ishoriz) {
//y is constant see if the y is on the other line
int y = s.y1;
//add 2 for round-off error
if (s2.y1 <= y + 2 && s2.y2 + 2 >= y) {
//solve and check if x is on both segments
int x=(int) ((y-s.b)/s.m);
if(s.x1<=x+2&&s.x2+2>=x)
{
if(s2.x1<=x+2&&s2.x2+2>=x)
return true;}
return false;
}}
if (s2.ishoriz) {
//y is constant see if the y is on the other line
int y = s2.y1;
//add 2 for round-off error
if (s.y1 <= y + 2 && s.y2 + 2 >= y) {
//solve and check if x is on both segments
int x=(int) ((y-s.b)/s.m);
if(s.x1<=x+2&&s.x2+2>=x)
{
if(s2.x1<=x+2&&s2.x2+2>=x)
return true;}
}
return false;
}
if (s.m == s2.m) {
//parallel
return false;
}
// use substitution
// (s.m-s2.m)x=s2.b-s.b
int x = (int) (s.m - s2.m);
x = (int) ((s2.b - s.b) / x);
// find y
int y = (int) ((x * s.m) + s.b);
//check if the values are in between for both lines
//add 2 for round-off error
if (s.y1 <= y + 2) {
if (s.y2 + 2 >= y) {
if (s2.y1 <= y + 2) {
if (s2.y2 + 2 >= y) {
if (s.x1 <= x + 2) {
if (s.x2 + 2 >= x) {
if (s2.x1 <= x + 2) {
if (s2.x2 + 2 >= x) {
return true;
}
}
}
}
}
}
}
}
return false;
}
public final boolean intersects2(Segment s, Rect r) {
//created lines of the rect
Segment top = new Segment(r.left, r.top, r.right, r.top);
Segment left = new Segment(r.left, r.top, r.left, r.bottom);
Segment bottom = new Segment(r.left, r.bottom, r.right, r.bottom);
Segment right = new Segment(r.right, r.top, r.right, r.bottom);
boolean topp = s.intersected(s, top);
if (topp) {
return true;
}
boolean leftp = s.intersected(s, left);
if (leftp) {
return true;
}
boolean bottomp = s.intersected(s, bottom);
if (bottomp) {
return true;
}
boolean rightp = s.intersected(s, right);
if (rightp) {
return true;
} else {
return false;
}
}
}
It's possible that you're not initialising all your member variables correctly. In general, you should aim to make as many member variables final as possible, for (at least) two reasons:
the compiler will enforce that they must be initialised in the constructor.
the compiler will prevent you from overwriting them accidentally in a normal member function.
In other words, you should always aim to get the compiler to spot your bugs for you!

Categories

Resources