Boardchess
Boardchess
/*
* 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;
///
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() {
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;
//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 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);
@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;
@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.
if(possiblePiece != null){
return false;
}
if(possiblePiece.isBlack() && this.isBlack()){
return false;
}
}
return false;
}
return true;
}
}
Mã
package pack1;
import javax.swing.ImageIcon;
public class Ma extends QuanCo {
@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:
//
// * * *** * *
// * * *** *
// * **
if(possiblePiece != null){
return false;
}
if(possiblePiece.isBlack() && this.isBlack()){
return false;
}
}
if(this.getX() == destination_x || this.getY() == destination_y){
return false;
}
}
else{
return false;
}
return true;
}
}
Quân cờ
package pack1;
import javax.swing.ImageIcon;
tốt
package pack1;
import javax.swing.ImageIcon;
@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
if(possiblePiece != null){
return false;
}
if(possiblePiece.isBlack() && this.isBlack()){
return false;
}
}
return true;
}
}
Vua
package pack1;
import javax.swing.ImageIcon;
@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.
if(possiblePiece != null){
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);
}
}
Tướng
package pack1;
import javax.swing.ImageIcon;
@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.
if(possiblePiece != null){
return false;
}
if(possiblePiece.isBlack() && this.isBlack()){
return false;
}
}
return false;
}
return true;
}
}
Xe
package pack1;
import javax.swing.ImageIcon;
@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.
if(possiblePiece != null){
return false;
}
if(possiblePiece.isBlack() && this.isBlack()){
return false;
}
}
return false;
}
//string
return true;
}
}