import java.util.Scanner;
public class Main2 {
static String white = "☆";
static String black ="★";
static String[][] qp = new String[15][15];
static String[] num = {"⒈", "⒉", "⒊", "⒋", "⒌", "⒍", "⒎", "⒏", "⒐", "⒑", "⒒", "⒓", "⒔", "⒕", "⒖"};
static String line = "十";
static boolean flag;
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args){
// 1.1 要对棋盘这个二维数组进行初始化数据
// 1.2 打印输出
Main2.initQp();
// 1.1
Main2.printQp();
// 1.2
Main2.start();
// 1.3开始下棋
}
// 开始下棋
public static void start(){
while(true){
if(flag){
System.out.println("请黑棋下子");
int x=scanner.nextInt()-1;
int y=scanner.nextInt()-1;
boolean res=Main2.check(x,y);
if(res){
qp[y][x] = black;
// 落子
Main2.printQp();
// 打印棋盘
// 判断输赢
boolean win = iswin(y,x,black);
flag=false;
}
else{
System.out.println("坐标有误,请重新测试");
}
}
else{
System.out.println("请白旗下子");
flag=true;
}
}
}
// 初始化棋盘
// 判断输赢
private static boolean iswin(int y,int x,String qz){
int spsum = 1;
// 水平计数器
for(int lx=x-1;x>=0;x--){
// 向左找
if(qp[y][x].equals(black)){
spsum++;
}
if(spsum>=5){
return true;
}
}
for(int rx=x+1;x<qp.length-1;x++){
if(qp[y][x].equals(qz)){
spsum++;
}
if(spsum>=5){
return true;
}
}
return false;
}
// 基本判断坐标,判断重复
public static boolean check(int x,int y){
if(x<0||x>qp.length-1||y<0||y>qp.length-1){
return false;
}
if(qp[y][x].equals(line)){
return false;
}
return true;
}
public static void initQp(){
for(int i=0;i<qp.length;i++){
for(int j=0;j<qp[i].length;j++){
qp[i][j]=line;
// 把默认值替换成+
if(j==qp[i].length-1){
qp[i][j]=num[i];
}
if(i==qp[i].length-1){
qp[i][j]=num[j];
}
}
}
}
// 打印棋盘
public static void printQp(){
for(int i=0;i<qp.length;i++){
for(int j=0;j<qp[i].length;j++){
System.out.print(qp[i][j]);
}
System.out.println();
}
}
}
未命名文件夹 2.zip
需积分: 15 97 浏览量
2022-11-20
00:13:44
上传
评论
收藏 1KB ZIP 举报
王文婷778
- 粉丝: 44
- 资源: 1