A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© lihaotian_120 中级黑马   /  2013-10-3 13:31  /  1744 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 lihaotian_120 于 2013-10-3 13:39 编辑

学习到很累的时候,自己就喜欢写一些小程序来自娱自乐,这个是我最开始的时候写的五子棋,是控制台的对战版的,由于技术原因,没有加入AI的设计,而且也没有转成GUI的,有时间了再弄GUI,写得不咋地,但是就当鼓舞自己吧。
  1. import java.util.*;
  2. class Wuziqi
  3. {
  4.         private static char[][] jm=new char[16][16];
  5.         private Wuziqi()
  6.         {
  7.                 init();
  8.         }
  9.         private static Wuziqi w=new Wuziqi();
  10.         public static Wuziqi getInstance()
  11.         {
  12.                 return w;
  13.         }
  14.         public static void main(String[] args)
  15.         {
  16.                 play(1);
  17.         }
  18.         private static void init()//棋盘界面初始化
  19.         {
  20.                 for(int i=0;i<=15;i++)
  21.                         for(int j=0;j<=15;j++)
  22.                         jm[i][j]='.';
  23.                 showjm();
  24.        
  25.         }
  26.         public static int[] input()//获取输入的信息
  27.         {
  28.                 int[] x=new int[2];
  29.                 Scanner sc =new Scanner(System.in);               
  30.                 x[0]=sc.nextInt()-1;
  31.                 x[1]=sc.nextInt()-1;               
  32.                 return x;
  33.         }       
  34.         private static void play(int i)//角色下棋的动作方法
  35.         {
  36.                 int x[]=new int[2];
  37.                 char q[]={'*','O'};//两种标记来区分不懂的角色
  38.                 sop("play"+i+"请下棋,输入x,y坐标(空格间隔):");
  39.                 x=input();
  40.                 if(x[0]>=16||x[0]<0||x[1]>=16||x[1]<0)//判断当前下的地方是否超出棋盘的范围
  41.                 {
  42.                         sop("超出棋盘范围,请重下……");
  43.                         if(i==1)
  44.                                 play(1);
  45.                         else
  46.                                 play(2);
  47.                 }
  48.                 if(jm[x[1]][x[0]]=='.')//判断当前位置是否为空位置
  49.                 jm[x[1]][x[0]]=q[i-1];
  50.                 else
  51.                 {
  52.                         sop("该点已经有棋子,请重下……");
  53.                         if(i==1)
  54.                                 play(1);
  55.                         else
  56.                                 play(2);
  57.                 }
  58.                 if(!check(x[0],x[1],i)){
  59.                         if(i==1)
  60.                         {
  61.                                 showjm();
  62.                                 play(2);
  63.                         }
  64.                         else
  65.                         {
  66.                                 showjm();
  67.                                 play(1);
  68.                         }
  69.                 }
  70.                 else
  71.                 {
  72.                         showjm();
  73.                         sop("恭喜您play"+i+",获得胜利");
  74.                         return;
  75.                 }
  76.         }
  77.         private static boolean check(int x,int y,int player)//检查是否获得胜利的方法,其中分别有四个方法,都是用递归来实现深度遍历的
  78.         {
  79.                 //checkxie();
  80.                 int n=checkheng(x,y,1,player)+checkheng(x,y,-1,player);
  81.                 int m=checkshu(x,y,1,player)+checkshu(x,y,-1,player);
  82.                 int l=checkxie1(x,y,1,player)+checkxie1(x,y,-1,player);
  83.                 int u=checkxie2(x,y,1,player)+checkxie2(x,y,-1,player);
  84.                 sop(m);
  85.                 if(n>=4||m>=4||l>=4||u>=4)
  86.                 {                       
  87.                         return true;
  88.                
  89.                 }//checkshu();
  90.                 return false;
  91.         }
  92.         private static int checkxie1(int x,int y,int direction,int player)//检查斜线1的方法
  93.         {
  94.                 char q[]={'*','O'};
  95.                 if(direction==1)
  96.                 {                       
  97.                         if(x<15&&y<15&&jm[y+1][x+1]==q[player-1])
  98.                         {
  99.                                 //sop("...");
  100.                                 return checkxie1(x+1,y+1,direction,player)+1;
  101.                         }
  102.                         else
  103.                                 return 0;
  104.                 }
  105.                 else
  106.                 {
  107.                         //sop(jm[y][x-1]);
  108.                         if(x>0&&y>0&&jm[y-1][x-1]==q[player-1])
  109.                         {

  110.                                 return checkxie1(x-1,y-1,direction,player)+1;
  111.                         }
  112.                         else
  113.                                 return 0;
  114.                 }
  115.         }
  116.         private static int checkxie2(int x,int y,int direction,int player)//检查斜线2的方法
  117.         {
  118.                 char q[]={'*','O'};
  119.                 if(direction==1)
  120.                 {                       
  121.                         if(x<15&&y>0&&jm[y-1][x+1]==q[player-1])
  122.                         {
  123.                                 //sop("...");
  124.                                 return checkxie2(x+1,y-1,direction,player)+1;
  125.                         }
  126.                         else
  127.                                 return 0;
  128.                 }
  129.                 else
  130.                 {
  131.                         //sop(jm[y][x-1]);
  132.                         if(x>0&&y<15&&jm[y+1][x-1]==q[player-1])
  133.                         {

  134.                                 return checkxie2(x-1,y+1,direction,player)+1;
  135.                         }
  136.                         else
  137.                                 return 0;
  138.                 }
  139.         }
  140.         private static int checkheng(int x,int y,int direction,int player)//检测横的方法
  141.         {
  142.                 char q[]={'*','O'};
  143.                 if(direction==1)
  144.                 {                       
  145.                         if(x<15&&jm[y][x+1]==q[player-1])
  146.                         {
  147.                                 //sop("...");
  148.                                 return checkheng(x+1,y,direction,player)+1;
  149.                         }
  150.                         else
  151.                                 return 0;
  152.                 }
  153.                 else
  154.                 {
  155.                         //sop(jm[y][x-1]);
  156.                         if(x>0&&jm[y][x-1]==q[player-1])
  157.                         {

  158.                                 return checkheng(x-1,y,direction,player)+1;
  159.                         }
  160.                         else
  161.                                 return 0;
  162.                 }
  163.         }
  164.         private static int checkshu(int x,int y,int direction,int player)//检测竖的方法
  165.         {
  166.                 char q[]={'*','O'};
  167.                 if(direction==1)
  168.                 {
  169.                         if(y<15&&jm[y+1][x]==q[player-1])
  170.                         {
  171.                                 //sop("...");
  172.                                 return checkshu(x,y+1,direction,player)+1;
  173.                         }
  174.                         else
  175.                                 return 0;
  176.                 }
  177.                 else
  178.                 {                       
  179.                         if(y>0&&jm[y-1][x]==q[player-1])
  180.                         {
  181.                                 return checkshu(x,y-1,direction,player)+1;
  182.                         }
  183.                         else
  184.                                 return 0;
  185.                 }
  186.         }
  187.         private static void  showjm()//显示界面的方法
  188.         {
  189.                 for(int i=0;i<=15;i++)
  190.                 {       
  191.                         for(int j=0;j<=15;j++)
  192.                         System.out.print(jm[i][j]+" ");
  193.                         System.out.println();
  194.                 }
  195.                
  196.         }
  197.         private static void sop(Object obj)
  198.         {
  199.                 System.out.println(obj);
  200.         }
  201. }
复制代码
注释不是蛮多。。。。

评分

参与人数 1技术分 +2 收起 理由
黄文伯 + 2 赞一个!

查看全部评分

2 个回复

倒序浏览
整个界面会不会好点{:soso_e102:}
回复 使用道具 举报
那么牛啊!自己写的
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马