五子棋(面向对象)

五子棋(面向对象)

index.java

package com.lv.Gobang;

import java.util.Scanner;

public class index {
	public static void main(String[] args) {
		Gobang gobang = new Gobang();
		
		Scanner scan = new Scanner(System.in);
		boolean flag=true;
		while(true){
			
			//输入坐标
			System.out.println("请" + ((flag)?"黑":"白") + "子输入坐标:");
			int x = scan.nextInt() - 1;//-1是因为用户看到的界面是坐标(从1开始),数组是下标(从0开始)
			int y = scan.nextInt() - 1;//-1是因为用户看到的界面是坐标(从1开始),数组是下标(从0开始)
			
			//落子
			int play = gobang.play(x, y, flag);
			if(play == -1){
				System.out.println("坐标错误 - 坐标超出棋盘范围,请重新输入");
				continue;
			}else if(play == -2){
				System.out.println("坐标错误 - 坐标上已有棋子,请重新输入");
				continue;
			}
			
			//打印棋盘
			gobang.printGobang();
			
			//判断输赢
			
			boolean isWinOrLose=gobang.isWinOrLose(x, y, flag);
			if(isWinOrLose){
				System.out.println(((flag)?"黑":"白") + "子赢");
				scan.close();
				break;
			}
			
			//置反
			flag = !flag;
		}	
	}
}

Gobang.java

package com.lv.Gobang;

public class Gobang {
	//棋盘长度
	private int length = 20;
	//棋盘容器
	private String[][] gobang = new String[length][length];
	//棋盘坐标
	private String[] nums = {"⒈","⒉","⒊","⒋","⒌","⒍","⒎","⒏","⒐","⒑","⒒","⒓","⒔","⒕","⒖","⒗","⒘","⒙","⒚","⒛"};
	//棋盘符号
	private String add = "┼";
	private String black = "●";
	private String white = "○";
	
	public Gobang(){
		this.init();
		this.printGobang();
	}
	//初始化棋盘
	private void init() {
		for (int i = 0; i < gobang.length; i++) {
			for (int j = 0; j < gobang[i].length; j++) {
				if(j == length-1){//每行的一列,设置行数

					gobang[i][j] = nums[i];
					
				}else if(i == length-1){//最后一行,设置列数
					
					gobang[i][j] = nums[j];
					
				}else{
					gobang[i][j] = add;
				}
			}
		}	

	}
	//打印棋盘
	public void printGobang() {
		for (String[] strings : gobang) {
			for (String str : strings) {
				System.out.print(str);
			}
			System.out.println();
		}
	}
	//判断下标是否在棋盘范围内
	public boolean isIndexOutOfGobang(int x,int y) {
		if(x<0 || x>length-2 || y<0 || y>length-2){
			return false;
		}
		return true;
	}	
	//判断下标上是否有棋子
	public boolean isPiece(int x,int y) {
		if(!gobang[x][y].equals(add)){
			return false;
		}
		return true;
	}	
	//落子
	public int play(int x,int y,boolean flag) {
		if (!isIndexOutOfGobang(x, y)) {
			return -1;
		}
		if (!isPiece(x, y)) {
			return -2;
		}
		
		String piece = piece(black, white, flag);
		gobang[x][y] = piece;
		return 1;
	}

	public static String piece(String black,String white,boolean flag){
		String piece = (flag)?black:white;
		return piece;
	}
	//判断输赢
	public boolean isWinOrLose(int x, int y,boolean flag) {
		
		boolean eastAndWest = eastAndWest(x, y, Gobang.piece(black, white, flag), gobang, length);
		boolean northAndSouth=northAndSouth(x, y,Gobang.piece(black, white, flag), gobang, length);
		boolean northeastAndSouthwest=northeastAndSouthwest(x, y,Gobang.piece(black, white, flag), gobang, length);
		boolean southeastAndNorthwest=southeastAndNorthwest(x, y,Gobang.piece(black, white, flag), gobang, length);
		if(eastAndWest || northAndSouth || northeastAndSouthwest || southeastAndNorthwest){
			return true;
		}
		return false;
	}
	public static boolean eastAndWest(int x,int y,String piece,String[][] gobang,int length){
		int count = 1;
		//计算左边连续棋子
		int index = y;
		while(index > 0){
			index--;
			if(gobang[x][index].equals(piece)){
				count++;
			}else{
				break;
			}
		}	
		//计算右边连续棋子
		index = y;
		while(index < length-2){
			index++;
			if(gobang[x][index].equals(piece)){
				count++;
			}else{
				break;
			}
		}
		if(count >= 5){
			return true;
		}
		return false;
	}
	public static boolean northAndSouth(int x,int y,String piece,String[][] gobang,int length){
		int count=1;
		//计算上边连续棋子
		int index = x;
		while(index >0){
			index--;
			if(gobang[index][y].equals(piece)){
				count++;
			}else{
				break;
				}
			}
		//计算下边连续棋子
		index = x;
		while(index < length-2){
		index++;
		if(gobang[x][index].equals(piece)){
			count++;
			}else{
				break;
			}
		}
		if(count >= 5){
			return true;
		}
		return false;
	}
	public static boolean northeastAndSouthwest(int x,int y,String piece,String[][] gobang,int length){
		int count=1;  
		//计算左上边连续棋子
		int index = x;
		int index1= y;
		while(index >0 && index1 > 0){
			index--;
		    index1--;
			if(gobang[index][index1].equals(piece)){
				count++;
			}else{
				break;
			}
		}
		//计算右下边连续棋子
	    index = x;
	    index1= y;
		while(index < length-2 && index1 <length-2){
			index++;
	        index1++;
			if(gobang[index][index1].equals(piece)){
				count++;
			}else{
				break;
			}
		}
		if(count >= 5){
			return true;
		}
		return false;
	}
	public static boolean southeastAndNorthwest(int x,int y,String piece,String[][] gobang,int length){
		int  count=1;
		 //计算右上边连续棋子
	    int index = x;
	    int index1= y;
		while(index >0 && index1 < length-2 ){
			index--;
	        index1++;
			if(gobang[index][index1].equals(piece)){
				count++;
			}else{
				break;
			}
		}
	    //计算左下边连续棋子
	     index = x;
	     index1= y;
		while(index < length-2 && index1 > 0){
			index++;
	        index1--;
			if(gobang[index][index1].equals(piece)){
				count++;
			}else{
				break;
			}
		}
		if(count >= 5){
			return true;
		}
		return false;
	}
}

运行结果:

在这里插入图片描述

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值