Java制作的五子棋小例子

本文介绍了一个简单的Java控制台程序,用于教学围棋的基本操作,包括棋盘的初始化、棋子的放置及基本的输入验证。

1.本实例基于java控制台输出

2.本实例未包含胜负判断算法,待完善

3.本实例用于数组、类和对象、输入输出、流程控制等知识点的教学用

PlayGo.java

package cn.edu.lecheng;

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

public class PlayGo {
	
	private static final int BOARD_SIZE = 10;

	/**
	 * @param args
	 * 创建者:TOM
	 * 创建日期:2013-7-16
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		String inputStr = "";
		int stepCount = 1;
		//初始化棋盘
		Board board = new Board(BOARD_SIZE);
		Position position = new Position(BOARD_SIZE);
		//board[3][6]="◎";
		//board[2][6]="◆";
		//输出初始化的棋盘
		System.out.println("游戏开始,初始化棋盘:");
		board.printBoard();
		System.out.println("玩家1所用棋子:◎;玩家2所用棋子:◆。");
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		try{
			System.out.print("第1步——请输入棋子的行列坐标,用逗号隔开:");
			while((inputStr=br.readLine())!=null){
				if(position.checkPositionFormat(inputStr)){ //判断输入的坐标格式是否正确
					if(board.checkBoard(inputStr)){
						board.play(inputStr, stepCount);
						board.printBoard();
						stepCount++;
					}else{
						System.out.println("该位置已经有子,请重新下。");
						continue;
					}
					
				}else{
					System.out.println("输入的坐标不正确!请重新输入:");
					continue;
				}
				System.out.print("第"+stepCount+"步——请输入棋子的行列坐标,用逗号隔开:");
				
			}
		}catch(IOException ioe){
			System.out.println(ioe.getMessage());
		}
		
	}

}
Board.java

package cn.edu.lecheng;

public class Board {

	private String[][] board;
	private int boardSize = 0;

	// 构造方法
	public Board(int size) {
		// TODO Auto-generated constructor stub
		boardSize = size;
		board = new String[size][size];
		for (int i = 0; i < size; i++) {
			for (int j = 0; j < size; j++) {
				if (i == 0 && j == 0) {
					board[i][j] = " ";// 左上角
				} else if (i == 0 && j != 0) {
					board[i][j] = (new Integer(j)).toString() + " ";// 列号.空格为对齐表格
				} else if (i != 0 && j == 0) {
					board[i][j] = (new Integer(i)).toString();// 行号
				} else {
					board[i][j] = "╋";
				}
			}
		}
	}
	/**
	 * 输出棋盘
	 * 
	 * 创建者:TOM
	 * 创建日期:2013-7-17
	 */
	public void printBoard() {
		for (int i = 0; i < boardSize; i++) {
			for (int j = 0; j < boardSize; j++) {
				System.out.print(board[i][j]);
			}
			System.out.println("");
		}
	}
	/**
	 * 下子
	 * @param pos
	 * @param step
	 * 创建者:TOM
	 * 创建日期:2013-7-17
	 */
	public void play(String pos, int step){
		String[] tempStr = pos.split(",");
		int row = Integer.parseInt(tempStr[0]);
		int col = Integer.parseInt(tempStr[1]);
		if(step % 2 !=0){
			board[row][col]="◎";
		}else{
			board[row][col]="◆";
		}
	}
	/**
	 * 检测该位置是否已有子
	 * @param pos
	 * @return 无子返回true,有子返回false
	 * 创建者:TOM
	 * 创建日期:2013-7-17
	 */
	public boolean checkBoard(String pos){
		boolean flag = false;
		String[] tempStr = pos.split(",");
		int row = Integer.parseInt(tempStr[0]);
		int col = Integer.parseInt(tempStr[1]);
		if(board[row][col].equals("╋")){
			flag = true;
		}
		return flag;
	}

}
Position.java

package cn.edu.lecheng;

public class Position {

	private int bSize = 0;
	public Position(int size){
		bSize = size;
	}
	/**
	 * 检查坐标输入格式
	 * @param pos
	 * @return
	 * 创建者:TOM
	 * 创建日期:2013-7-18
	 */
	public boolean checkPositionFormat(String pos){
		boolean flag=false;
		String[] tempStr = pos.split(",");
		try{
			if(Integer.parseInt(tempStr[0]) > bSize-1 || Integer.parseInt(tempStr[0]) <=0 ){
				return false;
			}
			if(Integer.parseInt(tempStr[1]) > bSize-1 || Integer.parseInt(tempStr[1]) <=0 ){
				return false;
			}
			flag = true;
		}catch(Exception e){
			flag=false;
		}
		return flag;
	}
}

演示效果


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值