0

I currently have to make a four square chess board and move a piece around the board in the directions up, down, left, right, right up diag, left up diag, right down diag and left down diag. I have the board and buttons laid out, but I'm confused as to moving the piece. I only need to move one piece. Another task confusing me if that I can't move right, how do I find that out?

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class javaAssignment extends JFrame implements ActionListener {
    JPanel top, bottom, panel1, panel2, panel3, panel4, buttons;
    JButton up, down, right, left, lud, rud, rdd, ldd;
    JLabel l1, l2, l3, l4;

    javaAssignment() {

        Container c = getContentPane();

        // Import image.

        Icon chess = new ImageIcon("images/piece.png");

        // Create panels.

        top = new JPanel();
        bottom = new JPanel();
        buttons = new JPanel();

        // Create chess board.

        l1 = new JLabel(chess);
        l2 = new JLabel();
        l3 = new JLabel();
        l4 = new JLabel();

        panel1 = new JPanel();
        panel1.setBackground(Color.black);
        panel1.setOpaque(true);
        panel1.setPreferredSize(new Dimension(90, 90));
        panel2 = new JPanel();
        panel2.setBackground(Color.white);
        panel2.setOpaque(true);
        panel2.setPreferredSize(new Dimension(90, 90));
        panel3 = new JPanel();
        panel3.setBackground(Color.white);
        panel3.setOpaque(true);
        panel3.setPreferredSize(new Dimension(90, 90));
        panel4 = new JPanel();
        panel4.setBackground(Color.black);
        panel4.setOpaque(true);
        panel4.setPreferredSize(new Dimension(90, 90));

        // Create buttons.

        up = new JButton("Up");
        down = new JButton("Down");
        right = new JButton("Right");
        left = new JButton("Left");
        lud = new JButton("Left Up Diag");
        ldd = new JButton("Left Down Diag");
        rud = new JButton("Right Up Diag");
        rdd = new JButton("Right Down Diag");

        // Add panels and buttons.

        panel1.add(l1);
        panel2.add(l2);
        panel3.add(l3);
        panel4.add(l4);
        top.add(panel1);
        top.add(panel2);
        bottom.add(panel3);
        bottom.add(panel4);
        buttons.add(up);
        buttons.add(down);
        buttons.add(left);
        buttons.add(right);
        buttons.add(lud);
        buttons.add(ldd);
        buttons.add(rud);
        buttons.add(rdd);
        c.add(top);
        c.add(bottom);
        c.add(buttons);

        // Set Layouts.

        top.setLayout(new GridLayout(2,3));
        bottom.setLayout(new GridLayout(2,3));
        buttons.setLayout(new GridLayout(2,3));
        c.setLayout(new FlowLayout());

        // Display frame.

        setVisible(true);
        setSize(600, 300);

    }

    public static void main(String[] args) {
        javaAssignment output = new javaAssignment();
    }

    public void actionPerformed(ActionEvent e) {

    }

}
user2978884
  • 47
  • 1
  • 10

1 Answers1

0

you could create a two dimension char array (char because you can use diffrend chars for differend figures) and set every char to 'e' where the chessfield is emtpy, otherwhise you have a 'k' for instance for king (just example).

with 2 for loops you can check for every field in the array if there is a "piece", so if there is a 'k' for king you can draw a king on that position (you get the position by

 x = i * square_size;
 y = j * square_size;

when you for vars are i and j).

when you want to move a piece, you have to check if the piece is still inside of your array bounds (you have to make the array as big as your chess field)

for more help i would need more details

Stefan
  • 528
  • 4
  • 12
  • 1
    Okay so if I make an array of [1][1] and start my piece at [0][0] which is the top left square of a 4 square board, then when somebody clicks a direction I go to my array and check if the move is available, example they cannot move to [2][2] because it is out of bounds. Am I picking this up right? – user2978884 Nov 11 '13 at 11:31