package day2;
import java.util.Scanner;
public class wuziqi {
/*
1.提供一个启动五子棋游戏的方法
初始化棋盘方法
打印棋盘方法
2.开始下棋,黑白棋交替下棋
3.判断坐标是否合法,是否重复
4.判断输赢
*/
static String white = "☆";
static String black = "★";
static String[][] qp = new String[15][15];
static String[] num = {"⒈", "⒉", "⒊", "⒋", "⒌", "⒍", "⒎", "⒏", "⒐", "⒑", "⒒", "⒓", "⒔", "⒕", "⒖"};
static String line = "十";
static Scanner scanner = new Scanner(System.in);
//启动五子棋游戏
public static void startGame() {
init();
print();
}
//初始化棋盘
public static void init() {
for (int i = 0; i < qp.length; i++) {
for (int j = 0; j < qp.length; j++) {
qp[i][j] = line;
if (j == qp.length - 1) {
qp[i][j] = num[i];
}
if (i == qp.length - 1) {
qp[i][j] = num[j];
}
}
}
}
//下棋
public static void play() {
while (true) {
boolean flag = true;
System.out.println("欢迎");
if (flag) {
System.out.println("黑子下棋");
System.out.println("请输入行:");
int s = scanner.nextInt() - 1;
System.out.println("请输入列:");
int c = scanner.nextInt() - 1;
boolean res = check(s, c);
if (res == true) {
qp[s][c] = black;//黑子下棋
print();
boolean ifwin = ifwin(s, c, black);
if (ifwin) {
System.out.println("黑棋胜利");
break;
}
flag = false;
} else
System.out.println("越界或者重复");
} if(!flag)
System.out.println("白子下棋");
System.out.println("请输入行:");
int s1 = scanner.nextInt() - 1;
System.out.println("请输入列:");
int c1 = scanner.nextInt() - 1;
boolean res = check(s1, c1);
if (res == true) {
qp[s1][c1] = white;//
print();
boolean ifwin = ifwin(s1, c1, white);
if (ifwin) {
System.out.println("白棋胜利");
break;
}
flag = false;
} else
System.out.println("越界或者重复");
}
}
//判断是否越界和重复
public static boolean check(int r, int s) {
if (r < 0 || s < 0 || r > qp.length || s > qp[0].length)//判断是否越界
{
return false;
}
if (qp[r][s] != line)//判断是否重复
{
return false;
}
return true;
}
//判断输赢
public static boolean ifwin(int s, int c, String qi) {//水平判断输赢
int a = 1;//水平棋子位置
//向左判断输赢
for (int left = c - 1; left >= 0; left--) {
if (qp[s][left] == qi) {
a++;
} else
break;
}
if (a == 5) {
return true;
}
//水平向右判断输赢
for (int right = c + 1; right < qp[0].length - 1; right++) {
if (qp[s][right] == qi) {
a++;
} else
break;
}
if (a == 5) {
return true;
}
//水平向上判断输赢
int b=1;
for (int height = s-1; height >= 0; height--) {
if (qp[height][c] == qi) {
b++;
} else
break;
}
if (b == 5) {
return true;
}
// 向下判断输赢
for (int low = s + 1; low < qp.length; low++) {
if (qp[low][c] == qi) {
b++;
} else
break;
}
if (b == 5) {
return true;
}
// 向左上判断输赢
int d=1;
for (int left = c - 1, height = s - 1; left >= 0 & height >= 0; left--, height--) {
if (qp[height][left] == qi) {
d++;
} else
break;
if (d == 5) {
return true;
}
}
// 向右下判断输赢
for (int right = c + 1, height1 = s + 1; right < qp[0].length & height1 < qp.length; right++, height1++) {
if (qp[height1][right] == qi) {
d++;
} else
break;
}
if (d == 5) {
return true;
}
// 向右上判断输赢
int e=1;
for (int right = c + 1, height2 = s - 1; right < qp[0].length && height2 >= 0; right++, height2--) {
if (qp[height2][right] == qi) {
e++;
} else
break;
if (e == 5) {
return true;
}
}
// 向左下判断输赢
for (int left = c - 1, height = s + 1; left >= 0 && height < qp.length; left--, height++) {
if (qp[height][left]==qi) {
e++;
} else {
break;
}
}
if (e == 5) {
return true;
}
return false;
}
package day2;
public class startwzq {
public static void main(String[] args) {
wuziqi.startGame();
wuziqi.play();
}
}
//打印棋盘
public static void print () {
for (int i = 0; i < qp.length; i++) {
for (int j = 0; j < qp.length; j++) {
System.out.print(qp[i][j]);
}
System.out.println();
}
}
}
package day2;
public class startwzq {
public static void main(String[] args) {
wuziqi.startGame();
wuziqi.play();
}
}
运行结果如图