プログラミングクラスへの私の紹介では、2人のプレイヤーConnect Fourゲームを作成しています。ユーザー間で交互に移動し、移動ごとに勝者を確認し、勝者を表示して、別のゲームをプレイするかどうか。表示はコンソールを介して行われます。私の勝利チェックのアルゴリズムはまともだと証明されていますが、私の問題は、勝者が選ばれた後、プレイヤーの動きを保持するために作った2-Dストリング配列をリセットすることにあります。私が作った配列でそれをどうやって行うのかはかなりわかりません。ユーザーが勝利したときに2次元配列の文字列をクリアする方法。 (Connect Four)
board = createPattern();
あなたは同じ行列を再使用することができ、または:
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;
あまりにも意味があります。ありがとうございました!時にはあなたは何かを見つめすぎて、それを見逃してしまいます。 – Kyle