目前我正在尝试让我的程序将变量xPos
和yPos
从ChessBoard
类传递到makeMove()
中的HumanPlayer
方法。
但目前它只是在HumanPlayer
中提供空指针异常。
这可以用BlockingQueues来完成,如果是这样,我做错了什么?
HumanPlayer
import java.util.*;
import java.util.concurrent.BlockingQueue;
public class HumanPlayer extends Player implements Runnable{
private Scanner in;
protected BlockingQueue<Integer> queue = null;
int x;
int y;
public HumanPlayer(String n, Pieces p, Board b, Player o, BlockingQueue<Integer> queue) {
super(n, p, b, o);
this.queue = queue;
}
@Override
public void run() {
try{
x = queue.take();
y = queue.take();
}
catch (InterruptedException e){
e.printStackTrace();
}
}
public boolean makeMove() {
// declare board and selected piece
Board chess = getBoard();
Piece selected;
//take input of piece player wants to move
// in = new Scanner(System.in);
// System.out.println("Select Piece: ");
// System.out.print("x: ");
// String inputX = in.nextLine().toUpperCase();
// System.out.print("y: ");
// String inputY = in.nextLine();
// System.out.println(inputX+inputY);
// //if one of the inputs is >1 length then return false to stop error
// if (inputY.length()!=1 || inputX.length() !=1)
// return false;
// //change inputs to numbers to use in array
// int x = (int)(inputX.charAt(0)-'A');
// int y = (int)(inputY.charAt(0)-'0')-1;
run();
// if the input is in range of the board array select that piece if not return false
if ( x<=7 && x>=0 && y<=7 && y>=0){
selected = chess.getPiece(x,y);
System.out.println(x+""+y);
}
else
return false;
// check the piece selected is not null and is the players own piece
if ((selected != null)
&& (getPieces().getPiece(0).getColour() == selected.getColour() )) {
}
else
return false;
// get the position the player wants to move the piece to
// System.out.println("Select Move: ");
// System.out.print("x: ");
// String moveX = in.nextLine().toUpperCase();
// System.out.print("y: ");
// String moveY = in.nextLine();
// System.out.println(moveX+moveY);
// check the inputs length are > 1 to stop errors
// if (moveY.length()!=1 || moveX.length() !=1)
// return false;
// //change inputs to ints for array
// int mX = (int)(moveX.charAt(0)-'A');
// int mY = (int)(moveY.charAt(0)-'0')-1;
run();
int mX = x;
int mY = y;
//check the inputs are not > 7 or less than 0
if ( mX>7 || mX<0 || mY>7 || mY<0) {
return false;
}
//declare boolean and move
boolean occupied = getBoard().occupied(mX, mY);
Move requested = new Move(selected,x,y,mX,mY,occupied);
//check if the move is and available move
if ( selected.availableMoves().contains(requested) ){
//delete the opponents piece if its taken
if (occupied){
Piece remove = chess.getPiece(mX,mY);
chess.remove(mX, mY);
getOpponent().getPieces().delete(remove);
}
// move the piece to desired position
selected.setPosition(mX, mY);
chess.setPosition(mX, mY, selected);
chess.remove(x, y);
return true;
}
//return false if not and available move
return false;
}
}
器ChessBoard
import java.awt.*;
import java.awt.event.*;
import java.util.concurrent.BlockingQueue;
import javax.swing.*;
@SuppressWarnings("serial")
public class ChessBoard extends JFrame implements ActionListener, Runnable{
JButton[][] squares;
private int xPos;
private int yPos;
protected BlockingQueue<Integer> queue = null;
public ChessBoard(BlockingQueue<Integer> queue){
this.queue = queue;
setSize(800,800);
setTitle("chess");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = getContentPane();
contentPane.setLayout(new GridLayout (8,8));
squares = new JButton[8][8];
Color lightBrown = new Color(222,184,135);
Color Brown = new Color(167,71,20);
for(int y=7; y>=0; y--){
for(int x=0; x<8; x++){
squares[x][y] = new JButton();
squares[x][y].addActionListener(this);
if ((x + y) % 2 == 0) {
squares[x][y].setBackground(Brown);
}
else {
squares[x][y].setBackground(lightBrown);
}
contentPane.add(squares[x][y]);
}
}
setVisible(true);
}
public JButton getButton(int x, int y){
return squares[x][y];
}
@Override
public void actionPerformed(ActionEvent e) {
JButton selected = (JButton) e.getSource();
for(int y=7; y>=0; y--){
for(int x=0; x<8; x++){
if( selected == squares[x][y]){
xPos = x;
yPos = y;
System.out.println(xPos+""+yPos);
}
}
}
}
public int getxPos() {
return xPos;
}
public int getyPos() {
return yPos;
}
@Override
public void run() {
try{
queue.put(xPos);
Thread.sleep(1000);
queue.put(yPos);
}
catch (InterruptedException e){
e.printStackTrace();
}
}
}