public void roundStart()
{
int player = Klasse.getPlayer();
List<Unit> unitList = Klasse.getUnitList(); //player und Unitlist in dem Beispiel static
Unit king = searchKing(unitList, player);
Point kingLocation = king.getLocation();
boolean kingsLocationAttakable = checkWheterFieldIsAttackable(player, unitList, kingLocation);
if(kingsLocationAttackable)
{
try
{
checkMatt(player, unitList, king);
System.out.println("Schach!"); //TODO
}
catch(RuntimeException e)
{
System.out.println("Schach Matt. 8)"); //TODO
}
}
else
System.out.println("Weder, noch");
}
private Unit searchKing(int player, List<Unit> unitList)
{
Unit king = null;
for(byte b = 0; b < unitList.size() && king != null; b++)
if(unitList.get(b).getPlayer == player)
if(unitList.get(b).getUnitId() == KING_ID)
king = unitList.get(b);
return(king);
}
private boolean checkWheterFieldIsAttackable(int player, List<Unit> unitList, Point fieldToCheck)
{
boolean result = false;
for(int b = 0; b < unitList.size() && !result; b++)
if(unitList.get(b).getPlayer() != player)
{
List<Point> attackableFields = unitList.get(b).getAttackableFields();
if(attackableFields.content(fieldToCheck)
result = true;
}
return(result);
}
private void checkMatt(int player, List<Unit> unitList, Unit king) throws RuntimeException
{
RuntimeException exceptionToThrow = new RuntimeException();
Point kingLocation = king.getLocation();
int x = kingLocation.x;
int y = kingLocation.y;
if(checkWheterFieldIsAttackable(player, unitList, new Point(x - 1, y - 1)))
throw exceptionToThrow;
else if(checkWheterFieldIsAttackable(player, unitList, new Point(x, y - 1)))
throw exceptionToThrow;
else if(checkWheterFieldIsAttackable(player, unitList, new Point(x + 1, y - 1)))
throw exceptionToThrow;
else if(checkWheterFieldIsAttackable(player, unitList, new Point(x - 1, y)))
throw exceptionToThrow;
else if(checkWheterFieldIsAttackable(player, unitList, new Point(x + 1, y)))
throw exceptionToThrow;
else if(checkWheterFieldIsAttackable(player, unitList, new Point(x - 1, y + 1)))
throw exceptionToThrow;
else if(checkWheterFieldIsAttackable(player, unitList, new Point(x, y + 1)))
throw exceptionToThrow;
else if(checkWheterFieldIsAttackable(player, unitList, new Point(x + 1, y + 1)))
throw exceptionToThrow;
}