java 控制台小程序:实现五子棋对弈

作者的个人分享网:分享时刻【www.itison.cn】

用法java在控制台实现五子棋对弈 ,界面虽然丑,但是功能确实基本实现,学习Java SE的时候收录的经典代码##

实现五子棋的全部代码如下,(复制直接可用,但是控制台的字体要设置为合适的)
Chess.java:

/**五子棋项目**/
package com.itjob.chess;
import java.util.Scanner;

public class Chess {
	
	static String[][] chess = new String[15][15];

	public static void main(String[] args) {		
		initial();//存储棋盘部分
		showChess();//打印棋盘
		playChess();//下棋
		
		while(true){
			System.out.println("是否继续游戏?(y/n)");
			Scanner input = new Scanner(System.in);
			String str = input.next();
			if (str.equals("y")) {
				initial();//存储棋盘部分
				showChess();//打印棋盘
				playChess();//下棋
			}else{
				System.exit(0);
			}
		}	
	}
	
	
	//存储棋盘部分
	private static void initial(){
		for (int i = 0; i < chess.length; i++) {
			for (int j = 0; j < chess[i].length; j++) {			
				if (i==0 && j==0) {
					chess[i][j] = "┏";
				}else if(i==0 && j > 0 && j < 14){
					chess[i][j] = "┳";
				}else if(j < 14 && j > 0 && i==14){
					chess[i][j] = "┻";
				}else if(i==0 && j==14){
					chess[i][j] = "┓";
				}else if (i > 0 && i < 14 && j==0) {
					chess[i][j] = "┣";
				}else if (i > 0 && i < 14 && j==14) {
					chess[i][j] = "┫";
				}else if (j==0 && i==14) {
					chess[i][j] = "┗";
				}else if (j==14 && i==14) {
					chess[i][j] = "┛";
				}else {
					chess[i][j] = "╋";
				}
			}
		}
	}
	
	//打印棋盘
	private static void showChess(){
		//打印棋盘
		for (int i = 0; i < 15; i++) {
			System.out.print("  " + (char)('A' + i) + " ");
		}
		
		System.out.println();

		for (int i = 0; i < chess.length; i++) {
			System.out.print((char)('A' + i));
			for (int j = 0; j < chess[i].length; j++) {
				if (j < 14) {
					System.out.print(chess[i][j] + "━");//加长棋盘
				}else{
					System.out.print(chess[i][j]); 
				}
			}
			
			System.out.println();
			
			if (i<14) {//加高度
				System.out.println(" ┃  ┃  ┃  ┃  ┃  ┃  ┃  ┃  ┃  ┃  ┃  ┃  ┃  ┃  ┃");
			}			
		}	
	}
	
	
	//下棋,落子
	private static void playChess(){
		Scanner input = new Scanner(System.in);
		int i = 0;
		int j = 0;
		String str = "";
		boolean flag = true;
		while(true){	
			String who = flag ? "○":"●";
			System.out.print("请" + who + "方落子(行,列)(0退出):");

			str = input.nextLine();//接收输入值		
			str = str.toUpperCase();//全部则转换为大写
			
			if (str.equals("0")) {
				System.exit(0);
			}
			//除去不输入异常的bug
			if (str.length() != 2) {
				System.out.println("输入的位置不对!请重新输入...");
				continue;
			}
			//判断是否为A到O范围
			if (str.charAt(0) <= 'O' && str.charAt(0) >= 'A' && str.charAt(1) <= 'O' && str.charAt(1) >= 'A' && str.length() == 2) {
				i = str.charAt(0) - (int)'A';
				j = str.charAt(1) - (int)'A';
			}else{
				System.out.println("输入的位置不对!请重新输入...");
				continue;
			}
			//将黑白子赋值给对应的位置,判断是否已有棋子
			if (!chess[i][j].equals("○") && !chess[i][j].equals("●")) {
				chess[i][j] = who;
			}else {
				System.out.println("此处已经有棋子!请重新输入...");
				continue;
			}
			
			showChess();//打印棋盘
			if(!isOver(i,j,who)){
				break;
			}
			
			flag = !flag;
		}
	}
	
	//判断输赢,没结束则返回true,有结果则返回false(也可以做成返回who的情况,在调用出输出哪方赢)
	private static boolean isOver(int x, int y, String who){			
		int i = x;
		int j = y;
		int count = 0;
		//判断横向向左是否五连
		for (j = y; j >= 0 ; j--) {
			if (chess[i][j].equals(who)) {
				count++;
			}else {
				break;
			}
		}
		
		//判断横向向右是否五连
		for (j = y; j <= 14 ; j++) {
			if (chess[i][j].equals(who)) {
				count++;
			}else {
				break;
			}
		}
		count--;
		if (count >= 5) {
			System.out.println(who + "方胜!游戏结束!!!");
			return false;
		}
				
		//判断纵向向上是否五连
		i = x;
		j = y;
		count = 0;
		for (i = x; i >= 0 ; i--) {
			if (chess[i][j].equals(who)) {
				count++;
			}else {
				break;
			}
		}
		//判断纵向向下是否五连
		for (i = x; i <= 14 ; i++) {
			if (chess[i][j].equals(who)) {
				count++;
			}else {
				break;
			}
		}
		count--;
		if (count >= 5) {
			System.out.println(who + "方胜!游戏结束!!!");
			return false;
		}
				
		//判断左上是否五连
		count = 0;
		for (i = x,j = y; i >= 0 && j >=0 ; i--,j--) {
			if (chess[i][j].equals(who)) {
				count++;
			}else {
				break;
			}
		}
		//判断右下是否五连
		for (i = x,j = y; i < chess.length && j < chess.length ; i++,j++) {
			if (chess[i][j].equals(who)) {
				count++;
			}else {
				break;
			}
		}
		count--;
		if (count >= 5) {
			System.out.println(who + "方胜!游戏结束!!!");
			return false;
		}
					
		//判断左下是否五连
		count = 0;
		for (i = x,j = y; i < chess.length && j >=0 ; i++,j--) {
			if (chess[i][j].equals(who)) {
				count++;
			}else {
				break;
			}
		}
		//判断右上是否五连
		for (i = x,j = y; i >= 0 && j < chess.length ; i--,j++) {
			if (chess[i][j].equals(who)) {
				count++;
			}else {
				break;
			}
		}
		count--;
		if (count >= 5) {
			System.out.println(who + "方胜!游戏结束!!!");
			return false;
		}
		
		return true;
	}

}

你可能感兴趣的:(java,SE,java,五子棋,java实现五子棋,java,se)