import java.util.Scanner;
public class Test {
//当前棋子的X坐标
public static int current_x;
//当前棋子的Y坐标
public static int current_y;
//定义黑棋子的形状
public static final String black_shap="O";
//定义白棋子的形状
public static final String white_shap="X";
//定义初始棋盘中棋子形状
public static final String default_shap = "+";
//当前棋子是黑棋还是白棋
public static String B_W;
public static void main(String[] args){
Test test = new Test();
String[][] str = test.getInitQiPan();
test.showQiPan(str);
test.xiaQi(str);
}
//获取初始棋盘
public String[][] getInitQiPan() {
String[][] str= new String[17][17];
for(int i=0;i<str.length;i++){
for(int j=0;j<str.length;j++){
if(i==0 && j==0){
str[i][j]=" ";
}
else if(i==0 && j!=0){
str[i][j]=String.valueOf(j-1);
}
else if(i!=0 && j==0){
str[i][j]=String.valueOf(i-1);
}
else {
str[i][j]=default_shap;
}
}
}
return str;
}
//显示棋盘
public void showQiPan(String[][] qiPan) {
for (int i = 0; i < qiPan.length; i++) {
for (int j = 0; j < qiPan.length; j++) {
if (i == 0 && j == 0) {
System.out.printf(" ");
} else if (i == 0 && j != 0) {
System.out.printf(" %x ", j - 1);
} else if (i != 0 && j == 0) {
System.out.printf(" %x ", i - 1);
} else {
System.out.printf(" "+qiPan[i][j]+" ");
}
}
System.out.println();
}
}
//下棋
public void xiaQi(String[][] qiPan){
System.out.println("开始下棋,棋盘大小为:"+qiPan.length+" * " +qiPan.length);
Scanner sc = new Scanner(System.in);
boolean win = false;//判断是否赢了,true为赢
B_W = "balck"; //黑方棋子
while(!win){
B_W = "balck";
System.out.print("黑方下棋,请黑方输入棋子的X坐标:");
int black_x = sc.nextInt()+1;
current_x = black_x;//当前棋子的X坐标
System.out.print("黑方下棋,请黑方输入棋子的Y坐标:");
int black_y = sc.nextInt()+1;
current_y = black_y;//当前棋子的Y坐标
qiPan = getRightValue(qiPan, black_x,black_y,B_W);//判断输入是否合法
showQiPan(qiPan);//显示黑方下棋之后的棋盘信息
win = isWinner(qiPan);//判断是否赢了
if(win){
System.out.println("*****恭喜黑方获得本局胜利!*****");
break;
}
B_W="white";
System.out.print("白方下棋,请白方输入棋子的X坐标:");
int white_x = sc.nextInt()+1;
current_x = white_x;//当前白棋的X坐标
System.out.print("白方下棋,请白方输入棋子的Y坐标:");
int white_y = sc.nextInt()+1;
current_y = white_y;//当前白棋的Y坐标
qiPan = getRightValue(qiPan, white_x,white_y,B_W);//判断输入是否合法
showQiPan(qiPan);//显示白方下棋之后的棋盘信息
win = isWinner(qiPan);//判断是否赢了
if(win){
System.out.println("#####恭喜白方获得本局胜利!#####");
break;
}
}
}
//判断输入是否合法,返回合法输入后的棋盘信息
public String[][] getRightValue(String[][] qiPan,int x, int y, String B_W){
//B_W balck 黑子 *, white 白子 #
String side="黑方";
if("white".equals(B_W)){
side="白方";
}
boolean right= false;//输入是否合法,true为合法
Scanner sc = new Scanner(System.in);
int curr_x = x;//当前棋子的x坐标
int curr_y = y;//当前棋子的y坐标
do{
if(curr_x>qiPan.length-1 || curr_x <0){
System.out.println("棋子的X坐标输入范围是0到"+(qiPan.length-2)+" ,请重输!");
curr_x = sc.nextInt()+1;
}
if(curr_y>qiPan.length-1 || curr_y <0){
System.out.println("棋子的Y坐标输入范围是0到"+(qiPan.length-2)+" ,请重输!");
curr_y = sc.nextInt()+1;
}
if(default_shap.equals(qiPan[curr_x][curr_y])){
right = true;
} else {
System.out.println(side+"输入错误,这个位子已有棋子哦!!!");
System.out.print(side+"下棋,请重新输入棋子的X坐标:");
curr_x = sc.nextInt()+1;
System.out.print(side+"下棋,请重新输入棋子的Y坐标:");
curr_y = sc.nextInt()+1;
}
}while(!right);
if("balck".equals(B_W)){
qiPan[curr_x][curr_y]=black_shap;
} else {
qiPan[curr_x][curr_y]=white_shap;
}
current_x = curr_x;
current_y = curr_y;
return qiPan;
}
// 判断是否赢了,返回true即赢了
public boolean isWinner(String[][] qiPan){
int continueTimes = 1;
//统计x轴上的直线上,连续相同棋子的个数
continueTimes = getTotalContinues(qiPan,"bubian","jiajian");
if(continueTimes == 5){
return true;
}
//统计Y轴上的直线上,连续相同棋子的个数
continueTimes = getTotalContinues(qiPan,"jiajian","bubian");
if(continueTimes == 5){
return true;
}
//统计左上到右下的斜线上,连续相同棋子的个数
continueTimes = getTotalContinues(qiPan,"tongjiatongjian","tongjiatongjian");
if(continueTimes == 5){
return true;
}
//统计左下到右上的斜线上,连续相同棋子的个数
continueTimes = getTotalContinues(qiPan,"yijianyijia","yijianyijia");
if(continueTimes == 5){
return true;
}
return false;
}
//统计相同棋子的个数
public int getTotalContinues(String[][] qiPan,String xCaoZuo, String yCaoZuo ) {
int curr_x = current_x;//当前棋子的X坐标
int curr_y = current_y;//当前棋子的Y坐标
int next_left_x = curr_x;//当前坐标的左边,下一个棋子的x坐标
int next_left_y = curr_y;//当前坐标的左边,下一个棋子的y坐标
int next_right_x = curr_x;//当前坐标的右边,下一个棋子的x坐标
int next_right_y = curr_y;//当前坐标的右边,下一个棋子的Y坐标
int continueTimes = 1;//连续相同棋子的个数
for (int i = 1; i < 5; i++) {
if (((curr_x - i >= 0 && curr_x + i <qiPan.length) || (curr_y-i>=0 && curr_y+i<qiPan.length)) && continueTimes < 5) {
if("jiajian".equals(xCaoZuo)){
next_left_x = curr_x - i;
}else if("bubian".equals(xCaoZuo)){
next_left_x = curr_x;
}else if("yijianyijia".equals(xCaoZuo)){
next_left_x = curr_x - i;
}else if("tongjiatongjian".equals(xCaoZuo)){
next_left_x = curr_x + i;
}
if("bubian".equals(yCaoZuo)){
next_left_y = curr_y;
} else if("jiajian".equals(yCaoZuo)){
next_left_y = curr_y+i;
} else if("yijianyijia".equals(yCaoZuo)){
next_left_y = curr_y+i;
} else if("tongjiatongjian".equals(yCaoZuo)){
next_left_y = curr_y +i;
}
if (nextInScope(qiPan,next_left_x) && nextInScope(qiPan,next_left_y) && (qiPan[curr_x][curr_y].equals(qiPan[next_left_x][next_left_y]))) {
continueTimes++;
if(continueTimes==5){
break;
}else{
continue;
}
} else {
break;
}
}
}
if (continueTimes >= 5) {
return continueTimes;
}
for (int i = 1; i < 5; i++) {
if (((curr_x - i >= 0 && curr_x + i <qiPan.length) || (curr_y-i>=0 && curr_y-i<qiPan.length)) && continueTimes < 5) {
if ("jiajian".equals(xCaoZuo)) {
next_right_x = curr_x + i;
} else if("bubian".equals(xCaoZuo)){
next_right_x = curr_x;
}else if("yijianyijia".equals(xCaoZuo)){
next_right_x = curr_x + i;
}else if("tongjiatongjian".equals(xCaoZuo)){
next_right_x = curr_x - i;
}
if("bubian".equals(yCaoZuo)){
next_right_y = curr_y;
} else if("jiajian".equals(yCaoZuo)){
next_right_y = curr_y-i;
} else if("yijianyijia".equals(yCaoZuo)){
next_right_y = curr_y-i;
}else if("tongjiatongjian".equals(yCaoZuo)){
next_right_y = curr_y-i;
}
if (nextInScope(qiPan,next_right_x) && nextInScope(qiPan,next_right_y)&& qiPan[curr_x][curr_y].equals(qiPan[next_right_x][next_right_y])) {
continueTimes++;
if(continueTimes==5){
break;
}else{
continue;
}
} else {
break;
}
}
}
return continueTimes;
}
public boolean nextInScope(String[][] qiPan,int next){
if(next >=0 && next <qiPan.length){
return true;
}
return false;
}
}
java 棋盘 java棋盘代码
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。

提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
java生成棋盘
第一步:新建java项目,具体的命名,看下面的文件结构。第二步:代码
java 载入图片 获取图片 -
马踏棋盘java代码 马跳棋盘源代码
r为矩阵的行,c为矩阵的列将结果输出到当前目录下的results.txt。
马踏棋盘java代码 #include #define 计算程序