Java实现文本框五子棋

实现模式

View-Control-Model

实现功能

  • 接收用户通过命令行输入下棋的位置
  • 判断下棋位置是否合法,并输出每一次下棋的结果
  • 判断哪一方胜利:从当前下棋的位置开始判断,垂直、水平、左斜线、右斜线是否有五个连续的同色棋子
  • 悔棋:Model中记录上一个棋子的位置,悔棋则将该位置置为空即可
  • 结束游戏,并判断退出或重新开始

View.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class View {
	//获取用户输入的先手棋子
	public void getPlayer() {
		BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
		String s=null;
		try { 
			System.out.println("Which one goes first?");
			System.out.println("Black: 0   White: 1");
			s=in.readLine();
			int player=Integer.parseInt(s);
			Control.getInstance().setPlayer(player);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//获取用户输入的下棋位置
	public void input() {
		BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
		String s=null;
		try { //用户输入要下棋的行列数
			System.out.print("Please input row: ");
			s=in.readLine();
			int row=Integer.parseInt(s);
			System.out.print("Please input col: ");
			s=in.readLine();
			int col=Integer.parseInt(s);
			Control.getInstance().localUserPutChess(row-1,col-1);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	private View() {}
	private static View instance;
	public static View getInstance() {
		if (instance==null) {
			instance = new View();
		}
		return instance;
	}
	//输出各种提示
	public void outputWarning(String string) {
		System.out.println(string);
	}
	//下棋位置合法,显示下棋的位置
	public void outputChess() {
		for (int row=0;row<Model.WIDTH;row++) {
			for (int col=0;col<Model.WIDTH;col++) {
				//获取该位置的棋子颜色(或空)
				int chess=Model.getInstance().getChess(row,col);
				switch(chess){
					case Model.BLACK:
						System.out.print("●");
						break;
					case Model.WHITE:
						System.out.print("O");
						break;
					case Model.SPACE:
						System.out.print("十");
						break;
				}
			}
			System.out.println();
		}
	}
	//分出胜负后结束游戏,判断是退出还是重新开始
	public int endGame() {
		BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
		String s=null;
		int end=1;
		try { 
			System.out.println("The game is ended! What do you want to do?");
			System.out.println("Exit: 0   Restart: 1");
			s=in.readLine();
			end=Integer.parseInt(s);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return end;
	}
}

Control.java

//中心控制

public class Control {
	private int color;
	private Control() {}
	private static Control instance;
	public static Control getInstance() {
		if (instance == null) {
			instance =  new Control();
		}
		return instance;
	}
	//设置先手方
	public void setPlayer(int i) {
		if(i==0)
			color=Model.BLACK;
		else if(i==1)
			color=Model.WHITE;
		else {
			View.getInstance().outputWarning("Illegal input!");
			View.getInstance().getPlayer();
		}
	}
	//主函数
	public static void main(String[] args) {
		do {
			//清除数据
			Model.getInstance().clear();
			//提示悔棋的规则
			View.getInstance().outputWarning("Notice: if you want to withdraw your move, please input (0,0)");
			//获取先手的一方
			View.getInstance().getPlayer();
			//当还没有分出胜负时,一直下棋
			while(Model.getInstance().whoWin()==Model.SPACE) {
				View.getInstance().input();
			}
		} while (View.getInstance().endGame()==1); //如果选择重新开始
		//如果选择退出游戏
		View.getInstance().outputWarning("Thank you for playing!");
	}
	public void localUserPutChess(int row, int col) { //用户下棋
		//如果要悔棋
		if(row==-1&&col==-1) {
			Model.getInstance().withdraw();
			View.getInstance().outputChess();
			color=-color; //让悔棋的一方重新下
			return ;
		}
		//判断是否下棋成功
		boolean success=Model.getInstance().putChess(row,col,color);
		//如果下棋的位置是合法的
		if(success) {
			View.getInstance().outputChess(); //显示下棋的结果
			int winner = Model.getInstance().whoWin(); //下棋后判断哪方胜利
			switch(winner) {
			case Model.BLACK: //黑方胜利
				View.getInstance().outputWarning("Congratulations, black wins!");
				break;
			case Model.WHITE: //白方胜利
				View.getInstance().outputWarning("Congratulations, white wins!");
				break;
			case Model.SPACE: //还未分出胜负
				break;
			}
			color=-color; //交换下棋方
		}
		//如果下棋的位置不合法
		else {
			View.getInstance().outputWarning("Illegal input!");
		}
	}
}

Model.java

//游戏规则的控制

public class Model {
	public static final int BLACK = 1; //棋盘的位置上下了黑棋
	public static final int WHITE = -1; //棋盘的位置上下了白棋
	public static final int SPACE = 0; //棋盘的位置为空
	public static final int WIDTH = 19; //棋盘的尺寸
	
	private int[][] data = new int[WIDTH][WIDTH]; //创建棋盘
	private int lastRow,lastCol,lastColor; //记录最新下的棋子的参数
	
	private Model() {}
	private static Model instance;
	public static Model getInstance() {
		if (instance == null) {
			instance = new Model();
		}
		return instance;
	}
	//返回是否下棋成功
	public boolean putChess(int row,int col,int color) { 
		//判断行列数是否合法、是否轮到正确的一方下棋
		if(row>=0&&row<WIDTH&&col>=0&&col<WIDTH&&(color==BLACK||color==WHITE)) {
			//该位置是否已经有棋子
			if(data[row][col]==SPACE) {
				data[row][col]=color;
				lastRow=row;
				lastCol=col;
				lastColor=color;
				return true;
			}
		}
		return false;
	}
	//判断哪一方赢了
	public int whoWin() {
		//只需要判断最新下的棋子一方有没有赢即可
		int count=1; //记录连续相同颜色的棋子个数
		//计算同一列连续连续相同颜色的棋子个数
		for(int i=lastRow-1;i>=0;i--) {
			if(getChess(i,lastCol)==lastColor) {
				count++;
				if(count==5)
					return lastColor;
			}
			else
				break;
		}
		for(int i=lastRow+1;i<WIDTH;i++) {
			if(getChess(i,lastCol)==lastColor) {
				count++;
				if(count==5)
					return lastColor;
			}
			else
				break;
		}
		count=1;
		//计算同一行连续颜色相同的棋子个数
		for(int i=lastCol-1;i>=0;i--) {
			if(getChess(lastRow,i)==lastColor) {
				count++;
				if(count==5)
					return lastColor;
			}
			else
				break;
		}
		for(int i=lastCol+1;i<WIDTH;i++) {
			if(getChess(lastRow,i)==lastColor) {
				count++;
				if(count==5)
					return lastColor;
			}
			else
				break;
		}
		count=1;
		//计算左斜线上连续颜色相同的棋子个数
		for(int i=lastRow-1,j=lastCol-1;i>=0&&j>=0;i--,j--) {
			if(getChess(i,j)==lastColor) {
				count++;
				if(count==5)
					return lastColor;
			}
			else
				break;
		}
		for(int i=lastRow+1,j=lastCol+1;i<WIDTH&&j<WIDTH;i++,j++) {
			if(getChess(i,j)==lastColor) {
				count++;
				if(count==5)
					return lastColor;
			}
			else
				break;
		}
		count=1;
		//计算右斜线上连续颜色相同的棋子个数
		for(int i=lastRow-1,j=lastCol+1;i>=0&&j<WIDTH;i--,j++) {
			if(getChess(i,j)==lastColor) {
				count++;
				if(count==5)
					return lastColor;
			}
			else
				break;
		}
		for(int i=lastRow+1,j=lastCol-1;i<WIDTH&&j>=0;i++,j--) {
			if(getChess(i,j)==lastColor) {
				count++;
				if(count==5)
					return lastColor;
			}
			else
				break;
		}
		return SPACE;
	}
	//返回该位置的棋子颜色(或空)
	public int getChess(int row,int col) {
		if(row>=0&&row<WIDTH&&col>=0&&col<WIDTH) {
			return data[row][col];
		}
		return SPACE;
	}
	//悔棋
	public void withdraw() {
		data[lastRow][lastCol]=SPACE;
	}
	//清除数据
	public void clear() {
		for (int row=0;row<WIDTH;row++) 
			for (int col=0;col<WIDTH;col++) 
				data[row][col]=SPACE;
	}
}

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值