Advertisement
Guest User

utility.dart

a guest
Mar 25th, 2022
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 1.16 KB | None | 0 0
  1. import 'package:minigames/src/games/tictactoe/tac_ai.dart';
  2.  
  3. class TacUtility {
  4.   //region utility
  5.   static bool isBoardFull(List<int> board) {
  6.     for (var val in board) {
  7.       if (val == TacAI.EMPTY_SPACE) return false;
  8.     }
  9.  
  10.     return true;
  11.   }
  12.  
  13.   static bool isMoveLegal(List<int> board, int move) {
  14.     if (move < 0 || move >= board.length || board[move] != TacAI.EMPTY_SPACE)
  15.       return false;
  16.  
  17.     return true;
  18.   }
  19.  
  20.   /// Returns the current state of the board [winning player, draw or no winners yet]
  21.   static int evaluateBoard(List<int> board) {
  22.     for (var list in TacAI.WIN_CONDITIONS_LIST) {
  23.       if (board[list[0]] !=
  24.               TacAI.EMPTY_SPACE && // if a player has played here AND
  25.           board[list[0]] ==
  26.               board[list[1]] && // if all three positions are of the same player
  27.           board[list[0]] == board[list[2]]) {
  28.         return board[list[0]];
  29.       }
  30.     }
  31.  
  32.     if (isBoardFull(board)) {
  33.       return TacAI.DRAW;
  34.     }
  35.  
  36.     return TacAI.NO_WINNERS_YET;
  37.   }
  38.  
  39.   /// Returns the opposite player from the current one.
  40.   static int flipPlayer(int currentPlayer) {
  41.     return -1 * currentPlayer;
  42.   }
  43. }
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement