Beispiel #1
0
        public static BridgeGame Replay(string pbn, Action <BridgeGame, PlayerPosition, string> onCardPaying = null, Action <int> onTrickPlayed = null)
        {
            var parseResult = PbnParser.ParseGame(pbn);
            var game        = BridgeHelper.GetGameFromPbn(parseResult.Deal, parseResult.Contract, parseResult.Declarer);
            var player      = PlayerPosition.Players.Find(x => x.FirstLetter == parseResult.FirstPlayer);

            if (!string.IsNullOrEmpty(parseResult.Play))
            {
                var play   = PbnParser.ParsePlay(parseResult.Play);
                var number = 0;
                foreach (var trick in play)
                {
                    number++;
                    for (int i = 0; i < trick.Length; i++)
                    {
                        var card = trick[trick.Length == 4 ? player.PbnIndex: i];
                        if (onCardPaying != null)
                        {
                            onCardPaying(game, player, card);
                        }
                        player = game.PlayCard(BridgeHelper.GetCard(card), player);
                    }
                    if (onTrickPlayed != null)
                    {
                        onTrickPlayed(number);
                    }
                }
            }
            return(game);
        }
Beispiel #2
0
        public PlayerPosition PlayCard(Card card, PlayerPosition playerPosition)
        {
            _nextPlayer = BridgeHelper.GetNextPlayerPosition(playerPosition);

            if (_currentTrick.Deck.Count == 0)
            {
                _currentTrick             = new Trick();
                _currentTrick.TrickDealer = playerPosition;
            }


            if (_currentTrick.Deck.Count <= 4)
            {
                card.PlayerPosition = playerPosition;
                _currentTrick.Deck.Cards.Add(card);
            }

            if (_currentTrick.Deck.Count == 4)
            {
                Tricks.Add(_currentTrick);
                var winner = FindWinner(_currentTrick, Contract.Trump);
                _currentTrick.TrickWinner = winner;
                _nextPlayer   = winner;
                _currentTrick = new Trick()
                {
                    TrickDealer = winner
                };
            }

            GameState[playerPosition].RemoveCard(card);

            return(_nextPlayer);
        }
Beispiel #3
0
        private void registerIncomingEvents()
        {
            if (coreWebView2 == null)
            {
                return;
            }

            coreWebView2.WebMessageReceived += (object sender, Microsoft.Web.WebView2.Core.CoreWebView2WebMessageReceivedEventArgs e) =>
            {
                var bytes    = System.Text.Encoding.UTF8.GetBytes(e.WebMessageAsJson);
                var document = System.Text.Json.JsonDocument.Parse(bytes);

                System.Text.Json.JsonElement projection;
                if (document.RootElement.TryGetProperty("projection", out projection))
                {
                    State.Projection.OnNext(BridgeHelper.ProjectionFromString(projection.GetString()));
                }

                System.Text.Json.JsonElement clearColor;
                if (document.RootElement.TryGetProperty("clearColor", out clearColor))
                {
                    State.ClearColor.OnNext(BridgeHelper.ColorFromString(clearColor.GetString()));
                }
            };
        }
Beispiel #4
0
        public static void TestMerge()
        {
            object o1 = new
            {
                p1 = 1
            };

            Data o2 = new Data()
            {
                p1 = 2.0m,
                p2 = 2.0m
            };

            o2 = BridgeHelper.Merge <Data>(o2, o1);

            decimal o3 = 3.0m;

#pragma warning disable 219
            object o4 = 1;
#pragma warning restore 219
            o3 = BridgeHelper.Merge <decimal>(o3, o4);

            Assert.True(o2.p1.Is <decimal>());
            Assert.True(o2.p2.Is <decimal>());
            Assert.True(o3.Is <decimal>());
        }
