《Java基本功練習九》講到了五子棋,並且提出了改進的建議。相信大家都掌握了多維數組的相關操作,上一篇提到了檢測輸贏方法有兩種:1)每下一顆棋子就遍歷棋盤;2)每下一顆棋子,判斷其周圍的情況來確定輸贏。
本篇博文就以懸掛的四子棋作爲例子,來講述2)中的判定方法,由於只需判定當前所下棋子的情況,而不用遍歷棋盤,所以效率更高。
懸掛的四子棋,又叫連接四子,相信大家小時候都玩過把。它是一個兩個人玩的棋盤遊戲,在遊戲中,玩家輪流將有顏色的棋子放在一個六行七列的垂直懸掛的網格中,如下所示:
這個遊戲的目的是在反方出現一行、一列或者一對角線上有四個相同顏色的棋子之前,正方能先做到。程序提示兩個玩家交替的下紅字Red或黃子Yellow。當丟下一子時,程序在控制檯重新顯示這個棋盤,然後確定遊戲的狀態(贏、平局還是繼續)。運行效果如下圖所示(只上傳了前兩張和後兩張):
設計此題的時候需要考慮幾個問題:1)如何模擬下子時只能下到每列的最下端的空位處;
2)如果某一列滿了,繼續往該列下子要報錯並提示重新輸入;
3)怎麼設計判斷方法,從而只需判斷當前所下子的情況以確定勝負;
4)怎麼判斷平局
5)棋盤怎麼刷新
設計的時候可以自己先把每步的情況考慮清楚,如何用代碼去實現,在紙上打個草稿,然後再開始寫代碼,並且每實現一個小的步驟都運行看看結果是否正確,正確之後再繼續往下設計,這樣可以減輕調試的負擔,降低調試的難度,切記!
以下是實現的源代碼:
package MutiArrays;
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--){
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("*");
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) {//紅方下子循環開始
column = input.nextInt() - 1;
if (column >= 0 && column <= 6) {// 輸入合法進行下子
for (row = 0; row < qi.length; row++) {
if (qi[row][column] == yuanShiYuanSu) {
qi[row][column] = red;
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)
//畫出棋盤
System.out.println("-----------------------------");
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;
break;
}
}
else {
for (int jj = column - 3; jj <= 3; jj++)
if (qi[row][jj] == yellow
&& qi[row][jj + 1] == yellow
&& qi[row][jj + 2] == yellow
&& qi[row][jj + 3] == yellow) {
flagForYWin = true;
break;
}
}
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 (flagForYWin) {
System.out.println("The yellow player win the game!");
break;
}
//正反對角檢測
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)
flagForYWin = true;
}
else 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)
flagForYWin = true;
}
else{
if ((qi[row][column] == yellow
&& qi[row - 1][column + 1] == yellow
&& qi[row - 2][column + 2] == yellow
&& qi[row - 3][column + 3] == yellow)
|| (qi[row][column] == yellow
&& qi[row - 1][column - 1] == yellow
&& qi[row - 2][column - 2] == yellow
&& qi[row - 3][column - 3] == yellow))
flagForYWin = true;
}
}
if (flagForYWin) {
System.out.println("The yellow player win the game!");
break;
}
}//yellow player is win?
else{//red player is win?
//行檢測開始
if (column <= 3) {
for (int jj = 0; jj <= column; jj++)
if (qi[row][jj] == red
&& qi[row][jj + 1] == red
&& qi[row][jj + 2] == red
&& qi[row][jj + 3] == red) {
flagForRWin = true;
break;
}
}
else {
for (int jj = column - 3; jj <= 3; jj++)
if (qi[row][jj] == red
&& qi[row][jj + 1] == red
&& qi[row][jj + 2] == red
&& qi[row][jj + 3] == red) {
flagForRWin = true;
break;
}
}
if (flagForRWin) {
System.out.println("The red player win the game!");
break;
}
//列檢測開始
if (row >= 3) {
if (qi[row][column] == red
&& qi[row - 1][column] == red
&& qi[row - 2][column] == red
&& qi[row - 3][column] == red)
flagForRWin = true;
}
if (flagForRWin) {
System.out.println("The red player win the game!");
break;
}
//正反對角檢測
if(row >= 3){
if(column < 3){
if (qi[row][column] == red
&& qi[row - 1][column + 1] == red
&& qi[row - 2][column + 2] == red
&& qi[row - 3][column + 3] == red)
flagForRWin = true;
}
else if(column > 3){
if (qi[row][column] == red
&& qi[row - 1][column - 1] == red
&& qi[row - 2][column - 2] == red
&& qi[row - 3][column - 3] == red)
flagForRWin = true;
}
else{
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][column] == red
&& qi[row - 1][column - 1] == red
&& qi[row - 2][column - 2] == red
&& qi[row - 3][column - 3] == red))
flagForRWin = true;
}
}
if (flagForRWin) {
System.out.println("The red player win the game!");
break;
}
}
count++;//棋子數加1,並用於誰下棋子的判斷
//棋盤下滿棋子,是平局
if(count == 6*7){
System.out.println("棋盤棋子已經下滿,是平局!");
break;
}
}//下棋循環結束
}//方法塊
}//類塊