/// <summary> /// Check the state of the board - Is there a winner? /// </summary> /// <returns> /// LarvaWin if larva is in winning row /// BirdWin if the birds have eaten the larva /// Running if no winner /// </returns> public GameState CheckForWinner() { var birdsCanMove = false; // Check if any bird has captured the larva foreach (var bird in Birds) { // Also check that at least one bird can move birdsCanMove |= bird.CanMove(); if (bird.Pos == Larva.Pos) { return(GameState.BirdWin); } } if (!birdsCanMove) { return(GameState.LarvaWin); } // Check if the larva is in winning row if (Larva.Pos.Row == NUM_ROWS - 1) { return(GameState.LarvaWin); } if (!Larva.CanMove()) { return(GameState.BirdWin); } // No winner yet return(GameState.Running); }
public void DoAIMove(Larva Larva, Bird[] Birds, Board Board) { BoardConfig nextConfig = AILarvaMove(Larva, Birds, Board); try { _larva.Move(new Move(_larva.Pos, nextConfig.LarvaPos)); } catch (InvalidMoveException) { } }
/// <summary> /// Createa a new game, with a board and all peices associated with it /// </summary> public Game() { _board = new Board(); _larva = _board.Larva; _birds = _board.Birds; _player1 = new LarvaPlayer(_larva); _player2 = new BirdPlayer(_birds); _currentPlayer = _player1; }
/// <summary> /// Create an empty board /// </summary> public Board() { EmptyBoard(); _rowSeperator = GetRowSeperator(); _headerString = GetHeaderString(); Larva = new Larva(INITIAL_LARVA_POS, this); SetCell(Larva.Pos, Larva); Birds = new Bird[NUM_BIRDS]; for (int i = 0; i < NUM_BIRDS; i++) { Position pos = INITIAL_BIRD_POS[i]; Birds[i] = new Bird(pos, this); SetCell(pos, Birds[i]); } }
public void DoAIMove(Larva Larva, Bird[] Birds, Board Board) { int birdIndex = 4; var to = new Position(); BoardConfig nextConfig = AIBirdsMove(Larva, Birds, Board); for (int i = 0; i < _birds.Length; ++i) { if (!(nextConfig.BirdsPos[i].Equals(_birds[i].Pos))) { birdIndex = i; to = nextConfig.BirdsPos[i]; } } try { _birds[birdIndex].Move(new Move(_birds[birdIndex].Pos, to)); } catch (InvalidMoveException) { } }
public BoardConfig AIBirdsMove(Larva Larva, Bird[] Birds, Board Board) { // Get original positions Position origLarvaPosition = Larva.Pos; Position[] origBirdsPosition = new Position[Birds.Length]; for (int i = 0; i < Birds.Length; ++i) { origBirdsPosition[i] = Birds[i].Pos; } BoardConfig origBC = new BoardConfig(0, origLarvaPosition, origBirdsPosition); BCTree <BoardConfig> MiniMaxTree = new BCTree <BoardConfig>(origBC); // Get level 1 kids for the Birds Utilities.generateBirdsChildren(ref MiniMaxTree, 1, Board); List <BCTree <BoardConfig> > level1Nodes = new List <BCTree <BoardConfig> >(); Utilities.drill(MiniMaxTree, 0, 1, ref level1Nodes); //Console.WriteLine("Count of nodes at Level 1 given by drill method = " + level1Nodes.Count); // Get level 2 kids for the Larva for (int i = 0; i < level1Nodes.Count; ++i) { var temp = level1Nodes[i]; Utilities.generateLarvaChildren(ref temp, 2, Board); } List <BCTree <BoardConfig> > level2Nodes = new List <BCTree <BoardConfig> >(); Utilities.drill(MiniMaxTree, 0, 2, ref level2Nodes); //Console.WriteLine("Count of nodes at Level 2 given by drill method = " + level2Nodes.Count); // Get level 3 kids for the Birds for (int i = 0; i < level2Nodes.Count; ++i) { var temp = level2Nodes[i]; Utilities.generateBirdsChildren(ref temp, 3, Board); } List <BCTree <BoardConfig> > level3Nodes = new List <BCTree <BoardConfig> >(); Utilities.drill(MiniMaxTree, 0, 3, ref level3Nodes); //Console.WriteLine("Count of nodes at Level 3 given by drill method = " + level3Nodes.Count); //Console.WriteLine("Calculating level 3 heuristics..."); Utilities.calculateLevelHeuristics(ref level3Nodes); for (int i = 0; i < level3Nodes.Count; i++) { //Console.WriteLine(i + " " + level3Nodes[i].data.heuristic); } //Console.WriteLine("Calculating level 2 heuristics..."); Utilities.calculateLevelHeuristics(ref level2Nodes); for (int i = 0; i < level2Nodes.Count; i++) { //Console.WriteLine(i + " " + level2Nodes[i].data.heuristic); } //Console.WriteLine("Calculating level 1 heuristics..."); Utilities.calculateLevelHeuristics(ref level1Nodes); for (int i = 0; i < level1Nodes.Count; i++) { //Console.WriteLine(i + " " + level1Nodes[i].data.heuristic); } Console.WriteLine(); Console.Write("Current Positions - Larva = " + Utilities.GetScoreForPos(MiniMaxTree.data.LarvaPos)); for (int i = 0; i < MiniMaxTree.data.BirdsPos.Length; ++i) { Console.Write(", Bird " + (i + 1) + " = " + Utilities.GetScoreForPos(MiniMaxTree.data.BirdsPos[i])); } Console.WriteLine(); Console.WriteLine("Calculating best move for Birds..."); BoardConfig nextConfig = Utilities.getBestMove(level1Nodes, ref MiniMaxTree, false); int birdToMove = getBirdToMove(nextConfig, origBC); Position nextBirdPosition = nextConfig.BirdsPos[birdToMove]; Utilities.PreOrderPrintBirds(MiniMaxTree); Console.WriteLine("The best next move for the Birds is for Bird " + (birdToMove + 1) + " to go to position " + Utilities.GetScoreForPos(nextBirdPosition)); Console.WriteLine(); return(nextConfig); }
public LarvaPlayer(Larva larva) { _larva = larva; }
public BoardConfig AIBirdsMove(Larva Larva, Bird[] Birds, Board Board) { // Get original positions Position origLarvaPosition = Larva.Pos; Position[] origBirdsPosition = new Position[Birds.Length]; for (int i = 0; i < Birds.Length; ++i) { origBirdsPosition[i] = Birds[i].Pos; } BoardConfig origBC = new BoardConfig(0, origLarvaPosition, origBirdsPosition); BCTree<BoardConfig> MiniMaxTree = new BCTree<BoardConfig>(origBC); // Get level 1 kids for the Birds Utilities.generateBirdsChildren(ref MiniMaxTree, 1, Board); List<BCTree<BoardConfig>> level1Nodes = new List<BCTree<BoardConfig>>(); Utilities.drill(MiniMaxTree, 0, 1, ref level1Nodes); //Console.WriteLine("Count of nodes at Level 1 given by drill method = " + level1Nodes.Count); // Get level 2 kids for the Larva for (int i = 0; i < level1Nodes.Count; ++i) { var temp = level1Nodes[i]; Utilities.generateLarvaChildren(ref temp, 2, Board); } List<BCTree<BoardConfig>> level2Nodes = new List<BCTree<BoardConfig>>(); Utilities.drill(MiniMaxTree, 0, 2, ref level2Nodes); //Console.WriteLine("Count of nodes at Level 2 given by drill method = " + level2Nodes.Count); // Get level 3 kids for the Birds for (int i = 0; i < level2Nodes.Count; ++i) { var temp = level2Nodes[i]; Utilities.generateBirdsChildren(ref temp, 3, Board); } List<BCTree<BoardConfig>> level3Nodes = new List<BCTree<BoardConfig>>(); Utilities.drill(MiniMaxTree, 0, 3, ref level3Nodes); //Console.WriteLine("Count of nodes at Level 3 given by drill method = " + level3Nodes.Count); //Console.WriteLine("Calculating level 3 heuristics..."); Utilities.calculateLevelHeuristics(ref level3Nodes); for (int i = 0; i < level3Nodes.Count; i++) { //Console.WriteLine(i + " " + level3Nodes[i].data.heuristic); } //Console.WriteLine("Calculating level 2 heuristics..."); Utilities.calculateLevelHeuristics(ref level2Nodes); for (int i = 0; i < level2Nodes.Count; i++) { //Console.WriteLine(i + " " + level2Nodes[i].data.heuristic); } //Console.WriteLine("Calculating level 1 heuristics..."); Utilities.calculateLevelHeuristics(ref level1Nodes); for (int i = 0; i < level1Nodes.Count; i++) { //Console.WriteLine(i + " " + level1Nodes[i].data.heuristic); } Console.WriteLine(); Console.Write("Current Positions - Larva = " + Utilities.GetScoreForPos(MiniMaxTree.data.LarvaPos)); for (int i = 0; i < MiniMaxTree.data.BirdsPos.Length; ++i) { Console.Write(", Bird " + (i + 1) + " = " + Utilities.GetScoreForPos(MiniMaxTree.data.BirdsPos[i])); } Console.WriteLine(); Console.WriteLine("Calculating best move for Birds..."); BoardConfig nextConfig = Utilities.getBestMove(level1Nodes, ref MiniMaxTree, false); int birdToMove = getBirdToMove(nextConfig, origBC); Position nextBirdPosition = nextConfig.BirdsPos[birdToMove]; Utilities.PreOrderPrintBirds(MiniMaxTree); Console.WriteLine("The best next move for the Birds is for Bird " + (birdToMove + 1) + " to go to position " + Utilities.GetScoreForPos(nextBirdPosition)); Console.WriteLine(); return nextConfig; }