欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

javafx實現(xiàn)五子棋游戲

 更新時間:2020年07月27日 08:54:06   作者:逆光al  
這篇文章主要為大家詳細介紹了javafx實現(xiàn)五子棋游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

需求描述

一個五子棋游戲,能實現(xiàn)雙方黑白對決,當一方獲勝時給出提示信息,利用GUI界面實現(xiàn)

項目結(jié)構(gòu)如下圖

一、實體

FiveChess類

  • 提供五子棋實體包含的所有信息
  • 判斷游戲是否結(jié)束
  • play方法改變chess[][]棋盤中的數(shù)據(jù)
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)){
 //游戲結(jié)束彈出信息框
 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){

 //判斷游戲是否結(jié)束
 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類實現(xiàn)棋盤和五子棋的繪制

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);//清理一個矩形取區(qū)域的內(nèi)容
 }

 }

 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);//設(shè)置填充色
 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并傳遞的參數(shù)是鼠標事件,表示接受鼠標點擊面板事件

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();

 }
}

效果圖

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot應(yīng)用啟動過程分析

    SpringBoot應(yīng)用啟動過程分析

    這篇文章主要介紹了SpringBoot應(yīng)用啟動過程分析,需要的朋友可以參考下
    2017-08-08
  • java中timer的schedule和scheduleAtFixedRate方法區(qū)別詳解

    java中timer的schedule和scheduleAtFixedRate方法區(qū)別詳解

    這篇文章主要為大家詳細介紹了java中timer的schedule和scheduleAtFixedRate方法區(qū)別,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • IO密集型任務(wù)設(shè)置線程池線程數(shù)實現(xiàn)方式

    IO密集型任務(wù)設(shè)置線程池線程數(shù)實現(xiàn)方式

    這篇文章主要介紹了IO密集型任務(wù)設(shè)置線程池線程數(shù)實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • SpringBoot+Redis Bitmap實現(xiàn)活躍用戶統(tǒng)計

    SpringBoot+Redis Bitmap實現(xiàn)活躍用戶統(tǒng)計

    Redis的Bitmap數(shù)據(jù)結(jié)構(gòu)是一種緊湊的位圖,它可以用于實現(xiàn)各種場景,其中統(tǒng)計活躍用戶是一種經(jīng)典的業(yè)務(wù)場景,下面我們就來學習一下SpringBoot如何利用Redis中的Bitmap實現(xiàn)活躍用戶統(tǒng)計吧
    2023-11-11
  • Java兩個變量的互換(不借助第3個變量)具體實現(xiàn)方法

    Java兩個變量的互換(不借助第3個變量)具體實現(xiàn)方法

    這篇文章主要介紹了Java兩個變量的互換(不借助第3個變量)具體實現(xiàn)方法,需要的朋友可以參考下
    2014-02-02
  • java中BigDecimal進行加減乘除的基本用法

    java中BigDecimal進行加減乘除的基本用法

    大家應(yīng)該對于不需要任何準確計算精度的數(shù)字可以直接使用float或double運算,但是如果需要精確計算的結(jié)果,則必須使用BigDecimal類,而且使用BigDecimal類也可以進行大數(shù)的操作。下面這篇文章就給大家介紹介紹關(guān)于java中BigDecimal進行加減乘除的基本用法。
    2016-12-12
  • Java?SWT中常見彈出框?qū)嵗偨Y(jié)

    Java?SWT中常見彈出框?qū)嵗偨Y(jié)

    剛開始寫Java工具的小伙伴可能不知道怎么寫消息對話框,在這里總結(jié)一些常用的幾種消息彈出框,下面這篇文章主要給大家介紹了關(guān)于Java?SWT中常見彈出框的相關(guān)資料,需要的朋友可以參考下
    2023-01-01
  • Java?8?Stream?處理數(shù)據(jù)方法匯總

    Java?8?Stream?處理數(shù)據(jù)方法匯總

    這篇文章主要介紹了Java?8?Stream處理數(shù)據(jù),Stream是Java?8?新引入的一個包它讓我們能用聲明式的方式處理數(shù)據(jù),Stream流式處理相較于傳統(tǒng)方法簡潔高效,也便于進行并發(fā)編程,更多相關(guān)內(nèi)容需要的小伙伴可以參考下面文章內(nèi)容
    2022-06-06
  • SpringBoot整合log4j日志與HashMap的底層原理解析

    SpringBoot整合log4j日志與HashMap的底層原理解析

    這篇文章主要介紹了SpringBoot整合log4j日志與HashMap的底層原理,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • 使用Servlet處理一個上傳的文件

    使用Servlet處理一個上傳的文件

    今天小編就為大家分享一篇關(guān)于使用Servlet處理一個上傳的文件,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01

最新評論