2016-04-07 6 views
0

私はシンプルなtic-tac-toeゲームを作成しようとしていますが、これまでのところよくできていますが、静的メソッド& "囲まれたクラス"のJava/javafxの問題

私は2つのクラスを持っています.1つはランと呼ばれ、もう1つはDrawBoardです。 DrawBoardには文字(X、O、ボード自体)を作成するためのすべてのメソッドがありますが、Runには多くのメインメソッド呼び出しとクリックハンドラがあります。

私が描画メソッドを静的にしないと、Run(さまざまなgetterとsetter)でボード状態変数を変更するメソッドを参照することはできませんが、静的にするとエラーになります実行時に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); 
     } 
    } 
} 
+1

ここでは継承を間違って実装しています。通常、継承のための "is-a"関係(つまり、トラは猫です)を確実にしたいとします。あなたの場合、私に最初に出てくるのは、 'Drawboard'は' Run'ではありません。これはあなたが理解しなければならない** OOPのコアコンセプトです。 –

答えて

0

なぜDrawBoardが実行を延長するのですか?これは私にとっては意味がありません。それを除く。

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のために真です。

この方法: 実行して、メンバーdrawBoardを持っている、あなたは、メインボードを追加することができます:root.getChildren().addAll(drawBoard.drawMainBoard(),characters);、あなたは

そしてA.SharmaのコメントをdrawBoard.drawO(x,y);のようなO Xを描くことができ:はい、あなたは何を学ぶものdifferencecompositioninheritanceの間です。

関連する問題