我正在尝试创建一个简单的井字游戏,到目前为止还不错,但我在以我想要的方式操纵变量时遇到了问题。
我有两个班,一个叫Run,另一个叫DrawBoard。DrawBoard保存了所有创建字母的方法(X、O、板本身),而Run保存了许多主方法调用和单击处理程序。
我的问题是,如果我让绘图方法不是静态的,我就不能在Run中引用修改板状态变量的方法(各种getter和setter),但如果我让它们是静态的,那么我在Run中尝试使用DrawBoard中的方法时会出错,但由于某种原因,只能使用封装在单击处理程序中的方法(请注意,DrawBoard.DrawMainBoard可以工作)。错误显示"Drawboard不是一个封闭类"。
我对Java有点陌生,所以这可能不是最有效的做事方式,但这是我的代码。
public class Run extends Application
{
Group root = new Group();
Pane characters = new Pane();
boolean isPlayerOneTurn = true;
//Values representing state of a square
int TL, TM, TR, ML, M, MR, BL, BM, BR;
public void start (Stage primaryStage)
{
Scene mainScene = new Scene(root, 600, 600);
DrawBoard.DrawMainBoard board = new DrawBoard.DrawMainBoard();
clicky(mainScene);
root.getChildren().addAll(board,characters);
primaryStage.setTitle("Test Game");
primaryStage.setScene(mainScene);
primaryStage.show();
}
void clicky (Scene scene)
{
scene.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent event) {
if (!event.isControlDown())
{
double mouseXPos = event.getSceneX();
double mouseYPos = event.getSceneY();
if(isPlayerOneTurn)
{
DrawBoard.drawX ex = new DrawBoard.drawX(mouseXPos, mouseYPos);
//The 2 method calls I was referring to
characters.getChildren().add(ex);
isPlayerOneTurn = false;
}
else
{
DrawBoard.drawO oh = new DrawBoard.drawO(mouseXPos, mouseYPos);
characters.getChildren().add(oh);
isPlayerOneTurn = true;
}
}
}
});
}
public int getTL()
{
return TL;
}
public int getTM()
{
return TM;
}
public int getTR()
{
return TR;
}
public int getML()
{
return ML;
}
public int getM()
{
return M;
}
public int getMR()
{
return MR;
}
public int getBL()
{
return BL;
}
public int getBM()
{
return BM;
}
public int getBR()
{
return BR;
}
public void setTL(int newTL)
{
TL = newTL;
}
public void setTM(int newTM)
{
TM = newTM;
}
public void setTR(int newTR)
{
TR = newTR;
}
public void setML(int newML)
{
ML = newML;
}
public void setM(int newM)
{
M = newM;
}
public void setMR(int newMR)
{
MR = newMR;
}
public void setBL(int newBL)
{
BL = newBL;
}
public void setBM(int newBM)
{
BM = newBM;
}
public void setBR(int newBR)
{
BR = newBR;
}
}
public class DrawBoard extends Run
{
public static class DrawMainBoard extends Pane
{
public DrawMainBoard()
{
Line v1 = new Line();
Line v2 = new Line();
Line h1 = new Line();
Line h2 = new Line();
v1.setStartX(200);
v1.setStartY(0);
v1.setEndX(200);
v1.setEndY(600);
v2.setStartX(400);
v2.setStartY(0);
v2.setEndX(400);
v2.setEndY(600);
h1.setStartX(0);
h1.setStartY(200);
h1.setEndX(600);
h1.setEndY(200);
h2.setStartX(0);
h2.setStartY(400);
h2.setEndX(600);
h2.setEndY(400);
getChildren().addAll(v1,v2,h1,h2);
}
}
public class drawX extends Pane
{
public drawX(double mouseXPos, double mouseYPos)
{
Line x1 = new Line();
Line x2 = new Line();
//Top Left
if(mouseXPos < 200 && mouseYPos < 200)
{
if(getTL() != 1 && getTL() != 2)
{
x1.setStartX(50);
x1.setStartY(50);
x1.setEndX(150);
x1.setEndY(150);
x2.setStartX(150);
x2.setStartY(50);
x2.setEndX(50);
x2.setEndY(150);
setTL(1);
}
}
//Top middle
else if (mouseXPos > 200 && mouseXPos < 400 && mouseYPos < 200 )
{
if(getTM() != 1 && getTM() != 2)
{
x1.setStartX(250);
x1.setStartY(50);
x1.setEndX(350);
x1.setEndY(150);
x2.setStartX(350);
x2.setStartY(50);
x2.setEndX(250);
x2.setEndY(150);
setTM(1);
}
}
//Top right
else if (mouseXPos > 400 && mouseYPos < 200)
{
if(getTR() != 1 && getTR() != 2)
{
x1.setStartX(450);
x1.setStartY(50);
x1.setEndX(550);
x1.setEndY(150);
x2.setStartX(550);
x2.setStartY(50);
x2.setEndX(450);
x2.setEndY(150);
setTR(1);
}
}
//Middle left
else if (mouseXPos < 200 && mouseYPos > 200 && mouseYPos < 400)
{
x1.setStartX(50);
x1.setStartY(250);
x1.setEndX(150);
x1.setEndY(350);
x2.setStartX(150);
x2.setStartY(250);
x2.setEndX(50);
x2.setEndY(350);
setML(1);
}
//middle
else if (mouseXPos > 200 && mouseXPos < 400 &mouseYPos > 200 && mouseYPos < 400)
{
x1.setStartX(250);
x1.setStartY(250);
x1.setEndX(350);
x1.setEndY(350);
x2.setStartX(350);
x2.setStartY(250);
x2.setEndX(250);
x2.setEndY(350);
setM(1);
}
//middle right
else if (mouseXPos > 400 && mouseYPos > 200 && mouseYPos < 400)
{
x1.setStartX(450);
x1.setStartY(250);
x1.setEndX(550);
x1.setEndY(350);
x2.setStartX(550);
x2.setStartY(250);
x2.setEndX(450);
x2.setEndY(350);
setMR(1);
}
//bottom left
else if (mouseXPos < 200 && mouseYPos > 400)
{
x1.setStartX(50);
x1.setStartY(450);
x1.setEndX(150);
x1.setEndY(550);
x2.setStartX(150);
x2.setStartY(450);
x2.setEndX(50);
x2.setEndY(550);
setBL(1);
}
//bottom middle
else if (mouseXPos > 200 && mouseXPos < 400 && mouseYPos > 400)
{
x1.setStartX(250);
x1.setStartY(450);
x1.setEndX(350);
x1.setEndY(550);
x2.setStartX(350);
x2.setStartY(450);
x2.setEndX(250);
x2.setEndY(550);
setBM(1);
}
//bottom right
else if(mouseXPos > 400 && mouseYPos > 400)
{
x1.setStartX(450);
x1.setStartY(450);
x1.setEndX(550);
x1.setEndY(550);
x2.setStartX(550);
x2.setStartY(450);
x2.setEndX(450);
x2.setEndY(550);
setBR(1);
}
getChildren().addAll(x1,x2);
}
}
public class drawO extends Pane
{
public drawO (double mouseXPos, double mouseYPos)
{
Circle circle = new Circle(75);
//Top Left
if(mouseXPos < 200 && mouseYPos < 200)
{
circle.setCenterX(100);
circle.setCenterY(100);
}
//Top middle
else if (mouseXPos > 200 && mouseXPos < 400 && mouseYPos < 200 )
{
circle.setCenterX(300);
circle.setCenterY(100);
}
//Top right
else if (mouseXPos > 400 && mouseYPos < 200)
{
circle.setCenterX(500);
circle.setCenterY(100);
}
//Middle left
else if (mouseXPos < 200 && mouseYPos > 200 && mouseYPos < 400)
{
circle.setCenterX(100);
circle.setCenterY(300);
}
//middle
else if (mouseXPos > 200 && mouseXPos < 400 &mouseYPos > 200 && mouseYPos < 400)
{
circle.setCenterX(300);
circle.setCenterY(300);
}
//middle right
else if (mouseXPos > 400 && mouseYPos > 200 && mouseYPos < 400)
{
circle.setCenterX(500);
circle.setCenterY(300);
}
//bottom left
else if (mouseXPos < 200 && mouseYPos > 400)
{
circle.setCenterX(100);
circle.setCenterY(500);
}
//bottom middle
else if (mouseXPos > 200 && mouseXPos < 400 && mouseYPos > 400)
{
circle.setCenterX(300);
circle.setCenterY(500);
}
//bottom right
else if(mouseXPos > 400 && mouseYPos > 400)
{
circle.setCenterX(500);
circle.setCenterY(500);
}
getChildren().addAll(circle);
}
}
}
为什么DrawBoard
要扩展Run?这对我来说毫无意义。删除它。
也有DrawBoard board;
作为Run
的成员。
你甚至不需要DrawMainBoard
类,它也是DrawBoard
:的一个简单函数
public Pane drawMainBoard()
{ ... your code here ... }
另一点是因为这个类嵌套非常奇怪:
有这样一个public class drawO extends Pane
班的背后是什么?
该方法可以简单地作为DrawBoard
:的方法
public Pane drawO(double mouseXPos, double mouseYPos)
drawX
也是如此。
这样:Run有成员drawBoard
,你可以把主板添加为:root.getChildren().addAll(drawBoard.drawMainBoard(),characters);
,你可以像drawBoard.drawO(x,y);
一样画X和O
对于A.Sharma的评论:是的,你将了解组成和继承之间的区别。