欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Javafx实现国际象棋游戏

程序员文章站 2024-02-23 20:01:28
本文实例为大家分享了javafx实现国际象棋游戏的具体代码,供大家参考,具体内容如下 基本规则 棋子马设计“日”的移动方式 兵设计只能向前直走,每次只能走...

本文实例为大家分享了javafx实现国际象棋游戏的具体代码,供大家参考,具体内容如下

基本规则

  • 棋子马设计“日”的移动方式
  • 兵设计只能向前直走,每次只能走一格。但走第一步时,可以走一格或两格的移动方式
  • 请为后设计横、直、斜都可以走,步数不受限制,但不能越子的移动方式。
  • 车只能横向或者竖向行走
  • 国王是在以自己为中心的九宫格内行走
  • 骑士只能走对角线

项目目录结构

Javafx实现国际象棋游戏

uml类图关系

以骑士为例

Javafx实现国际象棋游戏

实现基本功能

  • 吃子
  • 不能越子
  • 游戏结束提示
  • 基本移动策略
  • 背景音乐

效果

Javafx实现国际象棋游戏

控制器

pressedaction

package com.exercise3;

import com.exercise3.controller.pressedaction;
import com.exercise3.controller.releaseaction;
import com.exercise3.controller.resetaction;
import com.exercise3.view.chessboard;
import com.exercise3.view.chesspane;
import javafx.application.application;
import javafx.geometry.pos;
import javafx.scene.scene;

import javafx.scene.control.button;
import javafx.scene.layout.borderpane;
import javafx.scene.layout.hbox;
import javafx.scene.media.media;
import javafx.scene.media.mediaplayer;
import javafx.stage.stage;

public class test extends application {

 public static void main(string[] args) {
 launch(args);
 }

 @override
 public void start(stage primarystage) {

 string media_url = "file:/e:/ideaprojects/experiment/src/com/exercise3/music/backgroundmusic.mp3";
 chessboard chessboard = chessboard.getinstance(100,40,40);

 //添加媒体资源

 media media = new media(media_url);
 mediaplayer mediaplayer = new mediaplayer(media);
 mediaplayer.setautoplay(true);
 mediaplayer.setcyclecount(mediaplayer.indefinite);
 mediaplayer.play();


 chesspane pane = new chesspane(chessboard);
 pane.setonmousepressed(new pressedaction(pane,mediaplayer));

 pane.setonmousereleased(new releaseaction(pane));

 borderpane borderpane = new borderpane();
 borderpane.setcenter(pane);
 hbox hbox = new hbox();
 hbox.setalignment(pos.top_center);

 button button = new button("悔棋");
 button.setonaction(new resetaction(pane));

 hbox.getchildren().add(button);
 borderpane.setbottom(hbox);
 scene scene = new scene(borderpane,900,900);
 primarystage.setscene(scene);
 primarystage.settitle("国际象棋");
 primarystage.show();

 }
}

releasedaction

package com.exercise3.controller;

import com.exercise3.entity.piece.chesspiece;
import com.exercise3.entity.piecetype;
import com.exercise3.view.chessboard;
import com.exercise3.view.chesspane;
import javafx.event.eventhandler;
import javafx.scene.control.alert;
import javafx.scene.input.mouseevent;

import java.util.stack;

public class releaseaction implements eventhandler<mouseevent> {
 private chesspane chesspane;
 static stack<chesspiece> stack = new stack<>();

 public releaseaction(chesspane chesspane) {
 this.chesspane = chesspane;
 }

 @override
 public void handle(mouseevent e) {
 chesspane.drawboard();
 chessboard chessboard = chesspane.getchessboard();
 int x = (int) ((e.getx() - chessboard.getstartx()) / (chessboard.getcelllength()));
 int y = (int) ((e.gety() - chessboard.getstarty()) / (chessboard.getcelllength()));

 for (chesspiece o : chesspane.getchesspieces()) {
  if (o.isselected()) {

  system.out.println(o.isselected()+" "+o.getrow()+" "+o.getcol());
  if (chessboard.getcurrside()==o.getside()){
   if(o.getmovestrategy().move(x, y,chesspane.getchesspieces())){
   o.setselected(false);
   if(judgegame(x,y)){
    printtip(o.getside());
   }
   eatpiece(x,y);
   stack.push((chesspiece) o.clone());
   o.setcol(x);
   o.setrow(y);

   chessboard.changeside();
   }

  }

  break;
  }

 }

 chesspane.drawpiece();
 }

