gpt4 book ai didi

java - 国际象棋、棋盘翻转?

转载 作者:行者123 更新时间:2023-12-01 13:42:40 26 4
gpt4 key购买 nike

我正在尝试用 Java 为我的游戏板(国际象棋)建立初始状态。但是,当我运行该程序时,板似乎被翻转了?如果您查看初始状态方法,这就是我希望初始板启动的方式:

public void initialBoardState(){
gameBoard = new int [][]
{{22,23,24,25,26,24,23,22},
{21,21,21,21,21,21,21,21}, //Black pieces
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{11,11,11,11,11,11,11,11}, //White pieces
{12,13,14,15,16,14,13,12}};

}

但是当我运行它时,板似乎像这样翻转:

gameBoard= new int[][]
{{22,21,0,0,0,0,11,12},
{23,21,0,0,0,11,13}

等等..有人能解释一下吗?整个类(class)发布如下:

    import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JComponent;


/**
* @author Cian
* This class will draw our game board
*/

class Board extends JComponent implements MouseListener{

private int gameBoard[][] = new int[8][8];
public int row = 8;
public int column = 8;
public int cell = 80;
public int rowSelected;
public int colSelected;
public int currentPlayer = 1;
int x; int y; int a; int b;

public Board(){
addMouseListener(this);
initialBoardState();
}

public void paintComponent(Graphics g){
super.paintComponents(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, 640, 640);
drawBoard(g2d);
}

public void drawBoard(Graphics2D g2d){
g2d.setColor(Color.black);

// draw the edge lines (0,0,0,640), (0,0,640,0), (0,640,640,640), (640,0,640,640)
g2d.drawLine(0, 0, 0, 639);
g2d.drawLine(0, 0, 639, 0);
g2d.drawLine(0, 639, 639, 639);
g2d.drawLine(639, 0, 639, 639);

// draw the horizontal lines using a loop from one to 7, coordiates of each line is (0, x*80, 640, x*80) also
// draw vertical lines with coordinates of (x*80, 0, x*80, 640)
for(int i = 1; i < 8; i++) {
g2d.drawLine(0, i*80, 640, i*80);
g2d.drawLine(i*80, 0, i*80, 640);
}

for ( int row = 0; row < 8; ++row ) // ++ means "increment by one"
// This inner loop counts over all the (white or black) squares in a row. It starts at 0 if row is even, or 1 if row is odd, and goes up to no more than 7 (so it will count 1-3-5-7 or 0-2-4-6
for ( int i = row % 2; i <= 7; i+= 2 ) { // += means "add to that variable
g2d.setColor(Color.BLACK);
g2d.fillRect ( getX() + cell*i, getY() + row*cell, cell, cell);
}

BufferedImage img_black = null;
BufferedImage castle_black = null;
BufferedImage castle_white = null;
BufferedImage img_white = null;
try {
img_black = ImageIO.read(new File("imgs/pawn-black.png"));
castle_black = ImageIO.read(new File("imgs/castle-black.png"));
castle_white = ImageIO.read(new File("imgs/castle-white.png"));
img_white = ImageIO.read(new File("imgs/pawn-white.png"));
} catch (IOException e) {
}

for(int a=0;a<8;a++){
for(int b=0;b<8;b++){
if(gameBoard[a][b] == 22){
g2d.drawImage(castle_black,a*80, b*80,null);
}
if(gameBoard[a][b] == 12){
g2d.drawImage(castle_white,a*80, b*80,null);
}
if(gameBoard[a][b] == 0){
g2d.drawImage(castle_white,a*80, b*80,null);
}

}

}

/*for(int i=0;i<8;i++){
for(int j=1;j<2;j++){
g2d.drawImage(img_black,i*80, j*80,null);
}
}


for(int i=0;i<8;i++){
for(int j=6;j<7;j++){
g2d.drawImage(img_white,i*80, j*80,null);
}
}*/
}

@Override
public void mouseClicked(MouseEvent event) {
// TODO Auto-generated method stub
}

@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub

}


public void mousePressed(MouseEvent event) {
x = event.getX(); y = event.getY();
rowSelected = x / cell;
colSelected = y / cell;
if (rowSelected >= 0 && rowSelected < row && colSelected >= 0 && colSelected < column){
System.out.println("X: "+rowSelected+" | Y: "+colSelected+" | GameBoard: "+gameBoard[rowSelected][colSelected]);
}
}

@Override
public void mouseReleased(MouseEvent event) {
a = event.getX(); //a = mouse released x coordinates
b = event.getY(); //b = mouse released y coordinates
//isValidMove(x,y,a,b);
}

/*private void isValidMove(int x, int y, int a, int b){
if(gameBoard[x][y]/10 == currentPlayer){
int piece = gameBoard[x][y]%10;
if(piece == 1){
System.out.println("1");
}
}
}*/

public void initialBoardState(){
gameBoard = new int [][]
{{22,23,24,25,26,24,23,22},
{21,21,21,21,21,21,21,21}, //Black pieces
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{11,11,11,11,11,11,11,11}, //White pieces
{12,13,14,15,16,14,13,12}};

}

}

最佳答案

从您描述问题的方式来看,您似乎只是输入了应该输入 y 坐标的 x 坐标,反之亦然。所以这只是一个疯狂的猜测,但请尝试更改类似的行:

g2d.drawImage(castle_black,a*80, b*80,null);

对此:

g2d.drawImage(castle_black,b*80, a*80,null);

此外,根据 this ,该方法被声明为 public abstract void drawImage(BufferedImage img, BufferedImageOp op, int x, int y) ,所以对我来说,你似乎没有很好地使用它,它实际上应该是这样的这个:

g2d.drawImage(castle_black, null, b*80, a*80);

但我可能是错的,因为我没有这方面的经验。

关于java - 国际象棋、棋盘翻转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20587785/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com