如何开始将Connect 4控制器连接到connect4服务?

时间:2019-11-09 22:29:22

标签: java css javafx fxml

这段代码是经过JUnit测试的Connect 4游戏,我试图使其功能正常并接受X和O的输入。我是第一次使用Circle对象,不确定如何处理它们,这是我的主要目标课:

 package edu.citytech.gamesplus;
 import java.net.URL;


 import javafx.application.Application;
 import javafx.fxml.FXMLLoader;
 import javafx.scene.Parent;
 import javafx.scene.Scene;
 import javafx.stage.Stage;

public class GamesMainPlus extends Application {


 public static void main(String[] args) {
    Application.launch(GamesMainPlus.class, args);
 }

@Override
public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.
             load(getClass().getResource("GamesPlus.fxml"));

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


    URL url = getClass().getResource("GamesPlus.css");
    if(url == null) {
        System.out.println("Resources not found Aborting");
        System.exit(-1);
    }



    String css = url .toExternalForm();
    scene.getStylesheets().add(css);



    }


}

这是我一直在玩的控制器类:

package edu.citytech.gamesplus;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import edu.citytech.dao.WinningCombo;
import edu.citytech.dao.WinningComboDAO; 
import edu.citytech.service.Connect4Service;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;

public class GamesPlusController {

@FXML
private FlowPane fpGames;

@FXML
private HBox Row1;

@FXML
private Circle circ_1;

@FXML
private Circle circ_2;

@FXML
private Circle circ_3;

@FXML
private Circle circ_4;

@FXML
private Circle circ_5;

@FXML
private Circle circ_6;

@FXML
private HBox Row2;

@FXML
private Circle circ_7;

@FXML
private Circle circ_8;

@FXML
private Circle circ_9;

@FXML
private Circle circ_10;

@FXML
private Circle circ_11;

@FXML
private Circle circ_12;

@FXML
private HBox Row3;

@FXML
private Circle circ_13;

@FXML
private Circle circ_14;

@FXML
private Circle circ_15;

@FXML
private Circle circ_16;

@FXML
private Circle circ_17;

@FXML
private Circle circ_18;

@FXML
private HBox Row4;

@FXML
private Circle circ_19;

@FXML
private Circle circ_20;

@FXML
private Circle circ_21;

@FXML
private Circle circ_22;

@FXML
private Circle circ_23;

@FXML
private Circle circ_24;

@FXML
private HBox Row5;

@FXML
private Circle circ_25;

@FXML
private Circle circ_26;

@FXML
private Circle circ_27;

@FXML
private Circle circ_28;

@FXML
private Circle circ_29;

@FXML
private Circle circ_30;

@FXML
private HBox Row6;

@FXML
private Circle circ_31;

@FXML
private Circle circ_32;

@FXML
private Circle circ_33;

@FXML
private Circle circ_34;

@FXML
private Circle circ_35;

@FXML
private Circle circ_36;

@FXML
private Label Title;

@FXML
private Label Name;

@FXML
private Label lbl_Message;

@FXML
private Button btn_Reset;

private boolean isX = false;


  //    int nextspace(int x) {
  //        return x;   
  //      }

  //     void mycirc( ) {
  //    Circle circle = new Circle();
  //    List<Circle> grid =  new ArrayList<Circle>(); 

  //      for (Circle grid1: grid) {        
  //            System.out.println(grid);

          //    } 