 public void eatpiece(int x,int y){
 chesspane.getchesspieces().removeif(e->{
  if(e.getcol()==x&&e.getrow()==y){
  stack.push(e);
  return true;
  }
  return false;
 });
 }

 public boolean judgegame(int x,int y){
 for(chesspiece e:chesspane.getchesspieces()){
  if(e.getcol()==x&&e.getrow()==y&&(
   e.gettype()== piecetype.kingblack||e.gettype()==piecetype.kingwhite))
  return true;
 }

 return false;
 }

 public void printtip(char side){
 alert alert = new alert(alert.alerttype.information);
 alert.setcontenttext((side=='b'?"黑":"白")+"方取得胜利");
 alert.settitle("游戏结束");
 alert.showandwait();
 }


}

resetaction

package com.exercise3.controller;

import com.exercise3.entity.piece.chesspiece;
import com.exercise3.view.chesspane;
import javafx.event.actionevent;
import javafx.event.eventhandler;


import java.util.stack;

public class resetaction implements eventhandler<actionevent>{
 private chesspane chesspane;
 public resetaction(chesspane chesspane) {
 this.chesspane = chesspane;
 }

 @override
 public void handle(actionevent e) {
 stack<chesspiece> stack = releaseaction.stack;
 if(!stack.empty()){
  chesspane.getchesspieces().removeif(o->o.equals(stack.peek()));//去除原来的棋子
  chesspane.getchesspieces().add(stack.pop());//将以前压入堆栈的棋子重新加入

  chesspane.drawboard();
  chesspane.drawpiece();
 }
 }
}

实体

棋子

chesspiece

package com.exercise3.entity.piece;

import com.exercise3.entity.piecetype;
import com.exercise3.entity.strategy.carstrategy;

public class car extends chesspiece {
 public car(piecetype type, int row, int col) {
 super(type, row, col);
 setmovestrategy(new carstrategy(getcol(),getrow()));
 }
}

car

package com.exercise3.entity.piece;

import com.exercise3.entity.piecetype;
import com.exercise3.entity.strategy.carstrategy;

public class car extends chesspiece {
 public car(piecetype type, int row, int col) {
 super(type, row, col);
 setmovestrategy(new carstrategy(getcol(),getrow()));
 }
}

horse

package com.exercise3.entity.piece;

import com.exercise3.entity.piecetype;
import com.exercise3.entity.strategy.horsestategy;

public class horse extends chesspiece{
 public horse(piecetype type, int row, int col) {
 super(type, row, col);
 setmovestrategy(new horsestategy(getcol(),getrow()));
 }
}

king

package com.exercise3.entity.piece;

import com.exercise3.entity.piecetype;
import com.exercise3.entity.strategy.kingstrategy;

public class king extends chesspiece {
 public king(piecetype type, int row, int col) {
 super(type, row, col);
 setmovestrategy(new kingstrategy(getcol(),getrow()));
 }
}

knight

package com.exercise3.entity.piece;

import com.exercise3.entity.piecetype;
import com.exercise3.entity.strategy.knightstrategy;

public class knight extends chesspiece {
 public knight(piecetype type, int row, int col) {
 super(type, row, col);
 setmovestrategy(new knightstrategy(getcol(),getrow()));
 }
}

queen

package com.exercise3.entity.piece;

import com.exercise3.entity.piecetype;
import com.exercise3.entity.strategy.queenstrategy;

public class queen extends chesspiece {
 public queen(piecetype type, int row, int col) {
 super(type, row, col);
 setmovestrategy(new queenstrategy(getcol(),getrow()));
 }
}

soldier

package com.exercise3.entity.piece;

import com.exercise3.entity.piecetype;
import com.exercise3.entity.strategy.soldierstategy;

