48 lines
1.1 KiB
Dart
48 lines
1.1 KiB
Dart
import 'dart:math';
|
|
|
|
class CellPosition {
|
|
final int column;
|
|
final int row;
|
|
const CellPosition(this.column, this.row);
|
|
|
|
double distance(CellPosition to) {
|
|
return sqrt(pow(to.column - column, 2) + pow(to.row - row, 2));
|
|
}
|
|
|
|
CellPosition decreaseBy(int diff) {
|
|
int newCol =
|
|
column < 0 ? column + diff * diff.sign : column - diff * diff.sign;
|
|
int newRow = row < 0 ? row + diff * diff.sign : row - diff * diff.sign;
|
|
|
|
return CellPosition(newCol, newRow);
|
|
}
|
|
|
|
@override
|
|
int get hashCode => "${column}:${row}".hashCode;
|
|
|
|
bool operator ==(Object pos) {
|
|
return pos is CellPosition && this.column == pos.column && this.row == pos.row;
|
|
}
|
|
|
|
CellPosition operator -(CellPosition pos) {
|
|
return CellPosition(column - pos.column, row - pos.row);
|
|
}
|
|
|
|
CellPosition operator +(CellPosition pos) {
|
|
return CellPosition(column + pos.column, row + pos.row);
|
|
}
|
|
|
|
@override
|
|
String toString() {
|
|
return "Position(${column}x${row})";
|
|
}
|
|
|
|
toJSON() {
|
|
return {
|
|
"type": "BoardCell",
|
|
"column": column,
|
|
"row": row,
|
|
};
|
|
}
|
|
}
|