javafx 오목 게임 실현

9506 단어 javafx오목게임.
필요 설명
한 개의 오목 게임 은 쌍방의 흑백 대결 을 실현 할 수 있 고 한 측 이 이 길 때 제시 메 시 지 를 주 며 GUI 인터페이스 를 이용 하여 이 루어 진다.
프로젝트 구 조 는 아래 그림 과 같다.

실체
Five Chess 클래스
오목 실체 에 포 함 된 모든 정 보 를 제공 합 니 다게임 종료 여 부 를 판단 합 니 다.
  • play 방법 변경 chess[]바둑판 의 데이터
  • 
    package entity;
    
    
    import javafx.scene.control.Alert;
    
    public class FiveChess{
    
     public double getWidth() {
     return width;
     }
    
     public void setWidth(double width) {
     this.width = width;
     }
    
     public double getHeight() {
     return height;
     }
    
     public void setHeight(double height) {
     this.height = height;
     }
    
     public double getCellLen() {
     return cellLen;
     }
    
     public void setCellLen(double cellLen) {
     this.cellLen = cellLen;
     }
    
     /**
    
     *   
    
     */
    
     private int n;
    
     private double width;
    
     private double height;
    
     private double cellLen;
    
     private char currentSide='B';
    
     public char getFlag() {
     return flag;
     }
    
     private char flag=' ';
    
     private char[][] chess;
    
     public char[][] getChess() {
     return chess;
     }
    
     public void setChess(char[][] chess) {
     this.chess = chess;
     }
    
     public char getCurrentSide() {
     return currentSide;
     }
    
     public void setCurrentSide(char currentSide) {
     this.currentSide = currentSide;
     }
    
    
     //     
    
     public FiveChess(double width,double height,double cellLen){
     this.width=width;
     this.height=height;
     this.cellLen=cellLen;
     chess=new char[(int)height][(int)width];
     for(int i=0;i<height;i++)
     for(int j=0;j<width;j++)
     chess[i][j]=' ';
     }
    
    
     public void play(int x,int y){
    
     //         (x,y)
     if(chess[x][y]==' '){
     chess[x][y]=currentSide;
     if(!judgeGame(x,y,currentSide)){
     //         
     Alert alert = new Alert(Alert.AlertType.INFORMATION);
     alert.setTitle("     ");
     alert.setHeaderText("    ");
     alert.setContentText(currentSide+"     !");
    
     alert.showAndWait();
     }
     changeSide();
     }
     }
    
    
     public void changeSide(){
    
     //     
    
     setCurrentSide(currentSide=='B'?'W':'B');
    
     }
    
    
     public boolean judgeGame(int row, int col, char chessColor){
    
     //        
     if(rowJudge(row,col,chessColor)&&colJudge(row,col,chessColor)&&mainDiagonalJudge(row,col,chessColor)&&DeputyDiagonalJudge(row,col,chessColor))
     return true;
     return false;
    
    
     }
    
     public boolean rowJudge(int row,int col,char chessColor){
     //          
     int count=0;
     for(int j=col;j<width;j++){
     if(chess[row][j]!=chessColor)
     break;
     count++;
     }
     for(int j=col-1;j>=0;j--){
     if(chess[row][j]!=chessColor)
     break;
     count++;
     }
     if(count>=5)
     return false;
     return true;
     }
    
     public boolean colJudge(int row,int col,char chessColor){
     //          
     int count=0;
     for(int i=row;i<height;i++){
     if(chess[i][col]!=chessColor)
     break;
     count++;
     }
     for(int i=row-1;i>=0;i--){
     if(chess[i][col]!=chessColor)
     break;
     count++;
     }
     if(count>=5)
     return false;
    
     return true;
     }
    
    
     public boolean mainDiagonalJudge(int row,int col,char chessColor){
     //            
     int count=0;
     for(int i=row,j=col;i<height&&j<width;i++,j++){
     if(chess[i][j]!=chessColor)
     break;
     count++;
     }
    
    
     for(int i=row-1,j=col-1;i>=0&&j>=0;i--,j--){
     if(chess[i][j]!=chessColor)
     break;
     count++;
     }
    
     if(count>=5)
     return false;
    
     return true;
     }
    
    
     public boolean DeputyDiagonalJudge(int row,int col,char chessColor){
     //            
     int count=0;
     for(int i=row,j=col;i>=0&&j<width;i--,j++){
     if(chess[i][j]!=chessColor)
     break;
    
     count++;
     }
    
     for(int i=row+1,j=col-1;i<height&&j>=0;i++,j--){
     if(chess[i][j]!=chessColor)
     break;
    
     count++;
     }
    
    
     if(count>=5)
     return false;
    
     return true;
     }
    }
    보기
    ChessPane 류 는 Pane 류 를 계승 하여 바둑판 과 오목 을 그립 니 다.
    
    package view;
    
    
    import entity.FiveChess;
    import javafx.scene.canvas.Canvas;
    import javafx.scene.canvas.GraphicsContext;
    
    import javafx.scene.layout.Pane;
    import javafx.scene.paint.Color;
    
    
    public class ChessPane extends Pane {
     public Canvas getCanvas() {
     return canvas;
     }
    
     public Canvas canvas;
    
     public GraphicsContext getGc() {
     return gc;
     }
    
     public GraphicsContext gc;
    
     public FiveChess getFiveChess() {
     return fiveChess;
     }
    
     public void setFiveChess(FiveChess fiveChess) {
     this.fiveChess = fiveChess;
     }
    
     public FiveChess fiveChess;
    
    
     public ChessPane(FiveChess fiveChess){
     this.fiveChess=fiveChess;
     double cell=fiveChess.getCellLen();
     drawPane(cell);
     drawChess(cell);
     getChildren().add(canvas);
     }
    
     public void drawPane(double cell){
     canvas = new Canvas(800,700);
     gc = canvas.getGraphicsContext2D();
    
     gc.clearRect(0,0,canvas.getWidth(),canvas.getHeight());
    
     //    
     gc.setStroke(Color.BLACK);
    
     for(int i=0;i<fiveChess.getWidth();i++)
     for(int j=0;j<fiveChess.getHeight();j++){
     gc.strokeRect(100+i*cell,100+cell*j,cell,cell);//            
     }
    
     }
    
     public void drawChess(double cell){
    
     char[][] chess=fiveChess.getChess();
     for(int i=0;i<fiveChess.getHeight();i++)
     for(int j=0;j<fiveChess.getWidth();j++){
     if(chess[i][j]=='B'){
     gc.setFill(Color.BLACK);//     
     gc.fillOval(100+i*cell-cell/2,100+j*cell-cell/2,cell,cell);
     }
     else if(chess[i][j]=='W'){
     gc.setFill(Color.WHITE);
    
     gc.fillOval(100+i*cell-cell/2,100+j*cell-cell/2,cell,cell);//    
     gc.strokeOval(100+i*cell-cell/2,100+j*cell-cell/2,cell,cell);//    
     }
     }
    
     }
    
    }
    컨트롤 러
    playAction 클래스 는 이벤트 프로세서 EventHandler 에서 계승 하여 전달 하 는 매개 변 수 는 마우스 이벤트 입 니 다.마우스 클릭 패 널 이 벤트 를 받 아들 이 는 것 을 의미 합 니 다.
    
    package controller;
    
    import entity.FiveChess;
    import javafx.event.EventHandler;
    import javafx.scene.control.Alert;
    import javafx.scene.input.MouseEvent;
    import view.ChessPane;
    
    public class PlayAction implements EventHandler<MouseEvent> {
    
     /**fiveChess         */
    
     private FiveChess fiveChess;
    
     /**chessPane         */
    
     private ChessPane chessPane;
    
    
     public PlayAction(FiveChess fiveChess,ChessPane chessPane){
     this.chessPane=chessPane;
    
     this.fiveChess = fiveChess;
    
     }
    
     @Override
    
     public void handle(MouseEvent event) {
    
     //        
     double cell=fiveChess.getCellLen();
    
     //event.getX()      x  ,  double  
     double x=event.getX();
     double y=event.getY();
    
     int i=(int)((x-100+cell/2)/cell);
     int j=(int)((y-100+cell/2)/cell);
    
     System.out.println(i+" "+j);
     fiveChess.play(i,j);
     chessPane.drawChess(cell);
     if(!fiveChess.judgeGame(i,j,fiveChess.getCurrentSide()=='B'?'W':'B')){
     Alert alert = new Alert(Alert.AlertType.INFORMATION);
     alert.setTitle("     ");
     alert.setHeaderText("    ");
     alert.setContentText((fiveChess.getCurrentSide()=='B'?" ":" ")+"     !");
     alert.showAndWait();
     }
    
     }
    
    }
    테스트
    
    import controller.PlayAction;
    import entity.FiveChess;
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.Alert;
    import javafx.stage.Stage;
    import view.ChessPane;
    
    import javax.print.attribute.standard.Fidelity;
    
    public class Test extends Application {
    
     public static void main(String[] args) {
     launch(args);
     }
    
    
     @Override
     public void start(Stage primaryStage) {
     FiveChess fiveChess = new FiveChess(20,20,28.0);
     ChessPane chesspane=new ChessPane(fiveChess);
    
     chesspane.setOnMouseClicked(new PlayAction(fiveChess,chesspane));//        
    
     Scene scene=new Scene(chesspane,800,700);
     primaryStage.setScene(scene);
     primaryStage.setTitle("     ");
     primaryStage.show();
    
     }
    }
    효과 도

    이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

    좋은 웹페이지 즐겨찾기