Как проверить, соединены ли 4 совпадающих токена в серии строк[] в непрерывной строке, столбце или диагоналиJAVA

Программисты JAVA общаются здесь
Ответить Пред. темаСлед. тема
Anonymous
 Как проверить, соединены ли 4 совпадающих токена в серии строк[] в непрерывной строке, столбце или диагонали

Сообщение Anonymous »

Как я могу проверить, что 4 жетона цвета R или Y находятся в строке, столбце или диагонали, чтобы объявить победителя (используя строки, показанные ниже в коде) в моей игре Connect Four.
Пожалуйста, сохраните весь приведенный ниже код (пожалуйста, не используйте частный класс или что-то еще, о чем новичок не знает, поскольку он передан моему учителю).
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=0; i--)
{
System.out.print(column1);
System.out.print(column2);
System.out.print(column3);
System.out.print(column4);
System.out.print(column5);
System.out.print(column6);
System.out.print(column7);
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

Подробнее здесь: https://stackoverflow.com/questions/647 ... ed-in-a-co
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «JAVA»