Java数组实现五子棋

package day04hw;
//老师写的
import java.util.Arrays;
import java.util.Scanner;

public class FiveChess {
	private static final int CELL = 16;
	private static final char CELL_CHAR = '\u253C';
	private char[][] cells;
	private boolean turn;//true:黑;false:白
	
	public FiveChess() {
		cells = new char[FiveChess.CELL][FiveChess.CELL];
		for (int i=0; i<cells.length; i++) {
			Arrays.fill(cells[i],FiveChess.CELL_CHAR);
		}
		turn = true;
	}
	
	public void printCells() {
		System.out.print("  ");
		for (int i=0; i<FiveChess.CELL; i++) {
			System.out.print((char)(i+'a')+" ");
		}
		System.out.println();
		for (int i=0; i<FiveChess.CELL; i++) {
			for (int j=0; j<FiveChess.CELL; j++) {
				if (j==0) {
					System.out.print((char)(i+'a')+" ");
				}
				System.out.print(cells[i][j]+" ");
			}
			System.out.println();
		}
	}
	
	public boolean downChess(int row, int column) {
		boolean flag = true;
		if (row >= FiveChess.CELL || row <0
				|| column >= FiveChess.CELL || column <0) {
			System.out.println("输入有误,请重新输入!");
			flag = false;
		} else {
			if (cells[row][column] != FiveChess.CELL_CHAR) {
				System.out.println("输入位已落子,请重新输入!");
				flag = false;
			} else {
				cells[row][column] = turn?'@':'o';
			}
		}
		return flag;
	}
	
	public boolean isWin(int row, int column) {
		boolean isWin = false;
		char curChess = turn?'@':'o';
		if (this.isHorizontalWin(row, column, curChess)
				|| this.isVerticalWin(row, column, curChess)
				|| this.isLeftObliqueDownWin(row, column, curChess)
				|| this.isLeftObliqueUpWin(row, column, curChess)) {
			isWin = true;
		}
		return isWin;
	}
	
	public boolean isHorizontalWin(int row, int column, char curChess) {
		boolean isWin = false;
		int count = 0;
		for (int c = column-1; c>=0; c--) {
			if (cells[row][c] == curChess) {
				count++;
			} else {
				break;
			}
		}
		for (int c = column+1; c<FiveChess.CELL; c++) {
			if (cells[row][c] == curChess) {
				count++;
			} else {
				break;
			}
		}
		System.out.println("horizontal count :"+ count);
		if (count >=4) {
			isWin = true;
		}
		return isWin;
	}
	
	public boolean isVerticalWin(int row, int column, char curChess) {
		boolean isWin = false;
		
		return isWin;
	}
	
	public boolean isLeftObliqueDownWin(int row, int column, char curChess) {
		boolean isWin = false;
		
		return isWin;
	}
	
	public boolean isLeftObliqueUpWin(int row, int column, char curChess) {
		boolean isWin = false;
		int count = 0;
		for (int r = row-1, c = column-1; r>=0 && c>=0; r--, c--){
			if (cells[r][c] == curChess) {
				count++;
			} else {
				break;
			}
		}
		
		if (count >=4) {
			isWin = true;
		}
		return isWin;
	}
	
	public void console() {
		Scanner scanner = new Scanner(System.in);
		System.out.println("欢迎使用山寨版五子棋!");
		while( true ) {
			printCells();
			System.out.println("请 ["+(turn?"黑":"白")+"] 方落子...");
			String command = scanner.next();
			if (command.equalsIgnoreCase("quit")) {
				System.out.println("谢谢使用,再见!");
				break;
			} else {
				int row = command.charAt(0)-'a';
				int column = command.charAt(1)-'a';
				boolean isDown = downChess(row, column);
				if (!isDown) continue;
				boolean isWin = isWin(row, column);
				if (isWin) {
					printCells();
					System.out.println("["+(turn?"黑":"白")+"] 方获胜,游戏结束!");
					break;
				} else {
					turn = !turn;
				}
			}
			
		}
	}
	
	public static void main(String[] args) {
		FiveChess chess = new FiveChess();
		chess.console();

	}

}
package tarena;

import java.io.*;
import java.util.Arrays;

public class Chess {

	/**
	 * @param args
	 */
	private static final int CELL = 16;

	private static final char CELL_CHAR = '\u253C';

	private char[][] cells;

	private char a = 'a';

