一个简单的五子棋程序

一个简单的五子棋程序



/**
 * @Version: 1
 * @JDK: jdk 1.7
 * @Module: MyTag

 * 2012-6-11 - 下午10:33:15 Created by Sharp
 */ 

 /*- 				History
 **********************************************
 *  ID      DATE           PERSON       REASON
 *  1     2012-6-11         Sharp        Created
 **********************************************
 */

package com.sharp.tags;

/**
 * @Version: 1
 * @JDK: jdk 1.7
 * @Module: MyTag

 * 2012-6-11 - 下午10:33:15 Created by Sharp
 */ 

 /*- 				History
 **********************************************
 *  ID      DATE           PERSON       REASON
 *  1     2012-6-11         Sharp        Created
 **********************************************
 */



import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Random;

public class Test{

	private static final int GOBANG_SIZE = 10;
	private static final String NO_CHESS = "□";
	private static final String BLACK_CHESS = "★";//人
	private static final String WHITE_CHESS = "☆";//电脑
	private static String[][] status = new String[GOBANG_SIZE][GOBANG_SIZE];
	public static void main(String[] args) throws Exception{
		init();
		paint();
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String line = null;
		while((line = br.readLine()) != null){
			String[] signs = line.split(",");
			int x  = Integer.parseInt(signs[0]);
			int y  = Integer.parseInt(signs[1]);
			if(!status[x][y].equals(NO_CHESS)){
				System.out.println("该坐标已有棋子,请重新输入");
				continue;
			}
			status[x][y] = BLACK_CHESS;
			while(true){
				int pc_x = new Random().nextInt(GOBANG_SIZE);
				int pc_y = new Random().nextInt(GOBANG_SIZE);
				if(status[pc_x][pc_y].equals(NO_CHESS)){
					status[pc_x][pc_y] = WHITE_CHESS;
					break;
				}
			}
			
			paint();
			if(isWin() == 1){
				System.out.println("恭喜你,赢了!");
			}
			if(isWin() == -1){
				System.out.println("你输了!");
			}
		}
	}
	public static void init(){
		for(int i=0; i<GOBANG_SIZE; i++){
			for(int j=0; j<GOBANG_SIZE;j++){
				status[i][j] = NO_CHESS;
			}
		}
	}

	public static void paint(){
		for(int i=0; i<GOBANG_SIZE; i++){
			for(int j=0; j<GOBANG_SIZE;j++){
				System.out.print(status[i][j]);
			}
			System.out.println();
		}
	}
	
	public static int isWin(){
		StringBuilder xWin = null;
		StringBuilder yWin = null;
		StringBuilder ZxyWin = new StringBuilder();
		StringBuilder FxyWin = new StringBuilder();
		for(int i =0; i< GOBANG_SIZE; i++){
			xWin = new StringBuilder();
			yWin = new StringBuilder();
			for(int j=0; j< GOBANG_SIZE; j++){
				xWin.append(status[i][j]);
				yWin.append(status[j][i]);
			}
			if(xWin.toString().contains("★★★★★") || yWin.toString().contains("★★★★★")){
				return 1;
			}
			if(xWin.toString().contains("☆☆☆☆☆") || yWin.toString().contains("☆☆☆☆☆")){
				return -1;
			}
//			System.out.println("查询结果xWin---->" + xWin);
//			System.out.println("查询结果yWin---->" + yWin);
		}

		
		return 0;
	}
}



你可能感兴趣的:(jdk,exception,Date,String,Module,Random)