Results 1 to 3 of 3
Like Tree2Likes
  • 1 Post By DarrylBurke
  • 1 Post By KevinWorkman

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

  1. #1
    Sufiyana is offline Member
    Join Date
    Oct 2012
    Location
    Asia
    Posts
    2
    Rep Power
    0

    Question 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.add(knight);
    //f.add(box2);
    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. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: I need to make chess pieces visible and select two squares to make any pieces to

    Please go through Guide For New Members and BB Code List - Java Programming Forum and edit your post accordingly.

    db
    Sufiyana likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,879
    Rep Power
    8

    Default 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.
    Sufiyana likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Cut Java image into different pieces with an ImageBuffer
    By The original stinger in forum Java 2D
    Replies: 2
    Last Post: 04-30-2012, 03:46 PM
  2. How to make image invisible and visible in applet
    By KSBeyaz in forum Java Applets
    Replies: 15
    Last Post: 12-04-2011, 05:58 PM
  3. Battleship Game - Pieces lay on top of eachother.
    By lolerskates2695 in forum New To Java
    Replies: 7
    Last Post: 04-25-2011, 01:05 AM
  4. How can I make the graphics visible?
    By AcousticBruce in forum New To Java
    Replies: 9
    Last Post: 03-01-2011, 03:53 AM
  5. Triangle Areas - Please help before I smash my laptop into pieces!
    By Atia of the julii in forum New To Java
    Replies: 67
    Last Post: 11-10-2010, 05:40 PM

Posting Permissions

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