我一直在用Java编写一个跳棋游戏。我有GUI,用户可以点击移动部件并开始创建用户通过互联网连接并相互对战所需的代码。然而,当我试图玩游戏vs我自己只有1(服务器)显示它的板,另一个是空白。为什么会这样?我觉得它可能与屏幕上重叠的电路板有关。
大部分围绕移动规则的代码(任何涉及跳跃的部分)和所有服务器 - 客户端代码在此时都未经过测试,因为我还没有能够玩游戏。您可能还会注意到,有些问题尚未实施(有关国王和每回合1次运动)。我计划在启动并运行网络代码后添加这些内容。因此,如果有错误,你很高兴指出它们,但我现在的主要问题是让两个窗口同时运行。我将包括绘图代码和完整的CheckersServer代码来查看。 CheckersClient代码基本相同,除了围绕移动不同颜色部分的微小变化,它必须连接到服务器而不是等待连接。如果有人问我也可以发布客户端代码。非常感谢!
绘图代码
public static class Board
{
private JFrame boardFrame = new JFrame();//outside frame
private JPanel gameBoard = new JPanel(new GridLayout(9,9)); //actual game board
private JPanel backPanel = new JPanel(new BorderLayout()); //panel to hold game board and done button
private boardSquare selected; //piece currently selected to move
private boolean pieceSelected = false; //is a piece currently selected?
private Map<Position,boardSquare> grid = new HashMap<Position,boardSquare>(); //Map keying positions to squares on the board
private boolean serverTurn = true; //is it the Servers turn to move?
private BufferedWriter bw; //data to client
Board(Socket sock) throws IOException
{
boardFrame.setSize(1000,1000);
backPanel.setPreferredSize(new Dimension(1000, 1000));
boardFrame.add(backPanel);
backPanel.add(gameBoard, BorderLayout.WEST);
boardFrame.setTitle("Checkers");
boardFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
boardFrame.setVisible(true);
gameBoard.setVisible(true);
gameBoard.setPreferredSize(new Dimension(900, 650));
backPanel.setVisible(true);
JButton doneButton = new JButton("Done");
doneListener s = new doneListener();
doneButton.addActionListener(s);
doneButton.setBackground(Color.GREEN);
doneButton.setOpaque(true);
backPanel.add(doneButton,BorderLayout.EAST);
/*Then some loops for populating the gameBoard with objects of this type, see full code below for details if interested*/
private class boardSquare extends JComponent
{
private boolean isBlack = false; //is there a black chip on this square?
private boolean isRed = false; //is there a red chip on this square?
private boolean colored = false; //is the square itself colored?
private Position pos = null; //location of the square in the board
public boardSquare(String type, boolean c, Position p)
{
if (type.equals("Black"))
{
isBlack = true;
isRed = false;
}
else if (type.equals("Red"))
{
isRed = true;
isBlack = false;
}
else if (type.equals("Blank"))
{
isBlack = false;
isRed = false;
}
colored = c;
pos = p;
}
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
setPreferredSize(new Dimension(100, 100));
Rectangle box = new Rectangle(0,0,100,100);
g2.draw(box);
if(colored)
{
g2.setPaint(Color.BLUE);
g2.fill(box);
}
if(isBlack)
{
g2.setColor(Color.black);
g2.fillOval(13, 1,70 ,70 );
}
else if(isRed)
{
g2.setColor(Color.red);
g2.fillOval(13, 1,70 ,70);
}
}
完整代码
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.net.*;
import java.io.*;
public class CheckersServer
{
public static class Board
{
private JFrame boardFrame = new JFrame();//outside frame
private JPanel gameBoard = new JPanel(new GridLayout(9,9)); //actual game board
private JPanel backPanel = new JPanel(new BorderLayout()); //panel to hold game board and done button
private boardSquare selected; //piece currently selected to move
private boolean pieceSelected = false; //is a piece currently selected?
private Map<Position,boardSquare> grid = new HashMap<Position,boardSquare>(); //Map keying positions to squares on the board
private boolean serverTurn = true; //is it the Servers turn to move?
private BufferedWriter bw; //data to client
Board(Socket sock) throws IOException
{
try
{
bw = new BufferedWriter(new OutputStreamWriter( sock.getOutputStream() ));
}
catch(IOException e )
{
System.out.println("error: " + e );
}
boardFrame.setSize(1000,1000);
backPanel.setPreferredSize(new Dimension(1000, 1000));
boardFrame.add(backPanel);
backPanel.add(gameBoard, BorderLayout.WEST);
boardFrame.setTitle("Checkers");
boardFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
boardFrame.setVisible(true);
gameBoard.setVisible(true);
gameBoard.setPreferredSize(new Dimension(900, 650));
backPanel.setVisible(true);
JButton doneButton = new JButton("Done");
doneListener s = new doneListener();
doneButton.addActionListener(s);
doneButton.setBackground(Color.GREEN);
doneButton.setOpaque(true);
backPanel.add(doneButton,BorderLayout.EAST);
boardSquare bs;
String type = null;
boolean colored = true;
Position pos = null;
//Filling in Red Side
for (int i = 0; i < 2; i++)
{
for(int j = 0; j < 9; j++)
{
if(i == 0)
{
if(j % 2 == 0)
{
type = "Red";
colored = false;
}
else
{
type = "Blank";
colored = true;
}
}
else
{
if(j % 2 == 0)
{
type = "Blank";
colored = true;
}
else
{
type = "Red";
colored = false;
}
}
pos = new Position(i,j);
bs = new boardSquare(type, colored, pos);
squareListener listener = new squareListener();
bs.addMouseListener(listener);
grid.put(pos,bs);
gameBoard.add(bs);
}
}
//Filling in empty middle
type = "Blank";
for (int i = 2; i < 7; i++)
{
for(int j = 0; j < 9; j++)
{
if(i % 2 == 0)
{
if(j % 2 == 0)
{
colored = false;
}
else
{
colored = true;
}
}
else
{
if(j % 2 == 0)
{
colored = true;
}
else
{
colored = false;
}
}
pos = new Position(i,j);
bs = new boardSquare(type, colored, pos);
squareListener listener = new squareListener();
bs.addMouseListener(listener);
grid.put(pos,bs);
gameBoard.add(bs);
}
}
//Filling in Black side
for (int i = 7; i < 9; i++)
{
for(int j = 0; j < 9; j++)
{
if(i == 7)
{
if(j % 2 == 0)
{
type = "Blank";
colored = true;
}
else
{
type = "Black";
colored = false;
}
}
else
{
if(j % 2 == 0)
{
type = "Black";
colored = false;
}
else
{
type = "Blank";
colored = true;
}
}
pos = new Position(i,j);
bs = new boardSquare(type, colored, pos);
squareListener listener = new squareListener();
bs.addMouseListener(listener);
grid.put(pos,bs);
gameBoard.add(bs);
}
}
boardFrame.repaint();
}
private class Position
{
private int x;
private int y;
Position(int a, int b)
{
x = a;
y = b;
}
public int xCord()
{
return x;
}
public int yCord()
{
return y;
}
}
//this class represents the squares on the game board
private class boardSquare extends JComponent
{
private boolean isBlack = false; //is there a black chip on this square?
private boolean isRed = false; //is there a red chip on this square?
private boolean colored = false; //is the square itself colored?
private Position pos = null; //location of the square in the board
public boardSquare(String type, boolean c, Position p)
{
if (type.equals("Black"))
{
isBlack = true;
isRed = false;
}
else if (type.equals("Red"))
{
isRed = true;
isBlack = false;
}
else if (type.equals("Blank"))
{
isBlack = false;
isRed = false;
}
colored = c;
pos = p;
}
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
setPreferredSize(new Dimension(100, 100));
Rectangle box = new Rectangle(0,0,100,100);
g2.draw(box);
if(colored)
{
g2.setPaint(Color.BLUE);
g2.fill(box);
}
if(isBlack)
{
g2.setColor(Color.black);
g2.fillOval(13, 1,70 ,70 );
}
else if(isRed)
{
g2.setColor(Color.red);
g2.fillOval(13, 1,70 ,70);
}
}
public boolean isBlack()
{
return isBlack;
}
public boolean isRed()
{
return isRed;
}
public boolean isBlank()
{
if(isBlack || isRed)
{
return false;
}
return true;
}
public Position getPosition()
{
return pos;
}
public void setColor(String color)
{
if(color.equals("Black"))
{
isBlack = true;
isRed = false;
}
if(color.equals("Red"))
{
isBlack = false;
isRed = true;
}
if(color.equals("Blank"))
{
isBlack = false;
isRed = false;
}
}
}
//used to detect the mouse clicks used to move checker pieces
private class squareListener implements MouseListener
{
public void mousePressed(MouseEvent event) {}
// Called when a mouse button has been pressed on a component
public void mouseReleased(MouseEvent event) {}
// Called when a mouse button has been released on a component
public void mouseClicked(MouseEvent event)
// Called when the mouse has been clicked on a component
{
try{
if(serverTurn){
boardSquare square = (boardSquare)event.getComponent();
if(!pieceSelected) //picking a piece to move, server is black
{
if(square.isRed())
{
System.out.println();
System.out.println("Invalid Square");
}
else if(square.isBlank())
{
System.out.println();
System.out.println("Invalid Square");
}
else //chose a black piece to move
{
selected = square;
pieceSelected = true;
}
}
else //piece already picked, picking square to move to
{
if(!square.isBlank())//can't move to occupied square
{
System.out.println();
System.out.println("Invalid Square");
pieceSelected = false; //deselect piece in case of accidental selection of a surronded piece
}
else
{
Position piecePos = selected.getPosition();
Position destPos = square.getPosition();
int pieceXCord = piecePos.xCord();
int pieceYCord = piecePos.yCord();
int destXCord = destPos.xCord();
int destYCord = destPos.yCord();
int deltaX = pieceXCord - destXCord;
int deltaY = pieceYCord - destYCord;
//Moving up and to the right one diagional square
if(deltaX == 1 && deltaY == -1)
{
selected.setColor("Blank"); //leave old square
square.setColor("Black"); //move to new square
pieceSelected = false;
//Sending Client information so it can update it's board
bw.write(pieceXCord + "\n");
bw.write(pieceYCord + "\n");
bw.write(destXCord + "\n");
bw.write(destYCord + "\n");
bw.write(-1 + "\n"); //no piece was jumped use -1 to indicate this
bw.write(-1 + "\n");
}
else
{ //jumping a piece up and to the right
if(deltaX == 2 && deltaY == -2)
{
selected.setColor("Blank"); //leave old square
square.setColor("Black"); //move to new square
boardSquare captured = grid.get(new Position(pieceXCord-1,pieceYCord+1));
captured.setColor("Blank"); //remove captured piece
Position capturedPos = captured.getPosition();
pieceSelected = false;
bw.write(pieceXCord + "\n");
bw.write(pieceYCord + "\n");
bw.write(destXCord + "\n");
bw.write(destYCord + "\n");
bw.write(capturedPos.xCord() + "\n");
bw.write(capturedPos.yCord() + "\n");
}
else
{
//Moving up and to the left one diagional square
if(deltaX == 1 && deltaY == 1)
{
selected.setColor("Blank"); //leave old square
square.setColor("Black"); //move to new square
pieceSelected = false;
bw.write(pieceXCord + "\n");
bw.write(pieceYCord + "\n");
bw.write(destXCord + "\n");
bw.write(destYCord + "\n");
bw.write(-1 + "\n"); //no piece was jumped use -1 to indicate this
bw.write(-1 + "\n");
}
else
{
//jumping a piece up and to the left
if(deltaX == 2 && deltaY == 2)
{
selected.setColor("Blank"); //leave old square
square.setColor("Black"); //move to new square
boardSquare captured = grid.get(new Position(pieceXCord-1,pieceYCord-1));
captured.setColor("Blank"); //remove captured piece
Position capturedPos = captured.getPosition();
pieceSelected = false;
bw.write(pieceXCord + "\n");
bw.write(pieceYCord + "\n");
bw.write(destXCord + "\n");
bw.write(destYCord + "\n");
bw.write(capturedPos.xCord() + "\n");
bw.write(capturedPos.yCord() + "\n");
}
else //tried to move in an incorrect way
{
System.out.println();
System.out.println("Invalid Square");
pieceSelected = false;
}
}
}
}
}
}
boardFrame.repaint();
}
}
catch(IOException e)
{
System.out.println("error: " + e );
}
}
public void mouseEntered(MouseEvent event) {}
// Called when the mouse enters a component
public void mouseExited(MouseEvent event) {}
// Called when the mouse exits a component
}
//This class is attached to the DONE button and is used to detect the end of a turn
private class doneListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if(serverTurn)
{
serverTurn = false;
JButton done = (JButton)event.getSource();
done.setBackground(Color.YELLOW); //background is yellow means it's not server's turn
try
{
bw.write("DONE" + "\n"); //alert client that server is done.
}
catch (IOException e)
{
System.out.println("error: " + e );
}
}
}
}
//updates boardSquares given by indices to reflect the clients moves
public void update(int pieceXCord, int pieceYCord,int destXCord, int destYCord, int capturedXCord, int capturedYCord)
{
boardSquare pieceSquare = grid.get(new Position(pieceXCord,pieceYCord));
boardSquare destSquare = grid.get(new Position(destXCord,destYCord));
pieceSquare.setColor("Blank");
destSquare.setColor("Red");
if(capturedXCord > -1 && capturedYCord > -1)
{
boardSquare capturedSquare = grid.get(new Position(capturedXCord,capturedYCord));
capturedSquare.setColor("Blank");
}
boardFrame.repaint();
}
public void setTurn()
{
serverTurn = true;
}
}
public static void main(String[] args)
{
try
{
ServerSocket listen = new ServerSocket( 0 );
System.out.println("Server port is " + listen.getLocalPort() );
Socket client = listen.accept();
Board game = new Board(client);
/* data from client */
BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream() ));
String enemyMove = null;
int pieceXCord;
int pieceYCord;
int destXCord;
int destYCord;
int capturedXCord;
int capturedYCord;
while(true)
{
pieceXCord = -2; //using -2 to indcate failure
pieceYCord = -2;
destXCord = -2;
destYCord = -2;
capturedXCord = -2;
capturedYCord = -2;
enemyMove = br.readLine();
if(!enemyMove.equals("DONE"))
{
pieceXCord = Integer.parseInt(enemyMove);
}
enemyMove = br.readLine();
if(!enemyMove.equals("DONE"))
{
pieceYCord = Integer.parseInt(enemyMove);
}
enemyMove = br.readLine();
if(!enemyMove.equals("DONE"))
{
destXCord = Integer.parseInt(enemyMove);
}
enemyMove = br.readLine();
if(!enemyMove.equals("DONE"))
{
destYCord = Integer.parseInt(enemyMove);
}
enemyMove = br.readLine();
if(!enemyMove.equals("DONE"))
{
capturedXCord = Integer.parseInt(enemyMove);
}
enemyMove = br.readLine();
if(!enemyMove.equals("DONE"))
{
capturedYCord = Integer.parseInt(enemyMove);
}
if(pieceXCord != -2 && pieceYCord !=-2 && destXCord != -2 && destYCord != -2 && capturedXCord != -2 && capturedYCord != -2)
{
game.update(pieceXCord,pieceYCord,destXCord,destYCord,capturedXCord,capturedYCord);
}
if(enemyMove.equals("DONE"));
{
game.setTurn();
}
}
}
catch( IOException e )
{
System.out.println("error: " + e );
}
}
}