Add helper functions and fix the bug that replayed the move sent from the server.

This commit is contained in:
Marco 2023-01-30 23:40:46 +01:00
parent 8572aa73e6
commit cf12bc08c4
3 changed files with 62 additions and 6 deletions

View File

@ -1,12 +1,16 @@
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:mchess/utils/chess_utils.dart';
typedef ChessPositionType = Map<ChessCoordinate, ChessPiece>;
typedef ChessMoveHistory = List<ChessMove>;
class ChessPosition {
static final ChessPosition _instance = ChessPosition._internal();
late ChessPositionType position;
final ChessPositionType position;
ChessMoveHistory history = ChessMoveHistory.empty(growable: true);
static ChessPosition getInstance() {
return _instance;
@ -62,11 +66,17 @@ class ChessPosition {
}
ChessPositionType get currentPosition => position;
ChessMove get lastMove => history.last;
void recordMove(ChessCoordinate from, ChessCoordinate to) {
position[to] = position[from] ?? const ChessPiece.none();
position[from] = const ChessPiece.none();
history.add(ChessMove(from: from, to: to));
logPosition(position);
logHistory(history);
}
void logPosition(ChessPositionType p) {
@ -86,4 +96,10 @@ class ChessPosition {
log(logString);
}
void logHistory(ChessMoveHistory hist) {
hist.forEach((element) {
log('${element.from.toAlphabetical()} -> ${element.to.toAlphabetical()}');
});
}
}

View File

@ -1,7 +1,7 @@
import 'dart:developer';
import 'package:flutter/foundation.dart';
import 'package:mchess/chess_bloc/chess_bloc.dart';
import 'package:mchess/chess_bloc/chess_events.dart';
import 'package:mchess/chess_bloc/chess_position.dart';
import 'package:mchess/utils/chess_utils.dart';
import 'package:web_socket_channel/web_socket_channel.dart';
@ -64,11 +64,17 @@ class ServerConnection {
if (splitString[0] == ('mv')) {
var startSquare = ChessCoordinate.fromString(splitString[1]);
var endSquare = ChessCoordinate.fromString(splitString[2]);
if (ChessMove(from: startSquare, to: endSquare) ==
ChessPosition.getInstance().lastMove) {
//This is our own move that got resent by the server. Do not process.
} else {
log('lastMove: from: ${ChessPosition.getInstance().lastMove.from} to: ${ChessPosition.getInstance().lastMove.to}');
log('constructed move: from: $startSquare to: $endSquare)}');
log('Move received : ${startSquare.toAlphabetical()}:${endSquare.toAlphabetical()}');
log('Move received : ${splitString[1]}:${splitString[2]}');
ChessBloc.getInstance().add(
OpponentPieceMoved(startSquare: startSquare, endSquare: endSquare));
ChessBloc.getInstance().add(OpponentPieceMoved(
startSquare: startSquare, endSquare: endSquare));
}
}
});
}

View File

@ -91,6 +91,23 @@ class ChessCoordinate {
return '$colStr$rowStr';
}
String toAlphabetical() {
Map<int, String> columnMap = {
1: "a",
2: "b",
3: "c",
4: "d",
5: "e",
6: "f",
7: "g",
8: "h"
};
String rowStr = row.toString();
String colStr = columnMap[column]!;
return '$colStr$rowStr';
}
@override
operator ==(other) {
return other is ChessCoordinate &&
@ -136,6 +153,23 @@ class ChessPiece extends StatelessWidget {
}
}
class ChessMove {
ChessCoordinate from;
ChessCoordinate to;
ChessMove({required this.from, required this.to});
@override
operator ==(other) {
return other is ChessMove && other.from == from && other.to == to;
}
@override
int get hashCode {
return hash2(from, to);
}
}
class PieceMovedFrom {
ChessCoordinate fromSquare;
ChessPiece? movedPiece;