# Thread: I need to make chess pieces visible and select two squares to make any pieces to move

1. Member
Join Date
Oct 2012
Location
Asia
Posts
2
Rep Power
0

## I need to make chess pieces visible and select two squares to make any pieces to move

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
//e.g. notation(41) ==> "B6"

public class Knight extends JPanel {
int dim = 8; //so that it fits the JPanel
int cellSize = 40;
public Knight() {//constructor for this
setPreferredSize(new Dimension(dim * cellSize,dim * cellSize));
}
public static String notation(int sq) {

int row = sq / 8;
int col = sq %8;
char letter =(char)(col + 'A');//conversion from digit into letter
char digit = (char) (row + '1');
String notation = "" + letter + digit;
return notation;
}
//e.g. square("B6") ==> 41
public static int square(String s) {
//conversion from letter to digit
char letter = s.charAt(0);
char digit = s.charAt(1);

int col = letter - 'A';
int row = digit -'1';
int sq = row * 8 + col;
return sq;
}

public static int[] knightMoves(int sq) {
int[] dRow = {-2, -2, -1, -1, 1, 1, 2, 2}; //two rows down,two rows down,one row down,one row up,one row up,one row up, two row up,two row up
int[] dCol = {-1, 1, -2, 2, -2, 2, -1, 1}; //one col to left,one col to right,two col to left,two col to right,two col to left,two col to right,one col to left,one col to right
int[] moves = new int[8]; //8 possible moves

//split squares into row and column
int row = sq/8; //
int col =sq%8; //remainder is the col no.
for (int i = 0; i < moves.length; i++) {
int row2 = row + dRow[i]; //new position from the previous one,row + -2,-2,-1,1,1,1,2,2
int col2 = col + dCol[i];
if (row2 < 0|| col2 < 0 || row2 > 7 ||col2 > 7) {//row2 smaller than 0,col2 smaller then 0,row2 bigger than 7,col2 bigger than 7
moves[i] = -1;

}
else {
int sq2 = row2 * 8 + col2;
moves[i] = sq2;
}
}
return selectNonNegativeNumbers(moves);//only select the positive numbers from the list of moves
}
static int[] selectNonNegativeNumbers(int[] list) {
int count = 0;
for (int i = 0; i < list.length; i++){
if (list[i] >= 0 ) {
count++;
// System.out.println(list[i]);
}
}

int[] nonNegative = new int[count]; //space for non negative numbers
int n = 0;
for (int i = 0; i < list.length; i++) {
if (list[i]>=0){
nonNegative[n] = list[i];//nonNegative zero asign list i
n++;
}
}
return nonNegative;
}

public static String findPath(String sq1, String sq2) {
for (int i = 0; i < 20; i++) {
String path = findPath("", square(sq1), square(sq2),i);
if (path != null) return path;
}
return null;
}

static String findPath(String path, int sq1, int sq2, int limit) {
if (sq1 == sq2) return path;//solved
else if (limit == 0) return null;//if no solution
else {
int[] nextSq = knightMoves(sq1);

for (int i = 0; i < nextSq.length; i++) {

String path2 = path + " " + notation(nextSq[i]);//square add to the new path
String finalPath = findPath(path2, nextSq[i], sq2, limit-1);
if(finalPath != null) return finalPath;//if there is solution,if there is path there, return the solution
}
return null;
}
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);//fresh new canvas
Graphics2D g2 = (Graphics2D)g;
drawChessboard(g2);

}
public static void main (String [] args) {
JFrame c = new JFrame();
Knight knight = new Knight();
c.setTitle("box");
c.pack();
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c.setVisible(true);
c.setResizable(true);
}

void drawChessboard (Graphics2D g) {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8;j++) {
Shape chessBoard= new Rectangle2D.Double (i*cellSize,j*cellSize,cellSize,cellSize);
if ( i % 2 == 0 && j % 2 == 0 || i % 2 == 1 && j % 2 == 1) {
g.setColor(Color.DARK_GRAY);
g.fillRect(i*cellSize,j*cellSize,cellSize,cellSize );
}
else {
g.setColor(Color.WHITE);
g.draw(chessBoard);
}
}
}
}
}

2. ## Re: I need to make chess pieces visible and select two squares to make any pieces to

Please go through http://www.java-forums.org/forum-gui...w-members.html and BB Code List - Java Programming Forum and edit your post accordingly.

db

3. ## Re: I need to make chess pieces visible and select two squares to make any pieces to

Also, please do not privately message users asking them this question. Please keep all technical discussion on the forum, that's what it's there for. Now we have no idea what help you've already received from people you've messaged, so we don't know if we're wasting our time helping you now.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•