Beispiel #5
0
        public static void TestIsArrayFromIFrame()
        {
            var frame = new HTMLIFrameElement();

            Document.Body.AppendChild(frame);

            try
            {
                dynamic xFrame = Window.Frames[Window.Frames.Length - 1];
                object  xArray = xFrame.Array;

                // Create an array in the iframe
                var array = Script.Write <int[]>("new {0}(1,2,3)", xArray);

                var contains = array.Contains(3);
                Assert.True(contains, "Checks that an array [1, 2, 3] created in another frame contains 3");

                var isArray = BridgeHelper.isArray(array);
                Assert.True(isArray, "Checks that an array created in another frame returns true for Bridge.isArray(array)");
            }
            finally
            {
                if (frame != null && frame.ParentNode != null)
                {
                    frame.ParentNode.RemoveChild(frame);
                }
            }
        }
Beispiel #6
0
        public void Test()
        {
            var card = BridgeHelper.GetCard("HK");

            Assert.AreEqual(Suit.Hearts, card.Suit);
            Assert.AreEqual(Rank.King, card.Rank);
        }
Beispiel #7
0
        private void MouseDownSetBridge(MouseEventArgs e)
        {
            UnitPoint mousePoint = this.ToUnit(this.mouseDownPoint);

            if (this.bridgeStart.IsEmpty)
            {
                this.bridgeStart = mousePoint;
            }
            else
            {
                this.bridgeEnd = mousePoint;
                BridgingModel param = this.bridgeFunc?.Invoke();
                if (param != null)
                {
                    //1.获取图形区域与桥接直线交叉的图形
                    var  oldObjects = this.Model.DrawingLayer.Objects.FindAll(f => HitUtil.LineIntersectWithRect(this.bridgeStart, this.bridgeEnd, f.GetBoundingRectangle(BoundingRectangleType.All)));
                    bool isChanged  = false;
                    //2.计算直线与图形的交点
                    var newObjects = BridgeHelper.GetBridgeObjects(oldObjects, this.bridgeStart, this.bridgeEnd, param, out isChanged);
                    if (isChanged)
                    {
                        this.Model.DoBridge(newObjects, oldObjects, true);
                    }
                }
                this.CommandEscape();
            }
        }
Beispiel #8
0
        private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
        {
            // create new game instance for our main window gameshape control
            GameShape.Game = new Game();

            // call some setup methods to initialize the game components
            SetupPlayerHandDecks();
            SetupTrickDecks();

            var result = BridgeHelper.ParsePbn("E:AT5.AJT.A632.KJ7 Q763.KQ9.KQJ94.T 942.87653..98653 KJ8.42.T875.AQ42");
        }
        public FutureTricks SolveBoardPbn(BridgeGame game, int target, int solutions, int mode)
        {
            if (game.CurrentTrick.Deck.Count > 3)
            {
                throw new ArgumentException("Invalid current trick cards count", "CurrentTrick.Deck.Count");
            }

            var deal = new DealPbn();

            deal.trump = game.Contract.Trump.Order;
            deal.first = game.CurrentTrick.TrickDealer.Order;

            deal.currentTrickRank = new int[3];
            deal.currentTrickSuit = new int[3];
            for (int i = 0; i < game.CurrentTrick.Deck.Count; i++)
            {
                var card = game.CurrentTrick.Deck.Cards[i];
                deal.currentTrickRank[i] = card.Rank.Score;
                deal.currentTrickSuit[i] = card.Suit.Order;
            }
            deal.remainCards = DdsHelper.PbnStringToChars(BridgeHelper.ToPbn(game));

            var ddsResult = new Integration.FutureTricks();

            //TODO: Support mutiple threads.
            var res = DdsImport.SolveBoardPBN(deal, target, solutions, mode, ref ddsResult, 0);

            if (res != 1)
            {
                throw new DdsSolveBoardException(res);
            }

            var result = new FutureTricks();

            result.Cards  = ddsResult.cards;
            result.Nodes  = ddsResult.nodes;
            result.Scores = ddsResult.score.ToList();
            for (int i = 0; i < 13; i++)
            {
                if (ddsResult.rank[i] != 0)
                {
                    var rank = new Rank(ddsResult.rank[i]);
                    var suit = new Suit(ddsResult.suit[i]);

                    result.FutureCards.Cards.Add(new Card(rank, suit));
                }
                else
                {
                    break;
                }
            }

            return(result);
        }
Beispiel #10
0
        public BridgeGame(Dictionary <PlayerPosition, Deck> state, PlayerPosition declarer, string contract)
        {
            GameState = state;
            Tricks    = new List <Trick>();
            Declarer  = declarer;
            Dummy     = BridgeHelper.GetNextPlayerPosition(BridgeHelper.GetNextPlayerPosition(declarer));
            Contract  = new Contract(contract, declarer);

            _currentTrick = new Trick()
            {
                TrickDealer = BridgeHelper.GetNextPlayerPosition(declarer)
            };
        }
Beispiel #11
0
        public WinPcapAdapter(DEV9_State parDev9, string parDevice, bool isSwitch)
            : base(parDev9)
        {
            switched = isSwitch;

            NetworkInterface hostAdapter = GetAdapterFromGuid(parDevice);

            if (hostAdapter == null)
            {
                if (BridgeHelper.IsInBridge(parDevice) == true)
                {
                    hostAdapter = GetAdapterFromGuid(BridgeHelper.GetBridgeGUID());
                }
            }
            if (hostAdapter == null)
            {
                //System.Windows.Forms.MessageBox.Show("Failed to GetAdapter");
                throw new NullReferenceException("Failed to GetAdapter");
            }
            hostMAC = hostAdapter.GetPhysicalAddress().GetAddressBytes();
            //PcapInitIO needs correct MAC
            SetMAC(null);
            byte[] wMAC = (byte[])ps2MAC.Clone();
            //wMAC[3] = hostMAC[3];
            wMAC[5] = hostMAC[4];
            wMAC[4] = hostMAC[5];
            SetMAC(wMAC);

            //If parDevice starts with "{", assume device is given by GUID (as it would be under windows)
            //else, use the string as is (wine, linux)
            if (!PcapInitIO(parDevice.StartsWith("{") ? @"\Device\NPF_" + parDevice : parDevice))
            {
                Log_Error("Can't Open Device " + parDevice);
#if NETCOREAPP
#else
                System.Windows.Forms.MessageBox.Show("Can't Open Device " + parDevice);
#endif
                return;
            }

            if (DEV9Header.config.DirectConnectionSettings.InterceptDHCP)
            {
                InitDHCP(hostAdapter);
            }
        }
Beispiel #12
0
        public object Any(PlayGame request)
        {
            var dds    = new DdsConnect();
            var game   = GameReplayer.Replay(request.PBN);
            var player = BridgeHelper.GetNextPlayerPosition(game.Declarer);

            while (game.Tricks.Count != 13)
            {
                var result = dds.SolveBoard(game);
                var card   = result.FutureCards.Cards[0];
                player = game.PlayCard(card, player);
            }
            var play = game.Tricks.Select(x => BridgeHelper.DeckToPbnPlay(x.Deck));

            return(new PlayGameResponse
            {
                Play = string.Join("\n", play)
            });
        }
Beispiel #13
0
        static void Main(string[] args)
        {
            var dds     = new DoubleDummyModule();
            var pbnCode = "E:K9742.763.Q.K764 AT6.AJ98.A5.QT53 853.QT.KT742.J92 QJ.K542.J9863.A8";

            Console.WriteLine("Board: " + pbnCode);


            var res = DoubleDummyModule.CalculateMakeableContracts(pbnCode);

            Console.WriteLine("Best results:");
            foreach (var contract in res)
            {
                Console.WriteLine(contract);
            }

            var game = BridgeHelper.GetGameFromPbn(pbnCode, "4S", "W");

            Console.WriteLine("------------- Game Starts ----------------");
            Console.WriteLine("Trump: " + game.Contract.Trump);
            //var player = game.Declarer;
            var player = PlayerPosition.North;

            while (game.CardsRemaning > 0)
            {
                var result = dds.SolveBoardPbnBestCard(game);
                Console.WriteLine(player + ": " + result.Card + ". Score: " + result.Score);
                player = game.PlayCard(result.Card, player);

                if (game.CurrentTrick.Deck.Count == 0)
                {
                    Console.WriteLine("Trick Winner: " + game.Tricks.Last().TrickWinner);
                }
            }

            Console.WriteLine("-----------Results----------");
            Console.WriteLine("South/North: " + game.NorthSouthTricksMade + " tricks");
            Console.WriteLine("East/West: " + game.EastWestTricksMade + " tricks");

            Console.ReadKey();
        }
