コード例 #1
0
ファイル: GameCore.cs プロジェクト: trbarrett/Four-in-a-Row
 public GameCore Copy()
 {
     var copyGame = new GameCore(_player1, _player2);
     copyGame._currentPlayer = this._currentPlayer;
     copyGame._board = Board.Copy();
     return copyGame;
 }
コード例 #2
0
ファイル: AIBaseTest.cs プロジェクト: trbarrett/Four-in-a-Row
        public void TestFindAndBlockOpponentCompulsion()
        {
            //setup the initial condition
            var player1 = "Player 1";
            var player2 = "Player 2";
            var game = new GameCore(player1, player2, player2);
            var board = game.Board;

            // x x x
            // t t t
            // t t t
            // t t t x
            // t t t x t . .
            /////////////////

            board[0][0].OwningPlayer = player2;
            board[0][1].OwningPlayer = player2;
            board[0][2].OwningPlayer = player2;
            board[0][3].OwningPlayer = player1;
            board[0][4].OwningPlayer = player1;
            board[0][5].OwningPlayer = player2;

            board[1][0].OwningPlayer = player1;
            board[1][1].OwningPlayer = player1;
            board[1][2].OwningPlayer = player1;
            board[1][3].OwningPlayer = player2;
            board[1][4].OwningPlayer = player1;

            board[2][0].OwningPlayer = player2;
            board[2][1].OwningPlayer = player1;
            board[2][2].OwningPlayer = player2;
            board[2][3].OwningPlayer = player1;
            board[2][4].OwningPlayer = player1;

            board[3][0].OwningPlayer = player1;
            board[3][1].OwningPlayer = player1;

            board[4][0].OwningPlayer = player2;
            board[4][1].OwningPlayer = player2;

            var mocks = new MockRepository();
            var gc = mocks.StrictMock<IGameController>();
            Expect.Call(gc.Game).Return(game).Repeat.Any();
            mocks.ReplayAll();

            var ai = new AIBase(AIDifficulty.VeryHard);
            ai.PlayTurn(gc, player2);

            Assert.AreEqual(player2, board[3][2].OwningPlayer);
            mocks.VerifyAll();
        }
コード例 #3
0
        public void TestFindTwoTurnCompultionWinForTile_DoesntThrowGame()
        {
            //setup the initial condition
            var player1 = "Player 1";
            var player2 = "Player 2";
            var game = new GameCore(player1, player2);
            var board = game.Board;

            //If player one, x tries to play on 4,0 to start a compulsion
            //it would backfire, giving player two the win via a positive diagonal
            //connection. So make sure that option isn't considered

            // . . . . . x t
            // . . . . . t x
            // . . x x . x t
            // . . x t . t x
            ////////////////////

            board[2][0].OwningPlayer = player1;
            board[2][1].OwningPlayer = player1;

            board[3][0].OwningPlayer = player2;
            board[3][1].OwningPlayer = player1;

            //column four is empty

            board[5][0].OwningPlayer = player2;
            board[5][1].OwningPlayer = player1;
            board[5][2].OwningPlayer = player2;
            board[5][3].OwningPlayer = player1;

            board[6][0].OwningPlayer = player1;
            board[6][1].OwningPlayer = player2;
            board[6][2].OwningPlayer = player1;
            board[6][3].OwningPlayer = player2;

            List<Tile> compulsionWin = AIHelper.FindTwoTurnCompulsionWin(game, player1);

            Assert.AreEqual(0, compulsionWin.Count);
        }
コード例 #4
0
        public void TestFindTwoTurnCompultionWinForTile4()
        {
            //setup the initial condition
            var player1 = "Player 1";
            var player2 = "Player 2";
            var game = new GameCore(player1, player2);
            var board = game.Board;

            // x x x
            // t t t
            // t t t
            // t t t x
            // t t t x . . .
            /////////////////

            board[0][0].OwningPlayer = player2;
            board[0][1].OwningPlayer = player2;
            board[0][2].OwningPlayer = player2;
            board[0][3].OwningPlayer = player1;
            board[0][4].OwningPlayer = player1;

            board[1][0].OwningPlayer = player1;
            board[1][1].OwningPlayer = player1;
            board[1][2].OwningPlayer = player1;
            board[1][3].OwningPlayer = player2;
            board[1][4].OwningPlayer = player1;

            board[2][0].OwningPlayer = player2;
            board[2][1].OwningPlayer = player1;
            board[2][2].OwningPlayer = player2;
            board[2][3].OwningPlayer = player1;
            board[2][4].OwningPlayer = player1;

            board[3][0].OwningPlayer = player1;
            board[3][1].OwningPlayer = player1;

            List<Tile> compulsionWin = AIHelper.FindTwoTurnCompulsionWin(game, player1);

            Assert.AreEqual(1, compulsionWin.Count);
            Assert.AreEqual(board[3][2], compulsionWin[0]);
        }
