大概步骤
1,创建棋盘
2,下棋
3,判断输赢
下面看一下代码
import java.util.Scanner;
public class QiPan {
static String white = "☆";
static String black = "★";
static String[][] qp = new String[15][15];
static String[] num = {"⒈","⒉","⒊","⒋","⒌","⒍","⒎","⒏","⒐","⒑","⒒","⒓","⒔","⒕","⒖"};
static String line = "十";
public static void main(String[] args) {
QiPan.initQp();
QiPan.print();
QiPan.startXiaQi();
}
public static void initQp(){
for (int i = 0; i <=num.length-1 ; i++) {
for (int j = 0; j <=num.length-1 ; j++) {
qp[i][j] = "十";
if(j==num.length-1) qp[i][j] = num[i];
if(i==num.length-1) qp[i][j] = num[j];
}
}
}
public static void print(){
for (int i = 0; i <= num.length-1; i++) {
for (int j = 0; j <= num.length-1; j++) {
System.out.print(qp[i][j]);
}
System.out.println();
}
}
public static void startXiaQi(){
Scanner sr = new Scanner(System.in);
int row,list;
boolean flag = true;
while(true){
if(flag){
System.out.println("请黑方落子");
row = sr.nextInt();
list = sr.nextInt();
if(QiPan.check(row,list)){
qp[row-1][list-1] = black;
flag = false;
QiPan.print();
}
if(win(row-1,list-1,black)) {
System.out.println("黑方赢了");
break;
}
}
else{
System.out.println("请白方落子");
row = sr.nextInt();
list = sr.nextInt();
if(QiPan.check(row,list)){
qp[row-1][list-1] = white;
flag = true;
QiPan.print();
}
if(win(row-1,list-1,white)) {
System.out.println("白方赢了");
break;
}
}
}
}
public static boolean check(int row,int list){
if(qp[row-1][list-1]!=line&&row<=14&&list<=14&&row>=1&&list>=1) {
System.out.println("棋子重复请重新下");
return false;
}
else if(row>14||list>14||row<1||list<1){
System.out.println("棋子已越界,请重新下");
return false;
}
else {
return true;
}
}
public static boolean win(int row,int list,String qz){
int count = 1;
//横着判断
for (int i = list+1; i < qp[0].length-1 ; i++) {//向右边判断
if(!qp[row][i].equals(qz)){break;}
else count++;
}
if(count>=5){
return true;
}
for (int j = list-1; j >=0 ; j--) {//向左边判断
if(!qp[row][j].equals(qz)){count = 1;break;}
else count++;
}
if(count>=5){
return true;
}
//竖着判断
for (int k = row-1; k >=0 ; k--) {//向上边判断
if(qp[k][list].equals(qz)){count++;}
else {break;};
}
if(count>=5){
return true;
}
for (int l = row+1; l < qp.length-1; l++) {//向下边判断
if(qp[l][list].equals(qz)){count++;}
else {count = 1;break;};
}
if(count>=5){
return true;
}
//左上右下判断
for(int i = row-1,j = list-1; i >=0&&j>=0 ; i--,j--) {//左上的判断
if(qp[i][j].equals(qz)){count++;}
else {break;}
}
if(count>=5){
return true;
}
for (int m = row+1,n = list+1; m <qp.length-1&&n<qp[0].length-1 ; m++,n++) {//右下的判断
if(qp[m][n].equals(qz)){count++;}
else {count = 1;break;}
}
if(count>=5){
return true;
}
//右上左下判断
for (int i = row-1,j = list+1; i >=0&&j<qp[0].length-1; i--,j++) {//右上的判断
if(qp[i][j].equals(qz)){count++;}
else break;
}
if(count>=5){
return true;
}
for (int i = row+1,j = list-1; i <qp.length-1&&j>=0 ; i++,j--) {//左下的判断
if(qp[i][j].equals(qz)){count++;}
else {count = 1;break;}
}
if(count>=5){
return true;
}
else return false;
}
}