Exercises/chess/board/Field.java

122 lines
3.2 KiB
Java
Raw Permalink Normal View History

2025-02-26 10:05:52 +01:00
package de.vivi.chess.board;
import de.vivi.chess.pieces.Piece;
/**
* TODO: implement fields and constructor
* <p>
* This class contains information to identify a single field
* on the board by its column and row.
*/
public class Field {
private int column;
private int row;
private Board board;
public Field(int column, int row) {
this.column = column;
this.row = row;
//board = new Board();
}
/**
* TODO: implement
* <p>
* Returns a new field that represents the field at the
* column and row indices.
*/
public static Field from(int column, int row) {
return new Field(column, row);
}
/**
* TODO: implement
* <p>
* Returns a new field that represents the field represented
* by the provided string (like "A5", "E7", ...). If the string
* is not valid, because it is garbage (like "hello", "world", ...)
* or the indices are out of bounds (like "K3", "B9", ...")
* it should throw an IllegalArgument exception.
*/
public static Field fromString(String value) throws IllegalArgumentException {
2025-03-01 16:04:33 +01:00
try {
char[] array = value.toCharArray();
int column_ = 0;
int row_ = 0;
2025-02-26 10:05:52 +01:00
2025-03-01 16:04:33 +01:00
switch (array[0]) {
case 'A':
column_ = 0;
break;
case 'B':
column_ = 1;
break;
case 'C':
column_ = 2;
break;
case 'D':
column_ = 3;
break;
case 'E':
column_ = 4;
break;
case 'F':
column_ = 5;
break;
case 'G':
column_ = 6;
break;
case 'H':
column_ = 7;
break;
default:
System.out.println("Wrong input");
}
2025-02-26 10:05:52 +01:00
2025-03-01 16:04:33 +01:00
switch (array[1]) {
case '1':
row_ = 0;
break;
case '2':
row_ = 1;
break;
case '3':
row_ = 2;
break;
case '4':
row_ = 3;
break;
case '5':
row_ = 4;
break;
case '6':
row_ = 5;
break;
case '7':
row_ = 6;
break;
case '8':
row_ = 7;
break;
default:
System.out.println("Wrong input");
}
//System.out.println(from(column_, row_));
return from(column_, row_);
} catch (RuntimeException e) {
throw new RuntimeException(e);
2025-02-26 10:05:52 +01:00
}
}
public int getColumn() {
// TODO: implement
return column;
}
public int getRow() {
// TODO: implement
return row;
}
}