public class soldier extends chesspiece{
 public soldier(piecetype type, int row, int col) {
 super(type, row, col);
 setmovestrategy(new soldierstategy(getcol(),getrow(),getside()));
 }

}

移动策略

movestategy

package com.exercise3.entity.strategy;

import com.exercise3.entity.piece.chesspiece;

import java.util.set;

public interface movestrategy {
 boolean move(int x, int y, set<chesspiece> chesspieces);
}

carstrategy

package com.exercise3.entity.strategy;

import com.exercise3.entity.piece.chesspiece;

import java.util.list;
import java.util.set;

public class carstrategy implements movestrategy {
 private int curx;
 private int cury;

 public carstrategy() {
 }

 public carstrategy(int curx, int cury) {
 this.curx = curx;
 this.cury = cury;
 }

 public boolean move(int x, int y, set<chesspiece> chesspieces) {
 if(x!=curx&&y!=cury)
  return false;
 if(isoverpiece(math.min(curx,x),math.min(cury,y),
  math.max(curx,x),math.max(cury,y),chesspieces))
  return false;
 curx = x;
 cury = y;
 return true;
 }

 public static boolean isoverpiece(int stx,int sty,int edx,int edy,set<chesspiece> chesspieces){
 for(chesspiece e:chesspieces)
  if((e.getrow()>sty&&e.getrow()<edy)&&e.getcol()==stx||
   (e.getcol()>stx&&e.getcol()<edx&&e.getrow()==sty))
  return true;
 return false;
 }


 public int getcurx() {
 return curx;
 }

 public void setcurx(int curx) {
 this.curx = curx;
 }

 public int getcury() {
 return cury;
 }

 public void setcury(int cury) {
 this.cury = cury;
 }
}

horsestrategy

package com.exercise3.entity.strategy;

import com.exercise3.entity.piece.chesspiece;

import java.util.list;
import java.util.set;

public class horsestategy implements movestrategy{
 private int curx;
 private int cury;

 public horsestategy(int curx, int cury) {
 this.curx = curx;
 this.cury = cury;
 }


 @override
 public boolean move(int x, int y, set<chesspiece> chesspieces) {
 if((math.abs(curx-x)==1&&math.abs(cury-y)==2)||
  (math.abs(curx-x)==2&&math.abs(cury-y)==1)){
  curx = x;
  cury = y;
  return true;
 }
 return false;
 }

 public int getcurx() {
 return curx;
 }

 public void setcurx(int curx) {
 this.curx = curx;
 }

 public int getcury() {
 return cury;
 }

 public void setcury(int cury) {
 this.cury = cury;
 }
}

kingstrategy

package com.exercise3.entity.strategy;

import com.exercise3.entity.piece.chesspiece;

import java.util.list;
import java.util.set;

public class kingstrategy implements movestrategy {
 private int curx;
 private int cury;

 public kingstrategy(int curx, int cuy) {
 this.curx = curx;
 this.cury = cuy;
 }

 @override
 public boolean move(int x, int y, set<chesspiece> chesspieces) {
 if(math.abs(curx-x)<=1&&math.abs(cury-y)<=1){
  curx = x;
  cury = y;
  return true;
 }

 return false;
 }

 public int getcurx() {
 return curx;
 }

 public void setcurx(int curx) {
 this.curx = curx;
 }

 public int getcury() {
 return cury;
 }

 public void setcury(int cury) {
 this.cury = cury;
 }
}

knightstrage

package com.exercise3.entity.strategy;

import com.exercise3.entity.piece.chesspiece;

import java.util.list;
import java.util.map;
import java.util.set;

public class knightstrategy implements movestrategy {
 private int curx;
 private int cury;

 public knightstrategy(int curx, int cury) {
 this.curx = curx;
 this.cury = cury;
 }

 @override
 public boolean move(int x, int y, set<chesspiece> chesspieces) {
 if(math.abs(x-curx)==math.abs(y-cury)){
  if(isoverpiece(math.min(curx,x),math.min(cury,y),
   math.max(curx,x),math.max(cury,y),chesspieces))
  return false;
  curx=x;
  cury=y;
  return true;
 }
 return false;
 }

