本实例是通过二维数数组来实现这个简单的游戏实例,它是通过Scanner在控制台输入下棋的位置,本实例包括初始化棋盘、打印棋盘、判断棋子是否重复和是否越界、判断输赢、提示下棋信息五个部分。
public class WZQ {
static String white = "☆";
static String black = "★";
static String[][] qp = new String[15][15];//{{null,null}}
static String[] num = {"⒈","⒉","⒊","⒋","⒌","⒍","⒎","⒏","⒐","⒑","⒒","⒓","⒔","⒕","⒖"};
static String line = "十";
static boolean flag=true;//表示黑棋下
public static void main(String[] args) {
//1.初始化棋盘(方法)
WZQ.init();
//2.打印棋盘(打印棋盘方法)
WZQ.print();
//规定黑子先手
System.out.println("黑棋为先手");
System.out.println("黑棋落子");
Scanner scanner= new Scanner(System.in);
//下棋
games: while(true){
int x= scanner.nextInt();
int y=scanner.nextInt();
/*
判断棋子是否重复,及是否越界
*/
if( WZQ.checkPoint(x-1,y-1)){
System.out.println("棋子重复,请重新落字");
continue;
}
if(flag){
qp[x-1][y-1]=black;
}else{
qp[x-1][y-1]=white;
}
System.out.println(flag);
//调用print方法 打印棋盘
WZQ.print();
if(WZQ.isWin(x-1,y-1)){
if(flag){
System.out.println("黑子胜");
}else{
System.out.println("白字胜");
}
break games;
}
//开始游戏
WZQ.startGame();
}
//4.判断每次下子坐标是否越界,棋子是否重复
//5.判断输赢 每次下棋后判断输赢
}
/*
提示该哪方下棋
*/
public static void startGame(){
while(true){
if(flag){
System.out.println("白子落子");
flag = false;
break;
}else{
System.out.println("黑子落子");
flag=true;
break;
}
}
}
/*
初始化棋盘
*/
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 boolean checkPoint(int m,int n){
if(m>= qp.length-1||n>=qp[m].length-1||qp[m][n]!="十"){
return true;
}
return false;
}
/*
判断输赢
*/
public static boolean isWin(int x,int y){
int continueCount=1;
int jiance=1;
//横向判断
for ( int i=y-1 ; i>=0&&jiance<5; i--){
jiance++;
if (qp[x][i]!= null&&qp[x][i]==qp[x][y]){
continueCount++;
}else
break;
}
for ( int i=y+1 ; i<=qp.length&&jiance<5; i++) {
jiance++;
if (qp[x][i] != null && qp[x][i] == qp[x][y]) {
continueCount++;
} else
break;
}
//判断横向是否已经有结果,反之重置为1
if(continueCount>=5)
return true;
else{
continueCount=1;
jiance=1;
}
//纵向判断
for ( int i=x-1 ; i>0&&jiance<5; i--){
jiance++;
if (qp[i][y]!= null&&qp[i][y]==qp[x][y]){
continueCount++;
}else
break;
}
for ( int i=x+1 ; i< qp.length&&jiance<5; i++) {
jiance++;
if (qp[i][y] != null && qp[i][y] == qp[x][y]) {
continueCount++;
} else
break;
}
//判断纵向是否已经有结果,反之重置为1
if(continueCount>=5) {
return true;
}else{
continueCount=1;
jiance++;
}
//斜向判断
// 斜\方向
//西北
for ( int i=x-1,j=y-1 ; i>0&&j>0&&jiance<5; i--,j--){
jiance++;
if (qp[i][j]!= null&&qp[i][j]==qp[x][y]){
continueCount++;
}else
break;
}
//东南
for ( int i=x+1,j=y+1 ; i< qp.length&&y<qp[i].length&&jiance<5; i++) {
jiance++;
if (qp[i][j] != null && qp[i][j] == qp[x][y]) {
continueCount++;
} else
break;
}
// 斜 / 方向
//西南
for ( int i=x+1,j=y-1 ; i<qp[i].length&&j>0&&jiance<5; i++,j--){
jiance++;
if (qp[i][j]!= null&&qp[i][j]==qp[x][y]){
continueCount++;
}else
break;
}
//东北
for ( int i=x-1,j=y+1 ; i>0&&j<qp[i].length&&jiance<5; i--,i++){
jiance++;
if (qp[i][j]!= null&&qp[i][j]==qp[x][y]){
continueCount++;
}else
break;
}
//判断斜向是否已经有结果,反之返回false
if(continueCount>=5) {
return true;
}else
return false;
}
}