当前位置: 代码迷 >> J2SE >> 二维数组写一个3*3 的9方格棋盘,两个人轮流放有一个棋子,看哪位高手先连成一行,本人智力有限,求更精妙的写法
  详细解决方案

二维数组写一个3*3 的9方格棋盘,两个人轮流放有一个棋子,看哪位高手先连成一行,本人智力有限,求更精妙的写法

热度:264   发布时间:2016-04-24 12:35:45.0
二维数组写一个3*3 的9方格棋盘,两个人轮流放有一个棋子,看谁先连成一行,本人智力有限,求更精妙的写法
Java code
//Report some basic information about a fileimport java.io.*;   //for Fileimport java.util.*;public  class test1 {    public static void main(String [] args) throws FileNotFoundException, InterruptedException{                int[][]  test = new int[3][3];        Scanner console = new Scanner(System.in);                int flage = 0;        while(true){            System.out.println("该红方出手了:");            input(test, console);            flage=check(test, 1);            say(flage);            System.out.println("黑方出手了:");            input(test, console);            flage=check(test ,2);            say(flage);                    }                    }    public static int check(int[][] test ,int c) {        int count = 0;        for(int i = 0; i < 3; i++){            if(test[i][0]!=0&&test[i][0]==test[i][1]&&test[i][1]==test[i][2]){                return c;            }            if(test[0][i]!=0&&test[0][i]==test[1][i]&&test[1][i]==test[2][i]){                return c;            }                    }        if(test[0][0]!=0&&test[0][0]==test[1][1]&&test[1][1]==test[2][2]){            return c;        }        if(test[0][2]!=0&&test[0][2]==test[1][1]&&test[1][1]==test[2][0]){            return c;        }        for(int i  = 0; i < 3; i++){            for(int j = 0; j <3; j++){                if(test[i][j]!=0){                    count++;                }            }        }        if(count==9){            return 3;        }                        return 0;            }    public static void say(int flage){        if(flage == 1){            System.out.println("红方赢了");            System.exit(0);                    }else if(flage ==2){            System.out.println("黑方赢了");            System.exit(0);                    }else if(flage == 3 ){            System.out.println("平手,结束");            System.exit(0);                    }            }    public static void input(int[][] test, Scanner consoie){        Scanner console = new Scanner(System.in);        int a=0;        int b=0;        int c = 0;        a = console.nextInt();        b =console.nextInt();        c= console.nextInt();        if(test[a][b]==0){            test[a][b] = c;                    }else{            System.out.print("请重新落棋,你走到别人棋子上了");            input(test, console);        }                            }            }    

红方持 “1”棋子,黑方持“2”棋子 


------解决方案--------------------
帮顶。。。。。。。
------解决方案--------------------
呵呵,网上又java做的五子棋的代码

祝楼主好运,我自己没做过
------解决方案--------------------
面向对象 棋手 棋盘
------解决方案--------------------
呵呵,看不懂,我只知道我的算法。二维数组由i ,j 组成
若是一色连成3个就win。
当你下一色子时,检查从北到南,从西到东,从西北到东南,从西南到东北方向是否够3字,够就win,代码量多些,但很多都是copy。
  相关解决方案