我为跳棋游戏创建了两个类。一个是木板,另一个是碎片。我所面临的困难是,对于这些碎片,我似乎无法让它们移动,也无法让它们正确地停留在那个位置,事实上,由于我不了解这里到底出了什么问题,我无法再前进了。有人能纠正我做错了什么吗。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import javax.swing.JButton;
import javax.swing.JPanel;
public class Board extends JPanel {
private int[][] state;
public final static int EMPTY = 0;
public final static int RED = 1;
public final static int BLUE = 2;
public final static int BLUE_KING = 3;
public final static int RED_KING = 4;
public final static int HIGHLIGHT = 5;
public Board() {
state = new int[8][8];
setSize(400, 400);
}
public void paint(Graphics g){
for(int y=0;y<8;y++){
for(int x=0;x<8;x++){
if(x%2 == y%2){
g.setColor(Color.WHITE);
}
else{
g.setColor(Color.GRAY);
}
g.fillRect(x*50,y*50,50,50);
switch(state[x][y]){
//6 cases that will be available for this game
/*
* Empty will be the empty spot; Red will be the red checkers. Blue will be the blue checkers. The blue king will occur when it reaches other side
* and the red king will occur when it reaches other side. The highlight will show the possible moves that can occur when it [red/blue] king will be selected
*
*/
case EMPTY: break;
case RED:
g.setColor(Color.RED);
g.fillOval(x*50,y*50,50,50);
break;
case BLUE:
g.setColor(Color.BLACK);
g.fillOval(x*50,y*50,50,50);
break;
case BLUE_KING:
g.setColor(Color.BLACK);
g.fillOval(x*50,y*50,50,50);
g.setColor(Color.WHITE);
g.drawString("K",x*50+25,y*50+25);
break;
case RED_KING:
g.setColor(Color.RED);
g.fillOval(x*50,y*50,45,45);
g.setColor(Color.WHITE);
g.drawString("K",x*50+25,y*50+25);
break;
case HIGHLIGHT:
g.setColor(Color.YELLOW);
g.drawOval(x*50,y*50,50,50);
break;
}
}
}
}
public void setState(int x,int y, int s){
state[x][y]=s;
}
public int getState(int x,int y){
return state[x][y];
}
}
这是拼图课
import javax.swing.*;
import java.awt.*;
import javax.swing.ImageIcon;
import java.awt.event.*;
public class Checkers implements MouseListener
{
int p1pieces = 12;
int p2pieces = 12;
int xcord;
int ycord;
int exist;
int exist2;
int leftx = 0;
int lefty = 1;
int rightx = 0;
int righty = 1;
int oldXvalue, oldYvalue;
Board b=new Board();
JFrame frame = new JFrame("Checkers");
JPanel panel = new JPanel();
JButton newgame = new JButton("New Game");
JTextField turn = new JTextField("Players one turn");
JTextField pieces = new JTextField("Player 1 has " +p1pieces + " pieces. Player 2 has " + p2pieces + " pieces.");
public Checkers()
{
frame.setSize(500,500);
frame.add(b);
frame.add(panel);
frame.setVisible(true);
frame.addMouseListener(this);
b.setState(0,0, Board.BLUE);
b.setState(2,0, Board.BLUE);
b.setState(4,0, Board.BLUE);
b.setState(6,0, Board.BLUE);
b.setState(7,1, Board.BLUE);
b.setState(5,1, Board.BLUE);
b.setState(3,1, Board.BLUE);
b.setState(1,1, Board.BLUE);
b.setState(0,2, Board.BLUE);
b.setState(2,2, Board.BLUE);
b.setState(4,2, Board.BLUE);
b.setState(6,2, Board.BLUE);
b.setState(7,7, Board.RED);
b.setState(5,7, Board.RED);
b.setState(3,7, Board.RED);
b.setState(1,7, Board.RED);
b.setState(0,6, Board.RED);
b.setState(2,6, Board.RED);
b.setState(4,6, Board.RED);
b.setState(6,6, Board.RED);
b.setState(1,5, Board.RED);
b.setState(3,5, Board.RED);
b.setState(5,5, Board.RED);
b.setState(7,5, Board.RED);
}
public void mouseEntered(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mousePressed(MouseEvent e)
{
Object o = e.getSource();
{
oldXvalue = xcord;
oldYvalue = ycord;
}
xcord = (e.getX() / 50);
ycord = (e.getY() / 50) ;
exist = b.getState(xcord,ycord);
if(exist == 1 || exist == 2)
highlightleft();
highlightright();
move();
b.repaint();
}
public void highlightleft()
{
exist = b.getState(xcord, ycord);
if(exist == 1)
{
exist2 = b.getState(xcord-1, ycord -1);
}
if(exist == 2)
{
exist2 = b.getState(xcord-1, ycord +1);
}
if(exist == 1 && exist2 == 0)
{
b.setState(xcord-1,ycord -1, Board.HIGHLIGHT);
leftx = xcord -1;
lefty = ycord -1;
}
else if(exist == 2 && exist2 == 0)
{
b.setState(xcord -1, ycord + 1 ,Board.HIGHLIGHT);
leftx = xcord -1;
lefty = ycord +1;
}
}
public void highlightright()
{
exist = b.getState(xcord, ycord);
if(exist == 1)
{
exist2 = b.getState(xcord+1, ycord -1);
}
if(exist == 2)
{
exist2 = b.getState(xcord+1, ycord +1);
}
if(exist == 1 && exist2 == 0)
{
b.setState(xcord+1,ycord -1, Board.HIGHLIGHT);
rightx = xcord+1;
righty = ycord -1;
}
else if(exist == 2 && exist2 == 0)
{
b.setState(xcord +1, ycord + 1 ,Board.HIGHLIGHT);
rightx = xcord+1;
righty=ycord +1;
}
}
public void removehighlight()
{
exist = b.getState(xcord,ycord);
b.setState(leftx,lefty,Board.EMPTY);
b.setState(rightx,righty,Board.EMPTY);
}
public void move()
{
exist = b.getState(xcord,ycord);
exist2 = b.getState(oldXvalue,oldYvalue);
if(exist == 5)
{
if(exist2 == 1)
{
b.setState(oldXvalue, oldYvalue, Board.EMPTY);
b.setState(xcord,ycord, Board.EMPTY);
b.setState(xcord,ycord, Board.RED);
}
else if(exist2 == 2)
{
b.setState(oldXvalue, oldYvalue, Board.EMPTY);
b.setState(xcord,ycord, Board.EMPTY);
b.setState(xcord,ycord, Board.BLUE);
}
/*
* I think that there should be something here for the code to be cleared and then you can repeat it
*/
}
}
public static void main(String [] args){
new Checkers();
}
}
我不知道你什么时候得到你的错误,所以在不知道实际错误是什么的情况下很难说什么。
但我想说,您的突出显示代码可以删除任何红-蓝棋盘格位置信息,您不会检查是否已经有信息。高亮显示方法有几个ifs和if else语句。将检查if以及if else块。
但我可能会在这个假设上出错,因为很难知道所有==代码的含义,因为您不使用常量。
我已经创建了一个跳棋GUI。
虽然我已经开始编写MouseListener的代码,但我还没有实现移动。下面是Checkers GUI的外观。
我使用模型/视图/控制器模式(MVC)来编写这个GUI。我编写了4个模型类、3个视图类和5个控制器类。
当我编写Swing GUI时,我是如何实现MVC模式的:
我没有一个控制器类。到目前为止,我有5个控制器类。
这是主要的类,跳棋。
package com.ggl.checkers;
import javax.swing.SwingUtilities;
import com.ggl.checkers.model.CheckersModel;
import com.ggl.checkers.view.CheckersFrame;
public class Checkers implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Checkers());
}
@Override
public void run() {
new CheckersFrame(new CheckersModel());
}
}
短而有效。这个19行的Java类做了3件事。
接下来,让我们看看模型类。这是CheckersModel类。
package com.ggl.checkers.model;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.ggl.checkers.model.CheckerPiece.PieceType;
public class CheckersModel {
private Color boardEdgeColor;
private Color lightSquareColor;
private Color darkSquareColor;
private Color lightPieceColor;
private Color darkPieceColor;
private GameParameters gameParameters;
private List<CheckerPiece> pieces;
private List<CheckerSquare> board;
private List<GameParameters> parameters;
public CheckersModel() {
this.parameters = new ArrayList<GameParameters>();
setGameParameters();
this.boardEdgeColor = Color.ORANGE;
this.lightSquareColor = Color.WHITE;
this.darkSquareColor = Color.GRAY;
this.lightPieceColor = Color.RED;
this.darkPieceColor = Color.BLACK;
this.board = new ArrayList<CheckerSquare>();
setBoard();
this.pieces = new ArrayList<CheckerPiece>();
setPieces();
}
public void setGameParameters() {
this.gameParameters = new GameParameters(
"American Checkers", 8, 12, true, false);
gameParameters.setSelected(true);
parameters.add(gameParameters);
GameParameters gp = new GameParameters(
"Canadian Checkers", 12, 30, true, true);
parameters.add(gp);
gp = new GameParameters(
"International Checkers", 10, 20, true, true);
parameters.add(gp);
}
public void setGameParameters(GameParameters gameParameters) {
this.gameParameters = gameParameters;
}
public void setBoard() {
this.board.clear();
Color color1 = getLightSquareColor();
Color color2 = getDarkSquareColor();
if (!gameParameters.isLightSquareOnRight()) {
color1 = getDarkSquareColor();
color2 = getLightSquareColor();
}
int squareWidth = gameParameters.getSquareWidth();
int squaresPerSide = gameParameters.getSquaresPerSide();
int margin = squareWidth / 2;
int x = margin;
int y = margin;
for (int i = 0; i < squaresPerSide; i++) {
for (int j = 0; j < squaresPerSide; j++) {
Color color = null;
if (getGameParameters().isLightSquareOnRight()) {
if (i % 2 == j % 2) {
color = color1;
} else {
color = color2;
}
} else {
if (i % 2 == j % 2) {
color = color2;
} else {
color = color1;
}
}
Rectangle r = new Rectangle(x, y, squareWidth, squareWidth);
Point p = new Point(i, j);
CheckerSquare cs = new CheckerSquare(color, p, r);
board.add(cs);
x += squareWidth;
}
y += squareWidth;
x = margin;
}
}
public void setPieces() {
this.pieces.clear();
int squaresPerSide = getGameParameters().getSquaresPerSide();
Color topColor = getDarkPieceColor();
Color bottomColor = getLightPieceColor();
if (!getGameParameters().isLightMovesFirst()) {
topColor = getLightPieceColor();
bottomColor = getDarkPieceColor();
}
int rows = getGameParameters().getPiecesPerSide() * 2 / squaresPerSide;
for (int i = 0; i < squaresPerSide; i++) {
if (i < rows) {
if (getGameParameters().isLightSquareOnRight()) {
if ((i % 2) == 0) {
setPieceRow(topColor, i, 1);
} else {
setPieceRow(topColor, i, 0);
}
} else {
if ((i % 2) == 0) {
setPieceRow(topColor, i, 0);
} else {
setPieceRow(topColor, i, 1);
}
}
}
if (i > (squaresPerSide - rows - 1)) {
if (getGameParameters().isLightSquareOnRight()) {
if ((i % 2) == 0) {
setPieceRow(bottomColor, i, 1);
} else {
setPieceRow(bottomColor, i, 0);
}
} else {
if ((i % 2) == 0) {
setPieceRow(bottomColor, i, 0);
} else {
setPieceRow(bottomColor, i, 1);
}
}
}
}
}
public void resetPieceColor(Color previousColor, Color newColor) {
for (CheckerPiece cp : pieces) {
if (cp.getColor().equals(previousColor)) {
cp.setColor(newColor);
}
}
}
public void resetSquareColor(Color previousColor, Color newColor) {
for (CheckerSquare cs : board) {
if (cs.getColor().equals(previousColor)) {
cs.setColor(newColor);
}
}
}
private void setPieceRow(Color color, int row, int column) {
int squaresPerSide = gameParameters.getSquaresPerSide();
for (int j = column; j < squaresPerSide; j += 2) {
CheckerPiece cp = new CheckerPiece(color, PieceType.Man);
cp.setLocation(new Point(row, j));
pieces.add(cp);
}
}
public int getSquaresPerSide() {
return gameParameters.getSquaresPerSide();
}
public Color getBoardEdgeColor() {
return boardEdgeColor;
}
public void setBoardEdgeColor(Color boardEdgeColor) {
this.boardEdgeColor = boardEdgeColor;
}
public Color getLightSquareColor() {
return lightSquareColor;
}
public void setLightSquareColor(Color lightSquareColor) {
this.lightSquareColor = lightSquareColor;
}
public Color getDarkSquareColor() {
return darkSquareColor;
}
public void setDarkSquareColor(Color darkSquareColor) {
this.darkSquareColor = darkSquareColor;
}
public Color getLightPieceColor() {
return lightPieceColor;
}
public void setLightPieceColor(Color lightPieceColor) {
this.lightPieceColor = lightPieceColor;
}
public Color getDarkPieceColor() {
return darkPieceColor;
}
public void setDarkPieceColor(Color darkPieceColor) {
this.darkPieceColor = darkPieceColor;
}
public Dimension getPreferredBoardSize() {
int squareWidth = gameParameters.getSquareWidth();
int squaresPerSide = gameParameters.getSquaresPerSide();
int width = squareWidth * (squaresPerSide + 1);
return new Dimension(width, width);
}
public List<GameParameters> getGameParametersList() {
return Collections.unmodifiableList(parameters);
}
public GameParameters getGameParameters() {
return gameParameters;
}
public CheckerPiece getPieceOnSquare(Point p) {
for (CheckerPiece cp : pieces) {
if (cp.isLocation(p)) {
return cp;
}
}
return null;
}
public CheckerPiece getPieceOnSquare(int x, int y) {
for (CheckerSquare cs : board) {
if (cs.containsLocation(x, y)) {
CheckerPiece cp = getPieceOnSquare(cs.getCoordinate());
return cp;
}
}
return null;
}
public void draw(Graphics g) {
int squareWidth = gameParameters.getSquareWidth();
int squaresPerSide = gameParameters.getSquaresPerSide();
int width = squareWidth * (squaresPerSide + 1);
g.setColor(getBoardEdgeColor());
g.fillRect(0, 0, width, width);
for (CheckerSquare cs : board) {
cs.draw(g);
CheckerPiece cp = getPieceOnSquare(cs.getCoordinate());
if (cp != null) {
cp.draw(g, cs.getLocation(), squareWidth);
}
}
}
}
您将看到棋盘正方形以及格子块保存在列表中。棋盘正方形保存在列表中,因为我需要知道每个正方形的像素位置。正如您稍后将看到的,我将像素位置放在矩形中。这允许我将鼠标点击转换为正方形,并最终转换为格子块。
GameParameters类保存特定跳棋游戏的参数。现在,我有美国跳棋、加拿大跳棋和国际跳棋。您可以在GUI图像上看到,不同的棋盘游戏具有不同的参数。
接下来,让我们看看GameParameters类。
package com.ggl.checkers.model;
public class GameParameters {
private boolean isSelected;
private boolean lightSquareOnRight;
private boolean lightMovesFirst;
private int piecesPerSide;
private int squaresPerSide;
private int squareWidth;
private String gameName;
public GameParameters(String gameName, int squaresPerSide,
int piecesPerSide, boolean lightSquareOnRight,
boolean lightMovesFirst) {
this.gameName = gameName;
this.squaresPerSide = squaresPerSide;
this.piecesPerSide = piecesPerSide;
this.lightSquareOnRight = lightSquareOnRight;
this.lightMovesFirst = lightMovesFirst;
this.squareWidth = 500 / squaresPerSide;
this.isSelected = false;
}
public boolean isLightSquareOnRight() {
return lightSquareOnRight;
}
public boolean isLightMovesFirst() {
return lightMovesFirst;
}
public int getPiecesPerSide() {
return piecesPerSide;
}
public int getSquaresPerSide() {
return squaresPerSide;
}
public int getSquareWidth() {
return squareWidth;
}
public String getGameName() {
return gameName;
}
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean isSelected) {
this.isSelected = isSelected;
}
@Override
public String toString() {
return gameName;
}
}
这个类包含使每个跳棋变化唯一的游戏参数。
接下来,让我们看看CheckerSquare类。
package com.ggl.checkers.model;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
public class CheckerSquare {
private Color color;
private Point coordinate;
private Rectangle location;
public CheckerSquare(Color color, Point coordinate, Rectangle location) {
this.color = color;
this.coordinate = coordinate;
this.location = location;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public Point getCoordinate() {
return coordinate;
}
public Rectangle getLocation() {
return location;
}
public boolean containsLocation(int x, int y) {
return location.contains(x, y);
}
public void draw(Graphics g) {
g.setColor(color);
g.fillRect(location.x, location.y, location.width, location.height);
}
}
正如我之前提到的,每个正方形都由一个矩形定义,让我们知道正方形的位置。这允许鼠标点击转换为正方形坐标,最终转换为片坐标。
最后,我们来看看CheckerPiece类。
package com.ggl.checkers.model;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
public class CheckerPiece {
private Color color;
private PieceType pieceType;
private Point location;
public CheckerPiece(Color color, PieceType pieceType) {
this.color = color;
this.pieceType = pieceType;
}
public Point getLocation() {
return location;
}
public void setLocation(Point location) {
this.location = location;
}
public boolean isLocation(Point location) {
return ((this.location.x == location.x)
&& (this.location.y == location.y));
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public PieceType getPieceType() {
return pieceType;
}
public void draw(Graphics g, Rectangle r, int width) {
drawCircle(g, getCenter(r), width * 4 / 10);
}
private Point getCenter(Rectangle r) {
int x = r.x + r.width / 2;
int y = r.y + r.height / 2;
return new Point(x, y);
}
private void drawCircle(Graphics g, Point center, int radius) {
g.setColor(getColor());
g.fillOval(center.x - radius, center.y - radius,
radius + radius, radius + radius);
}
public enum PieceType {
Man, King
}
}
接下来,我们将查看视图类。首先,我们来看CheckersFrame类。
package com.ggl.checkers.view;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.ggl.checkers.controller.MoveMouseListener;
import com.ggl.checkers.model.CheckersModel;
import com.ggl.checkers.model.GameParameters;
public class CheckersFrame {
private BoardPanel boardPanel;
private CheckersModel model;
private ControlPanel controlPanel;
private JFrame frame;
public CheckersFrame(CheckersModel model) {
this.model = model;
createPartControl();
}
private void createPartControl() {
frame = new JFrame();
frame.setTitle("Checkers");
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent event) {
exitProcedure();
}
});
boardPanel = new BoardPanel(model);
boardPanel.addMouseListener(new MoveMouseListener(model));
controlPanel = new ControlPanel(this, model);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.LINE_AXIS));
mainPanel.add(boardPanel);
JPanel rightPanel = new JPanel();
rightPanel.add(controlPanel.getPanel());
mainPanel.add(rightPanel);
frame.add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private void exitProcedure() {
frame.dispose();
System.exit(0);
}
public JFrame getFrame() {
return frame;
}
public void updateControlPanelPartControl(GameParameters item) {
controlPanel.updatePartControl(item);
}
public void repaintBoardPanel() {
frame.setVisible(false);
frame.validate();
frame.pack();
frame.setVisible(true);
}
}
createPartControl方法是创建JFrame的样板。我们使用JFrame。在重写方法时,只应扩展Swing组件或任何其他Java类。
接下来,让我们看看BoardPanel类。
package com.ggl.checkers.view;
import java.awt.Graphics;
import javax.swing.JPanel;
import com.ggl.checkers.model.CheckersModel;
public class BoardPanel extends JPanel {
private static final long serialVersionUID = 7726343503195297509L;
private CheckersModel model;
public BoardPanel(CheckersModel model) {
this.model = model;
this.setPreferredSize(model.getPreferredBoardSize());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
model.draw(g);
}
}
此类调用模型中的绘图代码,该代码用于绘制棋盘格和棋盘格。
一个答案限制为30,000个字符。如果有人认为值得,我可以在另一个答案中继续讨论。我还有1个视图类和5个控制器类要讨论。
大家好,我有一个模拟ATM机的程序。它使用我创建的account类,在用户输入0到999999之间的id后,为用户生成一个帐户。然后,他们可以执行各种任务,如查看余额、取款、存款等。不过,我在检查程序时遇到了一个问题。它编译时没有错误,并且第一次通过循环时,它工作得非常完美。但是,如果他们点击退出并输入另一个无效id,它会显示两次无效输入消息。我复制了下面发生的事情的控制台。有人能给我解释一下为什
我得到了这个错误时启动检查风格分析在Spring引导应用程序: 代码: 有什么帮助吗?
Go 代码检查 统计代码行数 推荐我开发的开源软件--artHummer,目前只是简单的统计代码,后续可能开发更加复杂的功能。 go report 如果您的代码是放在github上面的,可以登录go report 进行代码扫描。 gofmt 整理代码 gofmt -l -w -s *.go gofmt -r '(a)->a' -l -w *.go go tool vet 这个
主要内容:运行代码检查,检查报告检查是一种静态代码分析工具,可以帮助我们发现运行时错误、定位死代码、检测性能问题、内存泄漏、拼写问题,并改善整体代码结构。它不仅会告诉我们代码中的问题出在哪里,而且还会立即提出更正建议。当我们执行代码分析时,代码检查可用 : 整个项目 特定文件 自定义范围 默认情况下,IntelliJ IDEA 对所有打开的文件执行分析。如果在项目中检测到错误,那么我们将在编辑器的右上角看到以下图标 运行代码检查
对于我的刽子手游戏,我想有一堆错误信息来检查输入的不止一个字母,猜测同一个字母两次,等等。到目前为止我的完整代码: 我遇到的最大问题是检查用户是否输入了相同的字母两次。所以你只能猜一次字母表中的一个字母。此外,它检查用户输入的多个字母是否有错误的代码,我相信,因为如果我不希望它将这些字母添加到猜测字母框中,它会以任何方式添加这些字母。(即,用户猜测“hf”,在猜测的字母中出现“hf”,在这里它应该
了解如何在 Adobe Dreamweaver 中使用 linting 检查 HTML、CSS 和 JS 文件。在“输出”面板中查看错误和警告并通过一次单击跳转到出错的代码行。 无论您是新手还是经验丰富的程序员,您的代码中都难免会因为疏忽或缺乏知识而出现错误。当网页或网页的一部分看上去不符合您的预期时,您将不得不调试代码,以查找任何语法或逻辑错误。调试可能是一个极其艰苦和耗时的过程,尤其是实施项目