   //   System.out.println(grid);
    //  
   //    }

@FXML
void onMouseClicked(MouseEvent event) {

    circ_1.setUserData("?");
    circ_2.setUserData("?");
    circ_3.setUserData("?");
    circ_3.setUserData("?");
    circ_4.setUserData("?");
    circ_5.setUserData("?");
    circ_6.setUserData("?");
    circ_7.setUserData("?");
    circ_8.setUserData("?");
    circ_9.setUserData("?");
    circ_10.setUserData("?");
    circ_11.setUserData("?");
    circ_12.setUserData("?");
    circ_13.setUserData("?");
    circ_14.setUserData("?");
    circ_15.setUserData("?");
    circ_16.setUserData("?");
    circ_17.setUserData("?");
    circ_18.setUserData("?");
    circ_19.setUserData("?");
    circ_20.setUserData("?");
    circ_21.setUserData("?");
    circ_22.setUserData("?");
    circ_23.setUserData("?");
    circ_24.setUserData("?");
    circ_25.setUserData("?");
    circ_26.setUserData("?");
    circ_27.setUserData("?");
    circ_28.setUserData("?");
    circ_29.setUserData("?");
    circ_30.setUserData("?");
    circ_31.setUserData("?");
    circ_32.setUserData("?");
    circ_33.setUserData("?");
    circ_33.setUserData("?");
    circ_34.setUserData("?");
    circ_35.setUserData("?");
    circ_36.setUserData("?");



//      Circle circ = (Circle) event.getSource();
//      circ = new
//      String getp1 = (String) circ_1.getUserData();
//      Boolean setcolor =  circ_1.setFill(Color.WHITE);


    Circle circ = (Circle) event.getSource();
    if (!circ.getUserData().equals("?")) {   
        String value = "illegal";
        lbl_Message.setText(value);

        return;
    }
    circ_1.setUserData("X");
    circ_2.setUserData("X");
    circ_3.setUserData("X");
    circ_3.setUserData("X");        
    //try testing colors
    String nextMove = isX ? "X":"O";
    circ.setUserData(nextMove);
    isX = !isX;

            Circle p1,p2,p3,p4;
     p1 = (Circle) circ_1.getUserData();
     p2 = (Circle) circ_2.getUserData();
     p3 = (Circle) circ_3.getUserData();
     p4 = (Circle) circ_4.getUserData();




     if (p1.equals("X") && p2.equals("X") && p3.equals("X") && p4.equals("X")) {

            circ_1.setFill(Color.RED);
            circ_2.setFill(Color.RED);
            circ_3.setFill(Color.RED);
            circ_4.setFill(Color.RED);
            lbl_Message.setText("Winner is X");
    }


     else if (p1.equals("O") && p2.equals("O") && p3.equals("O") &&     p4.equals("O")) {

        circ_1.setFill(Color.RED);
        circ_2.setFill(Color.RED);
        circ_3.setFill(Color.RED);
        circ_4.setFill(Color.RED);
        lbl_Message.setText("Winner is O");
            }



    circ_1.setFill(Color.RED);
    circ_2.setFill(Color.RED);
    circ_3.setFill(Color.RED);
    circ_4.setFill(Color.RED);
    circ_5.setFill(Color.RED);
    circ_6.setFill(Color.RED);
    circ_7.setFill(Color.RED);
    circ_8.setFill(Color.RED);
    circ_9.setFill(Color.RED);

    circ_10.setFill(Color.RED);
    circ_11.setFill(Color.RED);
    circ_12.setFill(Color.RED);
    circ_13.setFill(Color.RED);
    circ_14.setFill(Color.RED);
    circ_15.setFill(Color.RED);
    circ_16.setFill(Color.RED);
    circ_17.setFill(Color.RED);
    circ_18.setFill(Color.RED);

    circ_19.setFill(Color.RED);
    circ_20.setFill(Color.RED);
    circ_21.setFill(Color.RED);
    circ_22.setFill(Color.RED);
    circ_23.setFill(Color.RED);
    circ_24.setFill(Color.RED);
    circ_25.setFill(Color.RED);
    circ_26.setFill(Color.RED);
    circ_27.setFill(Color.RED);

    circ_28.setFill(Color.RED);
    circ_29.setFill(Color.RED);
    circ_30.setFill(Color.RED);
    circ_31.setFill(Color.RED);
    circ_32.setFill(Color.RED);
    circ_33.setFill(Color.RED);
    circ_34.setFill(Color.RED);
    circ_35.setFill(Color.RED);
    circ_36.setFill(Color.RED);



    circ_1.setFill(Color.YELLOW);
    circ_2.setFill(Color.YELLOW);
    circ_3.setFill(Color.YELLOW);
    circ_4.setFill(Color.YELLOW);
    circ_5.setFill(Color.YELLOW);
    circ_6.setFill(Color.YELLOW);
    circ_7.setFill(Color.YELLOW);
    circ_8.setFill(Color.YELLOW);
    circ_9.setFill(Color.YELLOW);

    circ_10.setFill(Color.YELLOW);
    circ_11.setFill(Color.YELLOW);
    circ_12.setFill(Color.YELLOW);
    circ_13.setFill(Color.YELLOW);
    circ_14.setFill(Color.YELLOW);
    circ_15.setFill(Color.YELLOW);
    circ_16.setFill(Color.YELLOW);
    circ_17.setFill(Color.YELLOW);
    circ_18.setFill(Color.YELLOW);

    circ_19.setFill(Color.YELLOW);
    circ_20.setFill(Color.YELLOW);
    circ_21.setFill(Color.YELLOW);
    circ_22.setFill(Color.YELLOW);
    circ_23.setFill(Color.YELLOW);
    circ_24.setFill(Color.YELLOW);
    circ_25.setFill(Color.YELLOW);
    circ_26.setFill(Color.YELLOW);
    circ_27.setFill(Color.YELLOW);

    circ_28.setFill(Color.YELLOW);
    circ_29.setFill(Color.YELLOW);
    circ_30.setFill(Color.YELLOW);
    circ_31.setFill(Color.YELLOW);
    circ_32.setFill(Color.YELLOW);
    circ_33.setFill(Color.YELLOW);
    circ_34.setFill(Color.YELLOW);
    circ_35.setFill(Color.YELLOW);
    circ_36.setFill(Color.YELLOW);



    }



@FXML
void reset(ActionEvent event) {

    circ_1.setFill(Color.WHITE);
    circ_2.setFill(Color.WHITE);
    circ_3.setFill(Color.WHITE);
    circ_4.setFill(Color.WHITE);
    circ_5.setFill(Color.WHITE);
    circ_6.setFill(Color.WHITE);
    circ_7.setFill(Color.WHITE);
    circ_8.setFill(Color.WHITE);
    circ_9.setFill(Color.WHITE);

    circ_10.setFill(Color.WHITE);
    circ_11.setFill(Color.WHITE);
    circ_12.setFill(Color.WHITE);
    circ_13.setFill(Color.WHITE);
    circ_14.setFill(Color.WHITE);
    circ_15.setFill(Color.WHITE);
    circ_16.setFill(Color.WHITE);
    circ_17.setFill(Color.WHITE);
    circ_18.setFill(Color.WHITE);

    circ_19.setFill(Color.WHITE);
    circ_20.setFill(Color.WHITE);
    circ_21.setFill(Color.WHITE);
    circ_22.setFill(Color.WHITE);
    circ_23.setFill(Color.WHITE);
    circ_24.setFill(Color.WHITE);
    circ_25.setFill(Color.WHITE);
    circ_26.setFill(Color.WHITE);
    circ_27.setFill(Color.WHITE);

    circ_28.setFill(Color.WHITE);
    circ_29.setFill(Color.WHITE);
    circ_30.setFill(Color.WHITE);
    circ_31.setFill(Color.WHITE);
    circ_32.setFill(Color.WHITE);
    circ_33.setFill(Color.WHITE);
    circ_34.setFill(Color.WHITE);
    circ_35.setFill(Color.WHITE);
    circ_36.setFill(Color.WHITE);

           }

    }

