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 7522939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:39:16+00:00 2026-05-30T02:39:16+00:00

I am writing a simple chess-playing game. I won’t post it all here, but

  • 0

I am writing a simple chess-playing game. I won’t post it all here, but I’ll give you the necessary details.

I move by clicking a square with a piece on it, then the square becomes selected, and then clicking where I want to piece to move. Sometimes in chess, a move may fail to respond to check or create a check on one’s own king and is therefore illegal. The best way, I’ve found, to decide whether a move is illegal is to make a move on an “ifBoard” (a clone of the board) and if I deem the move legal, set the real board equal to the ifBoard.

Here is a code snippet of me responding to mouse clicks (board is the real board, destination is the clicked square, selectedSquare is the square previously selected(if not null))

    public void mousePressed(MouseEvent e){
            Square selectedSquare = board.selectedSquare();
            Square destination = board.getSquare(e.getX(), e.getY());
            board.deselect();
            if(destination == null){
                repaint();
                return;
            }

            if(selectedSquare == null){
                System.out.println("SelectedSquare is null");
                if(destination.occupiedByTeam(turn)){
                    System.out.println("destination is occupied by right team and is null");
                    board.select(destination);
                }
            }
            else{
                if(!selectedSquare.occupiedByTeam(turn)){
                    System.out.println("SelectedSquare not occupied by correct team");
                    repaint();
                    return;
                }

                if(destination.occupiedByTeam(turn)){
                    System.out.println("ChosenSquare occupied by same team");
                    board.select(destination);
                    repaint();
                    return;
                }

                //move on a dummy board and check for conflicts
                Board ifBoard = (Board)board.clone();

                System.out.println(ifBoard.toString());
                System.out.println(board.toString());
                //check if you can't move due to piece movement limitations
//.place() is a coordinate of the square on the tile system (A-H and 1-8)
                if(
                !ifBoard.move((int)selectedSquare.place().getX(), (int)selectedSquare.place().getY(), (int)destination.place().getX(), (int)destination.place().getY())
                ){
                    repaint();
                    return;
                }

                //if moving results in self-check
                if(ifBoard.check(turn)){
                    //don't move
                    repaint();
                    return;
                }
                else{
                    //move
                    System.out.println("Board moved!");
                    board = new Board(ifBoard);
                    cycleTurns();
                }
            }

            repaint();
        }

The toString calls are registering differently, but I have narrowed the problem down to the ifBoard.move() call actually moving the real board.

Here is the board class, or part of it.

import java.awt.Color;
import java.lang.Cloneable;
import java.awt.geom.*;
import java.awt.Graphics;
import java.awt.Graphics2D;

public class Board implements Cloneable{
    private Square[][] squares;

    private Rectangle2D squareWrap;
    private Rectangle2D boardBorder;

    private Square selectedSquare;

    public Board(){
        squares = new Square[8][8];
        for(int i = 0; i < 8; i++){
            for(int j = 0; j < 8; j++){
                squares[i][j] = new Square(new Point2D.Double(i, j));
            }
        }

        boardBorder = new Rectangle2D.Double(Constants.boardX,
                                             Constants.boardY,
                                             Constants.borderWidth * 2 + Constants.boardSide,
                                             Constants.borderHeight * 2 + Constants.boardSide);

        squareWrap = new Rectangle2D.Double(Constants.boardX + Constants.borderWidth,
                                            Constants.boardY + Constants.borderHeight,
                                            Constants.boardSide,
                                            Constants.boardSide);

        selectedSquare = null;
    }

    public Object clone() {
        Board obj = new Board();
        obj.setSquares(this.squares);
        obj.setSelectedSquare(this.selectedSquare);

        return obj;
    }...

Am I cloning incorrectly? Is there a better way? Thank you in advance.

  • 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-30T02:39:17+00:00Added an answer on May 30, 2026 at 2:39 am

    Am I cloning incorrectly? Is there a better way?

    The clone method should always start by calling super.clone() for reasons that I won’t go into in this post.

    Also, you’re not cloning the attributes of the object (you’re doing a shallow copy instead of a deep copy). Thus a cloned Board would share the same reference to the squares structure. (Changing the cloned Board would change the original Board.)

    (Many people argue that you should avoid clone and Cloneable all together though.)

    If I were you, I would strongly consider making the Board class immutable and perhaps go with some copy-on-write mechanism. That would save you a lot of headache I believe.

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

Sidebar

Related Questions

I've been recently writing simple game in C++ with SFML. Here's my question: In
I'm writing a simple I/O class in order to use at a multiplayer game.
Writing a simple program that will find exact duplicate files on my computer, but
I'm writing a simple shopping cart in PHP, but I'm not quite sure how
I am writing simple game on flex. In the game there are 2 objects
iam writing a simple firefox toolbar, but i have failed to set a few
I've been writing simple JSON schemas but I ran into an API input call
I'm working on writing simple unit tests for a Rails 3 project, but I'm
I'm writing a simple game with python, pygame and py2app. (I use python 2.6)
I am writing a simple checkers game in Java. When I mouse over the

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.