发表新主题 回复该帖子
主题:初级Java实现简单单机版五子棋功能
初涉编程行
帖子档案  楼主 初级Java实现简单单机版五子棋功能   Post by : 2011-03-11 19:27:23.0
  • 幼儿园
  • 幼儿园
  • UID:645
  • 主题:2
  • 帖子:3
  • 加为好友 加为好友    发送短信 发送短信

刚开始学习编程不久,好不容易做出一个小东西。希望各位高手给出改进建议,以助我们新手学习。先谢谢了!!

package ambow.hpu.shixun;

import java.util.Scanner;

public class WuZiQi {

 /**
  * 演示简单五子棋功能。
  */
 
    
 private boolean iswrite=false;
  private int[][]qp=new int[9][9];
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
  WuZiQi  w =new WuZiQi();
  
  System.out.println("------欢迎体验五子棋------");
  System.out.println("1.新游戏");
  System.out.println("2.帮助");
  System.out.println("3.设置");
  System.out.println("4.关于");
  System.out.println("5.退出游戏");
 while(true){ 
  int a =input.nextInt();
  switch(a){
     case 1:
   w.newGame();
     break;
           case 2:
   w.help();
     break;
           case 3:
             w.set();
           break;
           case 4:
           w.about();
           break;
           case 5:
           System.exit(0);
            break;
       default:
        break;
  
     }
     }
  }
 public void about(){
  System.out.println("作者:菜鸟队;");
  System.out.println("指导:指导老师");
 }
 public void set(){
  System.out.println("1.背景声音设置");
  System.out.println("2.游戏模式设置");
  System.out.println("3.背景图片设置");
 }
 public void help(){
  System.out.println("输入你要落子位置的坐标,即可下子。");
 }
 public void qiPan(){
  for(int i=0;i<qp.length;i++){
   for(int j=0;j<qp[i].length;j++){
    qp[i][j]=0;
    
   }
  }
  
 }
 public void setQiPan(){
  System.out.print("  ");
  for(int i=0;i<qp.length;i++){
   System.out.print(i+1+" ");      
  }
  System.out.println();
  for(int i=0;i<qp.length;i++){
   System.out.print(i+1+" ");
   for(int j=0;j<qp[i].length;j++){
    if(qp[i][j]==0){
     System.out.print("-");
    }
    if(qp[i][j]==1){
     System.out.print("*");
    }
    if(qp[i][j]==-1){
     System.out.print("@");
    }
   System.out.print(" "); 
   }
   System.out.println();
  }
 }
 public void newGame(){
  Scanner input = new Scanner(System.in);
  
  
   while(true){
  setQiPan();    
  
  if(iswrite){
   System.out.println("请执白子者下:");
  }else{
   System.out.println("请执黑子者下:");
  }
  int x = input.nextInt();
  int y = input.nextInt();
  if(iswrite&&qp[x-1][y-1]==0){
   qp[x-1][y-1]=1;
                        
  }else if(qp[x-1][y-1]!=0){
   continue;
  }else {
   qp[x-1][y-1]=-1;           
   }
       if(iswin(x-1,y-1)){
        if(iswrite){
         System.out.println("白棋胜!");
        }else{
         System.out.println("黑棋胜!");
        }
        break;
       
       }
           
  iswrite=!iswrite;
   }
 }
 public boolean islegal(int x,int y){
  
  if(x<0||y<0||x>=9||y>=9){

   return false;  
  }else{
      
      return true;
  }
 }
 public boolean iswin(int x,int y) {
  int key=0;
  if(iswrite){
   key=1;
  }else{
   key=-1;
  }
  //1.5方向判断。
  int count =key;
  for(int i=0;i<5;i++){
   if( islegal(x-i,y-i)&&qp[x-i][y-i]==key){
    
   count += qp[x-i][y-i];
   }else {
    
    break;
   }
  }
  for(int i=0;i<5;i++){
   if(islegal(x,y)&&qp[x+i][y+i]==key){
    count = count+qp[x+i][y+i];
   }else {
    break;
   }
  }
  if(Math.abs(count)==7){
   
   return true;
   
  }
  //2.6方向判断
  
   count=key;
  for(int i=0;i<5;i++){
   if(islegal(x-i,y)&&qp[x-i][y]==key){
    count = count+qp[x-i][y];
   }else {
    break;
   }
  }
    for(int i=0;i<5;i++){
    if(islegal(x,y)&&qp[x+i][y]==key){
     count+=qp[x+i][y];
     }else {
    break;
      }
     }
  if(Math.abs(count)==7){
   return true;
  }
  //3.7方向判断  
  count=key;
  for(int i=0;i<5;i++){
   if(islegal(x-i,y)&&qp[x-i][y+i]==key){
    count+=qp[x-i][y+i];
   }else {
    break;
   }
  }
  for(int i=0;i<5;i++){
   if(islegal(x,y-i)&&qp[x+i][y-i]==key){
    count+=qp[x+i][y-i];
   }else {
    break;
   }
  }
  if(Math.abs(count)==7){
   return true;
  }
  //4.8方向判断。
   count=key;
  for(int i=0;i<5;i++){
   if(islegal(x,y)&&qp[x][y+i]==key){
    count+=qp[x][y+i];
   }else {
    break;
   }
  }
  for(int i=0;i<5;i++){
   if(islegal(x,y-i)&&qp[x][y-i]==key){
    count+=qp[x][y-i];
   }else {
    break;
   }
  }
  if(Math.abs(count)==7){
   return true;
  }
   
  return false;
  
 }
 

}
 