这是我的FXML文件:

    <?xml version="1.0" encoding="UTF-8"?>

    <?import java.lang.String?>
    <?import javafx.scene.control.Button?>
    <?import javafx.scene.control.Label?>
    <?import javafx.scene.layout.AnchorPane?>
    <?import javafx.scene.layout.FlowPane?>
    <?import javafx.scene.layout.HBox?>
    <?import javafx.scene.shape.Circle?>

    <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="1004.0" prefWidth="840.0" styleClass="gamecolor" stylesheets="@GamesPlus.css" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="edu.citytech.gamesplus.GamesPlusController">
     <children>
        <FlowPane fx:id="fpGames" layoutX="141.0" layoutY="186.0" prefHeight="610.0" prefWidth="607.0">
     <children>
        <HBox fx:id="Row1" prefHeight="100.0" prefWidth="200.0" styleClass="gamecolor">
           <children>
              <Circle fx:id="circ_1" fill="WHITE" onMouseClicked="#onMouseClicked" radius="45.0" stroke="#1261b7" strokeLineCap="ROUND" strokeLineJoin="ROUND" strokeType="OUTSIDE" strokeWidth="5.0">
                 <styleClass>
                    <String fx:value="custom-label" />
                    <String fx:value="border" />
                 </styleClass></Circle>
              <Circle fx:id="circ_2" fill="WHITE" onMouseClicked="#onMouseClicked" radius="45.0" stroke="#1261b7" strokeLineCap="ROUND" strokeLineJoin="ROUND" strokeType="OUTSIDE" strokeWidth="5.0" styleClass="custom-label" />
              <Circle fx:id="circ_3" fill="WHITE" onMouseClicked="#onMouseClicked" radius="45.0" stroke="#1261b7" strokeLineCap="ROUND" strokeLineJoin="ROUND" strokeType="OUTSIDE" strokeWidth="5.0" styleClass="custom-label" />
              <Circle fx:id="circ_4" fill="WHITE" onMouseClicked="#onMouseClicked" radius="45.0" stroke="#1261b7" strokeLineCap="ROUND" strokeLineJoin="ROUND" strokeType="OUTSIDE" strokeWidth="5.0" styleClass="custom-label" />
              <Circle fx:id="circ_5" fill="WHITE" onMouseClicked="#onMouseClicked" radius="45.0" stroke="#1261b7" strokeLineCap="ROUND" strokeLineJoin="ROUND" strokeType="OUTSIDE" strokeWidth="5.0" styleClass="custom-label" />
              <Circle fx:id="circ_6" fill="WHITE" onMouseClicked="#onMouseClicked" radius="45.0" stroke="#1261b7" strokeLineCap="ROUND" strokeLineJoin="ROUND" strokeType="OUTSIDE" strokeWidth="5.0" styleClass="custom-label" />
           </children>
        </HBox>
        <HBox fx:id="Row2" prefHeight="100.0" prefWidth="200.0">
           <children>
              <Circle fx:id="circ_7" fill="WHITE" onMouseClicked="#onMouseClicked" radius="45.0" stroke="#1261b7" strokeLineCap="ROUND" strokeLineJoin="ROUND" strokeType="OUTSIDE" strokeWidth="5.0" styleClass="custom-label" />
              <Circle fx:id="circ_8" fill="WHITE" onMouseClicked="#onMouseClicked" radius="45.0" stroke="#1261b7" strokeLineCap="ROUND" strokeLineJoin="ROUND" strokeType="OUTSIDE" strokeWidth="5.0" styleClass="custom-label" />
              <Circle fx:id="circ_9" fill="WHITE" onMouseClicked="#onMouseClicked" radius="45.0" stroke="#1261b7" strokeLineCap="ROUND" strokeLineJoin="ROUND" strokeType="OUTSIDE" strokeWidth="5.0" styleClass="custom-label" />
              <Circle fx:id="circ_10" fill="WHITE" onMouseClicked="#onMouseClicked" radius="45.0" stroke="#1261b7" strokeLineCap="ROUND" strokeLineJoin="ROUND" strokeType="OUTSIDE" strokeWidth="5.0" styleClass="custom-label" />
              <Circle fx:id="circ_11" fill="WHITE" onMouseClicked="#onMouseClicked" radius="45.0" stroke="#1261b7" strokeLineCap="ROUND" strokeLineJoin="ROUND" strokeType="OUTSIDE" strokeWidth="5.0" styleClass="custom-label" />
              <Circle fx:id="circ_12" fill="WHITE" onMouseClicked="#onMouseClicked" radius="45.0" stroke="#1261b7" strokeLineCap="ROUND" strokeLineJoin="ROUND" strokeType="OUTSIDE" strokeWidth="5.0" styleClass="custom-label" />
           </children>
        </HBox>
        <HBox fx:id="Row3" prefHeight="100.0" prefWidth="200.0">
           <children>
              <Circle fx:id="circ_13" fill="WHITE" onMouseClicked="#onMouseClicked" radius="45.0" stroke="#1261b7" strokeLineCap="ROUND" strokeLineJoin="ROUND" strokeType="OUTSIDE" strokeWidth="5.0" styleClass="custom-label" />
              <Circle fx:id="circ_14" fill="WHITE" onMouseClicked="#onMouseClicked" radius="45.0" stroke="#1261b7" strokeLineCap="ROUND" strokeLineJoin="ROUND" strokeType="OUTSIDE" strokeWidth="5.0" styleClass="custom-label" />
              <Circle fx:id="circ_15" fill="WHITE" onMouseClicked="#onMouseClicked" radius="45.0" stroke="#1261b7" strokeLineCap="ROUND" strokeLineJoin="ROUND" strokeType="OUTSIDE" strokeWidth="5.0" styleClass="custom-label" />
              <Circle fx:id="circ_16" fill="WHITE" onMouseClicked="#onMouseClicked" radius="45.0" stroke="#1261b7" strokeLineCap="ROUND" strokeLineJoin="ROUND" strokeType="OUTSIDE" strokeWidth="5.0" styleClass="custom-label" />
              <Circle fx:id="circ_17" fill="WHITE" onMouseClicked="#onMouseClicked" radius="45.0" stroke="#1261b7" strokeLineCap="ROUND" strokeLineJoin="ROUND" strokeType="OUTSIDE" strokeWidth="5.0" styleClass="custom-label" />
              <Circle fx:id="circ_18" fill="WHITE" onMouseClicked="#onMouseClicked" radius="45.0" stroke="#1261b7" strokeLineCap="ROUND" strokeLineJoin="ROUND" strokeType="OUTSIDE" strokeWidth="5.0" styleClass="custom-label" />
           </children>
        </HBox>
        <HBox fx:id="Row4" prefHeight="100.0" prefWidth="200.0">
           <children>
              <Circle fx:id="circ_19" fill="WHITE" onMouseClicked="#onMouseClicked" radius="45.0" stroke="#1261b7" strokeLineCap="ROUND" strokeLineJoin="ROUND" strokeType="OUTSIDE" strokeWidth="5.0" styleClass="custom-label" />
              <Circle fx:id="circ_20" fill="WHITE" onMouseClicked="#onMouseClicked" radius="45.0" stroke="#1261b7" strokeLineCap="ROUND" strokeLineJoin="ROUND" strokeType="OUTSIDE" strokeWidth="5.0" styleClass="custom-label" />
              <Circle fx:id="circ_21" fill="WHITE" onMouseClicked="#onMouseClicked" radius="45.0" stroke="#1261b7" strokeLineCap="ROUND" strokeLineJoin="ROUND" strokeType="OUTSIDE" strokeWidth="5.0" styleClass="custom-label" />
              <Circle fx:id="circ_22" fill="WHITE" onMouseClicked="#onMouseClicked" radius="45.0" stroke="#1261b7" strokeLineCap="ROUND" strokeLineJoin="ROUND" strokeType="OUTSIDE" strokeWidth="5.0" styleClass="custom-label" />
              <Circle fx:id="circ_23" fill="WHITE" onMouseClicked="#onMouseClicked" radius="45.0" stroke="#1261b7" strokeLineCap="ROUND" strokeLineJoin="ROUND" strokeType="OUTSIDE" strokeWidth="5.0" styleClass="custom-label" />
              <Circle fx:id="circ_24" fill="WHITE" onMouseClicked="#onMouseClicked" radius="45.0" stroke="#1261b7" strokeLineCap="ROUND" strokeLineJoin="ROUND" strokeType="OUTSIDE" strokeWidth="5.0" styleClass="custom-label" />
           </children>
        </HBox>
        <HBox fx:id="Row5" prefHeight="100.0" prefWidth="200.0">
           <children>
              <Circle fx:id="circ_25" fill="WHITE" onMouseClicked="#onMouseClicked" radius="45.0" stroke="#1261b7" strokeLineCap="ROUND" strokeLineJoin="ROUND" strokeType="OUTSIDE" strokeWidth="5.0" styleClass="custom-label" />
              <Circle fx:id="circ_26" fill="WHITE" onMouseClicked="#onMouseClicked" radius="45.0" stroke="#1261b7" strokeLineCap="ROUND" strokeLineJoin="ROUND" strokeType="OUTSIDE" strokeWidth="5.0" styleClass="custom-label" />
              <Circle fx:id="circ_27" fill="WHITE" onMouseClicked="#onMouseClicked" radius="45.0" stroke="#1261b7" strokeLineCap="ROUND" strokeLineJoin="ROUND" strokeType="OUTSIDE" strokeWidth="5.0" styleClass="custom-label" />
              <Circle fx:id="circ_28" fill="WHITE" onMouseClicked="#onMouseClicked" radius="45.0" stroke="#1261b7" strokeLineCap="ROUND" strokeLineJoin="ROUND" strokeType="OUTSIDE" strokeWidth="5.0" styleClass="custom-label" />
              <Circle fx:id="circ_29" fill="WHITE" onMouseClicked="#onMouseClicked" radius="45.0" stroke="#1261b7" strokeLineCap="ROUND" strokeLineJoin="ROUND" strokeType="OUTSIDE" strokeWidth="5.0" styleClass="custom-label" />
              <Circle fx:id="circ_30" fill="WHITE" onMouseClicked="#onMouseClicked" radius="45.0" stroke="#1261b7" strokeLineCap="ROUND" strokeLineJoin="ROUND" strokeType="OUTSIDE" strokeWidth="5.0" styleClass="custom-label" />
           </children>
        </HBox>
        <HBox fx:id="Row6" prefHeight="100.0" prefWidth="200.0">
           <children>
              <Circle fx:id="circ_31" fill="WHITE" onMouseClicked="#onMouseClicked" radius="45.0" stroke="#1261b7" strokeLineCap="ROUND" strokeLineJoin="ROUND" strokeType="OUTSIDE" strokeWidth="5.0" styleClass="custom-label" />
              <Circle fx:id="circ_32" fill="WHITE" onMouseClicked="#onMouseClicked" radius="45.0" stroke="#1261b7" strokeLineCap="ROUND" strokeLineJoin="ROUND" strokeType="OUTSIDE" strokeWidth="5.0" styleClass="custom-label" />
              <Circle fx:id="circ_33" fill="WHITE" onMouseClicked="#onMouseClicked" radius="45.0" stroke="#1261b7" strokeLineCap="ROUND" strokeLineJoin="ROUND" strokeType="OUTSIDE" strokeWidth="5.0" styleClass="custom-label" />
              <Circle fx:id="circ_34" fill="WHITE" onMouseClicked="#onMouseClicked" radius="45.0" stroke="#1261b7" strokeLineCap="ROUND" strokeLineJoin="ROUND" strokeType="OUTSIDE" strokeWidth="5.0" styleClass="custom-label" />
              <Circle fx:id="circ_35" fill="WHITE" onMouseClicked="#onMouseClicked" radius="45.0" stroke="#1261b7" strokeLineCap="ROUND" strokeLineJoin="ROUND" strokeType="OUTSIDE" strokeWidth="5.0" styleClass="custom-label" />
              <Circle fx:id="circ_36" fill="WHITE" onMouseClicked="#onMouseClicked" radius="45.0" stroke="#1261b7" strokeLineCap="ROUND" strokeLineJoin="ROUND" strokeType="OUTSIDE" strokeWidth="5.0" styleClass="custom-label" />
           </children>
        </HBox>
     </children>
     <styleClass>
        <String fx:value="gamecolor" />
        <String fx:value="border" />
     </styleClass>
  </FlowPane>
  <Label fx:id="Title" layoutX="255.0" layoutY="52.0" styleClass="title-label" text="Connect 4" textFill="#e8d102" />
  <Label fx:id="Name" layoutX="554.0" layoutY="796.0" text="Joshua Carpentier" textFill="#e8d102" />
  <Label fx:id="lbl_Message" layoutX="198.0" layoutY="796.0" text="Label" textFill="#e8d102" />
  <Button fx:id="btn_Reset" layoutX="392.0" layoutY="812.0" mnemonicParsing="false" onAction="#reset" prefHeight="59.0" prefWidth="83.0" styleClass="gamecolor1" text="Reset" textAlignment="CENTER" textFill="#e8d102" />
         </children>
      </AnchorPane>

