java实现单人版五子棋游戏
程序员文章站
2024-03-09 11:34:29
本文实例讲述了java实现的五子棋游戏代码,分享给大家供大家参考,具体代码如下
package gyb.exam;
import java.awt.bor...
本文实例讲述了java实现的五子棋游戏代码,分享给大家供大家参考,具体代码如下
package gyb.exam; import java.awt.borderlayout; import java.awt.color; import java.awt.dimension; import java.awt.font; import java.awt.graphics; import java.awt.point; import java.awt.toolkit; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; import java.util.arraylist; import javax.swing.icon; import javax.swing.imageicon; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jmenu; import javax.swing.jmenubar; import javax.swing.jmenuitem; import javax.swing.joptionpane; import javax.swing.jpanel; import javax.swing.border.titledborder; public class mainframe extends jframe{ private static int big=15; private static final int one=25; private static final int start=10; private static int x=big*start/2; private static int y=big*start/2; private int hl=big*one+start; private static boolean isblack; canvasframe canvas=new canvasframe(); canvasframe can; graphics g; public mainframe(){ init(); } private point point; private arraylist<point> blacklist=new arraylist<point>(); private arraylist<point> whitelist=new arraylist<point>(); private void init() { jmenubar bar=new jmenubar(); setjmenubar(bar); setsize(600,600); settitle("五子棋"); jmenu menu=new jmenu("game"); bar.add(menu); jmenuitem start=new jmenuitem("start"); jmenuitem end=new jmenuitem("棋盘大小"); end.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { } }); jmenuitem renew=new jmenuitem("重新开始"); jmenuitem exit=new jmenuitem("退出"); exit.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e) { system.exit(0); } }); renew.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e) { blacklist.clear(); whitelist.clear(); } }); menu.add(start); menu.add(renew); menu.add(end); menu.add(exit); setlayout(new borderlayout()); add(borderlayout.center,createcenter()); add(borderlayout.east,createeast()); } private boolean iswin(arraylist<point> list){ int x; int y; int idx=0; loop:for (point p : list) { x=p.x; y=p.y; idx=0; while(list.contains(new point(x,y))){ idx++; x=x-one; if(idx==5) break loop; } x=p.x; y=p.y; idx=0; while(list.contains(new point(x,y))){ idx++; y=y-one; if(idx==5) break loop; } x=p.x; y=p.y; idx=0; while(list.contains(new point(x,y))){ idx++; x=x-one; y=y-one; if(idx==5) break loop; } x=p.x; y=p.y; idx=0; while(list.contains(new point(x,y))){ idx++; x=x-one; y=y+one; if(idx==5) break loop; } } return idx==5; } private jpanel createeast() { jpanel pane=new jpanel(); borderlayout border=new borderlayout(); pane.setlayout(border); pane.setborder(new titledborder("五子棋")); icon image=new imageicon(getclass().getresource("mm.png")); pane.add(borderlayout.north,new jlabel(image,jlabel.center)); pane.add(borderlayout.center,ballcolor()); pane.add(borderlayout.south,button()); return pane; } private jpanel ballcolor() { jpanel pane=new jpanel(); pane.setlayout(new borderlayout()); can=new canvasframe(){ public void paint(graphics g){ g.setcolor(color.pink); g.fillrect(1, 1,200,300); g.setfont(new font("sans",font.bold,20)); g.setcolor(color.black); g.filloval(55, 20,25,30); g.drawstring("黑子", 10, 43); g.setcolor(color.white); g.filloval(55, 60,25,30); g.drawstring("白子", 10, 83); if(isblack){ g.setcolor(color.black); g.drawstring("黑方落子", 5, 120); }else{ g.setcolor(color.white); g.drawstring("白方落子",5,120); } can.repaint(); } }; pane.add(borderlayout.center,can); return pane; } private jpanel button() { jpanel pane=new jpanel(); borderlayout bor=new borderlayout(); jbutton start=new jbutton("开始"); jbutton end=new jbutton("结束"); end.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e) { system.exit(0); }}); pane.setlayout(bor); bor.setvgap(5); pane.setborder(new titledborder("")); pane.add(borderlayout.north,start); pane.add(borderlayout.south,end); return pane; } private jpanel createcenter() { jpanel pane=new jpanel(); canvas.setpreferredsize(new dimension(hl+10,hl+10)); canvas.addmouselistener(new mouseadapter() { public void mouseclicked(mouseevent e){ loop:if(e.getbutton()==mouseevent.button1){ x=e.getx(); y=e.gety(); int x=(x/one)*one; int y=(y/one)*one; point=new point(x,y); if(!(blacklist.contains(point)||whitelist.contains(point))){ if(isblack){ blacklist.add(point); if(iswin(blacklist)){ joptionpane.showmessagedialog(null, "黑方胜利!"); blacklist.clear(); whitelist.clear(); } }else{ whitelist.add(point); if(iswin(whitelist)){ joptionpane.showmessagedialog(null, "白方胜利!"); blacklist.clear(); whitelist.clear(); } } } else{ joptionpane.showmessagedialog(null, "这里己有子了,换个地方落子吧!"); break loop; } canvas.repaint(); isblack=!isblack; } } }); pane.add(canvas); return pane; } public static void main(string[] args) { mainframe frame=new mainframe(); frame.center(frame); frame.pack(); frame.setvisible(true); } public void center(jframe frame){ //toolkit 是当前 绘图系统集合工具包 toolkit toolkit=toolkit.getdefaulttoolkit(); dimension screen=toolkit.getscreensize();//屏幕的宽高 int framewidth=frame.getwidth(); int framehigh=frame.getheight(); int x=(screen.width-framewidth)/2; int y=(screen.height-framehigh)/2; frame.setlocation(x, y); } class canvasframe extends jpanel { public void paint(graphics g){ g.setcolor(color.pink); g.fillrect(0, 0, hl+20, hl+20); g.setcolor(color.black); for(int i=start;i<=hl;i=i+one){ for(int j=start;j<=hl;j=j+one){ g.drawline(i, j, hl, j); } } for(int i=start;i<=hl;i=i+one){ for(int j=start;j<=hl;j=j+one){ g.drawline(i, j, i, hl); } } g.setcolor(color.yellow); g.drawrect(start/2, start/2, hl+1, hl+1); g.setcolor(color.black); for(point point : blacklist){ g.filloval(point.x, point.y, 20, 25); } g.setcolor(color.white); for(point point : whitelist){ g.filloval(point.x, point.y, 20, 25); } } } }
效果图:
以上所述就是本文的全部内容了,希望能够对大家熟练掌握java有所帮助。
上一篇: Java并发编程之性能、扩展性和响应