C
CelticFC
The following is code I have written for an Applet. The code is very
large and cumbersome (I am not that confident with Arrays as I am very
new to this).
However the applet works fine and does everything I want it to do
except for one crucial piece. I have tried over the last few days to
get this part working but with no success the following is the part
that does not work:
//This is the solution for the puzzle based on points within the grid
that pawns are placed
if(x >= 90 && x <= 130 && y >= 10 && y <= 50){
if(x >= 250 && x <= 290 && y >= 10 && y <= 50){
if(x >= 90 && x <= 130 && y >= 50 && y <= 90){
if(x >= 170 && x <= 210 && y >= 50 && y <= 90){
if(x >= 10 && x <= 50 && y >= 90 && y <= 130){
if(x >= 50 && x <= 90 && y >= 90 && y <= 130){
if(x >= 250 && x <= 290 && y >= 130 && y <= 170){
if(x >= 50 && x <= 90 && y >= 170 && y <= 210){
if(x >= 210 && x <= 250 && y >= 210 && y <= 250){
if(x >= 290 && x <= 350 && y >= 210 && y <= 250){
if(x >= 10 && x <= 50 && y >= 250 && y <= 290){
if(x >= 130 && x <= 170 && y >= 250 && y <= 290){
if(x >= 210 && x <= 250 && y >= 290 && y <= 350){
if(x >= 290 && x <= 350 && y >= 290 && y <= 350){
message = "Congratulations!";
message1 = "You solved it";
pawncount = 14;
}
}
}
}
}
}
}
}
}
}
}
}
}
}
Could anybody point me in the right direction or perhaps have a look at
the code and fix it. As I said it is not for lack of trying to get it
to work, I just lack the knowledge although I am learning every day
I would be very grateful for any help.
Here is the full code;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class MyPawns extends Applet implements
MouseListener,ActionListener,
MouseMotionListener{
public MyPawns(){
points = new ArrayList();
pawncount = 0;
pawncount1 = 14;
message = "";
message1 = "";
message2 = "";
title = new Font ("Arial", Font.BOLD, 18);
title1 = new Font ("Arial", Font.BOLD, 12);
}
private Point square1, square2, square3, square4, square5, square6,
square7,square8,square9,
square10,square11,square12,square13,square14,square15,square16,square17,square18,square19,
square20,square21,square22,square23,square24,square25,square26,square27,square28,square29,
square30,square31,square32,mouse;
private int select;
private Image pawn;
private Point icon;
private ArrayList points;
private int x;
private int y;
private int pawncount;
private int pawncount1,pawncount2;
private Button undoButton;
private String message,message1,message2;
private Font title;
private Font title1;
public void init(){
setBackground(new Color(210,255,210));
this.addMouseMotionListener(this);
this.addMouseListener(this);
select = 0;
square1=new Point(50,10);
square2=new Point(130,10);
square3=new Point(210,10);
square4=new Point(290,10);
square5=new Point(10,50);
square6=new Point(90,50);
square7=new Point(170,50);
square8=new Point(250,50);
square9=new Point(50,90);
square10=new Point(130,90);
square11=new Point(210,90);
square12=new Point(290,90);
square13=new Point(10,130);
square14=new Point(90,130);
square15=new Point(170,130);
square16=new Point(250,130);
square17=new Point(50,170);
square18=new Point(130,170);
square19=new Point(210,170);
square20=new Point(290,170);
square21=new Point(10,210);
square22=new Point(90,210);
square23=new Point(170,210);
square24=new Point(250,210);
square25=new Point(50,250);
square26=new Point(130,250);
square27=new Point(210,250);
square28=new Point(290,250);
square29=new Point(10,290);
square30=new Point(90,290);
square31=new Point(170,290);
square32=new Point(250,290);
undoButton = new Button("Undo");
add(undoButton);
undoButton.addActionListener(this);
mouse= new Point();
icon = new Point();
pawn = getImage(getCodeBase(), "pawn.gif");
}
//This allows the user to be able to undo any pawn placed wrongly on
the grid
//It also resets the pawncount by -1 for every pawn taken off
public void actionPerformed(ActionEvent actionevent){
if(actionevent.getSource() == undoButton){
int a = points.size();
int b = a - 1;
points.remove(b);
a = points.size();
if(a == 0){
pawncount = 0;
}
repaint();
}
}
public void paint (Graphics g){
drawBox(g);
g.fillRect(square1.x, square1.y,40,40);
g.fillRect(square2.x, square2.y,40,40);
g.fillRect(square3.x, square3.y,40,40);
g.fillRect(square4.x, square4.y,40,40);
g.fillRect(square5.x, square5.y,40,40);
g.fillRect(square6.x, square6.y,40,40);
g.fillRect(square7.x, square7.y,40,40);
g.fillRect(square8.x, square8.y,40,40);
g.fillRect(square9.x, square9.y,40,40);
g.fillRect(square10.x, square10.y,40,40);
g.fillRect(square11.x, square11.y,40,40);
g.fillRect(square12.x, square12.y,40,40);
g.fillRect(square13.x, square13.y,40,40);
g.fillRect(square14.x, square14.y,40,40);
g.fillRect(square15.x, square15.y,40,40);
g.fillRect(square16.x, square16.y,40,40);
g.fillRect(square17.x, square17.y,40,40);
g.fillRect(square18.x, square18.y,40,40);
g.fillRect(square19.x, square19.y,40,40);
g.fillRect(square20.x, square20.y,40,40);
g.fillRect(square21.x, square21.y,40,40);
g.fillRect(square22.x, square22.y,40,40);
g.fillRect(square23.x, square23.y,40,40);
g.fillRect(square24.x, square24.y,40,40);
g.fillRect(square25.x, square25.y,40,40);
g.fillRect(square26.x, square26.y,40,40);
g.fillRect(square27.x, square27.y,40,40);
g.fillRect(square28.x, square28.y,40,40);
g.fillRect(square29.x, square29.y,40,40);
g.fillRect(square30.x, square30.y,40,40);
g.fillRect(square31.x, square31.y,40,40);
g.fillRect(square32.x, square32.y,40,40);
g.setFont(title);
g.drawString(message, 350, 300);
g.drawString(message1, 350, 320);
g.drawString(message2, 350, 340);
for(int i = 0; i < points.size(); i++){
icon = (Point)points.get(i);
g.drawImage(pawn, icon.x, icon.y, 25, 25, this);
}
g.drawImage(pawn,175,135,25,25,this);
g.drawImage(pawn,135,175,25,25,this);
g.setFont(title);
g.drawString("The Puzzle", 350, 50);
g.setFont(title1);
g.drawString("Two pawns are placed on the centre of a
chessboard.",350,70);
g.drawString("Now place the remaining fourteen pawns (16 in
all)",350,90);
g.drawString("So that no three shall be in a straight line in
any",350,110);
g.drawString("possible direction.", 350, 130);
g.setFont(title);
g.drawString("Click A Square To Place A Pawn", 350, 170);
g.setFont(title1);
g.drawString("At any time during the game, you can click the
Undo.",350,190);
g.drawString("button to rectify any mistakes.",350,210);
}
//required for the interface
public void mouseDragged(MouseEvent e){}
public void mouseMoved(MouseEvent e){}
public void mouseClicked(MouseEvent event){}
public void mouseReleased(MouseEvent event){}
public void mouseEntered(MouseEvent event){}
public void mouseExited(MouseEvent event){}
//This ensures a user cannot place a pawn outwith the grid(board)
//It also starts the pawncount
public void mousePressed(MouseEvent mouseevent){
setBackground(new Color(210,255,210));
x = mouseevent.getX();
y = mouseevent.getY();
if(x > 19 && x < 312 && y > 19 && y < 312){
pawncount = pawncount + 1;
points.add(mouseevent.getPoint());
//Keeps a count of the number of pawns entered and returns error
message if pawncount > 14
if(pawncount > pawncount1){
setBackground(Color.red);
message = "Too Many Pawns!You can only place 14";
message1 = "Hit Reset to play again";
pawncount1 = 14;
}
//This ensures that no pawn can be placed on the same line as the
starting 2 pawns
//It will give an error message as well as turning the background
red(stop)
if(x >= 10 && x <= 50 && y >= 290 && y <= 330){
setBackground(Color.red);
message = "A Pawn Can't be placed here";
message1 = "This line already has 2 Pawns";
message2 = "Hit Reset to play again";
pawncount = 15;
}
if(x >= 50 && x <= 90 && y >= 250 && y <= 290){
setBackground(Color.red);
message = "A Pawn Can't be placed here";
message1 = "This line already has 2 Pawns";
message2 = "Hit Reset to play again";
pawncount = 15;
}
if(x >= 90 && x <= 130 && y >= 210 && y <= 250){
setBackground(Color.red);
message = "A Pawn Can't be placed here";
message1 = "This line already has 2 Pawns";
message2 = "Hit Reset to play again";
pawncount = 15;
}
if(x >= 210 && x <= 250 && y >= 90 && y <= 130){
setBackground(Color.red);
message = "A Pawn Can't be placed here";
message1 = "This line already has 2 Pawns";
message2 = "Hit Reset to play again";
pawncount = 15;
}
if(x >= 250 && x <= 290 && y >= 50 && y <= 90){
setBackground(Color.red);
message = "A Pawn Can't be placed here";
message1 = "This line already has 2 Pawns";
message2 = "Hit Reset to play again";
pawncount = 15;
}
if(x >= 290 && x <= 350 && y >= 10 && y <= 50){
setBackground(Color.red);
message = "A Pawn Can't be placed here";
message1 = "This line already has 2 Pawns";
message2 = "Hit Reset to play again";
pawncount = 15;
}
//This is the solution for the puzzle based on points within the grid
that pawns are placed
if(x >= 90 && x <= 130 && y >= 10 && y <= 50){
if(x >= 250 && x <= 290 && y >= 10 && y <= 50){
if(x >= 90 && x <= 130 && y >= 50 && y <= 90){
if(x >= 170 && x <= 210 && y >= 50 && y <= 90){
if(x >= 10 && x <= 50 && y >= 90 && y <= 130){
if(x >= 50 && x <= 90 && y >= 90 && y <= 130){
if(x >= 250 && x <= 290 && y >= 130 && y <= 170){
if(x >= 50 && x <= 90 && y >= 170 && y <= 210){
if(x >= 210 && x <= 250 && y >= 210 && y <= 250){
if(x >= 290 && x <= 350 && y >= 210 && y <= 250){
if(x >= 10 && x <= 50 && y >= 250 && y <= 290){
if(x >= 130 && x <= 170 && y >= 250 && y <= 290){
if(x >= 210 && x <= 250 && y >= 290 && y <= 350){
if(x >= 290 && x <= 350 && y >= 290 && y <= 350){
message = "Congratulations!";
message1 = "You solved it";
pawncount = 14;
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
repaint();
}
public void drawBox(Graphics g){
for(int i=10;i<=350;i+=40){
g.drawLine(i,10,i,330);
g.drawLine(10,i,330,i);
}
}
}
large and cumbersome (I am not that confident with Arrays as I am very
new to this).
However the applet works fine and does everything I want it to do
except for one crucial piece. I have tried over the last few days to
get this part working but with no success the following is the part
that does not work:
//This is the solution for the puzzle based on points within the grid
that pawns are placed
if(x >= 90 && x <= 130 && y >= 10 && y <= 50){
if(x >= 250 && x <= 290 && y >= 10 && y <= 50){
if(x >= 90 && x <= 130 && y >= 50 && y <= 90){
if(x >= 170 && x <= 210 && y >= 50 && y <= 90){
if(x >= 10 && x <= 50 && y >= 90 && y <= 130){
if(x >= 50 && x <= 90 && y >= 90 && y <= 130){
if(x >= 250 && x <= 290 && y >= 130 && y <= 170){
if(x >= 50 && x <= 90 && y >= 170 && y <= 210){
if(x >= 210 && x <= 250 && y >= 210 && y <= 250){
if(x >= 290 && x <= 350 && y >= 210 && y <= 250){
if(x >= 10 && x <= 50 && y >= 250 && y <= 290){
if(x >= 130 && x <= 170 && y >= 250 && y <= 290){
if(x >= 210 && x <= 250 && y >= 290 && y <= 350){
if(x >= 290 && x <= 350 && y >= 290 && y <= 350){
message = "Congratulations!";
message1 = "You solved it";
pawncount = 14;
}
}
}
}
}
}
}
}
}
}
}
}
}
}
Could anybody point me in the right direction or perhaps have a look at
the code and fix it. As I said it is not for lack of trying to get it
to work, I just lack the knowledge although I am learning every day
I would be very grateful for any help.
Here is the full code;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class MyPawns extends Applet implements
MouseListener,ActionListener,
MouseMotionListener{
public MyPawns(){
points = new ArrayList();
pawncount = 0;
pawncount1 = 14;
message = "";
message1 = "";
message2 = "";
title = new Font ("Arial", Font.BOLD, 18);
title1 = new Font ("Arial", Font.BOLD, 12);
}
private Point square1, square2, square3, square4, square5, square6,
square7,square8,square9,
square10,square11,square12,square13,square14,square15,square16,square17,square18,square19,
square20,square21,square22,square23,square24,square25,square26,square27,square28,square29,
square30,square31,square32,mouse;
private int select;
private Image pawn;
private Point icon;
private ArrayList points;
private int x;
private int y;
private int pawncount;
private int pawncount1,pawncount2;
private Button undoButton;
private String message,message1,message2;
private Font title;
private Font title1;
public void init(){
setBackground(new Color(210,255,210));
this.addMouseMotionListener(this);
this.addMouseListener(this);
select = 0;
square1=new Point(50,10);
square2=new Point(130,10);
square3=new Point(210,10);
square4=new Point(290,10);
square5=new Point(10,50);
square6=new Point(90,50);
square7=new Point(170,50);
square8=new Point(250,50);
square9=new Point(50,90);
square10=new Point(130,90);
square11=new Point(210,90);
square12=new Point(290,90);
square13=new Point(10,130);
square14=new Point(90,130);
square15=new Point(170,130);
square16=new Point(250,130);
square17=new Point(50,170);
square18=new Point(130,170);
square19=new Point(210,170);
square20=new Point(290,170);
square21=new Point(10,210);
square22=new Point(90,210);
square23=new Point(170,210);
square24=new Point(250,210);
square25=new Point(50,250);
square26=new Point(130,250);
square27=new Point(210,250);
square28=new Point(290,250);
square29=new Point(10,290);
square30=new Point(90,290);
square31=new Point(170,290);
square32=new Point(250,290);
undoButton = new Button("Undo");
add(undoButton);
undoButton.addActionListener(this);
mouse= new Point();
icon = new Point();
pawn = getImage(getCodeBase(), "pawn.gif");
}
//This allows the user to be able to undo any pawn placed wrongly on
the grid
//It also resets the pawncount by -1 for every pawn taken off
public void actionPerformed(ActionEvent actionevent){
if(actionevent.getSource() == undoButton){
int a = points.size();
int b = a - 1;
points.remove(b);
a = points.size();
if(a == 0){
pawncount = 0;
}
repaint();
}
}
public void paint (Graphics g){
drawBox(g);
g.fillRect(square1.x, square1.y,40,40);
g.fillRect(square2.x, square2.y,40,40);
g.fillRect(square3.x, square3.y,40,40);
g.fillRect(square4.x, square4.y,40,40);
g.fillRect(square5.x, square5.y,40,40);
g.fillRect(square6.x, square6.y,40,40);
g.fillRect(square7.x, square7.y,40,40);
g.fillRect(square8.x, square8.y,40,40);
g.fillRect(square9.x, square9.y,40,40);
g.fillRect(square10.x, square10.y,40,40);
g.fillRect(square11.x, square11.y,40,40);
g.fillRect(square12.x, square12.y,40,40);
g.fillRect(square13.x, square13.y,40,40);
g.fillRect(square14.x, square14.y,40,40);
g.fillRect(square15.x, square15.y,40,40);
g.fillRect(square16.x, square16.y,40,40);
g.fillRect(square17.x, square17.y,40,40);
g.fillRect(square18.x, square18.y,40,40);
g.fillRect(square19.x, square19.y,40,40);
g.fillRect(square20.x, square20.y,40,40);
g.fillRect(square21.x, square21.y,40,40);
g.fillRect(square22.x, square22.y,40,40);
g.fillRect(square23.x, square23.y,40,40);
g.fillRect(square24.x, square24.y,40,40);
g.fillRect(square25.x, square25.y,40,40);
g.fillRect(square26.x, square26.y,40,40);
g.fillRect(square27.x, square27.y,40,40);
g.fillRect(square28.x, square28.y,40,40);
g.fillRect(square29.x, square29.y,40,40);
g.fillRect(square30.x, square30.y,40,40);
g.fillRect(square31.x, square31.y,40,40);
g.fillRect(square32.x, square32.y,40,40);
g.setFont(title);
g.drawString(message, 350, 300);
g.drawString(message1, 350, 320);
g.drawString(message2, 350, 340);
for(int i = 0; i < points.size(); i++){
icon = (Point)points.get(i);
g.drawImage(pawn, icon.x, icon.y, 25, 25, this);
}
g.drawImage(pawn,175,135,25,25,this);
g.drawImage(pawn,135,175,25,25,this);
g.setFont(title);
g.drawString("The Puzzle", 350, 50);
g.setFont(title1);
g.drawString("Two pawns are placed on the centre of a
chessboard.",350,70);
g.drawString("Now place the remaining fourteen pawns (16 in
all)",350,90);
g.drawString("So that no three shall be in a straight line in
any",350,110);
g.drawString("possible direction.", 350, 130);
g.setFont(title);
g.drawString("Click A Square To Place A Pawn", 350, 170);
g.setFont(title1);
g.drawString("At any time during the game, you can click the
Undo.",350,190);
g.drawString("button to rectify any mistakes.",350,210);
}
//required for the interface
public void mouseDragged(MouseEvent e){}
public void mouseMoved(MouseEvent e){}
public void mouseClicked(MouseEvent event){}
public void mouseReleased(MouseEvent event){}
public void mouseEntered(MouseEvent event){}
public void mouseExited(MouseEvent event){}
//This ensures a user cannot place a pawn outwith the grid(board)
//It also starts the pawncount
public void mousePressed(MouseEvent mouseevent){
setBackground(new Color(210,255,210));
x = mouseevent.getX();
y = mouseevent.getY();
if(x > 19 && x < 312 && y > 19 && y < 312){
pawncount = pawncount + 1;
points.add(mouseevent.getPoint());
//Keeps a count of the number of pawns entered and returns error
message if pawncount > 14
if(pawncount > pawncount1){
setBackground(Color.red);
message = "Too Many Pawns!You can only place 14";
message1 = "Hit Reset to play again";
pawncount1 = 14;
}
//This ensures that no pawn can be placed on the same line as the
starting 2 pawns
//It will give an error message as well as turning the background
red(stop)
if(x >= 10 && x <= 50 && y >= 290 && y <= 330){
setBackground(Color.red);
message = "A Pawn Can't be placed here";
message1 = "This line already has 2 Pawns";
message2 = "Hit Reset to play again";
pawncount = 15;
}
if(x >= 50 && x <= 90 && y >= 250 && y <= 290){
setBackground(Color.red);
message = "A Pawn Can't be placed here";
message1 = "This line already has 2 Pawns";
message2 = "Hit Reset to play again";
pawncount = 15;
}
if(x >= 90 && x <= 130 && y >= 210 && y <= 250){
setBackground(Color.red);
message = "A Pawn Can't be placed here";
message1 = "This line already has 2 Pawns";
message2 = "Hit Reset to play again";
pawncount = 15;
}
if(x >= 210 && x <= 250 && y >= 90 && y <= 130){
setBackground(Color.red);
message = "A Pawn Can't be placed here";
message1 = "This line already has 2 Pawns";
message2 = "Hit Reset to play again";
pawncount = 15;
}
if(x >= 250 && x <= 290 && y >= 50 && y <= 90){
setBackground(Color.red);
message = "A Pawn Can't be placed here";
message1 = "This line already has 2 Pawns";
message2 = "Hit Reset to play again";
pawncount = 15;
}
if(x >= 290 && x <= 350 && y >= 10 && y <= 50){
setBackground(Color.red);
message = "A Pawn Can't be placed here";
message1 = "This line already has 2 Pawns";
message2 = "Hit Reset to play again";
pawncount = 15;
}
//This is the solution for the puzzle based on points within the grid
that pawns are placed
if(x >= 90 && x <= 130 && y >= 10 && y <= 50){
if(x >= 250 && x <= 290 && y >= 10 && y <= 50){
if(x >= 90 && x <= 130 && y >= 50 && y <= 90){
if(x >= 170 && x <= 210 && y >= 50 && y <= 90){
if(x >= 10 && x <= 50 && y >= 90 && y <= 130){
if(x >= 50 && x <= 90 && y >= 90 && y <= 130){
if(x >= 250 && x <= 290 && y >= 130 && y <= 170){
if(x >= 50 && x <= 90 && y >= 170 && y <= 210){
if(x >= 210 && x <= 250 && y >= 210 && y <= 250){
if(x >= 290 && x <= 350 && y >= 210 && y <= 250){
if(x >= 10 && x <= 50 && y >= 250 && y <= 290){
if(x >= 130 && x <= 170 && y >= 250 && y <= 290){
if(x >= 210 && x <= 250 && y >= 290 && y <= 350){
if(x >= 290 && x <= 350 && y >= 290 && y <= 350){
message = "Congratulations!";
message1 = "You solved it";
pawncount = 14;
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
repaint();
}
public void drawBox(Graphics g){
for(int i=10;i<=350;i+=40){
g.drawLine(i,10,i,330);
g.drawLine(10,i,330,i);
}
}
}