这是我的connet4service类:

package edu.citytech.service;

import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Arrays; 
import java.util.List;
import org.junit.jupiter.api.Test;
import edu.citytech.dao.WinningCombo;
import edu.citytech.dao.WinningComboDAO;

public class Connect4Service {

    public static int getValidCellMoves(String[] moves, int i) {


             int p = i;
             boolean status;


                     while (p < moves.length) {
             status = moves [p].equals("?");

            if (status) {
                p = p + 7;

            }else{
                break;
            }
        }

            return p;
        }


public static int[] getWinner(String[] moves) {

    int [] status = {};
    WinningComboDAO  dao = new WinningComboDAO();
    List<WinningCombo> list = dao.findAll();
    // Rows

    for (WinningCombo wc : list) {


    if (findWinner(moves[wc.p1], moves[wc.p2], moves[wc.p3], moves[wc.p4])) {
        status = new int [] {wc.p1,wc.p2,wc.p3,wc.p4};
        break;
    }
    }

    return status;
}

private static boolean findWinner(String p1, String p2, String p3, String p4) {
    boolean status = p1.equals(p2)&& p2.equals(p3) && p3.equals(p4)&& !p1.equals("?") ; 


    return status;
     }
  }

这是我的winningcombodao类,它使用带有获奖组合的txt:

