- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
嘿,目前我正在开发一款国际象棋游戏,但犯了一个错误,将所有 java 代码放在一个 java 文件中
我需要帮助将我的代码分割成更易于管理的部分
我希望有人能告诉我如何取出所有的棋子 Action 并将它们放入一个单独的文件中,然后链接回主文件?
我自己尝试过,但最终只是破坏了游戏
这是完整的凌乱的java文件,目前我只编码了一些部分,所以我想如果不解决这里可怕的架构,我不会进一步
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class ChessProject extends JFrame implements MouseListener, MouseMotionListener {
JLayeredPane layeredPane;
JPanel chessBoard;
JLabel chessPiece;
int xAdjustment;
int yAdjustment;
int startX;
int startY;
int initialX;
int initialY;
JPanel panels;
JLabel pieces;
public ChessProject() {
Dimension boardSize = new Dimension(600, 600);
// Use a Layered Pane for this application
layeredPane = new JLayeredPane();
getContentPane().add(layeredPane);
layeredPane.setPreferredSize(boardSize);
layeredPane.addMouseListener(this);
layeredPane.addMouseMotionListener(this);
//Add a chess board to the Layered Pane
chessBoard = new JPanel();
layeredPane.add(chessBoard, JLayeredPane.DEFAULT_LAYER);
chessBoard.setLayout(new GridLayout(8, 8));
chessBoard.setPreferredSize(boardSize);
chessBoard.setBounds(0, 0, boardSize.width, boardSize.height);
for (int i = 0; i < 64; i++) {
JPanel square = new JPanel(new BorderLayout());
chessBoard.add(square);
int row = (i / 8) % 2;
if (row == 0) {
square.setBackground(i % 2 == 0 ? Color.white : Color.gray);
} else {
square.setBackground(i % 2 == 0 ? Color.gray : Color.white);
}
}
// Setting up the Initial Chess board.
for (int i = 8; i < 16; i++) {
pieces = new JLabel(new ImageIcon("WhitePawn.png"));
panels = (JPanel) chessBoard.getComponent(i);
panels.add(pieces);
}
pieces = new JLabel(new ImageIcon("WhiteRook.png"));
panels = (JPanel) chessBoard.getComponent(0);
panels.add(pieces);
pieces = new JLabel(new ImageIcon("WhiteKnight.png"));
panels = (JPanel) chessBoard.getComponent(1);
panels.add(pieces);
pieces = new JLabel(new ImageIcon("WhiteKnight.png"));
panels = (JPanel) chessBoard.getComponent(6);
panels.add(pieces);
pieces = new JLabel(new ImageIcon("WhiteBishup.png"));
panels = (JPanel) chessBoard.getComponent(2);
panels.add(pieces);
pieces = new JLabel(new ImageIcon("WhiteBishup.png"));
panels = (JPanel) chessBoard.getComponent(5);
panels.add(pieces);
pieces = new JLabel(new ImageIcon("WhiteKing.png"));
panels = (JPanel) chessBoard.getComponent(3);
panels.add(pieces);
pieces = new JLabel(new ImageIcon("WhiteQueen.png"));
panels = (JPanel) chessBoard.getComponent(4);
panels.add(pieces);
pieces = new JLabel(new ImageIcon("WhiteRook.png"));
panels = (JPanel) chessBoard.getComponent(7);
panels.add(pieces);
for (int i = 48; i < 56; i++) {
pieces = new JLabel(new ImageIcon("BlackPawn.png"));
panels = (JPanel) chessBoard.getComponent(i);
panels.add(pieces);
}
pieces = new JLabel(new ImageIcon("BlackRook.png"));
panels = (JPanel) chessBoard.getComponent(56);
panels.add(pieces);
pieces = new JLabel(new ImageIcon("BlackKnight.png"));
panels = (JPanel) chessBoard.getComponent(57);
panels.add(pieces);
pieces = new JLabel(new ImageIcon("BlackKnight.png"));
panels = (JPanel) chessBoard.getComponent(62);
panels.add(pieces);
pieces = new JLabel(new ImageIcon("BlackBishup.png"));
panels = (JPanel) chessBoard.getComponent(58);
panels.add(pieces);
pieces = new JLabel(new ImageIcon("BlackBishup.png"));
panels = (JPanel) chessBoard.getComponent(61);
panels.add(pieces);
pieces = new JLabel(new ImageIcon("BlackKing.png"));
panels = (JPanel) chessBoard.getComponent(59);
panels.add(pieces);
pieces = new JLabel(new ImageIcon("BlackQueen.png"));
panels = (JPanel) chessBoard.getComponent(60);
panels.add(pieces);
pieces = new JLabel(new ImageIcon("BlackRook.png"));
panels = (JPanel) chessBoard.getComponent(63);
panels.add(pieces);
}
private Boolean piecePresent(int x, int y) {
Component c = chessBoard.findComponentAt(x, y);
if (c instanceof JPanel) {
return false;
} else {
return true;
}
}
//Check if a piece is a Black piece.
private Boolean checkWhiteOponent(int newX, int newY) {
Boolean oponent;
Component c1 = chessBoard.findComponentAt(newX, newY);
JLabel awaitingPiece = (JLabel) c1;
String tmp1 = awaitingPiece.getIcon().toString();
if (((tmp1.contains("Black")))) {
oponent = true;
} else {
oponent = false;
}
return oponent;
}
//Check if a piece is a White piece.
private Boolean checkBlackOponent(int newX, int newY) {
Boolean oponent;
Component c1 = chessBoard.findComponentAt(newX, newY);
JLabel awaitingPiece = (JLabel) c1;
String tmp1 = awaitingPiece.getIcon().toString();
if (((tmp1.contains("White")))) {
oponent = true;
} else {
oponent = false;
}
return oponent;
}
public void mousePressed(MouseEvent e) {
chessPiece = null;
Component c = chessBoard.findComponentAt(e.getX(), e.getY());
if (c instanceof JPanel) {
return;
}
Point parentLocation = c.getParent().getLocation();
xAdjustment = parentLocation.x - e.getX();
yAdjustment = parentLocation.y - e.getY();
chessPiece = (JLabel) c;
initialX = e.getX();
initialY = e.getY();
startX = (e.getX() / 75);
startY = (e.getY() / 75);
chessPiece.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
chessPiece.setSize(chessPiece.getWidth(), chessPiece.getHeight());
layeredPane.add(chessPiece, JLayeredPane.DRAG_LAYER);
}
public void mouseDragged(MouseEvent me) {
if (chessPiece == null) {
return;
}
chessPiece.setLocation(me.getX() + xAdjustment, me.getY() + yAdjustment);
}
public void mouseReleased(MouseEvent e) {
if (chessPiece == null) {
return;
}
chessPiece.setVisible(false);
Boolean success = false;
Component c = chessBoard.findComponentAt(e.getX(), e.getY());
String tmp = chessPiece.getIcon().toString();
String pieceName = tmp.substring(0, (tmp.length() - 4));
Boolean validMove = false;
//Pawn Moves
//White Pawn
if (pieceName.equals("WhitePawn")) {
if (startY == 1) {
if ((startX == (e.getX() / 75)) && ((((e.getY() / 75) - startY) == 1) || ((e.getY() / 75) - startY) == 2)) {
if ((((e.getY() / 75) - startY) == 2)) {
if ((!piecePresent(e.getX(), (e.getY()))) && (!piecePresent(e.getX(), (e.getY() + 75)))) {
validMove = true;
} else {
validMove = false;
}
} else {
if ((!piecePresent(e.getX(), (e.getY())))) {
validMove = true;
} else {
validMove = false;
}
}
} else {
validMove = false;
}
} else {
int newY = e.getY() / 75;
int newX = e.getX() / 75;
if ((startX - 1 >= 0) || (startX + 1 <= 7)) {
if ((piecePresent(e.getX(), (e.getY()))) && ((((newX == (startX + 1) && (startX + 1 <= 7))) || ((newX == (startX - 1)) && (startX - 1 >= 0))))) {
if (checkWhiteOponent(e.getX(), e.getY())) {
validMove = true;
if (startY == 6) {
success = true;
}
} else {
validMove = false;
}
} else {
if (!piecePresent(e.getX(), (e.getY()))) {
if ((startX == (e.getX() / 75)) && ((e.getY() / 75) - startY) == 1) {
if (startY == 6) {
success = true;
}
validMove = true;
} else {
validMove = false;
}
} else {
validMove = false;
}
}
} else {
validMove = false;
}
}
}
//Black Pawn
if (pieceName.equals("BlackPawn")) {
if (startY == 6) {
if ((startX == (e.getX() / 75)) && ((((e.getY() / 75) - startY) == -1) || ((e.getY() / 75) - startY) == -2)) {
if ((((e.getY() / 75) - startY) == -2)) {
if ((!piecePresent(e.getX(), (e.getY()))) && (!piecePresent(e.getX(), (e.getY() + 75)))) {
validMove = true;
} else {
validMove = false;
}
} else {
if ((!piecePresent(e.getX(), (e.getY())))) {
validMove = true;
} else {
validMove = false;
}
}
} else {
validMove = false;
}
} else {
int newY = e.getY() / 75;
int newX = e.getX() / 75;
if ((startX - 1 >= 0) || (startX + 1 <= 7)) {
if ((piecePresent(e.getX(), (e.getY()))) && ((((newX == (startX + 1) && (startX + 1 <= 7))) || ((newX == (startX - 1)) && (startX - 1 >= 0))))) {
if (checkBlackOponent(e.getX(), e.getY())) {
validMove = true;
if (startY == 1) {
success = true;
}
} else {
validMove = false;
}
} else {
if (!piecePresent(e.getX(), (e.getY()))) {
if ((startX == (e.getX() / 75)) && ((e.getY() / 75) - startY) == -1) {
if (startY == 2) {
success = true;
}
validMove = true;
} else {
validMove = false;
}
} else {
validMove = false;
}
}
} else {
validMove = false;
}
}
}
//End of Pawn Moves
//Knight Moves
//White Knight Code
else if (pieceName.contains("WhiteKnight")) {
// next we need to get the new coordinates for where the piece is being dropped.
int newY = e.getY() / 75;
int newX = e.getX() / 75;
// We need to make sure that the piece is being put back on the board...if its not being on
// the board why would we want to check anything else!
if (((newX < 0) || (newX > 7)) || ((newY < 0) || (newY > 7))) {
validMove = false;
} else {
if (((newX == startX + 1) && (newY == startY + 2)) || ((newX == startX - 1) && (newY == startY + 2)) || ((newX == startX + 2) && (newY == startY + 1)) || ((newX == startX - 2) && (newY == startY + 1)) || ((newX == startX + 1) && (newY == startY - 2)) || ((newX == startX - 1) && (newY == startY - 2)) || ((newX == startX + 2) && (newY == startY - 1)) || ((newX == startX - 2) && (newY == startY - 1))) {
validMove = true;
if (piecePresent(e.getX(), (e.getY()))) {
if (pieceName.contains("White")) {
if (checkWhiteOponent(e.getX(), e.getY())) {
validMove = true;
} else {
validMove = false;
}
} else {
if (checkBlackOponent(e.getX(), e.getY())) {
validMove = true;
} else {
validMove = false;
}
}
}
} else {
validMove = false;
}
}
} //Black Knight Code
else if (pieceName.contains("BlackKnight")) {
// next we need to get the new coordinates for where the piece is being dropped.
int newY = e.getY() / 75;
int newX = e.getX() / 75;
// We need to make sure that the piece is being put back on the board...if its not being on
// the board why would we want to check anything else!
if (((newX < 0) || (newX > 7)) || ((newY < 0) || (newY > 7))) {
validMove = false;
} else {
if (((newX == startX + 1) && (newY == startY + 2)) || ((newX == startX - 1) && (newY == startY + 2)) || ((newX == startX + 2) && (newY == startY + 1)) || ((newX == startX - 2) && (newY == startY + 1)) || ((newX == startX + 1) && (newY == startY - 2)) || ((newX == startX - 1) && (newY == startY - 2)) || ((newX == startX + 2) && (newY == startY - 1)) || ((newX == startX - 2) && (newY == startY - 1))) {
validMove = true;
if (piecePresent(e.getX(), (e.getY()))) {
if (pieceName.contains("Black")) {
if (checkBlackOponent(e.getX(), e.getY())) {
validMove = true;
} else {
validMove = false;
}
} else {
if (checkWhiteOponent(e.getX(), e.getY())) {
validMove = true;
} else {
validMove = false;
}
}
}
} else {
validMove = false;
}
}
}
//End of Knight Code
//Bishop Code
//White Bishup
else if (pieceName.contains("WhiteBishup")) {
int newY = e.getY() / 75;
int newX = e.getX() / 75;
boolean inTheWay = false;
int distance = Math.abs(startX - newX);
if (((newX < 0) || (newX > 7)) || ((newY < 0) || (newY > 7))) {
validMove = false;
} else {
validMove = true;
if (Math.abs(startX - newX) == Math.abs(startY - newY)) {
if ((startX - newX < 0) && (startY - newY < 0)) {
for (int i = 0; i < distance; i++) {
if (piecePresent((initialX + (i * 75)), (initialY + (i * 75)))) {
inTheWay = true;
}
}
} else if ((startX - newX < 0) && (startY - newY > 0)) {
for (int i = 0; i < distance; i++) {
if (piecePresent((initialX + (i * 75)), (initialY - (i * 75)))) {
inTheWay = true;
}
}
} else if ((startX - newX > 0) && (startY - newY > 0)) {
for (int i = 0; i < distance; i++) {
if (piecePresent((initialX - (i * 75)), (initialY - (i * 75)))) {
inTheWay = true;
}
}
} else if ((startX - newX > 0) && (startY - newY < 0)) {
for (int i = 0; i < distance; i++) {
if (piecePresent((initialX - (i * 75)), (initialY + (i * 75)))) {
inTheWay = true;
}
}
}
if (inTheWay) {
validMove = false;
} else {
if (piecePresent(e.getX(), (e.getY()))) {
if (pieceName.contains("White")) {
if (checkWhiteOponent(e.getX(), e.getY())) {
validMove = true;
} else {
validMove = false;
}
} else {
if (checkBlackOponent(e.getX(), e.getY())) {
validMove = true;
} else {
validMove = false;
}
}
} else {
validMove = true;
}
}
} else { // the move that is being tried is not a diagonal move...
validMove = false;
}
}
} //Black Bishup
else if (pieceName.contains("BlackBishup")) {
int newY = e.getY() / 75;
int newX = e.getX() / 75;
boolean inTheWay = false;
int distance = Math.abs(startX - newX);
if (((newX < 0) || (newX > 7)) || ((newY < 0) || (newY > 7))) {
validMove = false;
} else {
validMove = true;
if (Math.abs(startX - newX) == Math.abs(startY - newY)) {
if ((startX - newX < 0) && (startY - newY < 0)) {
for (int i = 0; i < distance; i++) {
if (piecePresent((initialX + (i * 75)), (initialY + (i * 75)))) {
inTheWay = true;
}
}
} else if ((startX - newX < 0) && (startY - newY > 0)) {
for (int i = 0; i < distance; i++) {
if (piecePresent((initialX + (i * 75)), (initialY - (i * 75)))) {
inTheWay = true;
}
}
} else if ((startX - newX > 0) && (startY - newY > 0)) {
for (int i = 0; i < distance; i++) {
if (piecePresent((initialX - (i * 75)), (initialY - (i * 75)))) {
inTheWay = true;
}
}
} else if ((startX - newX > 0) && (startY - newY < 0)) {
for (int i = 0; i < distance; i++) {
if (piecePresent((initialX - (i * 75)), (initialY + (i * 75)))) {
inTheWay = true;
}
}
}
if (inTheWay) {
validMove = false;
} else {
if (piecePresent(e.getX(), (e.getY()))) {
if (pieceName.contains("Black")) {
if (checkBlackOponent(e.getX(), e.getY())) {
validMove = true;
} else {
validMove = false;
}
} else {
if (checkWhiteOponent(e.getX(), e.getY())) {
validMove = true;
} else {
validMove = false;
}
}
} else {
validMove = true;
}
}
} else { // the move that is being tried is not a diagonal move...
validMove = false;
}
}
}
//End of Bishup Code
//Changes to new pawn Piece and Validates Move
if(!validMove){
int location=0;
if(startY ==0){
location = startX;
}
else{
location = (startY*8)+startX;
}
String pieceLocation = pieceName+".png";
pieces = new JLabel( new ImageIcon(pieceLocation) );
panels = (JPanel)chessBoard.getComponent(location);
panels.add(pieces);
}
else{
if(success){
if (c instanceof JLabel){
Container parent = c.getParent();
parent.remove(0);
String promoteTo;
do {
promoteTo = (String) JOptionPane.showInputDialog(null,
"Promote Pawn to :", "Pawn Promotion",
JOptionPane.QUESTION_MESSAGE, null,
new String[]{"Queen", "Bishup", "Knight", "Rook"}, "Queen");
} while (promoteTo == null);
String newPiece = null;
int location = 0;
if (pieceName.contains("White"))
{
location = 56 + (e.getX()/75);
newPiece = "White"+promoteTo;
}
else
{
location = (e.getX()/75);
newPiece = "Black"+promoteTo;
}
pieces = new JLabel( new ImageIcon(newPiece+".png") );
parent = (JPanel)chessBoard.getComponent(location);
parent.add(pieces);
validate();
repaint();
}
}
else{
if (c instanceof JLabel){
Container parent = c.getParent();
parent.remove(0);
parent.add( chessPiece );
}
else {
Container parent = (Container)c;
parent.add( chessPiece );
}
chessPiece.setVisible(true);
}
}
}
public void mouseClicked(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public static void main(String[] args) {
JFrame frame = new ChessProject();
frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
frame.pack();
frame.setResizable(true);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
感谢您为我提供的任何帮助
最佳答案
您所追求的需要重构您的代码。我建议您在开始重构之前编写一些测试来验证您的工作逻辑。一旦您认为您的代码有足够的覆盖范围,就开始一次重构一小段代码(一次仅重构一小段代码)。每次进行更改后,请运行测试以查看是否损坏了任何内容。重复进行小的更改和运行测试的过程,直到您满意为止。
关于java - 寻求 Java 代码架构方面的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26725693/
是否可以简化在裸机上运行的这条链: 具有随时间变化的副本数的 StatefulSet 服务 使用 proxy-next-upstream: "error http_502 timeout invali
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我需要为应用程序制定架构。它专为销售产品而设计。 系统每天将接受大约 30-40k 的新产品。它将导致在表 product 中创建新记录。 系统应保留价格历史记录。用户应该能够看到产品 A 的价格在去
我需要一些帮助来理解 PHP 的内部工作原理。 还记得,在过去,我们曾经写过 TSR(Terminate and stay resident)例程(pre-windows 时代)吗?一旦该程序被执行,
1.Nginx 基础架构 nginx 启动后以 daemon 形式在后台运行,后台进程包含一个 master 进程和多个 worker 进程。如下图所示: master与
本文深入探讨了Kubernetes(K8s)的关键方面,包括其架构、容器编排、网络与存储管理、安全与合规、高可用性、灾难恢复以及监控与日志系统。 关注【TechLeadCloud】,
我知道 CNN 的工作原理,包括每一层的用途(Dropout、Pooling 等)。但是,在为新数据集设计 CNN 时,我不知道要使用多少个 Conv-Relu-Pool 层,在最终获得输出之前我应该
在基于 REST 的架构中,资源和方法之间有什么区别。有吗? 最佳答案 资源是您的应用程序定义的东西;它们与物体非常相似。方法是 HTTP 动词之一,例如 GET、POST、PUT、DELETE。它们
我想用 oneOf仅在 xyType 的值上不同的模式属性(property)。我想要其中两个:一个是 xyType设置为 "1"第二个在哪里xyType是 任何其他值 .这可以使用 json 模式完
寻求 PHP 架构师的建议! 我对 PHP 不是很熟悉,但已经接管了一个用该语言编写的大型分析包的维护工作。该架构旨在将报告的数据读取到大型键/值数组中,这些数组通过各种解析模块传递,以提取每个模块已
这些存在吗? 多年来,我一直是大型强类型面向对象语言(Java 和 C#)的奴隶,并且是 Martin Fowler 及其同类的信徒。 Javascript,由于它的松散类型和函数性质,似乎不适合我习
我已经阅读了 Manning 的 Big Data Lambda Architecture ( http://www.manning.com/marz/BD_meap_ch01.pdf ),但仍然无法
在过去的几年里,我做了相当多的 iOS 开发,所以我非常熟悉 iOS 架构和应用程序设计(一切都是一个 ViewController,您可以将其推送、弹出或粘贴到选项卡栏中)。我最近开始探索正确的 M
我有以下应用程序,我在其中循环一些数据并显示它。 {{thing.title}} {{thing.description}}
昨天我和我的伙伴讨论了我正在开发的这个电子购物网站的架构。请注意,我为此使用 ASP.NET。他非常惊讶地发现我没有将添加到购物车的项目保留在 ArrayList 或其他通用列表中,而是使用 LINQ
我正在使用在 tridion 蓝图层次结构中处于较低位置的出版物。从蓝图中较高级别的出版物继承的一些内容和模式不适合我的出版物,并且永远不会被我的出版物使用。 我将跟进添加这些项目的内部团队,并尝试说
我目前已经在 Cassandra 中设计了一个架构,但我想知道是否有更好的方法来做事情。基本上,问题在于大多数(如果不是全部)读取都是动态的。我构建了一个分段系统作为应用程序服务,读取动态自定义查询(
我正在按照 documentation 中给出的 icingaweb UI v 2.0 布局执行在服务器上设置 icinga 的步骤。 。我成功进入设置页面,该页面要求您输入 token ,然后按照步
我必须保存来自不同社交媒体的用户的不同个人资料。例如用户可能有 1 个 Facebook 和 2 个 Twitter 个人资料。如果我保存每个配置文件它作为新文档插入不同的集合中,例如 faceboo
我的团队使用 Puppet 架构,该架构目前可在多个环境(流浪者、暂存、生产)中容纳单个应用程序。 我们现在想要扩展此设置的范围以支持其他应用程序。他们中的许多人将使用我们已经定义的现有模块的子集,而
我是一名优秀的程序员,十分优秀!