编程实现控制台版并支持两人对战的五子棋游戏
程序员文章站
2022-06-28 16:23:39
实现import java.util.Scanner;public class Homework2 {Scanner sc = new Scanner(System.in);private char[][] chessboard = new char[17][17]; //构造二维数组存储棋盘public boolean flag = true; //用于判断是否胜利//...
实现
import java.util.Scanner;
public class Homework2 {
Scanner sc = new Scanner(System.in);
private char[][] chessboard = new char[17][17]; //构造二维数组存储棋盘
public boolean flag = true; //用于判断是否胜利
//用二位数组初始化棋盘
public void drawChessboard() {
for (int i = 0; i < chessboard.length; i++) {
for (int j = 0; j < chessboard[i].length; j++) {
if (i == 0 && j == 0) {
chessboard[i][j] = (char) 32; //棋盘左上角为空格
} else if (i == 0 && j <= 10) {
chessboard[i][j] = (char) (47 + j); //实现第一行的数字部分
} else if (i == 0 && j <= 16) {
chessboard[i][j] = (char) (j + 86); //实现第一行的字母部分
} else if (j == 0 && i <= 10) {
chessboard[i][j] = (char) (47 + i); //实现第一列的数字部分
} else if (j == 0 && i <= 16) {
chessboard[i][j] = (char) (i + 86); //实现第一列的字母部分
} else {
chessboard[i][j] = (char) 43; //实现其余位置的"+"
}
}
System.out.println();
}
}
//双重for循环显示当前的棋盘
public void show(){
System.out.println("现在棋盘是下面这样的:");
for (int i = 0; i < chessboard.length; i++) {
for (int j = 0; j < chessboard[i].length; j++) {
System.out.print(chessboard[i][j] + "\t");
}
System.out.println();
}
}
//下棋方法:黑白方轮流下棋,划定棋盘范围,并且判断下棋的位置是否已经有棋子存在
public void playChess(){
Scanner sc = new Scanner(System.in);
boolean swap = true; //用于控制黑白方棋手轮流下棋
int cow, column; //记录输入的坐标位置
while(flag){
//swap为ture时,黑方棋手下棋
if(swap) {
System.out.println("下面由黑方下棋!");
System.out.println("请输入黑棋下落的横坐标(有效位数只有一位):");
cow = sc.next().charAt(0); //此处的charAt不太明白是什么原理,但是发现它能实现在输入的字符串取第一位字符,并将字符型转换为整数型
System.out.println("请输入黑棋下落的纵坐标(有效位数只有一位):");
column = sc.next().charAt(0);
//下面分成四个不同的部分进行字符向数字的转换
//第一部分:横纵坐标都是数字的时候
if(cow <= 57 && cow >= 48 && column <= 57 && column >= 48){
if(chessboard[cow - 47][column - 47] == (char) 43) { //坐标的位置转换为对应的二位数组的位置
chessboard[cow - 47][column - 47] = (char) 42; //判断此处是否有棋子存在,如果此处是+号,表示此处没有棋子,否则提示该位置已经有棋子
this.judge(); //每次下完棋都要判断是否有一方赢得对弈
this.show(); //每次下完棋都要展示当前棋盘
}else{
System.out.println("该位置已经有棋子咯,请重新下棋!"); //提示重新选择下棋位置并且结束当前循环,开启下一轮循环,但是依然是该方选手下棋
continue;
}
//第二部分:横坐标为字母,纵坐标为数字的时候
}else if(cow <= 102 && cow >= 97 && column <= 57 && column >= 48) {
if (chessboard[cow - 86][column - 47] == (char) 43) {
chessboard[cow - 86][column - 47] = (char) 42;
this.judge();
this.show();
} else {
System.out.println("该位置已经有棋子咯,请重新下棋!");
continue;
}
//第三部分:横坐标为数字,纵坐标为字母的时候
}else if(cow <= 57 && cow >= 48 && column <= 102 && column >= 97){
if (chessboard[cow - 47][column - 86] == (char) 43) {
chessboard[cow - 47][column - 86] = (char) 42;
this.judge();
this.show();
} else {
System.out.println("该位置已经有棋子咯,请重新下棋!");
continue;
}
//第四部分:横纵坐标都为字母的时候
}else if(cow <= 102 && cow >= 97 && column <= 102 && column >= 97){
if (chessboard[cow - 86][column - 86] == (char) 43) {
chessboard[cow - 86][column - 86] = (char) 42;
this.judge();
this.show();
} else {
System.out.println("该位置已经有棋子咯,请重新下棋!");
continue;
}
//如果下棋位置超出棋盘范围,就会提示位置不合理并且由该方选手重新选择下棋位置
}else{
System.out.println("位置不合理哦!请重新下棋并把把棋子放在棋盘里面!");
continue;
}
//swap为false时,白方棋手下棋,与黑方选手下棋实现原理相同
}else {
System.out.println("下面由白方下棋!");
System.out.println("请输入白棋下落的横坐标(有效位数只有一位):");
cow = sc.next().charAt(0);
System.out.println("请输入白棋下落的纵坐标(有效位数只有一位):");
column = sc.next().charAt(0);
if(cow <= 57 && cow >= 48 && column <= 57 && column >= 48){
if(chessboard[cow - 47][column - 47] == (char) 43) {
chessboard[cow - 47][column - 47] = (char) 35;
this.judge();
this.show();
}else{
System.out.println("该位置已经有棋子咯,请重新下棋!");
continue;
}
}else if(cow <= 102 && cow >= 97 && column <= 57 && column >= 48) {
if (chessboard[cow - 86][column - 47] == (char) 43) {
chessboard[cow - 86][column - 47] = (char) 35;
this.judge();
this.show();
} else {
System.out.println("该位置已经有棋子咯,请重新下棋!");
continue;
}
}else if(cow <= 57 && cow >= 48 && column <= 102 && column >= 97){
if (chessboard[cow - 47][column - 86] == (char) 43) {
chessboard[cow - 47][column - 86] = (char) 35;
this.judge();
this.show();
} else {
System.out.println("该位置已经有棋子咯,请重新下棋!");
continue;
}
}else if(cow <= 102 && cow >= 97 && column <= 102 && column >= 97){
if (chessboard[cow - 86][column - 86] == (char) 43) {
chessboard[cow - 86][column - 86] = (char) 35;
this.judge();
this.show();
} else {
System.out.println("该位置已经有棋子咯,请重新下棋!");
continue;
}
}else{
System.out.println("位置不合理哦!请重新下棋并把把棋子放在棋盘里面!");
continue;
}
}
swap = !swap; //黑白双方轮流下棋
}
}
//判断是否取得胜利的方法:利用双重for循环实现每次下棋之后都在棋盘的所有位置判断一下,如果有一方取得胜利,就会结束轮流下棋的方法并且跳出该判断循环
public void judge(){
outer:for(int i = 0; i < chessboard.length; i ++){
for (int j = 0;j < chessboard[i].length; j ++){
//依然是分成四个部分判断输赢
//第一部分:可以横向并且往右判断有无连续五个相同棋子
if (j <= 12){
if(chessboard[i][j] == (char) 42 && chessboard[i][j + 1] == (char) 42 && chessboard[i][j + 2] == (char) 42 && chessboard[i][j + 3] == (char) 42 && chessboard[i][j + 4] == (char) 42){
System.out.println("恭喜黑方获胜!");
flag = false;
break outer;
}
if(chessboard[i][j] == (char) 35 && chessboard[i][j + 1] == (char) 35 && chessboard[i][j + 2] == (char) 35 && chessboard[i][j + 3] == (char) 35 && chessboard[i][j + 4] == (char) 35){
System.out.println("恭喜白方获胜!");
flag = false;
break outer;
}
}
//第二部分:可以纵向并且往下判断有无连续五个相同棋子
if(i <= 12){
if(chessboard[i][j] == (char) 42 && chessboard[i + 1][j] == (char) 42 && chessboard[i + 2][j] == (char) 42 && chessboard[i + 3][j] == (char) 42 && chessboard[i + 4][j] == (char) 42) {
System.out.println("恭喜黑方获胜!");
flag = false;
break outer;
}
if(chessboard[i][j] == (char) 35 && chessboard[i + 1][j] == (char) 35 && chessboard[i + 2][j] == (char) 35 && chessboard[i + 3][j] == (char) 35 && chessboard[i + 4][j] == (char) 35){
System.out.println("恭喜白方获胜!");
flag = false;
break outer;
}
}
//第三部分:可以往右下方判断有无连续五个相同棋子
if(i <= 12 && j <= 12){
if(chessboard[i][j] == (char) 42 && chessboard[i + 1][j + 1] == (char) 42 && chessboard[i + 2][j + 2] == (char) 42 && chessboard[i + 3][j + 3] == (char) 42 && chessboard[i + 4][j + 4] == (char) 42) {
System.out.println("恭喜黑方获胜!");
flag = false;
break outer;
}
if(chessboard[i][j] == (char) 35 && chessboard[i + 1][j + 1] == (char) 35 && chessboard[i + 2][j + 2] == (char) 35 && chessboard[i + 3][j + 3] == (char) 35 && chessboard[i + 4][j + 4] == (char) 35) {
System.out.println("恭喜白方获胜!");
flag = false;
break outer;
}
}
//第四部分:可以往右上方判断有无连续五个相同棋子
if(i >= 5 && j <= 12){
if(chessboard[i][j] == (char) 42 && chessboard[i - 1][j + 1] == (char) 42 && chessboard[i - 2][j + 2] == (char) 42 && chessboard[i - 3][j + 3] == (char) 42 && chessboard[i - 4][j + 4] == (char) 42) {
System.out.println("恭喜黑方获胜!");
flag = false;
break outer;
}
if(chessboard[i][j] == (char) 35 && chessboard[i - 1][j + 1] == (char) 35 && chessboard[i - 2][j + 2] == (char) 35 && chessboard[i - 3][j + 3] == (char) 35 && chessboard[i - 4][j + 4] == (char) 35) {
System.out.println("恭喜白方获胜!");
flag = false;
break outer;
}
}
}
}
}
}
测试
public class Homework2Test {
public static void main(String[] args) {
Homework2 h = new Homework2();
h.drawChessboard();
h.show();
h.playChess();
}
}
本文地址:https://blog.csdn.net/s19862388581/article/details/112016198
上一篇: IDEA配置springboot