package edu.citytech.dao;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

public class WinningComboDAO {

 private static final String WINNING_COMBO = "winning-combination.txt";

public static void main(String[] args) {
    WinningComboDAO  dao = new WinningComboDAO();
    dao.findAll().forEach(System.out::println);
}

public List<WinningCombo> findAll() {
    List<WinningCombo> list = new ArrayList<>();

    InputStream in = WinningComboDAO.class.getResourceAsStream(WINNING_COMBO);

    Stream<String> lines = new BufferedReader(new InputStreamReader(in))
                                  .lines();

    Object[] rows = lines.toArray();
    int p1 = 0, p2 = 0, p3 = 0, p4 = 0 ;
    Boolean isFirstTime = true;

    for (Object currentRow : rows) {
        if(isFirstTime) 
            isFirstTime = false;
        else    {
        String [] columns = currentRow.toString().split(",");
        p1 = Integer.parseInt(columns[2]);
        p2 = Integer.parseInt(columns[3]);
        p3 = Integer.parseInt(columns[4]);
        p4 = Integer.parseInt(columns[5]);

        WinningCombo wc = new WinningCombo(p1,p2,p3,p4);
        list.add(wc);//access data outside

        }
    }
    return list;

   }

}

这是我获奖的组合课:

package edu.citytech.dao;

