Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:minigames/src/games/tictactoe/tac_ai.dart';
- class TacUtility {
- //region utility
- static bool isBoardFull(List<int> board) {
- for (var val in board) {
- if (val == TacAI.EMPTY_SPACE) return false;
- }
- return true;
- }
- static bool isMoveLegal(List<int> board, int move) {
- if (move < 0 || move >= board.length || board[move] != TacAI.EMPTY_SPACE)
- return false;
- return true;
- }
- /// Returns the current state of the board [winning player, draw or no winners yet]
- static int evaluateBoard(List<int> board) {
- for (var list in TacAI.WIN_CONDITIONS_LIST) {
- if (board[list[0]] !=
- TacAI.EMPTY_SPACE && // if a player has played here AND
- board[list[0]] ==
- board[list[1]] && // if all three positions are of the same player
- board[list[0]] == board[list[2]]) {
- return board[list[0]];
- }
- }
- if (isBoardFull(board)) {
- return TacAI.DRAW;
- }
- return TacAI.NO_WINNERS_YET;
- }
- /// Returns the opposite player from the current one.
- static int flipPlayer(int currentPlayer) {
- return -1 * currentPlayer;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement