游戏设计五子棋游戏软件设计.docx
- 文档编号:9392704
- 上传时间:2023-02-04
- 格式:DOCX
- 页数:17
- 大小:17.80KB
游戏设计五子棋游戏软件设计.docx
《游戏设计五子棋游戏软件设计.docx》由会员分享,可在线阅读,更多相关《游戏设计五子棋游戏软件设计.docx(17页珍藏版)》请在冰豆网上搜索。
游戏设计五子棋游戏软件设计
importjava.awt.*;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.MouseEvent;
importjava.awt.event.MouseListener;
importjava.io.*;
importjava.util.*;
importjavax.swing.*;
classChessFrameextendsJFrame{
publicstaticbooleancomputer_Start=false;
publicChessFrame(){
super.setTitle("五子棋");
super.setSize(800,800);
super.setBackground(Color.WHITE);
JMenumenuFile=newJMenu("菜单");
JMenunewItem=newJMenu("新游戏");
JMenuItemnewItem_1=newJMenuItem("先手");
JMenuItemnewItem_2=newJMenuItem("后手");
newItem.add(newItem_1);
newItem.add(newItem_2);
JMenuItemopenItem=newJMenuItem("打开");
JMenuItemcloseItem=newJMenuItem("保存");
JMenuItemexitItem=newJMenuItem("退出");
newItem_1.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
System.out.println("===选择了“新游戏==先手”菜单项");
newGame();
ImagePanel.flag_NewGame=true;
}
});
newItem_2.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
System.out.println("===选择了“新游戏==后手”菜单项");
newGame();
computer_Start=true;
}
});
openItem.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
System.out.println("===选择了“打开”菜单项");
}
});
closeItem.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
System.out.println("===选择了“保存”菜单项");
}
});
exitItem.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
System.out.println("===选择了“退出”菜单项");
System.exit(0);
}
});
menuFile.add(newItem);
menuFile.add(openItem);
menuFile.add(closeItem);
menuFile.add(exitItem);
JMenuBarmenuBar=newJMenuBar();
menuBar.add(menuFile);
super.setJMenuBar(menuBar);
ImagePanelimagePanel=newImagePanel();
super.add(imagePanel);
super.setVisible(true);
super.setResizable(false);
super.setLocationRelativeTo(getOwner());
}
publicvoidnewGame(){
super.dispose();
FiveChess.start();
}
}
classImagePanelextendsJPanelimplementsMouseListener{
publicstaticbooleanflag_NewGame=false;
privatebooleanflag_BLACK=true;
privatebooleanflag_WHITE=false;
privateint[][]win_Count=newint[2][572];
privateboolean[][][]white_Win=newboolean[15][15][572];
privateboolean[][][]black_Win=newboolean[15][15][572];
privateint[][]chess=newint[15][15];
publicLinkedListlist=newLinkedList();
intcount=0;
publicImagePanel(){
//横
for(inti=0;i<15;i++){
for(intj=0;j<11;j++){
for(intk=0;k<5;k++){
black_Win[j+k][i][count]=true;
white_Win[j+k][i][count]=true;
}
count++;
}
}
//竖
for(inti=0;i<15;i++){
for(intj=0;j<11;j++){
for(intk=0;k<5;k++){
black_Win[i][j+k][count]=true;
white_Win[i][j+k][count]=true;
}
count++;
}
}
//右斜
for(inti=0;i<11;i++){
for(intj=0;j<11;j++){
for(intk=0;k<5;k++){
black_Win[j+k][i+k][count]=true;
white_Win[j+k][i+k][count]=true;
}
count++;
}
}
//左斜
for(inti=0;i<11;i++){
for(intj=14;j>=4;j--){
for(intk=0;k<5;k++){
black_Win[j-k][i+k][count]=true;
white_Win[j+-k][i+k][count]=true;
}
count++;
}
}
System.out.println("count----"+count);
for(intm=0;m<2;m++){
for(intn=0;n<572;n++){
win_Count[m][n]=0;
}
}
super.addMouseListener(this);
}
publicvoidpaint_BLACk(Graphicsg,intxx,intyy){
Graphics2Dg2=(Graphics2D)g;
g2.setColor(Color.BLACK);
g2.setStroke(newBasicStroke
(2));
g2.fillArc(xx-14,yy-14,30,30,0,360);
}
publicvoidpaint_WHITE(Graphicsg,intxx,intyy){
Graphics2Dg2=(Graphics2D)g;
g2.setColor(Color.WHITE);
g2.setStroke(newBasicStroke
(2));
g2.fillArc(xx-14,yy-14,30,30,0,360);
}
publicbooleanisWin(intx,inty,inttemp){//X为横坐标,Y为纵坐标,temp为当前颜色
for(inti=0;i<11;i++){//横
if(chess[i][y]==temp&&chess[i+1][y]==temp
&&chess[i+2][y]==temp&&chess[i+3][y]==temp
&&chess[i+4][y]==temp){
returntrue;
}
}
for(intj=0;j<11;j++){//竖
if(chess[x][j]==temp&&chess[x][j+1]==temp
&&chess[x][j+2]==temp&&chess[x][j+3]==temp
&&chess[x][j+4]==temp){
returntrue;
}
}
for(intm=4;m<15;m++){//左斜
for(intn=0;n<11;n++){
if(chess[m][n]==temp&&chess[m-1][n+1]==temp
&&chess[m-2][n+2]==temp
&&chess[m-3][n+3]==temp
&&chess[m-4][n+4]==temp){
returntrue;
}
}
}
for(intp=0;p<11;p++){//右斜
for(intq=0;q<11;q++){
if(chess[p][q]==temp&&chess[p+1][q+1]==temp
&&chess[p+2][q+2]==temp
&&chess[p+3][q+3]==temp
&&chess[p+4][q+4]==temp){
returntrue;
}
}
}
returnfalse;
}
publicvoidpaint(Graphicsg){
Graphics2Dg2=(Graphics2D)g;
super.paintComponent(g2);
//StringpicPath="image/qipan.jpg";
StringpicPath="d:
"+File.separator+"qipan.jpg";
Iconimg=newImageIcon(picPath);
img.paintIcon(this,g,121,121);
if(ChessFrameputer_Start){
this.paint_WHITE(this.getGraphics(),7*35+140,7*35+140);
this.white_Opera(7,7);
ChessFrameputer_Start=false;
flag_NewGame=true;
}
if(list!
=null){
Iteratorit=list.iterator();
while(it.hasNext()){
Aa=(A)it.next();
if(a.get_a_Color()==1){
paint_BLACk(g,a.get_a_X()*35+140,
a.get_a_Y()*35+140);
}elseif(a.get_a_Color()==2){
paint_WHITE(g,a.get_a_X()*35+140,
a.get_a_Y()*35+140);
}
}
}
}
classA{
privateinta_X;
privateinta_Y;
privateinta_Color;
publicA(intx,inty,intz){
this.a_X=x;
this.a_Y=y;
this.a_Color=z;
}
publicintget_a_X(){
returnthis.a_X;
}
publicintget_a_Y(){
returnthis.a_Y;
}
publicintget_a_Color(){
returnthis.a_Color;
}
}
publicvoidmouseClicked(MouseEvente){
if(flag_NewGame){
intc=e.getButton();
StringmouseInfo=null;
if(c==MouseEvent.BUTTON1){
mouseInfo="左键";
Pointp=e.getPoint();
System.out.println(p.toString());
intx=(int)p.getX();
inty=(int)p.getY();
intxx=0;
intyy=0;
if(x>=140&&x<=630&&y>=140&&y<=630){
intquotient_X=(x-140)/35;
intcompliment_X=(x-140)%35;
intquotient_Y=(y-140)/35;
intcompliment_Y=(y-140)%35;
if(compliment_X<=17){
xx=(quotient_X)*35+140;
}elseif(compliment_X>17){
quotient_X=quotient_X+1;
xx=quotient_X*35+140;
}
if(compliment_Y<=17){
yy=(quotient_Y)*35+140;
}elseif(compliment_Y>17){
quotient_Y=quotient_Y+1;
yy=quotient_Y*35+140;
}
System.out.println(xx+"---"+yy);
if(flag_BLACK&&chess[quotient_X][quotient_Y]==0){
System.out.println(quotient_X+"---"+quotient_Y);
this.paint_BLACk(this.getGraphics(),xx,yy);
chess[quotient_X][quotient_Y]=1;
list.add(newA(quotient_X,quotient_Y,1));
for(inti=0;i<572;i++){
if(black_Win[quotient_X][quotient_Y][i]
&&win_Count[0][i]!
=-1){
win_Count[0][i]++;
}
if(white_Win[quotient_X][quotient_Y][i]){
white_Win[quotient_X][quotient_Y][i]=false;
win_Count[1][i]=-1;
}
}
inttemp=1;
if(isWin(quotient_X,quotient_Y,temp)){
JOptionPane.showMessageDialog(this,"黑棋赢");
Graphicsg=this.getGraphics();
g.setColor(Color.RED);
g.setFont(newFont("华文行楷",0,20));
g.drawString("很帅的黑棋赢了",300,80);
flag_WHITE=false;
flag_BLACK=false;
flag_NewGame=false;
}else{
computer_chess();
}
}elseif(flag_WHITE&&chess[quotient_X][quotient_Y]==0){
}else{
JOptionPane.showMessageDialog(this,"该位置不能下子");
}
}
}
if(c==MouseEvent.BUTTON3){
mouseInfo="右键";
}
if(c==MouseEvent.BUTTON2){
mouseInfo="滚轴";
}
}
}
privatevoidcomputer_chess(){
int[][]black_Score=newint[15][15];
int[][]white_Score=newint[15][15];
intwhite_Temp_Score=0;
intblack_Temp_Score=0;
intwhite_x=0;
intwhite_y=0;
intblack_x=0;
intblack_y=0;
intx;
inty;
for(inti=0;i<15;i++){
for(intj=0;j<15;j++){
white_Score[i][j]=0;
if(chess[i][j]==0){
for(intk=0;k<572;k++){
if(white_Win[i][j][k]){
switch(win_Count[1][k]){
case1:
white_Score[i][j]+=5;
break;
case2:
white_Score[i][j]+=50;
break;
case3:
white_Score[i][j]+=180;
break;
case4:
white_Score[i][j]+=400;
break;
}
}
}
}
black_Score[i][j]=0;
if(chess[i][j]==0){
for(intk=0;k<572;k++){
if(black_Win[i][j][k]){
switch(win_Count[0][k]){
case1:
black_Score[i][j]+=6;
break;
case2:
black_Score[i][j]+=52;
break;
case3:
black_Score[i][j]+=181;
break;
case4:
black_Score[i][j]+=410;
break;
}
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 游戏 设计 五子棋 游戏软件