0% found this document useful (0 votes)
27 views

Boardchess

This document contains the code for a chess board game interface. It defines classes for the chess board panel, pieces and shapes. The BanCo class initializes the board grid and pieces arrays. It overrides the paint method to draw the board and pieces. GetPiece method returns the piece at a given position. Main method creates the frame and adds the board panel.

Uploaded by

Son Pham Hong
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Boardchess

This document contains the code for a chess board game interface. It defines classes for the chess board panel, pieces and shapes. The BanCo class initializes the board grid and pieces arrays. It overrides the paint method to draw the board and pieces. GetPiece method returns the piece at a given position. Main method creates the frame and adds the board panel.

Uploaded by

Son Pham Hong
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Main

/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this
license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package pack1 ;

/**
*
* @author Admin
*/
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.LinkedList;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class BanCo extends JPanel {

public int turnCounter = 0;


private static Image NULL_IMAGE = new BufferedImage(10, 10,
BufferedImage.TYPE_INT_ARGB);

private static final int BOARD_SIZE = 8;//có 8 ô


private static final int CELL_SIZE = 60;//mỗi ô rộng 60
//tạo mau cua tung ô
private static final Color otrang = Color.WHITE;
private static final Color oden = Color.darkGray;

public ArrayList<QuanCo> White_Pieces;


public ArrayList<QuanCo> Black_Pieces;

public ArrayList<DrawingShape> Static_Shapes;


public ArrayList<DrawingShape> Piece_Graphics;

public QuanCo Active_Piece;

///
private Integer[][] BoardGrid;
private ImageIcon icon;

/*
**
**
*/
public void initGrid()
{
for (int i = 0; i < BOARD_SIZE; i++)
{
for (int j = 0; j < BOARD_SIZE; j++)
{
BoardGrid[i][j] = 0;
}
};

White_Pieces.add(new Vua(3,0,true,new
ImageIcon("/ChessGame/src/image/vua_trang.png"),this));
White_Pieces.add(new Hau(4,0,true,new
ImageIcon("/ChessGame/src/image/hau_trang.png"),this));
White_Pieces.add(new Tuong(2,0,true,new
ImageIcon("/Users/phamhongson/Desktop/ChessGame/src/image/tuong_trang.png"),this));
White_Pieces.add(new Tuong(5,0,true,new
ImageIcon("/Users/phamhongson/Desktop/ChessGame/src/image/tuong_trang.png"),this));
White_Pieces.add(new Ma(1,0,true,new
ImageIcon("/Users/phamhongson/Desktop/ChessGame/src/image/ma_trang.png"),this));
White_Pieces.add(new Ma(6,0,true,new
ImageIcon("/Users/phamhongson/Desktop/ChessGame/src/image/ma_trang.png"),this));
White_Pieces.add(new Xe(0,0,true,new
ImageIcon("/Users/phamhongson/Desktop/ChessGame/src/image/xe_trang.png"),this));
White_Pieces.add(new Xe(7,0,true,new
ImageIcon("/Users/phamhongson/Desktop/ChessGame/src/image/xe_trang.png"),this));
White_Pieces.add(new Tot(0,1,true,new
ImageIcon("/Users/phamhongson/Desktop/ChessGame/src/image/tot_trang.png"),this));
White_Pieces.add(new Tot(1,1,true,new
ImageIcon("/Users/phamhongson/Desktop/ChessGame/src/image/tot_trang.png"),this));
White_Pieces.add(new Tot(2,1,true,new
ImageIcon("/Users/phamhongson/Desktop/ChessGame/src/image/tot_trang.png"),this));
White_Pieces.add(new Tot(3,1,true,new
ImageIcon("/Users/phamhongson/Desktop/ChessGame/src/image/tot_trang.png"),this));
White_Pieces.add(new Tot(4,1,true,new
ImageIcon("/Users/phamhongson/Desktop/ChessGame/src/image/tot_trang.png"),this));
White_Pieces.add(new Tot(5,1,true,new
ImageIcon("/Users/phamhongson/Desktop/ChessGame/src/image/tot_trang.png"),this));
White_Pieces.add(new Tot(6,1,true,new
ImageIcon("/Users/phamhongson/Desktop/ChessGame/src/image/tot_trang.png"),this));
White_Pieces.add(new Tot(7,1,true,new
ImageIcon("/Users/phamhongson/Desktop/ChessGame/src/image/tot_trang.png"),this));

Black_Pieces.add(new Vua(3,7,false,new
ImageIcon("/Users/phamhongson/Desktop/ChessGame/src/image/vua_den.png"),this));
Black_Pieces.add(new Hau(4,7,false,new
ImageIcon("/Users/phamhongson/Desktop/ChessGame/src/image/hau_den.png"),this));
Black_Pieces.add(new Tuong(2,7,false,new
ImageIcon("/Users/phamhongson/Desktop/ChessGame/src/image/tuong_den.png"),this));
Black_Pieces.add(new Tuong(5,7,false,new
ImageIcon("/Users/phamhongson/Desktop/ChessGame/src/image/tuong_den.png"),this));
Black_Pieces.add(new Ma(1,7,false,new
ImageIcon("/Users/phamhongson/Desktop/ChessGame/src/image/ma_den.png"),this));
Black_Pieces.add(new Ma(6,7,false,new
ImageIcon("/Users/phamhongson/Desktop/ChessGame/src/image/ma_den.png"),this));
Black_Pieces.add(new Xe(0,7,false,new
ImageIcon("/Users/phamhongson/Desktop/ChessGame/src/image/xe_den.png"),this));
Black_Pieces.add(new Xe(7,7,false,new
ImageIcon("/Users/phamhongson/Desktop/ChessGame/src/image/xe_den.png"),this));
Black_Pieces.add(new Tot(0,6,false,new
ImageIcon("/Users/phamhongson/Desktop/ChessGame/src/image/tot_den.png"),this));
Black_Pieces.add(new Tot(1,6,false,new
ImageIcon("/Users/phamhongson/Desktop/ChessGame/src/image/tot_den.png"),this));
Black_Pieces.add(new Tot(2,6,false,new
ImageIcon("/Users/phamhongson/Desktop/ChessGame/src/image/tot_den.png"),this));
Black_Pieces.add(new Tot(3,6,false,new
ImageIcon("/Users/phamhongson/Desktop/ChessGame/src/image/tot_den.png"),this));
Black_Pieces.add(new Tot(4,6,false,new
ImageIcon("/Users/phamhongson/Desktop/ChessGame/src/image/tot_den.png"),this));
Black_Pieces.add(new Tot(5,6,false,new
ImageIcon("/Users/phamhongson/Desktop/ChessGame/src/image/tot_den.png"),this));
Black_Pieces.add(new Tot(6,6,false,new
ImageIcon("/Users/phamhongson/Desktop/ChessGame/src/image/tot_den.png"),this));
Black_Pieces.add(new Tot(7,6,false,new
ImageIcon("/Users/phamhongson/Desktop/ChessGame/src/image/tot_den.png"),this));

public BanCo() {

BoardGrid = new Integer[BOARD_SIZE][BOARD_SIZE];


Static_Shapes = new ArrayList();
Piece_Graphics = new ArrayList();
White_Pieces = new ArrayList();
Black_Pieces = new ArrayList();

initGrid();

this.setBackground(new Color(37,13,84));
this.setPreferredSize(new Dimension(520, 520));
this.setMinimumSize(new Dimension(100, 100));
this.setMaximumSize(new Dimension(1000, 1000));

this.addMouseListener(mouseAdapter);
this.addComponentListener(componentAdapter);
this.addKeyListener(keyAdapter);

this.setVisible(true);
this.requestFocus();
drawBoard();
}
/**
*
*
*/
private void datquanco(Graphics g, int x, int y, ImageIcon icon) {
icon.paintIcon(this, g, x+10 , y+10 );//dung de dieu chinh co sao cho dep mat
}
@Override
public void paint(Graphics g) {//đối tượng Graphics được sử dụng để vẽ biểu tượng
super.paint(g);//được gọi để vẽ thành phần cơ bản nhất
for (int row = 0; row < BOARD_SIZE; row++) {
for (int col = 0; col < BOARD_SIZE; col++) {
//xác định các ô theo chiều ngang or dọc
int x = col * CELL_SIZE;
int y = row * CELL_SIZE;

Color color = (row+col) % 2 == 0 ? otrang : oden;


g.setColor(color);

//vẽ các ô hình chữ nhật với màu có sẵn


g.fillRect(x, y, CELL_SIZE, CELL_SIZE);

//quan den
if (row == 0 && col == 0) {
datquanco(g, x, y, xe_den_Icon); //phuong thuc tuy chinh duoc tao va truyen du lieu
vao
}
else if (row == 0 && col == 1){
datquanco(g, x, y, ma_den_Icon);
}
else if (row == 0 && col == 2){
datquanco(g, x, y, tuong_den_Icon);
}
else if (row == 0 && col == 3){
datquanco(g, x, y, hau_den_Icon);
}
else if (row == 0 && col == 4){
datquanco(g, x, y, vua_den_Icon);
}
else if (row == 0 && col == 5){
datquanco(g, x, y, tuong_den_Icon);
}
else if (row == 0 && col == 6){
datquanco(g, x, y, ma_den_Icon);
}
else if (row == 0 && col == 7){
datquanco(g, x, y, xe_den_Icon);
}
else if(row == 1){
datquanco(g, x, y, tot_den_Icon);
}
// quan trang
if (row == 7 && col == 0) {
datquanco(g, x, y, xe_trang_Icon);
}
else if (row == 7&& col == 1){
datquanco(g, x, y, ma_trang_Icon);
}
else if (row == 7 && col == 2){
datquanco(g, x, y, tuong_trang_Icon);
}
else if (row == 7 && col == 3) {
datquanco(g, x, y, hau_trang_Icon);
}
else if (row == 7&& col == 4){
datquanco(g, x, y, vua_trang_Icon);
}
else if (row == 7&& col == 5){
datquanco(g, x, y, tuong_trang_Icon);
}
else if (row == 7&& col == 6){
datquanco(g, x, y, ma_trang_Icon);
}
else if (row == 7&& col == 7){
datquanco(g, x, y, xe_trang_Icon);
}
else if (row == 6){
datquanco(g, x, y, tot_trang_Icon);
}

}
}

public QuanCo getPiece(int x, int y) {


for (QuanCo p : White_Pieces)
{
if (p.getX() == x && p.getY() == y)
{
return p;
}
}
for (QuanCo p : Black_Pieces)
{
if (p.getX() == x && p.getY() == y)
{
return p;
}
}
return null;
}
/*
@Override
public Dimension getPreferredSize() {
int boardWidth = BOARD_SIZE * CELL_SIZE;
int boardHeight = BOARD_SIZE * CELL_SIZE;
return new Dimension(boardWidth, boardHeight);
}

*/
public static void main(String[] args) {
JFrame frame = new JFrame("Chess Board");
BanCo chessBoard = new BanCo();
frame.add(chessBoard);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);

//lớp giao diện


interface DrawingShape {
boolean contains(Graphics2D g2, double x, double y);
void adjustPosition(double dx, double dy);
void draw(Graphics2D g2);
}
class DrawingImage implements DrawingShape {

public Image image;


public Rectangle2D rect;

public DrawingImage(Image image, Rectangle2D rect) {


this.image = image;
this.rect = rect;
}

@Override
public boolean contains(Graphics2D g2, double x, double y) {
return rect.contains(x, y);
}

@Override
public void adjustPosition(double dx, double dy) {
rect.setRect(rect.getX() + dx, rect.getY() + dy, rect.getWidth(), rect.getHeight());
}

@Override
public void draw(Graphics2D g2) {
Rectangle2D bounds = rect.getBounds2D();
g2.drawImage(image, (int)bounds.getMinX(), (int)bounds.getMinY(),
(int)bounds.getMaxX(), (int)bounds.getMaxY(),
0, 0, image.getWidth(null), image.getHeight(null), null);
}
}

Hậu
package pack1;

import javax.swing.ImageIcon;

public class Hau extends QuanCo {


public Hau(int x, int y, boolean is_white,ImageIcon icon, BanCo board)
{
super(x,y,is_white,icon, board);
}

@Override
public boolean canMove(int destination_x, int destination_y)
{
// Remember: A Queen can move as many squares as she wants forward,
// backward, sideways, or diagonally, without jumping over any pieces.
// She cannot attack her own pieces.

// WRITE CODE HERE


QuanCo possiblePiece = board.getPiece(destination_x , destination_y);

if(possiblePiece != null){

if(possiblePiece.isWhite() && this.isWhite()){

return false;
}
if(possiblePiece.isBlack() && this.isBlack()){

return false;
}
}

if((Math.abs(destination_x - this.getX()) != Math.abs(destination_y - this.getY())) &&


(this.getX()!= destination_x && this.getY()!= destination_y)){

return false;
}
return true;
}
}


package pack1;

import javax.swing.ImageIcon;
public class Ma extends QuanCo {

public Ma(int x, int y, boolean is_white, ImageIcon icon, BanCo board)


{
super(x,y,is_white, icon, board);
}

@Override
public boolean canMove(int destination_x, int destination_y)
{
// Remember: a knight can move in any L shape and can jump over anyone
// in order to do so. He cannot attack his own pieces.
// By an L shape, I mean it can move to a square that is 2 squares away
// horizontally and 1 square away vertically, or 1 square away horizontally
// and 2 squares away vertically.
// some examples:
//
// * * *** * *
// * * *** *
// * **

// WRITE CODE HERE


QuanCo possiblePiece = board.getPiece(destination_x , destination_y);

if(possiblePiece != null){

if(possiblePiece.isWhite() && this.isWhite()){

return false;
}
if(possiblePiece.isBlack() && this.isBlack()){

return false;
}
}
if(this.getX() == destination_x || this.getY() == destination_y){
return false;
}

int a = Math.abs(this.getX() - destination_x);


int b = Math.abs(this.getY() - destination_y);
if(a == 1 || a == 2 || b == 1 || b == 2){
if((a == 1) && (b != 2)){
return false;
}
if((a == 2) && (b != 1)){
return false;
}
if((b == 1) && (a != 2)){return false;}
if((b == 2) && (a != 1)){return false;}

}
else{
return false;
}

return true;
}
}

Quân cờ
package pack1;

import javax.swing.ImageIcon;

public class QuanCo {


private int x;
private int y;
final private boolean is_white;
private ImageIcon icon;
public BanCo board;

public QuanCo(int x, int y, boolean is_white, ImageIcon icon, BanCo board)


{
this.is_white = is_white;
this.x = x;
this.y = y;
this.icon = icon;
this.board = board;
}
public ImageIcon getFilePath()
{
return icon;
}

public void setFilePath(ImageIcon icon)


{
this.icon = icon;
}

public boolean isWhite()


{
return is_white;
}

public boolean isBlack()


{
return !is_white;
}

public void setX(int x)


{
this.x = x;
}

public void setY(int y)


{
this.y = y;
}

public int getX()


{
return x;
}

public int getY()


{
return y;
}

public boolean canMove(int destination_x, int destination_y)


{
return false;
}
}

tốt
package pack1;

import javax.swing.ImageIcon;

public class Tot extends QuanCo{

private boolean has_moved;

public Tot(int x, int y, boolean is_white,ImageIcon icon, BanCo board)


{
super(x,y,is_white,icon, board);
has_moved = false;
}

public void setHasMoved(boolean has_moved)


{
this.has_moved = has_moved;
}

public boolean getHasMoved()


{
return has_moved;
}

@Override
public boolean canMove(int destination_x, int destination_y)
{
// Remember: A pawn may only move towards the oponent's side of the board.
// If the pawn has not moved yet in the game, for its first move it can
// move two spaces forward. Otherwise, it may only move one space.
// When not attacking it may only move straight ahead.
// When attacking it may only move space diagonally forward
// WRITE CODE HERE

QuanCo possiblePiece = board.getPiece(destination_x , destination_y);

if(possiblePiece != null){

if(possiblePiece.isWhite() && this.isWhite()){

return false;
}
if(possiblePiece.isBlack() && this.isBlack()){

return false;
}
}

return true;
}
}

Vua
package pack1;

import javax.swing.ImageIcon;

public class Vua extends QuanCo {

public Vua(int x, int y, boolean is_white,ImageIcon icon, BanCo board)


{
super(x,y,is_white,icon, board);
}

@Override
public boolean canMove(int destination_x, int destination_y)
{
// Remember: a king can move one square up, right, left, or down, or
// diagonally, but he can never put himself in danger of an oposing
// piece attacking him on the next turn. He cannot attack his own pieces.

// WRITE CODE HERE

QuanCo possiblePiece = board.getPiece(destination_x , destination_y);

if(possiblePiece != null){

if(possiblePiece.isWhite() && this.isWhite()){

return false;
}
if(possiblePiece.isBlack() && this.isBlack()){

return false;
}
}
int a = Math.abs(this.getX() - destination_x);
int b = Math.abs(this.getY() - destination_y);

if(((a == 1) && (b == 1))){


return true;
}
else{return false;}

}
}

Tướng
package pack1;

import javax.swing.ImageIcon;

public class Tuong extends QuanCo {


public Tuong(int x, int y, boolean is_white, ImageIcon icon, BanCo board)
{
super(x,y,is_white,icon, board);
}

@Override
public boolean canMove(int destination_x, int destination_y)
{
// Remember: For attacking or just moving, a bishop is allowed to move
// as many squares diagonally as it wants without jumping over another
// piece. He cannot attack his own pieces.

// WRITE CODE HERE


QuanCo possiblePiece = board.getPiece(destination_x , destination_y);

if(possiblePiece != null){

if(possiblePiece.isWhite() && this.isWhite()){

return false;
}
if(possiblePiece.isBlack() && this.isBlack()){

return false;
}
}

if(Math.abs(destination_x - this.getX()) != Math.abs(destination_y - this.getY())){

return false;
}

return true;
}
}

Xe
package pack1;
import javax.swing.ImageIcon;

public class Xe extends QuanCo {

public Xe(int x, int y, boolean is_white, ImageIcon icon, BanCo board)


{
super(x,y,is_white,icon, board);
}

@Override
public boolean canMove(int destination_x, int destination_y)
{
// Remember: A rook can move as many squares as he wants either forward,
// backward, or sideways without jumping any pieces. He cannot attack
// his own pieces.

// WRITE CODE HERE

QuanCo possiblePiece = board.getPiece(destination_x , destination_y);

if(possiblePiece != null){

if(possiblePiece.isWhite() && this.isWhite()){

return false;
}
if(possiblePiece.isBlack() && this.isBlack()){

return false;
}
}

if(this.getX()!= destination_x && this.getY()!= destination_y){

return false;
}

//string

return true;
}
}

You might also like