javafx 오목 게임 실현
한 개의 오목 게임 은 쌍방의 흑백 대결 을 실현 할 수 있 고 한 측 이 이 길 때 제시 메 시 지 를 주 며 GUI 인터페이스 를 이용 하여 이 루어 진다.
프로젝트 구 조 는 아래 그림 과 같다.
실체
Five 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();
}
}
효과 도이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
자바 Fx TextArea 커서 포 지 셔 닝 실현1. 자바 FX TextArea 가 코드 에서 커서 를 찾 으 려 면 TextArea 구성 요소 의 positionCaret 속성 만 설정 해 야 합 니 다. 이렇게 하면 커서 를 4 의 위치 로 찾 을 수 있 습 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.