五子棋javaWord文档下载推荐.docx
- 文档编号:16729611
- 上传时间:2022-11-25
- 格式:DOCX
- 页数:25
- 大小:18.92KB
五子棋javaWord文档下载推荐.docx
《五子棋javaWord文档下载推荐.docx》由会员分享,可在线阅读,更多相关《五子棋javaWord文档下载推荐.docx(25页珍藏版)》请在冰豆网上搜索。
booleanturn;
Stringmessage;
finalJDialogdialog=newJDialog(this,"
叫匡"
true);
Fontfont=newFont("
new_font"
Font.BOLD,20);
Gridgrids[][]=newGrid[length][length];
Imagewhite=tk.getImage(cl.getResource("
res/white.png"
));
Imageblack=tk.getImage(cl.getResource("
res/black.png"
Imagetitle=tk.getImage(cl.getResource("
res/title.png"
Imagetemp;
JPanelboardPanel,bigpanel;
JRadioButton[]choice=newJRadioButton[2];
finalstaticintStart=0;
finalstaticintSelect=1;
finalstaticintPlaying=2;
finalstaticintEnd=3;
finalstaticintnil=-1;
/*礚よ*/
finalstaticintoblique_1=0;
/*オ*/
finalstaticintoblique_2=1;
/*オ*/
finalstaticinthorizontal=2;
/*绢*/
finalstaticintvertical=3;
/**/
Gobang()
{
super("
き囱"
);
boardPanel=newJPanel();
boardPanel.setLayout(newGridLayout(length,length,0,0));
boardPanel.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
for(inti=0;
i<
length;
i++)
for(intj=0;
j<
j++)
grids[i][j]=newGrid(i,j);
boardPanel.add(grids[i][j]);
}
bigpanel=newJPanel();
bigpanel.add(boardPanel,BorderLayout.CENTER);
getContentPane().add(bigpanel,BorderLayout.SOUTH);
game_state=Start;
ThreadStart();
dialog.setSize(160,100);
dialog.setResizable(false);
dialog.setLocationRelativeTo(null);
ButtonGroupchoice_group=newButtonGroup();
JPanelchoice_menu=newJPanel();
choice[0]=newJRadioButton("
堵"
newImageIcon(black),true);
choice[1]=newJRadioButton("
フ"
newImageIcon(white));
choice.length;
choice_menu.add(choice[i]);
choice_group.add(choice[i]);
command.length;
command[i]=newJMenuItem(command_str[i]);
command[i].addActionListener(this);
control_menu.add(command[i]);
JButtonselect=newJButton("
絋﹚"
JPanelselect_menu=newJPanel();
select_menu.add(select);
select.addActionListener(this);
dialog.getContentPane().add(choice_menu,BorderLayout.NORTH);
dialog.getContentPane().add(select_menu,BorderLayout.CENTER);
setIconImage(title);
setResizable(false);
setSize(300,335);
setVisible(true);
setLocationRelativeTo(null);
publicstaticvoidmain(String[]arg)
Gobangapplication=newGobang();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
publicvoidactionPerformed(ActionEventevent)
if(event.getSource().equals(command[0]))
if(steps.size()!
=0)
undo();
elseif(event.getSource().equals(command[1]))
Save();
elseif(event.getSource().equals(command[2]))
Load();
elseif(event.getSource().equals(command[3]))
ReStart();
else
if(choice[1].isSelected())
temp=white;
white=black;
black=temp;
dialog.dispose();
publicvoidWinCheck()
check=turn?
Player:
AI;
direction=oblique_1;
WinCheck2();
publicvoidWinCheck2()
count=1;
switch(direction)
caseoblique_1:
displace_x=1;
displace_y=-1;
direction=oblique_2;
break;
caseoblique_2:
displace_x=displace_y=1;
direction=horizontal;
casehorizontal:
displace_y=0;
direction=vertical;
casevertical:
displace_x=0;
displace_y=1;
direction=nil;
x=locX+displace_x;
y=locY+displace_y;
while(x>
=0&
&
x<
length&
y>
y<
grid[x][y]==check)
count=count+1;
x=x+displace_x;
y=y+displace_y;
x=locX-displace_x;
y=locY-displace_y;
x=x-displace_x;
y=y-displace_y;
if(count>
=5)
game_state=End;
winner=check;
elseif(direction!
=nil)
publicvoidReStart()
j++)
grids[i][j].Initial();
winner=0;
steps.clear();
game_state=Playing;
publicvoidgobangRandom()
do
displace_x=(int)(Math.random()*8);
x=locX+dir[displace_x][0];
y=locY+dir[displace_x][1];
if(!
dir2[displace_x])
displace_y=displace_y+1;
dir2[displace_x]=true;
}while((x<
0||x>
=length||y<
0||y>
=length||grid[x][y]!
=0)&
displace_y<
8);
8;
dir2[i]=false;
if(x>
grid[x][y]==0)
setMark(x,y);
gobangRandom2();
privatevoidgobangRandom2()
x=(int)(Math.random()*length);
y=(int)(Math.random()*length);
}while(grid[x][y]!
=0);
privatevoidgobangAI()
booleanplay=true;
for(inti=2;
i>
0;
i--)
play=!
play;
gobangAI4(play);
if(turn)
return;
for(inti=7;
=4;
step=i/2;
gobangAI2(play);
gobangAI5();
turn)
gobangRandom();
privatevoidgobangAI2(booleanplayer)
check=player?
if(grid[i][j]==check)
gobangAI3(i,j);
privatevoidgobangAI3(intx,inty)
if(count==1)
locX=x;
locY=y;
if(count==step)
if(x+displace_x>
x+displace_x<
y+displace_y>
y+displace_y<
grid[x+displace_x][y+displace_y]==0)
if(x-step*displace_x<
0||x-step*displace_x>
=length||y-step*displace_y<
0||y-step*displace_y>
=length||!
gobang_SpaceAI(x+displace_x,y+displace_y)||(grid[x-step*displace_x][y-step*displace_y]!
step<
3))/*癸翴猵*/
gobangAI3_2();
else
setMark2(x+displace_x,y+displace_y);
gobangAI3(x,y);
privatevoidgobangAI3_2()
if(direction!
=vertical)
direction=direction+1;
gobangAI3(locX,locY);
privatevoidgobangAI4(booleanplayer)
if(grid[i][j]==0)
locX=i;
locY=j;
gobangAI4();
privatevoidgobangAI4()
count=0;
=4)
setMark(locX,locY);
privatevoidgobangAI5()
if(grid[i][j]==-1)
gobangAI5_2();
/*璸衡硈絬フ耞糷*/
privatevoidgobangAI5_2()
grid[x][y]==0&
count<
4)
if(count==4&
x>
setMark(locX+displace_x,locY+displace_y);
elseif(count>
1)
count=count==4?
3:
count;
if(count==4)
privatebooleangobang_SpaceAI(intx,inty)/*璸衡逞緇フ竚*/
intspace=0;
space=space+1;
}while(x>
space<
4);
if(space+count>
=5)/*逞緇フ场だΤ硈絬ぇ*/
returntrue;
space=0;
if(spa
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 五子棋 java