public class WinningCombo {

public final int p1;
public final int p2;
public final int p3;
public final int p4;


public WinningCombo(int p1, int p2, int p3, int p4) {
    this.p1 = p1;
    this.p2 = p2;
    this.p3 = p3;
    this.p4 = p4;


}


@Override
public String toString() {
    return "WinningCombo [p1=" + p1 + ", p2=" + p2 + ", p3=" + p3 + ", p4=" + p4 + "]";
    }


}

这是一个测试用例,还有很多:

package edu.citytech.test;

import static org.junit.jupiter.api.Assertions.*;
import java.util.Arrays;
import org.junit.jupiter.api.Test;
import edu.citytech.service.Connect4Service;

class TestWinnerOnColum1 {

@Test
void testColum1_0_X() {
    String[] moves = {"X","?","?","?","?","?","?"
                     ,"X","?","?","?","?","?","?"
                     ,"X","?","?","?","?","?","?"
                     ,"X","?","?","?","?","?","?"
                     ,"?","?","?","?","?","?","?"
                     ,"?","?","?","?","?","?","?"};

    int [] winners = Connect4Service.getWinner(moves);
    boolean c1 = winners[0] == 0 && winners[1] == 7
            && winners[2] == 14 && winners[3] == 21;

    assertTrue(c1, "validPosition: " + Arrays.toString(winners));

}


@Test
void testColum1_1_X() {
    String[] moves = {"?","?","?","?","?","?","?"
                     ,"X","?","?","?","?","?","?"
                     ,"X","?","?","?","?","?","?"
                     ,"X","?","?","?","?","?","?"
                     ,"X","?","?","?","?","?","?"
                     ,"?","?","?","?","?","?","?"};

    int [] winners = Connect4Service.getWinner(moves);
    boolean c1 = winners[0] == 7 && winners[1] == 14
            && winners[2] == 21 && winners[3] == 28;

    assertTrue(c1, "validPosition: " + Arrays.toString(winners));

}

@Test
void testColum1_2_X() {
    String[] moves = {"?","?","?","?","?","?","?"
                     ,"?","?","?","?","?","?","?"
                     ,"X","?","?","?","?","?","?"
                     ,"X","?","?","?","?","?","?"
                     ,"X","?","?","?","?","?","?"
                     ,"X","?","?","?","?","?","?"};

    int [] winners = Connect4Service.getWinner(moves);
    boolean c1 = winners[0] == 14 && winners[1] == 21
            && winners[2] == 28 && winners[3] == 35;

    assertTrue(c1, "validPosition: " + Arrays.toString(winners));

}

@Test
void testColum1_0_Circle() {
    String[] moves = {"O","?","?","?","?","?","?"
                     ,"O","?","?","?","?","?","?"
                     ,"O","?","?","?","?","?","?"
                     ,"O","?","?","?","?","?","?"
                     ,"?","?","?","?","?","?","?"
                     ,"?","?","?","?","?","?","?"};

    int [] winners = Connect4Service.getWinner(moves);
    boolean c1 = winners[0] == 0 && winners[1] == 7
            && winners[2] == 14 && winners[3] == 21;

    assertTrue(c1, "validPosition: " + Arrays.toString(winners));

}


@Test
void testColum1_1_Circle() {
    String[] moves = {"?","?","?","?","?","?","?"
                     ,"O","?","?","?","?","?","?"
                     ,"O","?","?","?","?","?","?"
                     ,"O","?","?","?","?","?","?"
                     ,"O","?","?","?","?","?","?"
                     ,"?","?","?","?","?","?","?"};

    int [] winners = Connect4Service.getWinner(moves);
    boolean c1 = winners[0] == 7 && winners[1] == 14
            && winners[2] == 21 && winners[3] == 28;

    assertTrue(c1, "validPosition: " + Arrays.toString(winners));

}

@Test
void testColum1_2_Circle() {
    String[] moves = {"?","?","?","?","?","?","?"
                     ,"?","?","?","?","?","?","?"
                     ,"O","?","?","?","?","?","?"
                     ,"O","?","?","?","?","?","?"
                     ,"O","?","?","?","?","?","?"
                     ,"O","?","?","?","?","?","?"};

    int [] winners = Connect4Service.getWinner(moves);
    boolean c1 = winners[0] == 14 && winners[1] == 21
            && winners[2] == 28 && winners[3] == 35;

    assertTrue(c1, "validPosition: " + Arrays.toString(winners));

}

@Test
void testColum1_NoMatch() {
    String[] moves = {"O","?","?","?","?","?","?"
                     ,"X","?","?","?","?","?","?"
                     ,"X","?","?","?","?","?","?"
                     ,"X","?","?","?","?","?","?"
                     ,"?","?","?","?","?","?","?"
                     ,"?","?","?","?","?","?","?"};

    int [] winners = Connect4Service.getWinner(moves);
    boolean c1 = winners.length == 0;

    assertTrue(c1, "validPosition: " + Arrays.toString(winners));


}

@Test
void testColum1_Empty() {
    String[] moves = {"?","?","?","?","?","?","?"
                     ,"?","?","?","?","?","?","?"
                     ,"?","?","?","?","?","?","?"
                     ,"?","?","?","?","?","?","?"
                     ,"?","?","?","?","?","?","?"
                     ,"?","?","?","?","?","?","?"};

    int [] winners = Connect4Service.getWinner(moves);
    boolean c1 = winners.length == 0;

    assertTrue(c1, "validPosition: " + Arrays.toString(winners));

    }


   }

0 个答案:

没有答案