コード例 #5
0
        public void TestFindTwoTurnCompultionWinForTile1()
        {
            //setup the initial condition
            var player1 = "Player 1";
            var player2 = "Player 2";
            var game = new GameCore(player1, player2);
            var board = game.Board;

            // . . . . . x
            // . . . . . t
            // . . x x . x
            // . . x t . t .
            ////////////////////

            board[2][0].OwningPlayer = player1;
            board[2][1].OwningPlayer = player1;

            board[3][0].OwningPlayer = player2;
            board[3][1].OwningPlayer = player1;

            //column 4 is empty, with which we can create a compulsion.
            //If player one places a tile in column four, it will cause a potential
            //win if they place another one (4 horizontally) so player 2 will have to
            //block it. But that will let player 1 place a tile there to win
            //via a diagonal.

            board[5][0].OwningPlayer = player2;
            board[5][1].OwningPlayer = player1;
            board[5][2].OwningPlayer = player2;
            board[5][3].OwningPlayer = player1;

            List<Tile> compulsionWin = AIHelper.FindTwoTurnCompulsionWin(game, player1);

            Assert.AreEqual(1, compulsionWin.Count);
            Assert.AreEqual(board[4][0], compulsionWin[0]);
        }
コード例 #6
0
        public void TestFindFourTokenConnectionPossibilties()
        {
            //setup the initial condition
            var player1 = "Player 1";
            var player2 = "Player 2";
            var game = new GameCore(player1, player2);
            var board = game.Board;

            board[0][0].OwningPlayer = player1;
            board[1][0].OwningPlayer = player2;

            board[4][0].OwningPlayer = player1;
            board[4][1].OwningPlayer = player1;

            board[5][0].OwningPlayer = player1;
            board[5][1].OwningPlayer = player2;

            List<TileConnection> conns = AIHelper.FindFourTokenConnectionPossibilties(board, player1);

            //Around 0,0
            Assert.AreEqual("[0,0] - [0,3]", conns[0].ToString()); //vert
            Assert.AreEqual("[0,0] - [3,3]", conns[1].ToString()); //+diag

            //Around 4,0
            //Assert.AreEqual("[4,0] - [4,3]", conns[2].ToString()); //vert subset of [4,0] - [4,4]
            //Assert.AreEqual("[2,0] - [6,0]", conns[2].ToString()); //horiz dup of [2,0] - [6,0] around 5,0
            Assert.AreEqual("[1,3] - [4,0]", conns[2].ToString()); //-diag

            //Around 4,1
            Assert.AreEqual("[4,0] - [4,4]", conns[3].ToString()); //vert
            Assert.AreEqual("[1,1] - [4,1]", conns[4].ToString()); //horiz
            Assert.AreEqual("[3,0] - [6,3]", conns[5].ToString()); //+diag
            Assert.AreEqual("[1,4] - [5,0]", conns[6].ToString()); //-diag

            //Around 5,0
            Assert.AreEqual("[2,0] - [6,0]", conns[7].ToString());
            //Assert.AreEqual("[2,3] - [5,0]", conns[10].ToString()); //-diag subset of [1,4] - [5,0]

            Assert.AreEqual(8, conns.Count);
        }
