日期:2014-05-20 浏览次数:20859 次
//Report some basic information about a file import java.io.*; //for File import 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); } } }