public static ReversiAction Pass()
        {
            var act = new ReversiAction();

            act.isPass = true;
            return(act);
        }
        public static ReversiAction PutAt(int x, int y)
        {
            var act = new ReversiAction();

            act.isPass = false;
            act.putAt  = new Pos(x, y);
            return(act);
        }
Exemplo n.º 3
0
        public void CalcNextAction(ReversiBoard board)
        {
            var puttables = board.ListupPuttables(this.mark);

            if (puttables.Count == 0)
            {
                this.nextAction = ReversiAction.Pass();
                return;
            }
            Random rd = new Random();
            var    p  = puttables [rd.Next(puttables.Count)];

            this.nextAction = ReversiAction.PutAt(p.X, p.Y);
        }
Exemplo n.º 4
0
        /// <summary>
        /// このターンの動きをやります
        /// </summary>
        public ReversiAction Next()
        {
            if (this.IsGameEnd())
            {
                return(ReversiAction.Pass());
            }
            GetTurnPlayer().CalcNextAction(this.board);
            var nextAction = GetTurnPlayer().GetNextAction();

            if (!nextAction.IsPass())
            {
                var putAt = nextAction.GetPutAt();
                this.board.PutAt(putAt.X, putAt.Y, GetTurnPlayer().GetMark());
            }
            turn++;
            return(nextAction);
        }
Exemplo n.º 5
0
 public void SetNextAction(ReversiAction nextAction)
 {
     this.nextAction = nextAction;
 }
        // 戦うよ、君が立ち上がり続ける限りは
        public static void Battle(dynamic json)
        {
            ReversiRemotePlayer remote = new ReversiRemotePlayer(); // あいて
            ReversiRandomPlayer random = new ReversiRandomPlayer(); // じぶん
            ReversiManager      reversi;                            // こいつがリバーシと言える

            ReversiAction nextAction;                               // いろんなところで使うから先に宣言しているけどこういうの良くない

            if (!json.IsDefined("first"))
            {
                throw new Exception("first is not defined, contact to admin");
            }

            if (json.first == "true")
            {
                // 先攻
                random.SetMark(ReversiMark.BLACK);
                remote.SetMark(ReversiMark.WHITE);
                reversi = new ReversiManager(random, remote);

                // 先攻なので行動します
                nextAction = reversi.Next();
                var obj = new {
                    action = "put",
                    pos    = new int[] { nextAction.GetPutAt().X, nextAction.GetPutAt().Y }
                };
                Send(DynamicJson.Serialize(obj));

                dynamic recv = DynamicJson.Parse(Recv());
                Console.WriteLine(recv);
                string r = CheckResult(recv);
                if (r.Length > 0)
                {
                    // ここで false が返ってきててもどうにもできない
                    throw new Exception(r);
                }
            }
            else
            {
                // 後攻
                remote.SetMark(ReversiMark.BLACK);
                random.SetMark(ReversiMark.WHITE);
                reversi = new ReversiManager(remote, random);
            }

            // 戦いの渦中に身を置く
            while (true)
            {
                dynamic recv = DynamicJson.Parse(Recv());
                string  r    = CheckResult(recv);
                if (r.Length > 0)
                {
                    // ここで false が返ってきててもどうにもできない
                    throw new Exception(r);
                }
                if (CheckEnd(recv))
                {
                    break;
                }

                // あいての手をremoteに反映
                if (recv.action == "pass")
                {
                    remote.SetNextAction(ReversiAction.Pass());
                    reversi.Next();
                }
                else if (recv.action == "put")
                {
                    int x = (int)recv.pos [0];                     // double で返ってくる
                    int y = (int)recv.pos [1];
                    nextAction = ReversiAction.PutAt(x, y);
                    remote.SetNextAction(nextAction);
                    reversi.Next();
                }
                else                     // close がありうる(けどここまでこないな)
                {
                    break;
                }

                while (true)
                {
                    // 自分の番
                    nextAction = reversi.Next();
                    if (nextAction.IsPass())
                    {
                        var obj = new {
                            action = "pass"
                        };
                        Send(DynamicJson.Serialize(obj));
                    }
                    else
                    {
                        var obj = new {
                            action = "put",
                            pos    = new int[] { nextAction.GetPutAt().X, nextAction.GetPutAt().Y }
                        };
                        Send(DynamicJson.Serialize(obj));
                    }
                    recv = DynamicJson.Parse(Recv());
                    r    = CheckResult(recv);
                    if (r.Length > 0)
                    {
                        Console.WriteLine("ERROR: " + r);
                        continue;
                    }
                    break;
                }
                if (CheckEnd(recv))
                {
                    break;
                }
            }
        }