Как я могу проверить, что 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
Как проверить, соединены ли 4 совпадающих токена в серии строк[] в непрерывной строке, столбце или диагонали ⇐ JAVA
Программисты JAVA общаются здесь
1716321211
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[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
Подробнее здесь: [url]https://stackoverflow.com/questions/64779886/how-to-check-if-4-matching-tokens-in-a-series-of-strings-are-connected-in-a-co[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия