能夠畫出9*9格,有兩個棋手A,B,交替輸入列數0-9,由于重力原理,輸入這個列下幾行沒有棋子,則自動下落;A顯示棋子是X,B顯示棋子O,要求當出現四連勝利!A和B交替進行:
import java.util.Scanner;
public class practice7Arrays{
//懸挂的四子棋
public static void main(String[]args){
Scanner input = new Scanner(System.in);
String[][] qi = new String[6][7];
String yuanShiYuanSu = "| ";
String yellow = "| Y ";
String red = "| R ";
int count = 0;//誰下子及下了多少子的判斷計數器
//初始化棋子數組
for(int i = 0;i < qi.length;i++)
for(int j = 0;j < qi[0].length;j++)
qi[i][j] = yuanShiYuanSu;
//畫出原始棋盤
System.out.println("-----------------------------");
for(int i = qi.length - 1;i >= 0;i--){
System.out.print(qi[i][j]);
System.out.println("|");
System.out.println("-----------------------------");
}
for(int i = 0;i < qi[0].length;i++)
System.out.print("* "+(i+1)+" ");
System.out.println("*");
int row = 0,column = 0;//行列下标初始化
//下棋循環開始
while (true) {
row = 0;
column = 0;
if (count % 2 == 0) {//黃方下子
System.out.print("\n\nY player please drop a yellow disk at column(1~7):");
while (true) {//黃方下子循環開始
column = input.nextInt() - 1;
if (column >= 0 && column <= 6) {// 輸入合法進行下子
for (row = 0; row < qi.length; row++) {
if (qi[row][column] == yuanShiYuanSu) {
qi[row][column] = yellow;
break;
}
}
if (row == qi.length)//該列棋子滿,重新輸入
System.out.print("The column of you enter is full,"
+ "please reEnter! : ");
else//棋子沒滿,下子結束
break;
}
else// 輸入不合法,重新下子
System.out.print("You enter the wrong column,"
+ "please reEnter! : ");
}//黃方下子循環結束
}//if(count % 2 == 0)
else{//紅方下子
System.out.print("\n\nR player please drop a yellow disk at column(1~7):");
while (true) {//紅方下子循環開始
qi[row][column] = red;
}//紅方下子循環結束
}//if(count % 2 != 0)
//畫出棋盤
for(int i = qi.length - 1;i >= 0;i--){
for(int j = 0;j < qi[0].length;j++)
System.out.print(qi[i][j]);
System.out.println("|");
System.out.println("-----------------------------");
}
for(int i = 0;i < qi[0].length;i++)
System.out.print("* "+(i+1)+" ");
System.out.println("*");
//輸赢标志位
boolean flagForYWin = false;
boolean flagForRWin = false;
//隻需對目前下子的周圍情況進行判斷來決定棋局結果
if (count % 2 == 0) {//yellow player is win?
//行檢測開始
if (column <= 3) {
for (int jj = 0; jj <= column; jj++)
if (qi[row][jj] == yellow
&& qi[row][jj + 1] == yellow
&& qi[row][jj + 2] == yellow
&& qi[row][jj + 3] == yellow) {
flagForYWin = true;
}
else {
for (int jj = column - 3; jj <= 3; jj++)
if (flagForYWin) {
System.out.println("The yellow player win the game!");
break;
//列檢測開始
if (row >= 3) {
if (qi[row][column] == yellow
&& qi[row - 1][column] == yellow
&& qi[row - 2][column] == yellow
&& qi[row - 3][column] == yellow)
flagForYWin = true;
//正反對角檢測
if(row >= 3){
if(column < 3){
if (qi[row][column] == yellow
&& qi[row - 1][column + 1] == yellow
&& qi[row - 2][column + 2] == yellow
&& qi[row - 3][column + 3] == yellow)
else if(column > 3){
&& qi[row - 1][column - 1] == yellow
&& qi[row - 2][column - 2] == yellow
&& qi[row - 3][column - 3] == yellow)
else{
if ((qi[row][column] == yellow
|| (qi[row][column] == yellow
&& qi[row - 3][column - 3] == yellow))
}//yellow player is win?
else{//red player is win?
if (qi[row][jj] == red
&& qi[row][jj + 1] == red
&& qi[row][jj + 2] == red
&& qi[row][jj + 3] == red) {
flagForRWin = true;
if (flagForRWin) {
System.out.println("The red player win the game!");
if (qi[row][column] == red
&& qi[row - 1][column] == red
&& qi[row - 2][column] == red
&& qi[row - 3][column] == red)
flagForRWin = true;
if (qi[row][column] == red
&& qi[row - 1][column + 1] == red
&& qi[row - 2][column + 2] == red
&& qi[row - 3][column + 3] == red)
&& qi[row - 1][column - 1] == red
&& qi[row - 2][column - 2] == red
&& qi[row - 3][column - 3] == red)
if ((qi[row][column] == red
|| (qi[row][column] == red
&& qi[row - 3][column - 3] == red))
count++;//棋子數加1,并用于誰下棋子的判斷
//棋盤下滿棋子,是平局
if(count == 6*7){
System.out.println("棋盤棋子已經下滿,是平局!");
break;
}//下棋循環結束
}//方法塊
}//類塊