コード例 #7
0
ファイル: AIBaseTest.cs プロジェクト: trbarrett/Four-in-a-Row
        public void TestFindAndSetupWinningCompulsion()
        {
            //setup the initial condition
            var player1 = "Player 1";
            var player2 = "Player 2";
            var game = new GameCore(player1, player2);
            var board = game.Board;

            board[2][0].OwningPlayer = player1;
            board[2][1].OwningPlayer = player1;

            board[3][0].OwningPlayer = player2;
            board[3][1].OwningPlayer = player1;

            //column 4 is empty, with which we can create a compulsion.
            //If player one places a tile in column four, it will cause a potential
            //win if they place another one (4 horizontally) so player 2 will have to
            //block it. But that will let player 1 place a tile there to win
            //via a diagonal.

            board[5][0].OwningPlayer = player2;
            board[5][1].OwningPlayer = player1;
            board[5][2].OwningPlayer = player2;
            board[5][3].OwningPlayer = player1;

            var mocks = new MockRepository();
            var gc = mocks.StrictMock<IGameController>();
            Expect.Call(gc.Game).Return(game).Repeat.Any();
            mocks.ReplayAll();

            var ai = new AIBase();
            ai.PlayTurn(gc, player1);

            Assert.AreEqual(player1, board[4][0].OwningPlayer);
            mocks.VerifyAll();
        }
コード例 #8
0
ファイル: AIBase.cs プロジェクト: trbarrett/Four-in-a-Row
        //Test Me
        public int GetRatingForTile(GameCore gameCore, Tile tile, Object player)
        {
            int rating = 0;

            //First check if placing a tile here will force the opponent to block us, or give the
            //opponent a wining move. We shouldn't consider those tiles.
            var copy = gameCore.Copy();
            copy.DropTokenOnColumn(tile.ColumnNo);
            var otherPlayerDirectWinnable = AIHelper.FindDirectWinningPossibilities(copy.Board, gameCore.GetOtherPlayer(player));
            if (otherPlayerDirectWinnable.Count > 0) {

                //lower skilled players can miss an opponents winning move, especially if they are
                //focused on their own win
                if (_difficulty == AIDifficulty.VeryEasy) {  rating -= 2; }
                else if (_difficulty == AIDifficulty.Easy) {  rating -= 4; }
                else if (_difficulty == AIDifficulty.Medium) {  rating -= 6; }
                else {
                    //the other player can win if we play here, so don't play
                    return -1000;
                }
            }

            var usDirectWinnable = AIHelper.FindDirectWinningPossibilities(copy.Board, player);
            if (usDirectWinnable.Count > 0) {
                //we can win next turn if we place here, forcing the other player to make a blocking move.
                //It may be worth making the move anyway if it gives us a good position, and there isn't
                //anything better, so don't completley discount it.
                if (CanCreateCompulsion(copy.Board)) {
                    rating -= 3;
                } else {
                    //if the player can't create compulsions we shouldn't penalize them for
                    //trying to win

                    if (_difficulty < AIDifficulty.Medium) {
                        //lower skilled players will chase a win
                        rating += 2;
                    }
                }
            }

            //We are trying to create general opportunities to head into the end game by creating
            //potential token connections. So we want to look at where we have potential connections
            //and try to build that area up to generate a compulsion.
            rating += (FindTilesInConnection(gameCore, tile, player).Count * 2);

            //Future ToDos
            //Now check the opponents tile connections. Add points based on how many they have, sine we're
            //blocking their position
            if (_tryToBlockOpponentsGoodConnections) {
                rating += FindTilesInConnection(gameCore, tile, gameCore.GetOtherPlayer(player)).Count;
            }

            //Check what will happen once we play our tile. Does it open up any good positions?

            //Do some planning stuff now:

            //If placing a tile here will start a compulsion for us, add points
            //If placing a tile here will block an opponents compulsion then add some points.
            //Check the tile connections for the tile above this one. If the opponent would have good
            //connections then subtract points. If we would have a good connections subtract points
            //(since it alows the opponent to block us.

            return rating;
        }
コード例 #9
0
ファイル: AIBase.cs プロジェクト: trbarrett/Four-in-a-Row
        /// <summary>
        /// Method finds the amount of player tokens around the tile which are in a good connection
        /// to it.
        /// </summary>
        public List<Tile> FindTilesInConnection(GameCore gameCore, Tile tile, Object player)
        {
            //To do this we get all the potential tile connections that contain this placeable tile.
            var possibleConnections = AIHelper.FindFourTokenConnectionPossibilties(gameCore.Board, player);
            var containingConnections = (from pc in possibleConnections
                                        where pc.Tiles.Contains(tile)
                                        select pc).ToList();

            //And add points based on how many of our tokens in tile connections it is part of. More
            //is better
            var playerTilesInConnection = (from tc in containingConnections
                                           from t in tc.Tiles
                                           where t.OwningPlayer == player
                                           select t).ToList();

            return playerTilesInConnection;
        }