 public static boolean isoverpiece(int stx,int sty,int edx,int edy,set<chesspiece> chesspieces){
 for(chesspiece e:chesspieces){
  if(e.getcol()-stx==edx-e.getcol()&&edy-e.getrow()==e.getrow()-sty){
  system.out.println(e.isselected()+" "+e.getrow()+" "+e.getcol());
  return true;
  }
 }

 return false;
 }
 public int getcurx() {
 return curx;
 }

 public void setcurx(int curx) {
 this.curx = curx;
 }

 public int getcury() {
 return cury;
 }

 public void setcury(int cury) {
 this.cury = cury;
 }
}

queestrategy

package com.exercise3.entity.strategy;

import com.exercise3.entity.piece.chesspiece;

import java.util.list;
import java.util.set;


public class queenstrategy implements movestrategy{
 private int curx;
 private int cury;

 public queenstrategy(int curx, int cury) {
 this.curx = curx;
 this.cury = cury;
 }

 @override
 public boolean move (int x, int y, set<chesspiece> chesspieces) {
 if(math.abs(x-curx)==math.abs(y-cury)||!(x!=curx&&y!=cury)){
  if(isoverpiece(math.min(curx,x),math.min(cury,y),
   math.max(curx,x),math.max(cury,y),chesspieces))
  return false;
  curx = x;
  cury = y;
  return true;
 }
 return false;
 }

 public boolean isoverpiece (int stx,int sty,int edx,int edy,set<chesspiece> chesspieces) {
 for(chesspiece e:chesspieces){
  if(e.getrow()!=sty&&e.getcol()!=stx){
  return knightstrategy.isoverpiece(stx,sty,edx,edy,chesspieces);
  }
  else{
  return carstrategy.isoverpiece(stx,sty,edx,edy,chesspieces);
  }
 }
 return false;
 }


 public int getcurx() {
 return curx;
 }

 public void setcurx(int curx) {
 this.curx = curx;
 }

 public int getcury() {
 return cury;
 }

 public void setcury(int cury) {
 this.cury = cury;
 }
}

soldierstrategy

package com.exercise3.entity.strategy;

import com.exercise3.entity.piece.chesspiece;

import java.util.list;
import java.util.set;

public class soldierstategy implements movestrategy{
 private int curx;
 private int cury;
 private char side;
 private boolean firstmove = true;

 public soldierstategy(int curx, int cury,char side) {
 this.curx = curx;
 this.cury = cury;
 this.side = side;
 }


 @override
 public boolean move(int x, int y, set<chesspiece> chesspieces) {
 //直线移动
 if(cury==y){
  switch (side){
  case 'b': {
   if(isfirstmove()&&(x==curx+1||curx+2==x)){
   setfirstmove(false);
   cury = y;
   curx = x;
   return true;
   }
   else if(!isfirstmove()&&curx+1==x){
   cury = y;
   curx = x;
   return true;
   }
   break;
  }

  case 'w':{
   if(isfirstmove()&&(x==curx-1||x==curx-2)){
   setfirstmove(false);
   cury = y;
   curx = x;
   return true;
   }
   else if(!isfirstmove()&&curx-1==x){
   cury = y;
   curx = x;
   return true;
   }
   break;
  }
  }
 }

 //吃子移动
 for(chesspiece e:chesspieces){
  if(math.abs(e.getrow()-cury)==1){
  if(e.getcol()-curx==1&&e.getside()=='w'||
  curx-e.getcol()==1&&e.getside()=='b'){
   cury = y;
   curx = x;
   return true;
  }

  }
 }

 return false;
 }



 public boolean isfirstmove() {
 return firstmove;
 }

 public void setfirstmove(boolean firstmove) {
 this.firstmove = firstmove;
 }

 public int getcurx() {
 return curx;
 }

 public void setcurx(int curx) {
 this.curx = curx;
 }

 public int getcury() {
 return cury;
 }

 public void setcury(int cury) {
 this.cury = cury;
 }
}

棋子类型

package com.exercise3.entity;

