首页 > 编程 > Java > 正文

java初学者--实现用控制台与电脑下五子棋

2019-11-08 01:32:06
字体:
来源:转载
供稿:网友

这是我无聊的时候敲了几个小时代码搞出来的玩意 我才刚学java两个星期 什么拓展性复用性并发我统统都不懂 人工智能也设计得相当的蠢 技术大牛们看个乐呵就好。。。

XXX

实现效果图

下面给出源代码:

Chess.java

package chessgame;import java.util.Scanner;/** * 五子棋游戏 * @author SamuelElijah * 转载请注明原作者 *  * */public abstract class Chess {	//存储棋子数量	public static int count = 0;	//最少取得胜利的联珠数	public static final int toWin = 5;	//存储棋子坐标	public static int x=7;	public static int y=7;	//存储坐标最值	public static int minX=6;	public static int maxX=6;	public static int minY=8;	public static int maxY=8;	//存储棋盘,大小为15	public static final Board board = new Board(15);	//static人工智能算法	public static int search(int x,int y,Chessman c){		String color = c.getChessman();		Chessman oppositeC = c instanceof White?new Black():c instanceof Black?new White():null;		String opposite = oppositeC.getChessman();		int count = 1,count2 =1 ;		for(int i=x-1;i>=0;i--){			if(!board.getPos(i, y).getChessman().equals(color)){				break;			}			count++;		}		for(int i=x+1;i<board.boardSize;i++){			if(!board.getPos(i, y).getChessman().equals(color)){				break;			}			count++;		}		for(int i=y+1;i<board.boardSize;i++){			if(!board.getPos(x, i).getChessman().equals(color)){				break;			}			count++;		}		for(int i=y-1;i>=0;i--){			if(!board.getPos(x, i).getChessman().equals(color)){				break;			}			count++;		}		for(int i=x-1,j=y-1;i>=0&&j>=0;i--,j--){			if(!board.getPos(i, j).getChessman().equals(color)){				break;			}			count++;		}		for(int i=x+1,j=y+1;i<board.boardSize&&j<board.boardSize;i++,j++){			if(!board.getPos(i, j).getChessman().equals(color)){				break;			}			count++;		}		for(int i=x-1,j=y+1;i>=0&&j<board.boardSize;i--,j++){			if(!board.getPos(i, j).getChessman().equals(color)){				break;			}			count++;		}		for(int i=x+1,j=y-1;i<board.boardSize&&j>=0;i++,j--){			if(!board.getPos(i, j).getChessman().equals(color)){				break;			}			count++;		}		//OPPOSITE		for(int i=x-1;i>=0;i--){			if(!board.getPos(i, y).getChessman().equals(opposite)){				break;			}			count2++;		}		for(int i=x+1;i<board.boardSize;i++){			if(!board.getPos(i, y).getChessman().equals(opposite)){				break;			}			count2++;		}		for(int i=y+1;i<board.boardSize;i++){			if(!board.getPos(x, i).getChessman().equals(opposite)){				break;			}			count2++;		}		for(int i=y-1;i>=0;i--){			if(!board.getPos(x, i).getChessman().equals(opposite)){				break;			}			count2++;		}		for(int i=x-1,j=y-1;i>=0&&j>=0;i--,j--){			if(!board.getPos(i, j).getChessman().equals(opposite)){				break;			}			count2++;		}		for(int i=x+1,j=y+1;i<board.boardSize&&j<board.boardSize;i++,j++){			if(!board.getPos(i, j).getChessman().equals(opposite)){				break;			}			count2++;		}		for(int i=x-1,j=y+1;i>=0&&j<board.boardSize;i--,j++){			if(!board.getPos(i, j).getChessman().equals(opposite)){				break;			}			count2++;		}		for(int i=x+1,j=y-1;i<board.boardSize&&j>=0;i++,j--){			if(!board.getPos(i, j).getChessman().equals(opposite)){				break;			}			count2++;		}		return 2*count+count2;	}        //static判断某个点是否为冲四活三点	public static boolean isFour(int x,int y){    	Chessman c = new Black();		String color = c.getChessman();		Chessman oppositeC = new White();		String opposite = oppositeC.getChessman();		int count = 1;		boolean flag = true;		for(int i=x-1;i>=0;i--){			if(!board.getPos(i, y).getChessman().equals(color)){				if(board.getPos(i, y).getChessman().equals(opposite)){					flag =false;				}				break;			}			count++;		}		for(int i=x+1;i<board.boardSize;i++){			if(!board.getPos(i, y).getChessman().equals(color)){				if(board.getPos(i, y).getChessman().equals(opposite)){					flag =false;				}				break;			}			count++;		}		if(count>=toWin){			return true;		}else if(count>=toWin-1 && flag){			return true;		}		count=1;flag = true;		for(int i=y+1;i<board.boardSize;i++){			if(!board.getPos(x, i).getChessman().equals(color)){				if(board.getPos(x, i).getChessman().equals(opposite)){					flag =false;				}				break;			}			count++;		}		for(int i=y-1;i>=0;i--){			if(!board.getPos(x, i).getChessman().equals(color)){				if(board.getPos(x, i).getChessman().equals(opposite)){					flag =false;				}				break;			}			count++;		}		if(count>=toWin){			return true;		}else if(count>=toWin-1 && flag){			return true;		}		count=1;flag = true;		for(int i=x-1,j=y-1;i>=0&&j>=0;i--,j--){			if(!board.getPos(i, j).getChessman().equals(color)){				if(board.getPos(i, j).getChessman().equals(opposite)){					flag =false;				}				break;			}			count++;		}		for(int i=x+1,j=y+1;i<board.boardSize&&j<board.boardSize;i++,j++){			if(!board.getPos(i, j).getChessman().equals(color)){				if(board.getPos(i, j).getChessman().equals(opposite)){					flag =false;				}				break;			}			count++;		}		if(count>=toWin){			return true;		}else if(count>=toWin-1 && flag){			return true;		}		count=1;flag = true;		for(int i=x-1,j=y+1;i>=0&&j<board.boardSize;i--,j++){			if(!board.getPos(i, j).getChessman().equals(color)){				if(board.getPos(i, j).getChessman().equals(opposite)){					flag =false;				}				break;			}			count++;		}		for(int i=x+1,j=y-1;i<board.boardSize&&j>=0;i++,j--){			if(!board.getPos(i, j).getChessman().equals(color)){				if(board.getPos(i, j).getChessman().equals(opposite)){					flag =false;				}				break;			}			count++;		}		if(count>=toWin){			return true;		}else if(count>=toWin-1 && flag){			return true;		}		count=1;flag = true;		//OPPOSITE		for(int i=x-1;i>=0;i--){			if(!board.getPos(i, y).getChessman().equals(opposite)){				if(board.getPos(i, y).getChessman().equals(color)){					flag =false;				}				break;			}			count++;		}		for(int i=x+1;i<board.boardSize;i++){			if(!board.getPos(i, y).getChessman().equals(opposite)){				if(board.getPos(i, y).getChessman().equals(color)){					flag =false;				}				break;			}			count++;		}		if(count>=toWin){			return true;		}else if(count>=toWin-1 && flag){			return true;		}		count=1;flag = true;		for(int i=y+1;i<board.boardSize;i++){			if(!board.getPos(x, i).getChessman().equals(opposite)){				if(board.getPos(x, i).getChessman().equals(color)){					flag =false;				}				break;			}			count++;		}		for(int i=y-1;i>=0;i--){			if(!board.getPos(x, i).getChessman().equals(opposite)){				if(board.getPos(x, i).getChessman().equals(color)){					flag =false;				}				break;			}			count++;		}		if(count>=toWin){			return true;		}else if(count>=toWin-1 && flag){			return true;		}		count=1;flag = true;		for(int i=x-1,j=y-1;i>=0&&j>=0;i--,j--){			if(!board.getPos(i, j).getChessman().equals(opposite)){				if(board.getPos(i, j).getChessman().equals(color)){					flag =false;				}				break;			}			count++;		}		for(int i=x+1,j=y+1;i<board.boardSize&&j<board.boardSize;i++,j++){			if(!board.getPos(i, j).getChessman().equals(opposite)){				if(board.getPos(i, j).getChessman().equals(color)){					flag =false;				}				break;			}			count++;		}		if(count>=toWin){			return true;		}else if(count>=toWin-1 && flag){			return true;		}		count=1;flag = true;		for(int i=x-1,j=y+1;i>=0&&j<board.boardSize;i--,j++){			if(!board.getPos(i, j).getChessman().equals(opposite)){				if(board.getPos(i, j).getChessman().equals(color)){					flag =false;				}				break;			}			count++;		}		for(int i=x+1,j=y-1;i<board.boardSize&&j>=0;i++,j--){			if(!board.getPos(i, j).getChessman().equals(opposite)){				if(board.getPos(i, j).getChessman().equals(color)){					flag =false;				}				break;			}			count++;		}		if(count>=toWin){			return true;		}else if(count>=toWin-1 && flag){			return true;		}else{			return false;		}    }}//玩家接口interface Actor{	//选择一个坐标下棋	void setectPos(Scanner sc);}//人类class Player extends Chess implements Actor{	PRivate Chessman c;	public Player(Chessman c){		this.c = c;	}	@Override	public void setectPos(Scanner sc) {		System.out.println("请输入坐标:(行 列)");		String str = sc.nextLine();		String[] s = str.split(" ");		x=Integer.parseInt(s[0]);		y=Integer.parseInt(s[1]);	}}//人工智能class AI extends Chess implements Actor{	private Chessman c;	private boolean first = true;	public AI(Chessman c){		this.c = c;	}	@Override	public void setectPos(Scanner sc) {		if(first){			x =  x+(int)(-1+Math.random()*3);			y =  y+(int)(-1+Math.random()*3);			first = false;		}else{			for(int x=minX;x<=maxX;x++){				for(int y=minY;y<=maxY;y++){					if(board.getPos(x, y) instanceof Empty){						if(isFour(x,y)){							Chess.x = x;							Chess.y = y;							return;						}					}				}			}			int maxCount = 0;			for(int x=minX;x<=maxX;x++){				for(int y=minY;y<=maxY;y++){					if(board.getPos(x, y) instanceof Empty){						int count = search(x, y, c);						if(count>maxCount){							maxCount = count;							Chess.x = x;							Chess.y = y;						}					}				}			}		}	}}//棋子类abstract class Chessman{	private final String chessman;	public Chessman(String chessman){		this.chessman = chessman;	}	public String getChessman(){		return chessman;	}}//白棋class White extends Chessman{	public White(){		super("○");	}}//黑棋class Black extends Chessman{	public Black(){		super("●");	}}//空(没有棋子)class Empty extends Chessman{	public Empty(){		super("十");	}}//棋盘class Board extends Chess{	//存储棋盘大小	public final int boardSize;	//棋盘上点的合集	private Chessman[][] board;	public Board(int boardSize){		this.boardSize = boardSize;		board = new Chessman[boardSize][boardSize];		for(int i=0;i<board.length;i++){			for(int j=0;j<board[i].length;j++){				board[i][j]=new Empty();			}		}	}	//打印棋盘	public void print(){		int line = 0;		System.out.print("/t"+0+"/t");		for(int i=1;i<boardSize;i++ ){			System.out.print(i+"/t");		}		System.out.println("/n");		for(int i=0;i<boardSize;i++){			System.out.print(line+"/t");			line++;			for(int j=0;j<boardSize;j++){				String str = board[i][j].getChessman();				System.out.print(str+"/t");			}			System.out.println("/n");		}		System.out.println("----------------------------------");	}	//在棋盘上添加棋子并记录坐标的最值	public void add(Chessman c){		board[x][y]=c;		//棋子数量加1		count++;		if(x-1<minX && x>0){			minX = x-1;		}else if(x+1>maxX && x<boardSize-1){			maxX = x+1;		}		if(y-1<minY && y>0){			minY = y-1;		}else if(y+1>maxY && y<boardSize-1){			maxY = y+1;		}	}	//判断坐标上的点是否取得胜利	public boolean isWon(){		String curr = board[x][y].getChessman();		int count = 1;		for(int i=x-1;i>=0;i--){			if(!board[i][y].getChessman().equals(curr)){				break;			}			count++;		}		for(int i=x+1;i<boardSize;i++){			if(!board[i][y].getChessman().equals(curr)){				break;			}			count++;		}		if(count>=Chess.toWin){			return true;		}		count = 1;		for(int i=y+1;i<boardSize;i++){			if(!board[x][i].getChessman().equals(curr)){				break;			}			count++;		}		for(int i=y-1;i>=0;i--){			if(!board[x][i].getChessman().equals(curr)){				break;			}			count++;		}		if(count>=Chess.toWin){			return true;		}		count = 1;		for(int i=x-1,j=y-1;i>=0&&j>=0;i--,j--){			if(!board[i][j].getChessman().equals(curr)){				break;			}			count++;		}		for(int i=x+1,j=y+1;i<boardSize&&j<boardSize;i++,j++){			if(!board[i][j].getChessman().equals(curr)){				break;			}			count++;		}		if(count>=Chess.toWin){			return true;		}		count = 1;		for(int i=x-1,j=y+1;i>=0&&j<boardSize;i--,j++){			if(!board[i][j].getChessman().equals(curr)){				break;			}			count++;		}		for(int i=x+1,j=y-1;i<boardSize&&j>=0;i++,j--){			if(!board[i][j].getChessman().equals(curr)){				break;			}			count++;		}		if(count>=Chess.toWin){			return true;		}else{			return false;		}	}	//判断输入的坐标是否有效    public boolean isValid(){    	if(x>=boardSize    	   ||     		x<0    	   ||    	   y>=boardSize    	   ||    	   y<0){    		return false;    	}else if(!(board[x][y] instanceof Empty)){    		return false;    	}else{    		return true;    	}    }	//输出某坐标上棋子的位置    public Chessman getPos(int x,int y){    	return board[x][y];    }}

ChessTest.java

package chessgame;import java.util.Scanner;/** * 五子棋游戏(主方法) * @author SamuelElijah * 转载请注明原作者 *  * */public class ChessTest {	public static void main(String[] args) {		Scanner sc = new Scanner(System.in);		int i=-1;		Actor player1=null;		Actor player2=null;		do{			System.out.println("输入0选择黑方,输入1选择白方");			try{				i = sc.nextInt();sc.nextLine();			}catch(Exception e){				System.exit(0);			}		}while(i>1||i<0);		switch(i){		case 0:			player1 = new Player(new Black()); 			player2 = new AI(new White()); 			break;		case 1:			player1 = new AI(new Black()); 			player2 = new Player(new White()); 			break;		default:			}		Chess.board.print();		while(true){			try {				Thread.sleep(1000);			} catch (InterruptedException e1) {				e1.printStackTrace();			}			do{				try{					player1.setectPos(sc);				}catch(Exception e){					continue;				}							}while(!Chess.board.isValid());			Chess.board.add( new Black());			Chess.board.print();			if(Chess.board.isWon()){				System.out.println("黑方胜");				break;			}else if(Chess.count>=Chess.board.boardSize){				System.out.println("和棋");			}			try {				Thread.sleep(1000);			} catch (InterruptedException e1) {				e1.printStackTrace();			}			do{				try{					player2.setectPos(sc);				}catch(Exception e){					continue;				}					}while(!Chess.board.isValid());			Chess.board.add( new White());			Chess.board.print();			if(Chess.board.isWon()){				System.out.println("白方胜");				break;			}else if(Chess.count>=Chess.board.boardSize){					System.out.println("和棋");			}		}	}		}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表