Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7574697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:29:53+00:00 2026-05-30T16:29:53+00:00

I’m working on making a board game for chess and checkers (and a few

  • 0

I’m working on making a board game for chess and checkers (and a few variations that I want to make). I’ve got a Board class that extends JPanel and sets up a doubly dimensioned array of JPanels to act as my board. Here’s some of the code for my board class:

public class Board extends JPanel {

  private static final int COLS = 8;
  private static final int ROWS = 8;

  private JPanel[][] board = new JPanel[COLS][ROWS];
  private JPanel chessBoard;

  public Board() {
    super();
    super.setLayout(new BorderLayout());

    chessBoard = new JPanel();
    chessBoard.setLayout(new GridLayout(COLS,ROWS));

    // Set up JPanels on bottom and right to display letters and numbers for the board
    // JPanels are called south and west
    super.add(chessBoard, BorderLayout.CENTER);
    super.add(south, BorderLayout.SOUTH);
    super.add(west, BorderLayout.WEST);

    for (int i=0; i<COLS; i++) {
      for (int j=0; j<ROWS; j++) {
        // Set up the grid
        board[i][j] = new JPanel();
        board[i][j].setBackground(getColor(i,j));
        chessBoard.add(board[i][j]);
      }
    }
    super.validate();
  }

  private Color getColor(int x, int y) {
    if ((x + y) % 2 == 0) {
      return Constants.GOLD;
    } else {
      return Constants.PURPLE;
    }
  }

  public void addPiece(Piece piece) {
    JLabel p = piece.getImage();

    board[piece.getX()][piece.getY()].add(p);
    chessBoard.validate();
  }
}

Piece is an interface that that I’m going to use for all my pieces. I’ve set up the interface and set up one class that implements the interface (Checker class). I have all of that set up. The pieces are JLabels with ImageIcons in them. The only problem I’m having so far is writing a move method. I can figure out the logic make sure a move is a valid one, I just don’t know how to actually make the move happen.

EDIT: I’m not even asking about mouse listeners or anything like that, I just want some pseudocode to explain making a piece move from one spot in the array to another.

EDIT 2: Here’s the code for my Checkers class.

public class Checker implements Piece {
  private int side,xPos,yPos; 
  private JLabel img;

  public Checker(int team, int x, int y) {
    BufferedImage image;

    try {
      if (team == 0)
        image = ImageIO.read(new File("img/RedPiece.png"));
      else
        image = ImageIO.read(new File("img/BlackPiece.png"));
    } catch(IOException e) {
      image = null;
      System.out.printf("Image file wasn't found!!!");
      System.exit(1);
    }

    img = new JLabel(new ImageIcon(image), SwingConstants.CENTER);
    img.setVerticalAlignment(SwingConstants.CENTER);

    xPos = x;
    yPos = y;
  }

  // TODO Figure out move method
  public void move(int dx, int dy) {

  }

  // Also typical gets and sets for instance variables

So my thought is I call the move method for a checkers piece and, assuming I’m moving from the bottom of the screen to the top it would be piece.move(-1,1); and I have to remove the piece from it’s current position, then it’s new position in the array is [x + dx][y + dy]

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T16:29:54+00:00Added an answer on May 30, 2026 at 4:29 pm

    Can you use something like this:

    public void movePiece(Piece piece, int dx, int dy) {
      // Save current position, so we can erase the piece.
      int oldX = piece.getX();
      int oldY = piece.getY();
    
      // Update the location.
      piece.setX(oldX + dx);
      piece.setY(oldY + dy);
    
      // Remove piece from old position
      board[oldX][oldY].clear();
    
      // Add it to the new position.
      addPiece(piece);
    }
    

    You’d probably want to use a Point instead of individual x and y coordinates; I just thought this would be easier to understand.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.