创建Connect 4获胜结果

时间:2015-12-07 07:25:01

标签: java javafx

我创建了一个代表Connect 4游戏的基本JavaFX程序。除了胜利的结果,我创造了一切。当其中一个用户连续4次(对角线,垂直或水平)的磁盘颜色时,程序将宣布获胜者。我在这方面苦苦挣扎。我如何做到这一点,以便程序能够识别有人赢得比赛的时间。有任何想法吗? 这是我的代码:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import javafx.scene.shape.*;
import javafx.scene.paint.*;
import javafx.geometry.*;
import java.util.*;
import javafx.scene.text.*;
public class ConnectFour extends Application
{
   private BorderPane pane = new BorderPane();
   private GridPane gPane = new GridPane();
   private boolean player1 = true;
   private int row0 = 5;
   private int row1 = 5;
   private int row2 = 5;
   private int row3 = 5;
   private int row4 = 5;
   private int row5 = 5;
   private int row6 = 5;
   private Label label = new Label("Player1 turn");
   public void start(Stage stage)
   {
      //Create the Grid
      createGrid();

      Scene scene = new Scene(pane);
      stage.setTitle("Connect 4");
      stage.setScene(scene);
      stage.show();

      //Start the game
      initiateGame();



   }

   //Create the grid method
   public void createGrid()
   {


      for (int i = 0; i < 6; i++)
      {
         for (int j =0; j < 7; j++)
         {
            Rectangle rect = new Rectangle(50, 50);
            rect.setStroke(Color.BLACK);
            rect.setFill(null);
            gPane.add(rect, j, i);
         }  
      }

      pane.getChildren().add(gPane);

   }

   public void initiateGame()
   {
      HBox hBox = new HBox(10);




         label.setFont(Font.font(20)); 

         TextField colField = new TextField();
         colField.setPrefColumnCount(1);

         Button submit = new Button("Submit");

         submit.setOnAction(e->
         {
            int col = Integer.parseInt(colField.getText());
            dropDisk(col, player1);
         });

         hBox.getChildren().addAll(label, colField, submit);

         pane.setBottom(hBox);

   }


   public void dropDisk(int col, boolean playerOne)
   {

      if (playerOne)
      {
         switch (col)
         {
            case 0: Circle circ = new Circle(25); circ.setFill(Color.RED); gPane.add(circ, col, row0);
            gPane.setHalignment(circ, HPos.CENTER); row0--; label.setText("Player2 turn");
            player1 = false; break;

            case 1: Circle circ1 = new Circle(25); circ1.setFill(Color.RED); gPane.add(circ1, col, row1);
            gPane.setHalignment(circ1, HPos.CENTER); row1--; label.setText("Player2 turn");
            player1 = false; break;

            case 2: Circle circ2 = new Circle(25); circ2.setFill(Color.RED); gPane.add(circ2, col, row2);
            gPane.setHalignment(circ2, HPos.CENTER); row2--; label.setText("Player2 turn");
            player1 = false; break;

            case 3: Circle circ3 = new Circle(25); circ3.setFill(Color.RED); gPane.add(circ3, col, row3);
            gPane.setHalignment(circ3, HPos.CENTER); row3--; label.setText("Player2 turn");
            player1 = false; break;

            case 4: Circle circ4 = new Circle(25); circ4.setFill(Color.RED); gPane.add(circ4, col, row4);
            gPane.setHalignment(circ4, HPos.CENTER); row4--; label.setText("Player2 turn");
            player1 = false; break;

            case 5: Circle circ5 = new Circle(25); circ5.setFill(Color.RED); gPane.add(circ5, col, row5);
            gPane.setHalignment(circ5, HPos.CENTER); row5--; label.setText("Player2 turn");
            player1 = false; break;

            case 6: Circle circ6 = new Circle(25); circ6.setFill(Color.RED); gPane.add(circ6, col, row6);
            gPane.setHalignment(circ6, HPos.CENTER); row6--;
            player1 = false; break;

            default: System.out.println("Incorrect column");
               break;

         }
      }

      else
      {
         switch (col)
         {
            case 0: Circle circ = new Circle(25); circ.setFill(Color.YELLOW); gPane.add(circ, col, row0);
            gPane.setHalignment(circ, HPos.CENTER); row0--; label.setText("Player1 turn");
               player1 = true; break;
            case 1: Circle circ1 = new Circle(25); circ1.setFill(Color.YELLOW); gPane.add(circ1, col, row1);
            gPane.setHalignment(circ1, HPos.CENTER); row1--; label.setText("Player1 turn");
               player1 = true; break;
            case 2: Circle circ2 = new Circle(25); circ2.setFill(Color.YELLOW); gPane.add(circ2, col, row2);
            gPane.setHalignment(circ2, HPos.CENTER); row2--; label.setText("Player1 turn");
               player1 = true; break;
            case 3: Circle circ3 = new Circle(25); circ3.setFill(Color.YELLOW); gPane.add(circ3, col, row3);
            gPane.setHalignment(circ3, HPos.CENTER); row3--; label.setText("Player1 turn");
               player1 = true; break;
            case 4: Circle circ4 = new Circle(25); circ4.setFill(Color.YELLOW); gPane.add(circ4, col, row4);
            gPane.setHalignment(circ4, HPos.CENTER); row4--; label.setText("Player1 turn");
               player1 = true; break;
            case 5: Circle circ5 = new Circle(25); circ5.setFill(Color.YELLOW); gPane.add(circ5, col, row5);
            gPane.setHalignment(circ5, HPos.CENTER); row5--; label.setText("Player1 turn");
               player1 = true; break;
            case 6: Circle circ6 = new Circle(25); circ6.setFill(Color.YELLOW); gPane.add(circ6, col, row6);
            gPane.setHalignment(circ6, HPos.CENTER); row6--; label.setText("Player1 turn");
               player1 = true; break;
            default: System.out.println("Incorrect column");
               break;

         }
      }

   } 

}

