【发布时间】:2020-01-28 19:23:28
【问题描述】:
我正在读取一个文本文件并将其内容保存到一个 ArrayList 中,我知道我想用 Arraylist 的内容填充一个 8x8 数组。我的目标是读取包含棋子、颜色和位置的文件,然后检查有效的移动。但是,我是 java 新手,不确定如何检查 arrayList 的这些属性,即它是白色还是黑色。
感谢任何帮助。
public static void MoveCheck(String answer,ArrayList<String> board){
String move;
if(answer.equalsIgnoreCase("pawn")){
System.out.println("What position do you want to move to?");
Scanner scanner = new Scanner(System.in);
move = scanner.nextLine();
if(board.contains("white")){
if(move.equalsIgnoreCase("B3")||(move.equalsIgnoreCase("B4"))){
System.out.println("Valid move");
}
else{
System.out.println("Invalid move/Not on the board");
}
}
}
public static void main(String args[]){
String answer;
ArrayList<String> result = new ArrayList<>();
int [][] board = new int[7][7];
try (BufferedReader br = new BufferedReader(new FileReader("chess.txt"))){
while(br.ready()){
result.add(br.readLine());
}
} catch (Exception e){
System.out.print("Error occurred while reading.");
}
for (String line : result){
System.out.println(line);
}
System.out.println("Select a piece to move");
Scanner scanner = new Scanner(System.in);
answer = scanner.nextLine();
MoveCheck(answer,result);
}
【问题讨论】: