您的位置:首页 > 编程语言 > Java开发

java实现简单五子棋

2016-06-17 09:52 531 查看
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sunt1921/article/details/51697743
package crazyit;
/**Description:
* <br>CopyRight,stc
* <br>program 简单五子棋的设计
* <br>Date:6.17
* @author sunt
* @version 1.0
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Gobang {

//定义棋盘大小
private static  int BOARD_SIZE = 15;
//定义一个二维数组来充当棋盘
private String[][] board;
public void initBoard(){
//初始化棋盘数组
board = new String[BOARD_SIZE][BOARD_SIZE];

//把每个元素赋值为“+”
for (int i = 0 ; i < BOARD_SIZE  ; i++)
{
for (int j = 0 ; j< BOARD_SIZE ; j++)
{
board[i][j] = "+";
}
}
}

//在控制台输出棋盘的方法
public void printBoard()
{
for (int i =0 ; i < BOARD_SIZE ; i++)
{
{
for (int j = 0 ; j < BOARD_SIZE ; j++)
System.out.print(board[i][j]);
}
System.out.print("\n");
}
}

//判断插入的棋子是否合法
public boolean pd(String[][] pos , int x , int y ) {

boolean isKey = true;
if (pos[x][y] == "&" || pos[x][y] == "●") {
isKey = false;
}
return isKey;
}

//封装一个可以判断五子在同一条直线上的方法
public boolean  isWin(String[][] pos , int x , int y , String z) {
boolean isWin = false;
int  countY =0, countX =0;
int temp;
//判断在y轴是否 为五子,利用county计数但实际会多加一次自身,所以最后判断是否有5子的时候要减一
temp = y;
while (pos[x][temp] == z) {
if (temp <= 0 || temp >= 14) {
break;
}
--temp;
++countY;
}
temp = y;
while (pos[x][temp] == z) {
if (temp <= 0 || temp >= 14) {
break;
}
++temp;
++countY;
}
//判断在x轴是否为五子
temp = x;
while (pos[temp][y] == z) {
if (temp <= 0 || temp >= 14) {
break;
}
--temp;
++countX;
}
temp = x;
while (pos[temp][y] == z) {
if (temp <= 0 || temp >= 14) {
break;
}
++temp;
++countX;
}
//判断从左上到右下
int i = x , j = y;
int countXL = 0, countXR = 0;
while (pos[i][j] == z) {
if (i <= 0 || i >= 14 || j <= 0 || j >= 14)
{
break;
}
--i;
--j;
++countXL;
}
i = x ;
j = y ;
while (pos[i][j] == z) {
if (i <= 0 || i >= 14 || j <= 0 || j >= 14)
{
break;
}
++i;
++j;
++countXL;
}
//判断从右上到坐下
while (pos[i][j] == z) {
if (i <= 0 || i >= 14 || j <= 0 || j >= 14)
{
break;
}
++i;
--j;
++countXR;
}
while (pos[i][j] == z) {
if (i <= 0 || i >= 14 || j <= 0 || j >= 14)
{
break;
}
--i;
++j;
++countXR;
}
if (countX == 5 || countY == 5 || countXL == 5 || countXR == 5)
{
isWin = true;
}
return isWin;
}

public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
Gobang gb = new Gobang();
gb.initBoard();
gb.printBoard();

String inputstr = null;
//读取键盘输入的方法
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader bfr = new BufferedReader(isr);
//bfr.readLine()读取键盘每一行
System.out.println("请输入您下棋的坐标,应以x,y的格式:");

while ((inputstr = bfr.readLine()) != null) {
//将用户输入的字符串以逗号作为分隔符,分隔成2个字符串
String[] posStrArr = inputstr.split(",");
int xPos = Integer.parseInt(posStrArr[0]);
int yPos = Integer.parseInt(posStrArr[1]);

/**电脑随机生成2个整数,作为电脑下棋的坐标,赋给board数组
* 1、坐标的有效性,只能是数字,不能超出棋盘范围
* 2、下的棋的点,不能重复下棋
* 3、每次下棋后,需要扫描谁赢了
* */
int xcPos = (int)(Math.random() * 15);
int ycPos = (int)(Math.random() * 15);
boolean pd = gb.pd(gb.board, xPos - 1, yPos - 1);
if (pd) {
gb.board [xPos - 1][yPos - 1] = "●";
if (gb.isWin(gb.board, xPos - 1, yPos - 1, "●")) {
System.out.println("恭喜你赢了");
continue;
}
else {
System.out.println("请重新输入您下棋的坐标,应以x,y的格式:");

}

}
boolean pd2 = gb.pd(gb.board, xcPos - 1, ycPos - 1);
while (pd2 == false)
{
xcPos = (int)(Math.random() * 15);
ycPos = (int)(Math.random() * 15);
pd2 = gb.pd(gb.board, xcPos - 1, ycPos - 1);
}
gb.board [xcPos - 1][ycPos - 1] = "&";
if (gb.isWin(gb.board, xcPos - 1, ycPos - 1, "&")) {

System.out.println("你输了 ");
continue;
}
gb.printBoard();

}

}

}

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: