static void Main(string[] args) { Player p1 = new Player("Red", SpaceType.Player1, PlayerColor.Red); Player p2 = new Player("Blue", SpaceType.Player2, PlayerColor.Blue); Game plop = new Game(p1, p2); setUpBoard(p1, p2, plop);//calls Quinn's majestic randomized setup method that I copied over from his Eval class plop.start(); while (true) { int depth = 3;//change as needed Move p1Move = alphaBetaSearch(plop, p1, p2, depth); plop.movePiece(p1Move.start, p1Move.end); //update the machine learning record data on this line if (plop.checkWin(p1)) { //update and complete this instance of the record data with the fact that p1 won this game on this line break; } Move p2Move = alphaBetaSearch(plop, p2, p1, depth); plop.movePiece(p2Move.start, p2Move.end); //update the machine learning record data on this line if (plop.checkWin(p2)) { //update and complete this instance of the record data with the fact that p2 won this game on this line break; } } }
//works the same as actionsForMax private static List <Move> actionsForMin(Game state, Player max, Player min, double alpha, double beta, int depthCurrent, int depthFinal) { List <Move> moves = new List <Move>(); //looks through all the positions on the board for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { Position searchPos = new Position(); searchPos.row = i; searchPos.col = j; //if this position holds a piece that is min's... try { if (state.initialGrid.grid[i, j]._piece.piecePlayer == min) { //TODO: == is not defined for the class Player int returncode; List <Position> posMoves = state.getMoves(searchPos, out returncode); //let's see if it has some moves if (returncode == 1) { //if it's min's piece and it can move... for (int k = 0; k < posMoves.Count; k++) { int z = 0; Move move = new Move(searchPos, posMoves[k], z); //we don't know the value of these moves yet but we need them, so null value moves.Add(move); //add all those moves to the total list } } } } catch (System.NullReferenceException) { } } } List <Move> actions = new List <Move>(); //now we're going to minmax all those new potential states that we'll be in for (int i = 0; i < moves.Count; i++) { Game newStateNode = state; newStateNode.movePiece(moves[i].start, moves[i].end); double alphaNew = alpha; double betaNew = beta; depthCurrent++; Move move = new Move(moves[i].start, moves[i].end, maxValue(newStateNode, max, min, alphaNew, betaNew, depthCurrent, depthFinal)); actions.Add(move); //revert all the changes newStateNode.movePiece(moves[i].end, moves[i].start); depthCurrent--; } return(actions); }
//this method returns a list of all the moves the max player can take given a particular game state //the value of those moves involves the actions that the min player game take when given the new situation //caused by max's actions, creating the recursive tree. It works by first finding all the valid pieces max //can move, then finding all the places max can move those pieces to, and making a list of all the those //begining-end moves. After attaining that information, that move is "simulated" in the newStateNode, and //that new state is passed into the minValue method, furthering the recursion to find the actual value of //that potential game state. After the value of that state is determined, the move is undone so that the //original game object is left the same as it was before the search private List <Move> actionsForMax(Game state, Player max, Player min, int alpha, int beta, int depthCurrent, int DepthFinal) { List <> moves = new List <Move>(); //looks through all the positions on the board for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { Position searchPos = new Position(); searchPos.row = i; searchPos.col = j; //if this position holds a piece that is max's... if (state.initialGrid.GridSpace[i, j]._Piece._Player == max)//TODO: == is not defined for Player class { int returncode; List <Position> posMoves = state.getMoves(searchPos, returncode); //let's see if it has some moves if (returncode == 1) //if it's max's piece and it can move... { for (int k = 0; k < posMoves.Count; k++) { Move move = new Move(searchPos, posMoves[k], null); //we don't know the value of these moves yet but we need them, so null value moves.Add(move); //add all those moves to the total list } } } } } //now we're going to minmax all those new potential states that we'll be in for (int i = 0; i < moves.Count; i++) { List <Move> actions = new List <Move>(); Game newStateNode = state; newStateNode.movePiece(moves[i].start, moves[i].end); int alphaNew = alpha; int betaNew = beta; depthCurrent++; Move move = new Move(moves[i].start, moves[i].end, minValue(newStateNode, max, min, alphaNew, betaNew, depthCurrent, depthFinal)); actions.Add(move); //revert all the changes newStateNode.movePiece(moves[i].end, moves[i].start) depthCurrent--; } return(actions); }
static void Main(string[] args) { setUpBoard(); // start the game plop.start(); plop.initialGrid.displayGrid(); Console.WriteLine(distanceMetric(p2, p1)); Position pos1 = new Position(3, 0); for (int i = 0; i < 3; i++) { Position nextPos = new Position(); nextPos.row = pos1.row + 1; nextPos.col = pos1.col; switch (plop.movePiece(pos1, nextPos)) { case 1: Console.WriteLine("Piece MOVED !"); break; case 10: Console.WriteLine("WIN !!!"); break; case 20: Console.WriteLine("TIE !"); break; case 30: Console.WriteLine("LOST !!!"); break; case 50: Console.WriteLine("You found the flag !"); break; default: Console.WriteLine("Move not allowed !"); break; } plop.initialGrid.displayGrid(); pos1 = nextPos; } Console.WriteLine("EvalPlayer1: {0}", eval(p1, p2)); Console.WriteLine("EvalPlayer2: {0}", eval(p2, p1)); Console.WriteLine(plop.initialGrid.mainGrid[3, 0]._type); Console.WriteLine(plop.initialGrid.mainGrid[4, 0]._type); Console.WriteLine(distanceMetric(p2, p1)); Console.WriteLine(averageDistance(p2, p1)); Console.WriteLine("Player 1 lost:"); foreach (var p in plop.player1Lost) { p.displayPiece(); } Console.WriteLine("Player 2 lost:"); foreach (var p in plop.player2Lost) { p.displayPiece(); } //Console.ReadLine(); }
static void Main(string[] args) { Player p1 = new Player("David", SpaceType.Player1, PlayerColor.Red); Player p2 = new Player("Paul", SpaceType.Player2, PlayerColor.Blue); Game plop = new Game(p1, p2); Position pos1 = new Position(); pos1.row = 3; pos1.col = 4; Position pos2 = new Position(); pos2.row = 6; pos2.col = 4; plop.setPieceOnGrid(plop.player2Pieces[2], pos1); plop.setPieceOnGrid(plop.player1Pieces[0], pos2); plop.start(); plop.initialGrid.displayGrid(); for (int i = 1; i < 8; i++) { Position nextPos = new Position(); nextPos.row = pos1.row + 1; nextPos.col = pos1.col; switch (plop.movePiece(pos1, nextPos)) { case 1: Console.WriteLine("Piece MOVED !"); break; case 10: Console.WriteLine("WIN !!!"); break; case 20: Console.WriteLine("TIE !"); break; case 30: Console.WriteLine("LOST !!!"); break; case 50: Console.WriteLine("You found the flag !"); break; default: Console.WriteLine("Move not allowed !"); break; } pos1.row++; plop.initialGrid.displayGrid(); } //Console.ReadLine(); }
static void Main(string[] args) { Player p1 = new Player("David", SpaceType.Player1, PlayerColor.Red); Player p2 = new Player("Paul", SpaceType.Player2, PlayerColor.Blue); Game plop = new Game(p1,p2); Position pos1 = new Position(); pos1.row = 3; pos1.col = 4; Position pos2 = new Position(); pos2.row = 6; pos2.col = 4; plop.setPieceOnGrid(plop.player2Pieces[2], pos1); plop.setPieceOnGrid(plop.player1Pieces[0], pos2); plop.start(); plop.initialGrid.displayGrid(); for (int i = 1; i <8;i++ ) { Position nextPos = new Position(); nextPos.row = pos1.row+1; nextPos.col = pos1.col; switch (plop.movePiece(pos1, nextPos)) { case 1: Console.WriteLine("Piece MOVED !"); break; case 10: Console.WriteLine("WIN !!!"); break; case 20: Console.WriteLine("TIE !"); break; case 30: Console.WriteLine("LOST !!!"); break; case 50: Console.WriteLine("You found the flag !"); break; default: Console.WriteLine("Move not allowed !"); break; } pos1.row++; plop.initialGrid.displayGrid(); } Console.ReadLine(); }
static void Main(string[] args) { Player p1 = new Player("P1", SpaceType.Player1, PlayerColor.Red); Player p2 = new Player("P2", SpaceType.Player2, PlayerColor.Blue); Game plop = new Game(p1, p2); Position pos1 = new Position(); pos1.row = 0; pos1.col = 0; Position pos2 = new Position(); pos2.row = 6; pos2.col = 4; int pieceIndex = 0; for (int i = 0; i < 10; i++) { for (int j = 0; j < 4; j++) { Position posB = new Position(); // player 2 posB.row = j; posB.col = i; plop.setPieceOnGrid(plop.player2Pieces[pieceIndex], posB); // Player 1 Position posA = new Position(); posA.row = 9 - j; posA.col = 9 - i; plop.setPieceOnGrid(plop.player1Pieces[pieceIndex], posA); pieceIndex++; // go to next piece } } plop.start(); // start the game // check the player's piece lists foreach (var p in plop.player1Pieces) { p.displayPiece(); } Console.WriteLine("Player 2"); foreach (var p in plop.player2Pieces) { p.displayPiece(); } // display the initial grid plop.initialGrid.displayGrid(); // Test random movement int rc; // return code, needed for getMoves() Random rnd = new Random(); // random number generator int numMoves = 5; // how many moves do you want to test??? Position pos = new Position(); // initial position // this position will be where player 2s leftmost miner is, next to their flag // note that all movement is based on position, whatever piece is at that position will be moved. pos.row = 3; pos.col = 1; Position nextPos = new Position(); int moveIndex; // this helps choose a random move from the list of possible moves at a given state for (int i = 0; i < numMoves; i++) { List <Position> moves = plop.getMoves(pos, out rc); Console.WriteLine("Possible moves at {0}", i); foreach (var p in moves) { Console.WriteLine("row: {0}", p.row); Console.WriteLine("col: {0}", p.col); Console.WriteLine(); } moveIndex = rnd.Next(moves.Count); // random number no bigger than size of list Position next = nextPos = moves[moveIndex]; // update position // movePiece() moves whatever piece is at pos to next. // it returns a numeric code that represents what happened at the move. switch (plop.movePiece(pos, next)) { case 1: Console.WriteLine("Piece MOVED !"); break; case 10: Console.WriteLine("WIN !!!"); break; case 20: Console.WriteLine("TIE !"); break; case 30: Console.WriteLine("LOST !!!"); break; case 50: Console.WriteLine("You found the flag !"); break; default: Console.WriteLine("Move not allowed !"); break; } plop.initialGrid.displayGrid(); // show the grid pos = next; // update the piece's current position } }