开发工具:
文件大小: 15kb
下载次数: 0
上传时间: 2018-08-09
详细说明: java编的五子棋 import java.util.*; import java.io.*; import java.awt.*; import java.awt.event.*; import java.applet.*; import javax.swing.*; class Gobang 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 int[length][length]; 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 JMenuItem[4]; String[] command_str={"囱", "郎", "弄郎", "秨"}; int[][] dir = { {-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1} }; boolean[] dir2 = new boolean[8]; boolean turn; String message; final JDialog dialog = new JDialog(this, "叫匡", true); Font font=new Font("new_font", Font.BOLD, 20); Grid grids[][] = new Grid[length][length]; Image white= tk.getImage(cl.getResource("res/white.png")); Image black= tk.getImage(cl.getResource("res/black.png")); Image title= tk.getImage(cl.getResource("res/title.png")); Image temp; JPanel boardPanel, bigpanel; JRadioButton[] choice = new JRadioButton[2]; 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; /* */ Gobang() { super("き囱"); boardPanel = new JPanel(); boardPanel.setLayout(new GridLayout(length, length, 0, 0)); boardPanel.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); 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; i=length || y<0 || y>=length || grid[x][y]!=0) && displace_y<8); for(int i=0; i<8; i++) dir2[i]=false; if(x>=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; i=0 && x=0 && y=0 && x+displace_x=0 && y+displace_y=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]!=0 && step<3) ) /* 癸翴猵 */ 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; i=0 && x=0 && y=0 && x=0 && y=4) setMark(locX, locY); else if(direction!=nil) gobangAI4(); } private void gobangAI5() { for(int i=0; i=0 && x=0 && y=0 && x=0 && y1) { count = count==4? 3: count; while(x>=0 && x=0 && y=0 && x=0 && y=5) /* 逞緇フ场だΤ硈絬ぇ */ return true; else { space=0; x=locX-displace_x; y=locY-displace_y; while(x>=0 && x=0 && y=5) /* 逞緇フ场だΤ硈絬ぇ */ return true; else return false; } } public void ThreadStart() { new Thread(this).start(); } private void setMark(int x, int y) { steps.add(grids[x][y]); grids[x][y].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_y=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; i<2; 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; i
(系统自动生成,下载前可以参看下载内容)
下载文件列表
相关说明
- 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
- 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度。
- 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
- 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
- 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
- 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.