	private boolean flag = false;

	// private boolean turn;//true:黑;false:白

	public Chess() {
		cells = new char[CELL][CELL];
		for (int i = 0; i < cells.length; i++)
			Arrays.fill(cells[i], CELL_CHAR);

	}

	public void printChess() {// 打印棋盘
		System.out.print("   ");
		for (int i = 0; i < cells[0].length; i++)
			System.out.print((char) (a + i) + "  ");
		System.out.println();

		for (int i = 0; i < cells.length; i++) {
			for (int j = 0; j < cells[i].length; j++) {
				if (j == 0) {
					System.out.print((char) (a + i) + "  ");
				}
				System.out.print(cells[i][j] + "  ");
			}
			System.out.println();

		}

	}

	public void putChess(int row, int column) {// 放棋子 并同时判断输赢
		if (row > 15 || row < 0 || column > 15 || column < 0) {
			System.out.println("棋子位置错误,请重新输入!!");
		} else {
			if (cells[row][column] == '┼') {
				if (flag == false) {
					cells[row][column] = 'O';
					flag = true;
					printChess();
					boolean winFlag = isWin(row, column, 'O');
					if (winFlag == true) {
						System.out.println("白方win,game over!!");
						System.exit(1);
					}

				} else {
					cells[row][column] = '@';
					flag = false;
					printChess();
					boolean winFlag = isWin(row, column, '@');
					if (winFlag == true) {
						System.out.println("黑方win,game over!!");
						System.exit(1);
					}
				}
			} else {
				System.out.println("此位置棋子已经存在,请重新输入!!");
			}
		}

	}

	// /判断输赢的5个方法
	public boolean isWin(int row, int column, char c) {

		boolean win = false;
		if (rowSearch(row, column, c) >= 6           // 当前棋子重复计算 所以要>=6
				||columnSearch(row, column, c) >= 6
				|| leftSearch(row, column, c) >= 6
				|| rightSearch(row, column, c) >= 6)
			win = true;
		return win;
	}

	public int rowSearch(int row, int column, char c) {

		int tp1 = 0;
		int tp2 = 0;
		while (cells[row][column - tp1] == c) {
			tp1++;
			if ((column - tp1) < 0)
				break;

		}

		while (cells[row][column + tp2] == c) {
			tp2++;
			if ((column + tp2) > 15)
				break;
		}
		return tp1 + tp2;
	}

	public int columnSearch(int row, int column, char c) {
		int tp1 = 0;
		int tp2 = 0;
		while (cells[row - tp1][column] == c) {
			tp1++;
			if ((row - tp1) < 0)
				break;

		}

		while (cells[row + tp2][column] == c) {
			tp2++;
			if ((row + tp2) > 15)
				break;
		}
		return tp1 + tp2;
	}

	public int leftSearch(int row, int column, char c) {
		int tp1 = 0;
		int tp2 = 0;
		while (cells[row - tp1][column - tp1] == c) {
			tp1++;
			if ((row - tp1) < 0 || (column - tp1) < 0)
				break;
		}

		while (cells[row + tp2][column + tp2] == c) {
			tp2++;
			if ((row + tp2) > 15 || (column + tp2) > 15)
				break;
		}
		return tp1 + tp2;
	}

	public int rightSearch(int row, int column, char c) {
		int tp1 = 0;
		int tp2 = 0;
		while (cells[row - tp1][column + tp1] == c) {
			tp1++;
			if ((row - tp1) < 0 || (column + tp1) > 15)
				break;

		}

		while (cells[row + tp2][column - tp2] == c) {
			tp2++;
			if ((row + tp2) > 15 || (column - tp2) < 0)
				break;
		}
		return tp1 + tp2;
	}

	// 
	public void console() {

		System.out.println("欢迎使用五子棋系统!");
		printChess();

		while (true) {
			// printChess();
			System.out.println("现在请[" + (flag ? "黑" : "白") + "]方放子");
			System.out.println("\n请输入命令:");

			String str = null;
			try {
				BufferedReader buf;
				buf = new BufferedReader(new InputStreamReader(System.in));
				str = buf.readLine();
				// System.out.println(str);
			} catch (Exception e) {
				System.out.println("error");
			}

			int row = str.charAt(0) - 'a';
			int column = str.charAt(1) - 'a';
			putChess(row, column);

		}

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Chess ren = new Chess();
		ren.console();
	}

}
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值