本帖最后由 lihaotian_120 于 2013-10-3 13:39 编辑
学习到很累的时候,自己就喜欢写一些小程序来自娱自乐,这个是我最开始的时候写的五子棋,是控制台的对战版的,由于技术原因,没有加入AI的设计,而且也没有转成GUI的,有时间了再弄GUI,写得不咋地,但是就当鼓舞自己吧。- import java.util.*;
- class Wuziqi
- {
- private static char[][] jm=new char[16][16];
- private Wuziqi()
- {
- init();
- }
- private static Wuziqi w=new Wuziqi();
- public static Wuziqi getInstance()
- {
- return w;
- }
- public static void main(String[] args)
- {
- play(1);
- }
- private static void init()//棋盘界面初始化
- {
- for(int i=0;i<=15;i++)
- for(int j=0;j<=15;j++)
- jm[i][j]='.';
- showjm();
-
- }
- public static int[] input()//获取输入的信息
- {
- int[] x=new int[2];
- Scanner sc =new Scanner(System.in);
- x[0]=sc.nextInt()-1;
- x[1]=sc.nextInt()-1;
- return x;
- }
- private static void play(int i)//角色下棋的动作方法
- {
- int x[]=new int[2];
- char q[]={'*','O'};//两种标记来区分不懂的角色
- sop("play"+i+"请下棋,输入x,y坐标(空格间隔):");
- x=input();
- if(x[0]>=16||x[0]<0||x[1]>=16||x[1]<0)//判断当前下的地方是否超出棋盘的范围
- {
- sop("超出棋盘范围,请重下……");
- if(i==1)
- play(1);
- else
- play(2);
- }
- if(jm[x[1]][x[0]]=='.')//判断当前位置是否为空位置
- jm[x[1]][x[0]]=q[i-1];
- else
- {
- sop("该点已经有棋子,请重下……");
- if(i==1)
- play(1);
- else
- play(2);
- }
- if(!check(x[0],x[1],i)){
- if(i==1)
- {
- showjm();
- play(2);
- }
- else
- {
- showjm();
- play(1);
- }
- }
- else
- {
- showjm();
- sop("恭喜您play"+i+",获得胜利");
- return;
- }
- }
- private static boolean check(int x,int y,int player)//检查是否获得胜利的方法,其中分别有四个方法,都是用递归来实现深度遍历的
- {
- //checkxie();
- int n=checkheng(x,y,1,player)+checkheng(x,y,-1,player);
- int m=checkshu(x,y,1,player)+checkshu(x,y,-1,player);
- int l=checkxie1(x,y,1,player)+checkxie1(x,y,-1,player);
- int u=checkxie2(x,y,1,player)+checkxie2(x,y,-1,player);
- sop(m);
- if(n>=4||m>=4||l>=4||u>=4)
- {
- return true;
-
- }//checkshu();
- return false;
- }
- private static int checkxie1(int x,int y,int direction,int player)//检查斜线1的方法
- {
- char q[]={'*','O'};
- if(direction==1)
- {
- if(x<15&&y<15&&jm[y+1][x+1]==q[player-1])
- {
- //sop("...");
- return checkxie1(x+1,y+1,direction,player)+1;
- }
- else
- return 0;
- }
- else
- {
- //sop(jm[y][x-1]);
- if(x>0&&y>0&&jm[y-1][x-1]==q[player-1])
- {
- return checkxie1(x-1,y-1,direction,player)+1;
- }
- else
- return 0;
- }
- }
- private static int checkxie2(int x,int y,int direction,int player)//检查斜线2的方法
- {
- char q[]={'*','O'};
- if(direction==1)
- {
- if(x<15&&y>0&&jm[y-1][x+1]==q[player-1])
- {
- //sop("...");
- return checkxie2(x+1,y-1,direction,player)+1;
- }
- else
- return 0;
- }
- else
- {
- //sop(jm[y][x-1]);
- if(x>0&&y<15&&jm[y+1][x-1]==q[player-1])
- {
- return checkxie2(x-1,y+1,direction,player)+1;
- }
- else
- return 0;
- }
- }
- private static int checkheng(int x,int y,int direction,int player)//检测横的方法
- {
- char q[]={'*','O'};
- if(direction==1)
- {
- if(x<15&&jm[y][x+1]==q[player-1])
- {
- //sop("...");
- return checkheng(x+1,y,direction,player)+1;
- }
- else
- return 0;
- }
- else
- {
- //sop(jm[y][x-1]);
- if(x>0&&jm[y][x-1]==q[player-1])
- {
- return checkheng(x-1,y,direction,player)+1;
- }
- else
- return 0;
- }
- }
- private static int checkshu(int x,int y,int direction,int player)//检测竖的方法
- {
- char q[]={'*','O'};
- if(direction==1)
- {
- if(y<15&&jm[y+1][x]==q[player-1])
- {
- //sop("...");
- return checkshu(x,y+1,direction,player)+1;
- }
- else
- return 0;
- }
- else
- {
- if(y>0&&jm[y-1][x]==q[player-1])
- {
- return checkshu(x,y-1,direction,player)+1;
- }
- else
- return 0;
- }
- }
- private static void showjm()//显示界面的方法
- {
- for(int i=0;i<=15;i++)
- {
- for(int j=0;j<=15;j++)
- System.out.print(jm[i][j]+" ");
- System.out.println();
- }
-
- }
- private static void sop(Object obj)
- {
- System.out.println(obj);
- }
- }
复制代码 注释不是蛮多。。。。
|