public enum piecetype {
 kingblack("kingblack","com/exercise3/img/kingblack.jpg"),
 queenblack("queenblack","com/exercise3/img/queenblack.jpg"),
 carblack("carblack","com/exercise3/img/carblack.jpg"),
 horseblack("horseblack","com/exercise3/img/horseblack.jpg"),
 soldierblack("soldierblack","com/exercise3/img/soldierblack.jpg"),
 knightblack("knightblack","com/exercise3/img/knightblack.jpg"),

 kingwhite("kingwhite","com/exercise3/img/kingwhite.jpg"),
 queenwhite("queenwhite","com/exercise3/img/queenwhite.jpg"),
 carwhite("carwhite","com/exercise3/img/carwhite.jpg"),
 horsewhite("horsewhite","com/exercise3/img/horsewhite.jpg"),
 soldierwhite("soldierwhite","com/exercise3/img/soldierwhite.jpg"),
 knightwhite("knightwhite","com/exercise3/img/knightwhite.jpg");


 private string desc;
 private piecetype(string desc,string url ){
 this.desc = desc;
 this.url = url;
 }

 private string url;

 public string getdesc(){
 return desc;
 }

 public string geturl() {
 return url;
 }
}

视图

package com.exercise3.view;

public class chessboard {
 static chessboard chessboard = null;
 private int row;
 private int col;
 private double celllength;
 private double startx;
 private double starty;
 private char currside;

 private chessboard(double celllength, double startx, double starty) {
 this.row = 8;
 this.col = 8;
 this.celllength = celllength;
 this.startx = startx;
 this.starty = starty;
 this.currside = 'b';
 }

 public static chessboard getinstance(double celllength, double startx, double starty){
 if(chessboard == null)
  return new chessboard(celllength,startx,starty);
 return chessboard;
 }

 public chessboard getinstance(){
 return chessboard;
 }

 public int getcol() {
 return col;
 }


 public int getrow() {
 return row;
 }

 public double getcelllength() {
 return celllength;
 }

 public void changeside(){
 currside=(currside=='b'?'w':'b');
 }

 public void setcelllength(double celllength) {
 this.celllength = celllength;
 }

 public double getstartx() {
 return startx;
 }

 public void setstartx(double startx) {
 this.startx = startx;
 }

 public double getstarty() {
 return starty;
 }

 public void setstarty(double starty) {
 this.starty = starty;
 }

 public char getcurrside() {
 return currside;
 }
}
package com.exercise3.view;

import com.exercise3.entity.piece.*;
import com.exercise3.entity.piecetype;
import javafx.scene.canvas.canvas;
import javafx.scene.canvas.graphicscontext;
import javafx.scene.image.*;
import javafx.scene.layout.pane;
import javafx.scene.paint.color;

import java.util.*;

public class chesspane extends pane {
 private set<chesspiece> chesspieces;
 private chessboard chessboard;
 private canvas canvas;
 private graphicscontext gc;

 public chesspane(chessboard chessboard) {
 this.chessboard = chessboard;
 setchesspiece();
 canvas = new canvas(900,900);
 gc = canvas.getgraphicscontext2d();
 draw();
 }


 public void draw(){
 drawboard();
 drawpiece();
 getchildren().add(canvas);
 }


 public void drawboard(){
 gc.clearrect(0,0,900,900);
 double x = chessboard.getstartx();
 double y = chessboard.getstarty();
 double cell = chessboard.getcelllength();


 boolean flag = false;
 for(int i=0;i<chessboard.getrow();i++){
  flag = !flag;
  for(int j=0;j<chessboard.getcol();j++){
  gc.setfill(flag? color.valueof("#ededed"):color.valueof("cdc5bf"));
  gc.fillrect(x+j*cell,y+i*cell,cell,cell);
  flag = !flag;
  }
 }


 gc.setstroke(color.gray);
 gc.strokerect(x,y,cell*chessboard.getcol(),cell*chessboard.getrow());

 }

 public void drawpiece(){
 double cell = chessboard.getcelllength();
 chesspieces.foreach( e->{
  if(e.isselected()){
  gc.setfill(color.valueof("#6495ed"));
  gc.fillrect(chessboard.getstartx()+e.getcol()*cell,
   chessboard.getstarty()+e.getrow()*cell,
   cell,cell);
  }

  image image = new image(e.gettype().geturl());
  gc.drawimage(image,
   chessboard.getstartx()+10 + e.getcol() * cell,
   chessboard.getstarty()+10 + e.getrow() * cell,
   cell-20, cell-20);
 });
 }


