我想知道如何检查 4 Rs 或 Ys 是否在一行、一列和对角线上,以宣布我的 Connect 4 游戏的获胜者(使用代码中显示的字符串),我花了 5 个小时试图弄清楚但我'我对编码仍然太陌生,无法理解该做什么。请务必保留下面存在的所有代码(请不要使用私人课程或任何初学者在交给我的老师后不知道的东西)。感谢任何评论并给我回复的人。
package finalproject;
import java.util.*;
import java.util.Arrays;
public class ConnectFour
{
public static void main(String[] args)
{
System.out.println("Welcome to Connect 4, \n\nObjective:\nBe the first player to get four of your colored checkers in a row- in any direction.\nRemember, you have to drop your circle to the very bottom row, you can't leave circles floating in random areas.");
String [] tokenColor = {"R","Y"};
//methods arrays
int playerselect = menu();
System.out.println("Im in main");
int currentPlayer = players(1);
//arrays for each column of the board
String [] column1 = {"O","O","O","O","O","O"};
String [] column2 = {"O","O","O","O","O","O"};
String [] column3 = {"O","O","O","O","O","O"};
String [] column4 = {"O","O","O","O","O","O"};
String [] column5 = {"O","O","O","O","O","O"};
String [] column6 = {"O","O","O","O","O","O"};
String [] column7 = {"O","O","O","O","O","O"};
for (int i = 1; i<=42; i++)
{
//using a for loop to to change the board after every player move
printboard(column1, column2, column3, column4, column5, column6, column7);
System.out.println("before placement");
tokenPlacement(tokenColor[currentPlayer], column1, column2, column3, column4, column5, column6, column7);
System.out.println("after placement");
printboard(column1, column2, column3, column4, column5, column6, column7);
currentPlayer = players(currentPlayer);
}
}
public static void printboard(String[] column1, String[] column2, String[] column3, String[] column4,
String[] column5, String[] column6, String[] column7)
{
System.out.println(1234567);
//prints out board using loop
for (int i = 5; i>=0; i--)
{
System.out.print(column1[i]);
System.out.print(column2[i]);
System.out.print(column3[i]);
System.out.print(column4[i]);
System.out.print(column5[i]);
System.out.print(column6[i]);
System.out.print(column7[i]);
System.out.println();
}
}
public static int menu()
{
//enters menu method
//scanner looks for next input from user
Scanner myInput = new Scanner (System.in);
int playerX;
//prints out menu, this menu give the user the option to pick either P1, P2 or to exit the game
System.out.println("\nUse the corresponding numbers below to choose a eaither a player, or to exit the program.\n (Note that when you choose player 1 you will always go first)");
System.out.println("\n 1. Choose player 1 (R)" );
System.out.println(" 2. Choose player 2 (Y)");
System.out.println(" 3. Exit");
System.out.println("\n Select an option");
playerX = myInput.nextInt();
//if statements for player selection
if (playerX == 1)
{
System.out.println("You are Red");
System.out.println("\nPlayer 2 is Yellow");
}
else if (playerX == 2)
{
System.out.println("You are Yellow");
System.out.println("\nPlayer 2 is Red");
}
//exits program
else if (playerX == 3)
{
System.out.println("You didn't choose a player! Shutting Down connect_four.exe");
System.exit(0);
}
//exits program
else
{
System.out.println("Sorry, I dont't understand? Please re-run the program and choose again.");
System.exit(0);
}
return playerX;
}//end menu
public static int players(int currentPlayer) {
System.out.println("Im in the players method");
//displays and decides who's turn it is
if (currentPlayer == 0)
{
currentPlayer = (1);
System.out.println("\nIt's Yellow's Turn");
}
else if (currentPlayer == 1)
{
currentPlayer = (0);
System.out.println("\nIt's Red's Turn");
}
System.out.println(currentPlayer);
//loops on who goes
return currentPlayer;
}//end players
public static void tokenPlacement(String tokenColor, String [] column1, String [] column2, String [] column3, String [] column4, String [] column5, String [] column6, String [] column7)
{
//scans for user's next input
Scanner myInput = new Scanner (System.in);
System.out.println("\nOut of the 7 columns above, choose a number between 1-7 to signify which column you choose");
int columnChoice = myInput.nextInt();
boolean placed = false;
//if statement that runs a for loop if columnChoice/users's input == 1
if (columnChoice == 1)
{
for (int i=0; i<=5; i++)
{
//looks to see if the selected slot has a "O" and if so prints out either a "R" or a "Y" dependent of what player you are
if (column1[i].equals("O"))
{
column1[i] = tokenColor;
placed = true;
break;
}
}
//if a whole column if filled with "R"s or "Y"s tell the user to choose a different column
if (!placed)
{
System.out.println("Sorry that column is full. Choose another column");
tokenPlacement(tokenColor, column1, column2, column3, column4, column5, column6, column7);
}
}
else if (columnChoice == 2)
{
for (int i=0; i<=5; i++)
{
//looks to see if the selected slot has a "O" and if so prints out either a "R" or a "Y" dependent of what player you are
if (column2[i].equals("O"))
{
column2[i] = tokenColor;
placed = true;
break;
}
}
//if a whole column if filled with "R"s or "Y"s tell the user to choose a different column
if (!placed)
{
System.out.println("Sorry that column is full. Choose another column");
tokenPlacement(tokenColor, column1, column2, column3, column4, column5, column6, column7);
}
}
else if (columnChoice == 3)
{
for (int i=0; i<=5; i++)
{
//looks to see if the selected slot has a "O" and if so prints out either a "R" or a "Y" dependent of what player you are
if (column3[i].equals("O"))
{
column3[i] = tokenColor;
placed = true;
break;
}
}
//if a whole column if filled with "R"s or "Y"s tell the user to choose a different column
if (!placed)
{
System.out.println("Sorry that column is full. Choose another column");
tokenPlacement(tokenColor, column1, column2, column3, column4, column5, column6, column7);
}
}
else if (columnChoice == 4)
{
for (int i=0; i<=5; i++)
{
//looks to see if the selected slot has a "O" and if so prints out either a "R" or a "Y" dependent of what player you are
if (column4[i].equals("O"))
{
column4[i] = tokenColor;
placed = true;
break;
}
}
//if a whole column if filled with "R"s or "Y"s tell the user to choose a different column
if (!placed)
{
System.out.println("Sorry that column is full. Choose another column");
tokenPlacement(tokenColor, column1, column2, column3, column4, column5, column6, column7);
}
}
else if (columnChoice == 5)
{
for (int i=0; i<=5; i++)
{
if (column5[i].equals("O"))
{
column5[i] = tokenColor;
placed = true;
break;
}
}
//if a whole column if filled with "R"s or "Y"s tell the user to choose a different column
if (!placed)
{
System.out.println("Sorry that column is full. Choose another column");
tokenPlacement(tokenColor, column1, column2, column3, column4, column5, column6, column7);
}
}
else if (columnChoice == 6)
{
for (int i=0; i<=5; i++)
{
//looks to see if the selected slot has a "O" and if so prints out either a "R" or a "Y" dependent of what player you are
if (column6[i].equals("O"))
{
column6[i] = tokenColor;
placed = true;
break;
}
}
//if a whole column if filled with "R"s or "Y"s tell the user to choose a different column
if (!placed)
{
System.out.println("Sorry that column is full. Choose another column");
tokenPlacement(tokenColor, column1, column2, column3, column4, column5, column6, column7);
}
}
else if (columnChoice == 7)
{
for (int i=0; i<=5; i++)
{
//looks to see if the selected slot has a "O" and if so prints out either a "R" or a "Y" dependent of what player you are
if (column7[i].equals("O"))
{
column7[i] = tokenColor;
placed = true;
break;
}
}
//if a whole column if filled with "R"s or "Y"s tell the user to choose a different column
if (!placed)
{
System.out.println("Sorry that column is full. Choose another column");
tokenPlacement(tokenColor, column1, column2, column3, column4, column5, column6, column7);
}
}
}//end tokenPlacement
}//end main