📄 gobang.java
字号:
/**
程序重要说明:此程序必须由命令行编译,并运行。在eclipse下无法执行。
程序原理:此游戏由两个角色使用,分别是"AAA"和"BBB"。
用户选择角色,并通过输入棋盘上空格的横纵坐标放置棋子。
注意:棋子的坐标不能超过棋盘的边界,同时输入横坐标X=0作为程序终止的条件。
*/
import java.io.*;
public class gobang {
int Size = 10;//棋盘的大小
int [][] Chessboard;//棋盘数组,没有棋子的位置值为0,有AAA的棋子值为1,有BBB的棋子值为-1
String role = "AAA";//下棋的角色:AAA和BBB
//init为初始化函数,负责初始化棋盘数组,将所有位置置为0
//同时,输出提示信息,供用户选择首先下棋的角色。
public void init()
{
//初始化棋盘
Chessboard = new int[Size][Size];
for(int i = 0; i < Size; i++)
{
for(int j = 0; j < Size; j++)
Chessboard[i][j] = 0;
}
//输出提示信息
System.out.println("the game is gobang\n! the chessman of role :AAA is '*'\n" +
"the chessman of role :BBB is '@'\nplease select who first:AAA ?Y/N");
try
{
byte []s = new byte[128];
//接收用户输入的选择项,由AAA首先走棋(默认的)还是由BBB首先走棋
System.in.read(s);
if(s[0] == 'N')
role = "BBB";
else
role = "AAA";
}
catch(Exception e)
{
}
}
//print函数的功能是输出棋盘.
//将棋盘数组输出,值为1的输出标志"*",为-1的输出"@"
public void print()
{
//输出横向的标志数
for(int j = 0; j < Size; j++)
{
System.out.print(" "+(j+1)+" ");
}
System.out.println("");
//输出棋盘的横线
for(int i = 0; i < Size; i++)
{
for(int j = 0; j < Size; j++)
{
System.out.print("+---");
}
System.out.println("+");
//输出棋盘的竖线和棋子
for(int j = 0; j < Size; j++)
{
if(Chessboard[i][j] == 1)
System.out.print("| * ");
else if(Chessboard[i][j] == -1)
System.out.print("| @ ");
else
System.out.print("| ");
}
//输出最后一列的竖线和数字标志
System.out.println("|"+(i+1));
}
//输出棋盘的最后一行的横线
for(int j = 0; j < Size; j++)
{
System.out.print("+---");
}
System.out.println("+");
}
//check是五子棋的核心函数,用于判断棋手是否赢棋
public boolean Check(int x, int y)
{
//check函数判断的过程是对新插入棋子(x,y)所在的行、列、左对角线和右对角线进行判断。
//判断值相同(为flag的)棋子数目是否大于等于5,有一个成立,则说明棋手胜利。
/*****/
//判断棋手,设置标志值flag,棋手为"AAA"时,标志值为1,否则为-1
int flag = role.equals("AAA") ? 1 : -1;
//定义参数:count是棋盘数组中值相同的连续数,i是横向游标,j是纵向游标
int count = 0, i, j;
/**首先判断在棋子所在的行上连续的棋子数**/
//先计算左半边的连续棋子数
/*计算的原理是连续查找棋盘数组的值等于flag的数目*/
i = x;
j = y;
while(i >= 0)
{
if(Chessboard[i][y] == flag)
{
count ++;
i--;
}
else//值不等于flag后中断查找
break;
}
//再计算右半边的连续棋子数,将两项加和
i = x+1;
while(i < Size)
{
if(Chessboard[i][y] == flag)
{
count ++;
i++;
}
else
break;
}
if(count >= 5)
return true;
else
{
count = 0;
}
/**计算在棋子所在的列上连续的棋子数**/
//先计算上半边的连续棋子数
i = x;
j = y;
while(j >= 0)
{
if(Chessboard[x][j] == flag)
{
count ++;
j--;
}
else
break;
}
//再计算下半边的连续棋子数,将两项加和
j = y+1;
while(j < Size)
{
if(Chessboard[x][j] == flag)
{
count ++;
j++;
}
else
break;
}
if(count >= 5)
return true;
else
{
count = 0;
}
/**计算在棋子所在的左对角线上连续的棋子数**/
//先计算左上半边的连续棋子数
i = x;
j = y;
while(j >= 0 && i >= 0)
{
if(Chessboard[i][j] == flag)
{
count ++;
j--;
i--;
}
else
break;
}
//再计算右下半边的连续棋子数,将两项加和
j = y+1;
i = x+1;
while(j < Size && i < Size)
{
if(Chessboard[i][j] == flag)
{
count ++;
j++;
i++;
}
else
break;
}
if(count >= 5)
return true;
else
{
count = 0;
}
/**计算在棋子所在的右对角线上连续的棋子数**/
//先计算右上半边的连续棋子数
i = x;
j = y;
while(j >= 0 && i < Size)
{
if(Chessboard[i][j] == flag)
{
count ++;
j--;
i++;
}
else
break;
}
// 再计算左下半边的连续棋子数,将两项加和
j = y+1;
i = x-1;
while(j < Size && i >= 0)
{
if(Chessboard[i][j] == flag)
{
count ++;
j++;
i--;
}
else
break;
}
if(count >= 5)
return true;
else
{
count = 0;
}
//不存在连续五个的棋子,返回false代表没有赢棋
return false;
}
public void start()
{
byte [] bstr = new byte[128];
//x是棋子的横向坐标,y是棋子的纵向坐标
int x = 0, y = 0, k = 0;
//result是赢棋结果
boolean result = false;
//进入死循环while,在用户输入横坐标x为0时,程序终止。
//或者棋手的一方获胜后程序终止
while(true)
{
System.out.print(role+": please input your chessman's location:\nX=");
//获取棋子的横坐标x
try
{
k = System.in.read(bstr);
//将读入的字符串转换成数字(输入值减一,为了满足数组下标的要求)
x = Integer.parseInt(new String(bstr, 0, k-2)) - 1;
}
catch(Exception e)
{
}
if(x == -1)
{
//当输入的x的值为0时,即:x-1后为-1时程序终止
System.out.print("the game end!\n");
break;
}
//判断输入值是否合法,在棋盘范围内。
if(x < 0 || x >= Size)
{
System.out.print(role+": \nyou x value is error,please input again\nX=");
continue;
}
//读入用户输入的纵坐标y,并转换数字
System.out.print("\nY=");
try
{
k = System.in.read(bstr);
y = Integer.parseInt(new String(bstr, 0, k-2)) - 1;
}
catch(Exception e)
{
}
if(y < 0 || y >= Size)
{
System.out.print(role+": \nyou y value is error,please input again\ny=");
continue;
}
//判断用户输入的棋子位置上是否已经放置棋子。
//如果没有放置棋子,则放置并进行结果的正确性判断
if(Chessboard[x][y] != 1 && Chessboard[x][y] != -1)
{
//为放置的棋子的位置进行赋值
if(role.equals("AAA"))
{
Chessboard[x][y] = 1;
}
else
{
Chessboard[x][y] = -1;
}
//输出重新放置棋子后的棋盘图。
print();
//利用Check函数进行判断,检查棋子(x,y)放置后,此角色是否获胜
result = Check(x, y);
//如果result为true,说明此角色获胜,则输出胜利信息并终止程序
if(result)
{
System.out.println(role+": win the game!!");
break;
}
//切换角色,在"AAA"用户走完棋后,角色换成"BBB"
if(role.equals("AAA"))
role = "BBB";
else
role = "AAA";
}
else
{
//当用户放置棋子的位置已经有棋子存在,则让用户重新放置。
System.out.print(role+": \nyour location of put had a chessman, please input again\n");
continue;
}
}
}
public static void main(String[] args) {
gobang gb = new gobang();
//初始化棋盘
gb.init();
//打印空棋盘
gb.print();
//开始游戏
gb.start();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -