本篇内容介绍了“Java如何实现简单无界面五子棋”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
1、棋盘设计为10*10格,棋盘类型Chess[][] 二维数组,所含属性String chessType; 棋盘首先chessType值是”➕”。
2、初始化二维数组
3、玩家选择黑白圈后,开始下棋。输入要下棋子的行列坐标,黑白棋子轮流落子,当一方连成五子或下满棋盘时,游戏结束(连成五子的一方获胜,下满棋盘为和棋)。
4、每一次落子成功后,马上判断以该位置为中心的八个方向:上、下、左、右、左上、左下、右上、右下是否有相同颜色的棋子连成五子,如果连成五子,则游戏结束,输出相应的信息。
5、当游戏一方胜利后显示胜利信息。从程序表面看,这是一个二维平面图,所以数据用二维数组来表示,数组两个下标可以表示棋盘上的位置,数组元素的值代表棋格上的状态,共有三种情况,分别是,⭕代表白棋,●代表黑棋,➕代表格子。
1.棋子
/**
* @author hudongsheng
* @date 2020/10/29 - 9:28
*/
public class ChessType {
private String chessType;
private int x;
private int y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public ChessType() {
}
public String getChessType() {
return chessType;
}
public void setChessType(String chessType) {
this.chessType = chessType;
}
}
2.下棋
/**
* @author hudongsheng
* @date 2020/10/29 - 9:27
*/
public class Gobang {
private int size = 1;
private ChessType[][] chessTypes;
private int row;
private int colum;
private int x;
private int y;
//创建一个棋盘
public Gobang(int row,int colum){
this.row = row;
this.colum = colum;
chessTypes = new ChessType[row][colum];
}
//初始化棋盘
public void initChessType(){
for(int i = 0; i< chessTypes.length; i++){
for (int j = 0; j< chessTypes[i].length; j++){
chessTypes[i][j] = new ChessType();
chessTypes[i][j].setChessType("➕");
}
}
}
//下白棋
public void setWhiteChess(int x,int y){
chessTypes[x][y].setChessType("⭕");
}
//下黑棋
public void setBlackChess(int x,int y){
chessTypes[x][y].setChessType("●");
}
//判断是否胜利
public boolean checkWin(int i,int j) {
// TODO Auto-generated method stub
boolean flag = false;
//判断纵向是否有五个棋子是相同的颜色
int count1 = 1;//相同颜色棋子的个数
String color = chessTypes[i][j].getChessType(); //刚下的棋子的颜色
int a = 1; //棋子索引的增量
while((i+a)<row && color == chessTypes[i+a][j].getChessType()){
count1++;
a++;
}
a = 1;
while((i-a)>=0 && color == chessTypes[i-a][j].getChessType()){
count1++;
a++;
}
if(count1 >= 5){
flag = true;
}
//判断纵向是否有五个棋子是相同的颜色
int count2 = 1;
a = 1;
while((j+a)<colum && color == chessTypes[i][j+a].getChessType()){
count2++;
a++;
}
a = 1;
while((j-a)>=0 && color == chessTypes[i][j-a].getChessType()){
count2++;
a++;
}
if(count2 >= 5){
flag = true;
}
//右上 左下 是否有五个棋子是相同的颜色
int count3 = 1;
a = 1;
while((i+a)<row && (j-a)>=0 && color == chessTypes[i+a][j-a].getChessType()){
count3++;
a++;
}
a = 1;
while((i-a)>=0 && (j+a)<colum && color == chessTypes[i-a][j+a].getChessType()){
count3++;
a++;
}
if(count3 >= 5){
flag = true;
}
//左上 右下 是否有五个棋子是相同的颜色
int count4 = 1;
a = 1;
while((i-a)>0 && (j-a)>=0 && color == chessTypes[i-a][j-a].getChessType()){
count4++;
a++;
}
a = 1;
while((i+a)<row && (j+a)<colum && color == chessTypes[i+a][j+a].getChessType()){
count4++;
a++;
}
if(count4 >= 5){
flag = true;
}
return flag;
}
//落子后打印棋盘
public void print(){
for(int i = 0; i< chessTypes.length; i++){
for (int j = 0; j< chessTypes[i].length; j++){
System.out.print(chessTypes[i][j].getChessType());
}
System.out.println();
}
}
}
3.测试
**
* @author hudongsheng
* @date 2020/10/29 - 9:27
*/
public class Test {
public static void main(String[] args) {
boolean flag = true;
int x;
int y;
Gobang gobang = new Gobang(10,10);
Scanner scanner = new Scanner(System.in);
gobang.initChessType();
//下棋
System.out.println("黑棋执先");
while (true){
gobang.print();
System.out.println("请输入下黑棋的坐标:");
x = scanner.nextInt();
y = scanner.nextInt();
gobang.setBlackChess(x,y);
if(gobang.checkWin(x,y)){
gobang.print();
System.out.println("黑棋胜!");
break;
}
gobang.print();
System.out.println("请输入下白棋的坐标:");
x = scanner.nextInt();
y = scanner.nextInt();
gobang.setWhiteChess(x,y);
if(gobang.checkWin(x,y)){
gobang.print();
System.out.println("白棋胜!");
break;
}
}
}
}
“Java如何实现简单无界面五子棋”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。