1 个答案:

答案 0 :(得分:0)

我将向您展示我正在制作的游戏片段,名为Omok。这是一个连接5游戏。我使用2D数组存储我的数据,0为空,1为玩家1,2为玩家2。

它基本上循环通过水平,垂直和两个对角线来检查它是否以5种方式连接。它是通过存储最后一次移动的值并从那里进行检查来完成的。

b是我的2D整数数组(板)。

public int winCheck(int lastRow, int lastCol, int check){
    if(horizontal(lastRow, lastCol, check) == 5
            || vertical(lastRow, lastCol, check) == 5
            || h1check(lastRow,lastCol,check) == 5
            || h2check(lastRow, lastCol, check) == 5){
        this.winner = check;
        return this.winner;
    }//end if
    return this.winner;
}//end winCheck

private int horizontal(int lastRow, int lastCol, int check){
    int count = 1;
    int numMove = 1;
    while(lastCol - numMove >= 0){
        if(b[lastRow][lastCol - numMove] == check){
            count ++;
            numMove ++;
        }
        else{
            break;
        }
    }//end while for leftMove

    numMove = 1;
    while(lastCol + numMove < b.length){
        if(b[lastRow][lastCol + numMove] == check){
            count++;
            numMove++;
        }
        else{
            break;
        }
    }//end while
    return count;
}//end

private int vertical(int lastRow, int lastCol, int check){
    int count = 1;
    int numMove = 1;
    while(lastRow - numMove >= 0){
        if(b[lastRow - numMove][lastCol] == check){
            count ++;
            numMove ++;
        }
        else{
            break;
        }
    }//end while for leftMove

    numMove = 1;
    while(lastRow + numMove < b.length){
        if(b[lastRow + numMove][lastCol] == check){
            count++;
            numMove++;
        }
        else{
            break;
        }
    }//end while
    return count;
}//end

private int h1check(int lastRow, int lastCol, int check){
    int count = 1;
    int numMove = 1;
    while(lastRow - numMove >= 0 && lastCol - numMove >= 0){
        if(b[lastRow - numMove][lastCol - numMove] == check){
            count ++;
            numMove ++;
        }
        else{
            break;
        }
    }//end while for leftMove

    numMove = 1;
    while(lastRow + numMove < b.length && lastCol + numMove < b.length){
        if(b[lastRow + numMove][lastCol + numMove] == check){
            count++;
            numMove++;
        }
        else{
            break;
        }
    }//end while
    return count;
}//end

private int h2check(int lastRow, int lastCol, int check){
    int count = 1;
    int numMove = 1;
    while(lastRow + numMove < b.length && lastCol - numMove >= 0){
        if(b[lastRow + numMove][lastCol - numMove] == check){
            count ++;
            numMove ++;
        }
        else{
            break;
        }
    }//end while for leftMove

    numMove = 1;
    while(lastRow - numMove >= 0 && lastCol + numMove < b.length){
        if(b[lastRow - numMove][lastCol + numMove] == check){
            count++;
            numMove++;
        }
        else{
            break;
        }
    }//end while
    return count;
}//end