package grammars.day4;
import java.util.Scanner;
public class myWzq {
static String white = "☆";
static String black = "★";
static String[][] qp = new String[15][15];
static String[] num = new String[]{"⒈", "⒉", "⒊", "⒋", "⒌", "⒍", "⒎", "⒏", "⒐", "⒑", "⒒", "⒓", "⒔", "⒕", "⒖"};
static String line = "十";
static boolean flag = true;
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
startGame();
}
public static void startGame(){
init();//初始化
print();//打印棋盘
play();//开始游戏
}
//初始化
public static void init(){
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.length-1)
{
qp[i][j]=num[j];
}
}
}
}
//打印棋盘
public static void print() {
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();
}
}
//玩游戏
public static void play() {
System.out.println("欢迎开到五子棋游戏");
while(true) {
while(!flag) {
System.out.println("白棋下棋");
flag = true;
}
System.out.println("黑棋下子");
System.out.println("输入行:");
int r = scanner.nextInt() - 1;
System.out.println("输入列:");
int c = scanner.nextInt() - 1;
boolean res = check(r, c);
if (res) {
qp[r][c] = black;
print();
boolean iswin = isWin(r, c, black);
if (iswin) {
System.out.println("黑棋胜利");
return;
}
flag = false;
} else {
System.out.println("坐标越界或者重复");
}
}
}
//检查有没有棋子
public static boolean check(int r, int c) {
if (r >= 0 && c >= 0 && r < qp.length && c < qp[0].length) {
return qp[r][c] == line;
} else {
return false;
}
}
//判断输赢
public static boolean isWin(int r, int c, String qz) {
int spsum = 1;
// 水平向左
for(int leftc = c - 1; leftc >= 0 && qp[r][leftc] == qz; --leftc) {
++spsum;
}
if (spsum == 5) {
return true;
} else {
// 水平向右
for(int rightc = c + 1; rightc < qp[0].length && qp[r][rightc] == qz; ++rightc) {
++spsum;
}
return spsum == 5;
}
}
}
Java实现五子棋
最新推荐文章于 2024-07-29 18:00:00 发布
这篇文章详细介绍了使用Java编写的五子棋游戏程序,包括棋盘初始化、打印、玩家操作检查以及胜负判断的函数。
摘要由CSDN通过智能技术生成