对于我的编程课程介绍,我们正在创建一个2人游戏的Connect Four游戏,它在用户之间交替,在每次移动后检查获胜者,显示获胜者,然后询问玩家是否想要玩另一个游戏与否。显示是通过控制台。我的胜利检查的算法已被证明是不错的,但我的问题在于重置我选择赢家后我所做的2-D String数组来保持玩家的动作。我不太确定如何使用我创建的数组来做到这一点。
这就是我所拥有的:
import java.util.Scanner;
public class ConnectFourGame {
public static String [] [] createPattern() {
String [] [] board = new String [8][27];
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (j == 3 || j == 7 || j == 11 || j == 15 || j == 19 ||
J == 23) board[i][j] = "|";
else board [i][j] = " ";
if (i == 6) board[i][j] = "-";
if (i == 7 && j == 1) board [i][j] = "1";
if (i == 7 && j == 5) board [i][j] = "2";
if (i == 7 && j == 9) board [i][j] = "3";
if (i == 7 && j == 13) board [i][j] = "4";
if (i == 7 && j == 17) board [i][j] = "5";
if (i == 7 && j == 21) board [i][j] = "6";
if (i == 7 && j == 25) board [i][j] = "7";
}
}
return board;
}
public static void printPattern(String[][] board) {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
System.out.print(board[i][j]);
}
System.out.println();
}
}
public static void firstRowRed(String[][] board) {
System.out.println("Player 1: Your turn! (Select a column, 1-7)");
Scanner input = new Scanner (System.in);
int num;
num = input.nextInt();
int k = (2*((2*num)-1)-1);
for (int i = 7; i >= 0; i--) {
if (board[i][k] == " ") {
board[i][k] = "R";
break;
}
}
}
public static void firstRowYellow(String[][] board) {
System.out.println(" Player 2: Your turn! (select a column, 1-7)");
Scanner input = new Scanner (System.in);
int num;
num = input.nextInt();
int k = (2*((2*num)-1)-1);
for (int i = 5; i >= 0; i--) {
if (board[i][k] == " ") {
board[i][k] = "Y";
break;
}
}
}
public static String checkWinner (String [][] board) {
// Horizontal Line check
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 15; j += 4) {
if ((board[i][j+1] != " ")
&& (board[i][j + 5] != " ")
&& (board[i][j + 9] != " ")
&& (board[i][j + 13] != " ")
&& ((board[i][j + 1] == board [i][j + 5])
&& (board[i][j + 5] == board [i][j + 9])
&& (board[i][j + 9] == board [i][j + 13])))
return board[i][j+1];
}
}
// Vertical line win check
for (int i = 1; i < 27; i += 4) {
for (int j = 1; j < 4; j++) {
if ((board[j][i] != " ")
&& (board[j + 1][i] != " ")
&& (board[j + 2][i] != " ")
&& (board[j + 3][i] != " ")
&& (board[j][i] == board[j + 1][i])
&& (board[j + 1][i] == board[j + 2][i])
&& (board[j + 2][i] == board[j + 3][i]))
return board[j][i];
}
}
// Top Left to bottom right diagonal win check
for (int i = 0; i < 3; i++) {
for (int j = 1; j < 13 ; j +=4) {
if ((board[i][j] != " ")
&& (board[i + 1][j + 4] != " ")
&& (board[i + 2][j + 8] != " ")
&& (board[i + 3][j + 12] != " ")
&& ((board[i][j] == board[i + 1][j + 4])
&& (board[i + 1][j + 4] == board[i + 2][j + 8])
&& (board[i + 2][j + 8] == board[i + 3][j + 12])))
return board[i][j];
}
}
// Top right to bottom left win check
for (int i = 0; i < 3; i++) {
for (int j = 1; j < 27; j += 4) {
if ((board[i][j] != " ")
&& (board[i + 1][j - 4] != " ")
&& (board[i + 2][j - 8] != " ")
&& (board[i + 3][j - 12] != " ")
&& ((board[i][j] == board[i + 1][j - 4])
&& (board[i + 1][j - 4] == board [i + 2][j - 8])
&& (board[i + 2][j - 8] == board [i + 3][j - 12])))
return board[i][j];
}
}
return null;
}
public static void main (String[] args) {
String [][] board = createPattern();
boolean loop = true;
int count = 0;
printPattern(board);
int quit;
Scanner input = new Scanner(System.in);
while(loop) {
if ( count % 2 == 0) firstRowRed(board);
else firstRowYellow(board);
count++;
printPattern(board);
if(checkWinner(board) != null) {
if (checkWinner(board) == "R")
System.out.println("Player 1 wins!");
else if (checkWinner(board) == "Y")
System.out.println("Player 2 wins!");
if(checkWinner(board) == "R" || checkWinner(board) == "Y"){
System.out.println();
System.out.println("Enter -1 to end game, or enter 0 to
continue");
quit = input.nextInt();
if(quit == -1)
loop = false;
if(quit == 0)
continue;
答案 0 :(得分:0)
只是在游戏之间重新创建棋盘,而不仅仅是在程序启动时:
board = createPattern();
你可以重复使用相同的矩阵,或者每场比赛分配一个新矩阵。
答案 1 :(得分:0)
我不确定您是否询问如何将字符串设置为空字符串,但如果是这样,您的代码会像这样将字符串重置为空:
c#:创建空值
的字符串数组string[][] str = new string[][] { };
即如果要将其设置为空字符串值。你也可以按照下面的建议创建一个新的董事会,这可能是最好的做法。另一个选择是创建一个&#34;重置&#34;游戏功能或其他东西。我希望有所帮助。