Beispiel #14
0
        public TAPAdapter(DEV9_State parDev9, string parDevice)
            : base(parDev9)
        {
            htap = TAPOpen(parDevice);

            htapstream = new FileStream(htap, FileAccess.ReadWrite, 16 * 1024, true);

            if (DEV9Header.config.DirectConnectionSettings.InterceptDHCP)
            {
                NetworkInterface hostAdapter = GetAdapterFromGuid(parDevice);
                if (hostAdapter == null)
                {
                    if (BridgeHelper.IsInBridge(parDevice) == true)
                    {
                        hostAdapter = GetAdapterFromGuid(BridgeHelper.GetBridgeGUID());
                    }
                }
                if (hostAdapter == null)
                {
                    //System.Windows.Forms.MessageBox.Show("Failed to GetAdapter");
                    throw new NullReferenceException("Failed to GetAdapter");
                }
                InitDHCP(hostAdapter);
            }

            SetMAC(null);

            byte[] hostMAC = TAPGetMac(htap);
            if (hostMAC == null)
            {
                Log_Error("Unable to get host MAC address, using default MAC");
                return;
            }

            byte[] wMAC = (byte[])ps2MAC.Clone();
            //wMAC[3] = hostMAC[3];
            wMAC[5] = hostMAC[4];
            wMAC[4] = hostMAC[5];
            SetMAC(wMAC);
        }
Beispiel #15
0
        static void Main(string[] args)
        {
            var dds     = new DdsConnect();
            var pbnCode = "E:AT5.AJT.A632.KJ7 Q763.KQ9.KQJ94.T 942.87653..98653 KJ8.42.T875.AQ42";

            Console.WriteLine("Board: " + pbnCode);

            var game = BridgeHelper.GetGameFromPbn(pbnCode);
            var res  = dds.CalculateMakeableContracts(pbnCode);

            Console.WriteLine("Best results:");
            foreach (var contract in res)
            {
                Console.WriteLine(contract);
            }
            Console.WriteLine("------------- Game Starts ----------------");
            Console.WriteLine("Trump: " + game.Contract.Trump);
            var player = game.Declarer;

            while (game.CardsRemaning > 0)
            {
                var result = dds.SolveBoardPbnBestCard(game);
                Console.WriteLine(player + ": " + result.Card + ". Score: " + result.Score);
                player = game.PlayCard(result.Card, player);

                if (game.CurrentTrick.Deck.Count == 0)
                {
                    Console.WriteLine("Trick Winner: " + game.Tricks.Last().TrickWinner);
                }
            }

            Console.WriteLine("-----------Results----------");
            Console.WriteLine("South/North: " + game.NorthSouthTricksMade + " tricks");
            Console.WriteLine("East/West: " + game.EastWestTricksMade + " tricks");

            Console.ReadKey();
        }
Beispiel #16
0
 private void registerOutgoingEvents()
 {
     System.ObservableExtensions.Subscribe(State.Fps, (fps) => sendState("fps", fps));
     System.ObservableExtensions.Subscribe(State.Projection, (projection) => sendState("projection", BridgeHelper.ProjectionToString(projection)));
     System.ObservableExtensions.Subscribe(State.ClearColor, (clearColor) => sendState("clearColor", BridgeHelper.ColorToString(clearColor)));
 }