




已阅读5页,还剩16页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
import java.util.*;import java.io.*;import java.awt.*;import java.awt.event.*;import java.applet.*;import javax.swing.*;class WuZiQi extends JFrame implements Runnable, ActionListener final static int Player=1; final static int AI =-1; ClassLoader cl = this.getClass().getClassLoader(); Toolkit tk = Toolkit.getDefaultToolkit(); int length=14, game_state, winner, check, step; int grid = new intlengthlength; int locX, locY /* 囱竚 */, count /* 硈囱计 */, x, y /* 既竚 */, displace_x=0, displace_y=0 /* 簿秖 */, direction; ArrayList steps = new ArrayList(); /* 癘魁囱 */ JPopupMenu control_menu = new JPopupMenu(); /* 龄匡虫 */ JMenuItem command = new JMenuItem4; String command_str=囱, 郎, 弄郎, 秨; int dir = -1, -1, -1, 0, -1, 1, 0, -1, 0, 1, 1, -1, 1, 0, 1, 1 ; boolean dir2 = new boolean8; boolean turn; String message; final JDialog dialog = new JDialog(this, 选择, true); Font font=new Font(new_font, Font.BOLD, 20); Grid grids = new Gridlengthlength; Image white= tk.getImage(cl.getResource(white.png); Image black= tk.getImage(cl.getResource(black.png); Image title= tk.getImage(cl.getResource(title.png); Image temp; JPanel boardPanel, bigpanel; JRadioButton choice = new JRadioButton2; final static int Start =0; final static int Select =1; final static int Playing =2; final static int End =3; final static int nil=-1; /* 礚 */ final static int oblique_1 =0; /* */ final static int oblique_2 =1; /* */ final static int horizontal =2; /* 绢 */ final static int vertical=3; /* */ WuZiQi() super(五子棋); boardPanel = new JPanel(); boardPanel.setLayout(new GridLayout(length, length, 0, 0); boardPanel.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); for(int i=0; ilength; i+) for(int j=0; jlength; j+) gridsij = new Grid(i, j); boardPanel.add(gridsij); bigpanel = new JPanel(); bigpanel.add(boardPanel, BorderLayout.CENTER); getContentPane().add(bigpanel, BorderLayout.SOUTH); game_state=Start; ThreadStart(); dialog.setSize(160, 100); dialog.setResizable(false); dialog.setLocationRelativeTo(null); ButtonGroup choice_group = new ButtonGroup(); JPanel choice_menu = new JPanel(); choice0 = new JRadioButton(黑子, new ImageIcon(black), true); choice1 = new JRadioButton(白子, new ImageIcon(white); for(int i=0; ichoice.length; i+) choice_menu.add(choicei); choice_group.add(choicei); for(int i=0; i=0 & x=0 & y=0 & x=0 & y=5) game_state=End; winner=check; ThreadStart(); else if(direction!=nil) WinCheck2(); public void ReStart() for(int i=0; ilength; i+) for(int j=0; jlength; j+) gridsij.Initial(); winner=0; steps.clear(); game_state=Playing; public void gobangRandom() displace_y=0; do displace_x=(int)(Math.random()*8); x =locX+dirdisplace_x0; y =locY+dirdisplace_x1; if(!dir2displace_x) displace_y=displace_y+1; dir2displace_x=true; while(x=length | y=length | gridxy!=0) & displace_y8); for(int i=0; i=0 & x=0 & y0; i-) play=!play; gobangAI4(play); if(turn) return; for(int i=7; i=4; i-) play=!play; step=i/2; gobangAI2(play); if(turn) return; gobangAI5(); if(!turn) gobangRandom(); private void gobangAI2(boolean player) check = player? Player: AI; for(int i=0; ilength; i+) for(int j=0; j=0 & x=0 & y=0 & x+displace_x=0 & y+displace_ylength & gridx+displace_xy+displace_y=0) if(x-step*displace_x=length | y-step*displace_y=length | !gobang_SpaceAI(x+displace_x, y+displace_y) | (gridx-step*displace_xy-step*displace_y!=0 & step3) ) /* 癸翴猵 */ gobangAI3_2(); else setMark2(x+displace_x, y+displace_y); else gobangAI3_2(); else gobangAI3(x, y); else gobangAI3_2(); private void gobangAI3_2() if(direction!=vertical) count=1; direction=direction+1; gobangAI3(locX, locY); private void gobangAI4(boolean player) check = player? Player: AI; for(int i=0; ilength; i+) for(int j=0; j=0 & x=0 & y=0 & x=0 & y=4) setMark(locX, locY); else if(direction!=nil) gobangAI4(); private void gobangAI5() for(int i=0; ilength; i+) for(int j=0; j=0 & x=0 & ylength & gridxy=0 & count=0 & x=0 & y1) count = count=4? 3: count; while(x=0 & x=0 & ylength & gridxy=0 & count=0 & x=0 & ylength & gridxy=0 & space=5) return true; else space=0; x=locX-displace_x; y=locY-displace_y; while(x=0 & x=0 & ylength & gridxy=0 & space=5) return true; else return false; public void ThreadStart() new Thread(this).start(); private void setMark(int x, int y) steps.add(gridsxy); gridsxy.setValue(-1); WinCheck(); turn=true; private void setMark2(int x, int y) int space=0, temp_x=x, temp_y=y; do space=space+1; temp_x=temp_x+displace_x; temp_y=temp_y+displace_y; while(temp_x=0 & temp_x=0 & temp_ylength & gridtemp_xtemp_y=0 & space=5) setMark(x, y); else setMark(x-(step+1)*displace_x, y-(step+1)*displace_y); public void run() try switch(game_state) case Start: Thread.sleep(2000); dialog.show(); game_state=Playing; repaint(); break; case End: repaint(); Thread.sleep(1500); ReStart(); repaint(); break; catch(InterruptedException ex) public void paint(Graphics g) super.paint(g); switch(game_state) case Start: g.drawString(2005.7 by Yu Lin Tao, 91, 240); g.setFont(font); g.setColor(Color.BLUE); g.drawString(Gobang, 112, 120); g.drawImage(title, 134, 135, this); break; case Playing: case End: g.drawString(產: 筿福:, 105, 40); g.drawImage(black, 132, 30, this); g.drawImage(white, 182, 30, this); if(winner!=0) g.setFont(font); g.setColor(Color.RED); message = winner=1? 恭喜你:你输了!; g.drawString(message, 120, 185); break; public void undo() if(steps.size()0) for(int i=0; i2; i+) (Grid)steps.get(steps.size()-1).Initial(); steps.remove(steps.size()-1); private void Save() String str; try File file = new File(innings.men); file.createNewFile(); BufferedWriter write = new BufferedWriter(new FileWriter(file); for(int i=0; isteps.size(); i+) str=(Grid)steps.get(i).getLocX()+,+(Grid)steps.get(i).getLocY()+,+(Grid)steps.get(i).getValue(); write.write(str); write.newLine(); write.close(); catch(Exception ex) private void Load() try String step_array; String str; File file = new File(innings.men); if(!file.exists() JOptionPane.showMessageDialog(null, 郎); return; BufferedReader read = new BufferedReader(new FileReader(file); ReStart(); while(read.ready() str=read.readLine(); step_array=str.split(,); gridsInteger.parseInt(step_array0)Integer.parseInt(step_array1).setValue(Integer.parseInt(step_array2); steps.add(gridsInteger.parseInt(step_array0)Integer.parseInt(step_array1); catch(Exception ex) private class Grid extends JPanel implements MouseListener int x, y, value; boolean selected; public Grid(int x, int y) this.x=x; this.y=y; addMouseListener(this); public void mousePressed(MouseEvent event) if(game_state=Playing) int button=event.getButton(); if(button=MouseEvent.BUTTON1) if(value=0) steps.add(this); setValue(1); WinCheck(); turn=false; if(game_state=Playing) gobangAI(); else control_menu.show(this, event.getX(), event.getY(); public void mouseEntered(MouseEvent event) if(game_state=Playing) setSelected(true); public void mouseExited(MouseEvent event) if(game_state=Playing) setSelected(false); public void mouseClicked(MouseEvent event) public void mouseReleased(MouseEvent event) public Dimension getPreferredSize() return new Dimension(20, 20); public Dimension getMinimumSize() return getPreferredSize(); public int getLocX() return x; public int getLocY() return y; public int getValue() return value; public void
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年安全员模拟测试题及答案模拟模拟练习
- 2025年人口与发展硕士研究生入学考试试题及答案解析
- 2025年农业经济管理师资格考试试题及答案解析
- 2025年景观艺术设计师资格考试试题及答案解析
- 2025年政府文宣岗笔试冲刺题
- 2025年政府会计准则制度实施能力考试高频题解析
- 2025年建筑施工管理师技术考查试题及答案解析
- 2025年建筑工地安全题解
- 2025年家政服务技能考试试题及答案解析
- 课件中插入密码小程序
- 小学思政课《爱国主义教育》
- 《展示设计》课程教案
- 市政道路雨污水管道工程施工技术详细课件
- 村集体经济组织财务及会计知识讲座课件
- 热集成-4.夹点技术基础理论
- 银屑病教学讲解课件
- SMART200与ACS510通过modbus通信控制启停
- 山西省临汾市各县区乡镇行政村村庄村名居民村民委员会明细及行政区划代码
- 项目领导班子竞聘面试评分表
- 皮肤科常见疾病学习课件
- 工序质量报验单
评论
0/150
提交评论