Example #1
0
        //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);
        }
Example #2
0
        //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);
        }
Example #3
0
        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 = 0;
            pos1.col = 0;

            Position pos2 = new Position();

            pos2.row = 6;
            pos2.col = 4;

            //plop.setPieceOnGrid(plop.player2Pieces[0], pos1);
            //plop.setPieceOnGrid(plop.player1Pieces[0], pos2);
            //plop.initPlayerPieces(p1, p2);

            // set up a complete board
            //int numPieces = plop.player1Pieces.Count();

            //plop.setPieceOnGrid(plop.player2Pieces[3], pos1);
            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();
            //plop.initialGrid.displayGrid();

            // 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();
            }

            plop.initialGrid.displayGrid();
            /* Test the getMoves() function */
            Position flag2Pos = new Position();

            flag2Pos.col = 0;
            flag2Pos.row = 3;
            int             rc; // return code
            List <Position> flagMoves = plop.getMoves(flag2Pos, out rc);

            Console.WriteLine(rc);
            foreach (var p in flagMoves)
            {
                Console.WriteLine("row: {0}", p.row);
                Console.WriteLine("col: {0}", p.col);
                Console.WriteLine();
            }

            Position marsh2Pos = new Position();

            marsh2Pos.col = 1;
            marsh2Pos.row = 3;
            List <Position> marshMoves = plop.getMoves(marsh2Pos, out rc);

            Console.WriteLine("Marshall");
            Console.WriteLine(rc);
            foreach (var p in marshMoves)
            {
                Console.WriteLine("row: {0}", p.row);
                Console.WriteLine("col: {0}", p.col);
                Console.WriteLine();
            }
            //Console.Write(plop.initialGrid.mainGrid[0,6]._piece.displayPiece());

            /* Test a WIN */

            /*
             * int column = 0;
             * Position start1 = new Position();
             * start1.col = column;
             * start1.row = 6;
             * //Position next1 = new Position();
             * //next1.col = column;
             * //next1.row = start1
             * for (int i = 0; i < 3; i++) {
             *  // move player 1
             *  Position next = new Position();
             *  next.row = 5-i;
             *  next.col = column;
             *  switch (plop.movePiece(start1, 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();
             *  start1.row = next.row; // update
             * }
             */

            // test movement

            /*
             * Position firstPos = new Position();
             * firstPos.row = 3;
             * firstPos.col = 1;
             * Position nextPos = new Position();
             * nextPos.row = 4;
             * nextPos.col = 1;
             * switch (plop.movePiece(firstPos, 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();
             *
             * // another move
             * firstPos.row = nextPos.row;
             * firstPos.col = nextPos.col;
             * nextPos.row = 5;
             * switch (plop.movePiece(firstPos, 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();
             *
             * 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();
        }
Example #4
0
        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
            }
        }