返回页面顶部  

12781669
2F Re:~   Post by : 2011-03-11 19:51:30.0
  • 幼儿园
  • 幼儿园
  • UID:648
  • 主题:0
  • 帖子:1
  • 加为好友 加为好友    发送短信 发送短信
em09支持原创
返回页面顶部  

长江水菱
3F Re:~   Post by : 2011-05-14 17:24:14.0
  • 幼儿园
  • 幼儿园
  • UID:689
  • 主题:0
  • 帖子:4
  • 加为好友 加为好友    发送短信 发送短信
第一次见到这些功能呢
返回页面顶部  

长江水菱
4F Re:~   Post by : 2011-05-14 17:24:27.0
  • 幼儿园
  • 幼儿园
  • UID:689
  • 主题:0
  • 帖子:4
  • 加为好友 加为好友    发送短信 发送短信
这里有什么功能呢,都看不明白的
返回页面顶部  

长江水菱
5F Re:~   Post by : 2011-05-14 17:24:46.0
  • 幼儿园
  • 幼儿园
  • UID:689
  • 主题:0
  • 帖子:4
  • 加为好友 加为好友    发送短信 发送短信
应该写得详细点给大家去看看才行啊
返回页面顶部  

tarenalu
6F Re:~   Post by : 2011-08-29 17:40:23.0
  • 幼儿园
  • 幼儿园
  • UID:664
  • 主题:0
  • 帖子:4
  • 加为好友 加为好友    发送短信 发送短信
建议那些方法类似的可以做调用处理
签名
http://www.gztarena.com
http://www.gdtarena.com
http://www.tarenagz.com
返回页面顶部  

irene123
7F Re:~   Post by : 2011-12-07 15:47:58.0
  • 幼儿园
  • 幼儿园
  • UID:802
  • 主题:0
  • 帖子:31
  • 加为好友 加为好友    发送短信 发送短信
看起来有点复杂啊
签名
sf123
cqsf
返回页面顶部  


CopyRight © 2008-2009 JavaWind.Net Studio All Rights Reserved
Powered By JWind.BBS Vesion 1.0.0 Beta1 Processed in 21 ms,0 (Queries)  Gzip enabled

WAP - 清除Cookies - 联系我们 - JavaWind.Net Studio - Archiver - TOP Valid XHTML 1.0 Transitional Valid CSS! 粤ICP备07511478号