 //加入棋子
 public void setchesspiece() {
 chesspieces = new hashset<>();
 chesspieces.add(new car(piecetype.carblack,0,0));
 chesspieces.add(new horse(piecetype.horseblack,1,0));
 chesspieces.add(new knight(piecetype.knightblack,2,0));
 chesspieces.add(new king(piecetype.kingblack,3,0));
 chesspieces.add(new queen(piecetype.queenblack,4,0));
 chesspieces.add(new knight(piecetype.knightblack,5,0));
 chesspieces.add(new horse(piecetype.horseblack,6,0));
 chesspieces.add(new car(piecetype.carblack,7,0));
 for(int i=0;i<8;i++){
  chesspieces.add(new soldier(piecetype.soldierblack,i,1));
 }


 chesspieces.add(new car(piecetype.carwhite,0,7));
 chesspieces.add(new horse(piecetype.horsewhite,1,7));
 chesspieces.add(new knight(piecetype.knightwhite,2,7));
 chesspieces.add(new king(piecetype.kingwhite,3,7));
 chesspieces.add(new queen(piecetype.queenwhite,4,7));
 chesspieces.add(new knight(piecetype.knightwhite,5,7));
 chesspieces.add(new horse(piecetype.horsewhite,6,7));
 chesspieces.add(new car(piecetype.carwhite,7,7));
 for(int i=0;i<8;i++){
  chesspieces.add(new soldier(piecetype.soldierwhite,i,6));
 }
 }

 public chessboard getchessboard() {
 return chessboard;
 }

 public void setchessboard(chessboard chessboard) {
 this.chessboard = chessboard;
 }

 public set<chesspiece> getchesspieces() {
 return chesspieces;
 }

 public void setchesspieces(set<chesspiece> chesspieces) {
 this.chesspieces = chesspieces;
 }

 public canvas getcanvas() {
 return canvas;
 }

 public void setcanvas(canvas canvas) {
 this.canvas = canvas;
 }

 public graphicscontext getgc() {
 return gc;
 }

 public void setgc(graphicscontext gc) {
 this.gc = gc;
 }
}

测试

package com.exercise3;

import com.exercise3.controller.pressedaction;
import com.exercise3.controller.releaseaction;
import com.exercise3.controller.resetaction;
import com.exercise3.view.chessboard;
import com.exercise3.view.chesspane;
import javafx.application.application;
import javafx.geometry.pos;
import javafx.scene.scene;

import javafx.scene.control.button;
import javafx.scene.layout.borderpane;
import javafx.scene.layout.hbox;
import javafx.scene.media.media;
import javafx.scene.media.mediaplayer;
import javafx.stage.stage;

public class test extends application {

 public static void main(string[] args) {
 launch(args);
 }

 @override
 public void start(stage primarystage) {

 string media_url = "file:/e:/ideaprojects/experiment/src/com/exercise3/music/backgroundmusic.mp3";
 chessboard chessboard = chessboard.getinstance(100,40,40);

 //添加媒体资源

 media media = new media(media_url);
 mediaplayer mediaplayer = new mediaplayer(media);
 mediaplayer.setautoplay(true);
 mediaplayer.setcyclecount(mediaplayer.indefinite);
 mediaplayer.play();


 chesspane pane = new chesspane(chessboard);
 pane.setonmousepressed(new pressedaction(pane,mediaplayer));

 pane.setonmousereleased(new releaseaction(pane));

 borderpane borderpane = new borderpane();
 borderpane.setcenter(pane);
 hbox hbox = new hbox();
 hbox.setalignment(pos.top_center);

 button button = new button("悔棋");
 button.setonaction(new resetaction(pane));

 hbox.getchildren().add(button);
 borderpane.setbottom(hbox);
 scene scene = new scene(borderpane,900,900);
 primarystage.setscene(scene);
 primarystage.settitle("国际象棋");
 primarystage.show();

 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。