In general, the essence of the question is that there is a method that takes coordinates from the console, if the coordinates do not validate throws an exception and go out of work, but it is necessary that while the method works until it works
the code itself looks like this
case 3: System.out.println("Ход белого игрока"); System.out.println("Введите координату в формате e2-a4"); Scanner sc2=new Scanner(System.in); String sss2=sc2.nextLine(); while( menu.executeMenu(White,sss2)){ menu.executeMenu(White,sc2.nextLine()); } System.out.println("Ход черного игрока"); System.out.println("Введите координату в формате e2-a4"); String sss3=sc2.nextLine(); menu.executeMenu(Black,sss3); break; This is a game of chess, in general one player makes a move, then the second and everything would be fine, but if the coordinates are not correct, then instead of requesting them, the program moves to the second player’s move
Can you tell me the concept of how to implement it?
1. The step takes the coordinates 2. We transfer them to the method - if the method has worked we pass them below if no, we return to point 2 until the method has done the method itself has returned a boolean value
public boolean executeMenu(Gamer gamer, String s) { try { String from = s.split("-", 2)[0].toLowerCase(); String to = s.split("-", 2)[1].toLowerCase(); Postition a = new Postition(Interpritator.reverseInter(from).get(0), Interpritator.reverseInter(from).get(1)); Postition b = new Postition(Interpritator.reverseInter(to).get(0), Interpritator.reverseInter(to).get(1)); if (Game.validEat(a, b) && gamer.checkGamerColor(Field.getFigure(a.getX(), a.getY()))) { gamer.eatFigure(a, b); print(); return false; } else if (Game.validMove(a, b) && gamer.checkGamerColor(Field.getFigure(a.getX(), a.getY()))) { gamer.moveFigure(a, b); print(); return false; } else System.err.println("Ход не доступен"); // else System.err.println("Эта фигура не вашего цвета"); } catch (Exception e) { //e.printStackTrace(); System.err.println("Вводи правильные координаты "); } return false; }