Ejemplo n.º 1
0
        public override Action Think(GameState gameState, int myPlayerNum)
        {
            // 自分の情報
            var player = gameState.Players[myPlayerNum];
            var pos = player.Position;
            var dir = player.Direction;

            // 1マス先
            var lpos = GetNextPosition(pos, GetLeft(dir));
            var rpos = GetNextPosition(pos, GetRight(dir));
            var spos = GetNextPosition(pos, dir);

            // 1マス先に行けるか
            var field = gameState.Field;
            var l = IsIn(field, lpos) && field.Cells[lpos.X][lpos.Y].Passable;
            var r = IsIn(field, rpos) && field.Cells[rpos.X][rpos.Y].Passable;
            var s = IsIn(field, spos) && field.Cells[spos.X][spos.Y].Passable;

            // 行ける方向一覧
            var list = new List<Action>();
            if (l) list.Add(Action.Left);
            if (r) list.Add(Action.Right);
            if (s) list.Add(Action.Straight);

            // どこにも行けないなら諦めてランダム
            if (list.Count == 0)
            {
                list = new List<Action> { Action.Left, Action.Right, Action.Straight };
            }

            // ランダムで決定
            return list[rnd.Next(list.Count)];
        }
Ejemplo n.º 2
0
 public override Action Think(GameState gameState, int myPlayerNum)
 {
     while (true)
     {
         if (DX.CheckHitKey(keys) != 0) return Action.Straight;
         if (DX.CheckHitKey(keyl) != 0) return Action.Left;
         if (DX.CheckHitKey(keyr) != 0) return Action.Right;
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// ゲームの状態を描画する。
        /// </summary>
        /// <param name="state">ゲームの状態</param>
        public void Draw(GameState state)
        {
            var field = state.Field;
            var players = state.Players;

            // フィールド
            for (var y = 0; y < field.Height; ++y)
            {
                for (var x = 0; x < field.Width; ++x)
                {
                    var bas = new Position(
                        BasePos.X + x * CellSize.Width,
                        BasePos.Y + y * CellSize.Height);
                    var col = field.Cells[x][y].Color;

                    DX.DrawFillBox(
                        bas.X, bas.Y,
                        bas.X + CellSize.Width, bas.Y + CellSize.Height,
                        DX.GetColor(col.R, col.G, col.B));
                }
            }

            // プレイヤー
            for (var i = 0; i < players.Count; ++i)
            {
                var pos = players[i].Position;
                var col = players[i].Color;
                var dir = players[i].Direction;
                var angle = 0.0;
                switch (dir.Value)
                {
                    case Direction.Right: angle = 0; break;
                    case Direction.RightUp: angle = 1; break;
                    case Direction.Up: angle = 2; break;
                    case Direction.LeftUp: angle = 3; break;
                    case Direction.Left: angle = 4; break;
                    case Direction.LeftDown: angle = 5; break;
                    case Direction.Down: angle = 6; break;
                    case Direction.RightDown: angle = 7; break;
                }
                angle *= Math.PI / 4;
                var r = Math.Min(CellSize.Width, CellSize.Height) / 2 - 1;
                var center = new Position(
                    BasePos.X + pos.X * CellSize.Width + CellSize.Width / 2,
                    BasePos.Y + pos.Y * CellSize.Height + CellSize.Height / 2);

                DX.DrawCircle(center.X, center.Y, r,
                    DX.GetColor(col.R, col.G, col.B), DX.FALSE);
                DX.DrawLine(center.X, center.Y,
                    (int)(center.X + r * Math.Cos(angle)),
                    (int)(center.Y - r * Math.Sin(angle)),
                    DX.GetColor(col.R, col.G, col.B));
            }
        }
Ejemplo n.º 4
0
 public bool Equals(GameState other)
 {
     if ((object)other == null) return false;
     if (!this.Field.Equals(other.Field)) return false;
     if (!this.PlayersCount.Equals(other.PlayersCount)) return false;
     for (var i = 0; i < this.PlayersCount; ++i)
     {
         if (!this.Players[i].Equals(other.Players[i]))
         {
             return false;
         }
     }
     return true;
 }
Ejemplo n.º 5
0
 private void stepMove(IList<Action> actions)
 {
     var cells = State.Field.Cells.Select(row => row.ToList()).ToList();
     for (var i = State.PlayersCount - 1; i >= 0; --i)
     {
         var player = State.Players[i];
         var pos = player.Position;
         if (isInField(pos) && player.Alive)
         {
             cells[pos.X][pos.Y] = new CellState(player.Color, cells[pos.X][pos.Y].Passable);
         }
         stepMovePlayer(i, actions[i]);
     }
     State = new GameState(
         new FieldState(
             State.Field.Name,
             cells.Select(row => (IList<CellState>)row.AsReadOnly()).ToList().AsReadOnly()
         ),
         State.Players);
 }
Ejemplo n.º 6
0
 /// <summary>
 /// ゲームの状態を元にプレイヤーの行動を決定する。
 /// 各クライアントが実装しなければならない。
 /// </summary>
 /// <param name="gameState">ゲームの状態</param>
 /// <param name="myPlayerNum">対応するプレイヤーのプレイヤー番号</param>
 /// <returns>行動</returns>
 public abstract Action Think(GameState gameState, int myPlayerNum);
Ejemplo n.º 7
0
 private void stepMovePlayer(int playerNumber, Action action)
 {
     var player = State.Players[playerNumber];
     var dir = player.Direction.Value;
     if (player.Dead)
     {
         return;
     }
     if (action == Action.Right)
     {
         switch (dir)
         {
             case Direction.Right: dir = Direction.RightDown; break;
             case Direction.RightUp: dir = Direction.Right; break;
             case Direction.Up: dir = Direction.RightUp; break;
             case Direction.LeftUp: dir = Direction.Up; break;
             case Direction.Left: dir = Direction.LeftUp; break;
             case Direction.LeftDown: dir = Direction.Left; break;
             case Direction.Down: dir = Direction.LeftDown; break;
             case Direction.RightDown: dir = Direction.Down; break;
         }
     }
     else if (action == Action.Left)
     {
         switch (dir)
         {
             case Direction.Right: dir = Direction.RightUp; break;
             case Direction.RightUp: dir = Direction.Up; break;
             case Direction.Up: dir = Direction.LeftUp; break;
             case Direction.LeftUp: dir = Direction.Left; break;
             case Direction.Left: dir = Direction.LeftDown; break;
             case Direction.LeftDown: dir = Direction.Down; break;
             case Direction.Down: dir = Direction.RightDown; break;
             case Direction.RightDown: dir = Direction.Right; break;
         }
     }
     var pos = player.Position;
     switch (dir)
     {
         case Direction.Right: pos = new PositionState(pos.X + 1, pos.Y); break;
         case Direction.RightUp: pos = new PositionState(pos.X + 1, pos.Y - 1); break;
         case Direction.Up: pos = new PositionState(pos.X, pos.Y - 1); break;
         case Direction.LeftUp: pos = new PositionState(pos.X - 1, pos.Y - 1); break;
         case Direction.Left: pos = new PositionState(pos.X - 1, pos.Y); break;
         case Direction.LeftDown: pos = new PositionState(pos.X - 1, pos.Y + 1); break;
         case Direction.Down: pos = new PositionState(pos.X, pos.Y + 1); break;
         case Direction.RightDown: pos = new PositionState(pos.X + 1, pos.Y + 1); break;
     }
     var players = State.Players.ToList();
     players[playerNumber] = new PlayerState(
         player.Number, player.Name, player.Color,
         pos, new DirectionState(dir), player.Alive);
     State = new GameState(State.Field, players.AsReadOnly());
 }
Ejemplo n.º 8
0
        /// <summary>
        /// すべてのプレイヤーの位置のセルを通行可能でなくする。
        /// </summary>
        private void makeCellsUnderPlayersNotPassable()
        {
            var cells = State.Field.Cells.Select(row => row.ToList()).ToList();
            foreach (var player in State.Players.Where(player => isInField(player.Position)))
	        {
                var x = player.Position.X;
                var y = player.Position.Y;
                cells[x][y] = new CellState(cells[x][y].Color, false);
	        }
            State = new GameState(
                new FieldState(
                    State.Field.Name,
                    cells.Select(row => (IList<CellState>)row.AsReadOnly()).ToList()
                ),
                State.Players);
        }
Ejemplo n.º 9
0
 private void stepJudge()
 {
     for (var i = 0; i < State.PlayersCount; ++i)
     {
         var player = State.Players[i];
         if (player.Dead)
         {
             continue;
         }
         if (stepJudgePlayer(i))
         {
             var players = State.Players.ToList();
             players[i] = new PlayerState(
                 player.Number, player.Name, player.Color,
                 player.Position, player.Direction, false);
             State = new GameState(State.Field, players.AsReadOnly());
         }
     }
 }
Ejemplo n.º 10
0
 public Game(GameState initState)
 {
     this.State = initState;
     makeCellsUnderPlayersNotPassable();
 }