Esempio n. 1
0
 public static CellPosition[] ParseList(string s)
 { 
     var charAry = s.ToCharArray();
     var result = new CellPosition[charAry.Length / 2];
     for (var i = 0; i < charAry.Length; i += 2)
     {
         result[i / 2] = Parse(charAry[i] + "" + charAry[i + 1]);
     }
     return result;
 }
Esempio n. 2
0
 public static List<Point> GetWallOutline(CellPosition cpWallLocation)
 {
     if (cpWallLocation == CellPosition.Left)
         return new List<Point>(new Point[] { new Point(0, 8), new Point(4, 0), new Point(4, 8), new Point(0, 16), new Point(0, 8) });
     else if (cpWallLocation == CellPosition.Front)
         return new List<Point>(new Point[] { new Point(4, 0), new Point(4, 8), new Point(12, 8), new Point(12, 0), new Point(4, 0) });
     else if (cpWallLocation == CellPosition.Right)
         return new List<Point>(new Point[] { new Point(12, 0), new Point(16, 8), new Point(16, 16), new Point(12, 8), new Point(12, 0) });
     return new List<Point>(new Point[] { new Point(0, 0) });
 }
Esempio n. 3
0
 public void MoveCell(GameTime time, CellPosition diff)
 {
     MoveToCell(time, c_position + diff);
 }
Esempio n. 4
0
    private IEnumerable<CellPosition> GetReversableCellPositionsByDirection(int row, int column, int nr, int nc, CellState player, CellState other)
    {
        var npt = new CellPosition(row + nr, column + nc);
        if (GetCellState(npt.Row, npt.Column) != other)
        {
            return new CellPosition[0];
        }

        var list = new List<CellPosition>();
        list.Add(npt);
        for (
            var ipt = new CellPosition(npt.Row + nr, npt.Column + nc);
            ipt.Row >= 0 && ipt.Column >= 0 &&
            ipt.Row < Rows && ipt.Column < Columns;
            ipt.Row += nr, ipt.Column += nc)
        {
            var s = GetCellState(ipt.Row, ipt.Column);
            if (s == player) { return list; }
            else if (s == other) { list.Add(ipt); }
            else { break; }
        }
        return new CellPosition[0];
    }
Esempio n. 5
0
 public CellPositionEvaluation(CellPosition pt, int ev)
 {
     Position = pt;
     Evaluation = ev;
 }
Esempio n. 6
0
 /// <summary>
 /// Marks the cell as holiday.
 /// </summary>
 /// <param name='pos'>
 /// The position of the cell, as a CellPosition object.
 /// </param>
 protected void MarkCellAsHoliday(CellPosition pos)
 {
     this.MarkCellAsHoliday( pos.Row, pos.Col );
 }
Esempio n. 7
0
        /// <summary>
        /// Gets the day for a given cell position.
        /// </summary>
        /// <returns>
        /// The day for cell position.
        /// </returns>
        /// <param name='pos'>
        /// Position.
        /// </param>
        public int GetDayForCellPosition(CellPosition pos)
        {
            // Check for sanity
            if ( pos.Row < 0
              || pos.Col < 0 )
            {
                throw new ArgumentOutOfRangeException( "invalid cell: " + pos.ToString() );
            }

            // Do it
            string strDay = (string) this.grdDayGrid.Rows[ pos.Row ].Cells[ pos.Col ].Value;
            int toret = 1;

            if ( strDay != null ) {
                strDay = strDay.Trim();
                if (!( Int32.TryParse( strDay, out toret ) )) {
                    toret = 1;
                }
            } else {
                throw new ArgumentOutOfRangeException( "invalid cell: " + pos.ToString() );
            }

            return toret;
        }
    private IEnumerable<CellPosition> GetReversibleCellPositionsByDirection(int row, int column, int dr, int dc, Player player)
    {
        var own = player.ToCellState();
        var other = player.GetOtherPlayer().ToCellState();

        var ncp = new CellPosition(row + dr, column + dc);
        if (GetCellState(ncp.Row, ncp.Column) != other)
        {
            return new CellPosition[0];
        }

        var list = new List<CellPosition>() { ncp };
        for(var icp = new CellPosition(ncp.Row + dr, ncp.Column + dc);
            icp.Row >= 0 && icp.Column >= 0 &&
            icp.Row < Rows && icp.Column < Columns;
            icp = new CellPosition(icp.Row + dr, icp.Column + dc)
        )
        {
            var cell = GetCellState(icp.Row, icp.Column);
            if (cell == other) { list.Add(icp); }
            else if (cell == own) { return list; }
            else { break; }
        }
        return new CellPosition[0];
    }
 private CellPosition[] GetPlaceableCellPositions(Player player)
 {
     var placeableCellPositions = new List<CellPosition>();
     for (var r = 0; r < Rows; r++)
     {
         for (var c = 0; c < Columns; c++)
         {
             if (IsPlaceable(r, c, player))
             {
                 var pt = new CellPosition(r, c);
                 placeableCellPositions.Add(pt);
             }
         }
     }
     return placeableCellPositions.ToArray();
 }
Esempio n. 10
0
        GetReversibleCellPositionsByDirection(
            CellPosition pt, int dr, int dc, Player player)
    {
        var other = player.ToOtherPlayer().ToCellState();
        var npt = new CellPosition(pt.Row + dr, pt.Column + dc);
        if (GetCellState(npt.Row, npt.Column) != other)
        {
            return new CellPosition[0];
        }

        var list = new List<CellPosition>();
        list.Add(npt);
        var pc = player.ToCellState();
        for (
            var ipt = new CellPosition(npt.Row + dr, npt.Column + dc);
            ipt.Row >= 0 && ipt.Column >= 0 &&
            ipt.Row < Rows && ipt.Column < Columns;
            ipt = new CellPosition(ipt.Row + dr, ipt.Column + dc))
        {
            var s = GetCellState(ipt.Row, ipt.Column);
            if (s == pc) { return list.ToArray(); }
            else if (s == other) { list.Add(ipt); }
            else { break; }
        }
        return new CellPosition[0];
    }
Esempio n. 11
0
     GetReversibleCellPositions(CellPosition pt, Player player)
 {
     var list = new List<CellPosition>();
     list.AddRange(GetReversibleCellPositionsByDirection(pt, -1, -1, player));//左上
     list.AddRange(GetReversibleCellPositionsByDirection(pt, -1, 0, player)); //上
     list.AddRange(GetReversibleCellPositionsByDirection(pt, -1, 1, player)); //右上
     list.AddRange(GetReversibleCellPositionsByDirection(pt, 0, -1, player)); //左
     list.AddRange(GetReversibleCellPositionsByDirection(pt, 0, 1, player)); //右
     list.AddRange(GetReversibleCellPositionsByDirection(pt, 1, -1, player)); //左下
     list.AddRange(GetReversibleCellPositionsByDirection(pt, 1, 0, player)); //下
     list.AddRange(GetReversibleCellPositionsByDirection(pt, 1, 1, player)); //右
     return list.ToArray();
 }
Esempio n. 12
0
    private IEnumerator PlaceAnime(CellPosition pt, Player player)
    {
        var positions = GetReversibleCellPositions(pt, player);
        if (positions.Length == 0) { yield break; }
        
        // 新規に自分の石を置く
        _othelloCells[pt.Row, pt.Column].CellState = player.ToCellState();

        // 挟まれた石をひっくり返す
        var list = new List<OthelloCellView>();
        foreach(var rcpt in positions)
        {
            var cell = _othelloCells[rcpt.Row, rcpt.Column];
            var e = cell.UpdateCell(player.ToCellState());
            list.Add(cell);

            StartCoroutine(e);
            yield return new WaitForSeconds(0.1F);
        }

        while (true)
        {
            var isContinue = false;
            foreach (var cell in list)
            {
                if (cell.CellState != player.ToCellState())
                {
                    isContinue = true;
                    break;
                }
            }

            if (!isContinue) { break; }
            yield return null;
        }
    }
Esempio n. 13
0
    private void Place(CellPosition pt, Player player)
    {
        var positions = GetReversibleCellPositions(pt, player);
        if (positions.Length == 0) { return; }

        // 新規に自分の石を置く
        _othelloCells[pt.Row, pt.Column].CellState = player.ToCellState();
        foreach (var rcpt in positions)
        {
            var cell = _othelloCells[rcpt.Row, rcpt.Column];
            cell.CellState = player.ToCellState();
        }
    }
Esempio n. 14
0
 private IEnumerator GoNextAnime(CellPosition pt)
 {
     yield return StartCoroutine(PlaceAnime(pt, _currentPlayer));
     GoNext(pt);
 }
Esempio n. 15
0
    private void GoNext(CellPosition pt)
    {
        _record.Add(pt);
        Place(pt, _currentPlayer);

        var other = _currentPlayer.ToOtherPlayer();
        if (HasPlaceableCell(other)) { _currentPlayer = other; }
        else if (!HasPlaceableCell(_currentPlayer))
        {
            _isGameOver = true;
        }
        UpdateStoneCount();
    }
Esempio n. 16
0
 private int Eval(CellPosition pt, Player player)
 {
     return UnityEngine.Random.Range(0, 100);
 }
Esempio n. 17
0
    private IEnumerator UpdateUser()
    {
        while (true)
        {
            if (Input.GetKeyDown(KeyCode.UpArrow))
            { UpdateSelectedCell(SelectedRow - 1, SelectedColumn); }
            if (Input.GetKeyDown(KeyCode.DownArrow))
            { UpdateSelectedCell(SelectedRow + 1, SelectedColumn); }
            if (Input.GetKeyDown(KeyCode.LeftArrow))
            { UpdateSelectedCell(SelectedRow, SelectedColumn - 1); }
            if (Input.GetKeyDown(KeyCode.RightArrow))
            { UpdateSelectedCell(SelectedRow, SelectedColumn + 1); }

            if (Input.GetKeyDown(KeyCode.Escape)) { Reset(); break; }
            if (Input.GetKeyDown(KeyCode.Return)
                && IsPlaceable(SelectedRow, SelectedColumn, _currentPlayer))
            {
                var pt = new CellPosition(SelectedRow, SelectedColumn);
                GoNext(pt);
                UpdatePlaceableCells();
                UpdateStoneCount();
                break;
            }
            yield return null;
        }
    }
Esempio n. 18
0
 private IEnumerator UpdatePlayer()
 {
     while (true)
     {
         if (Input.GetKeyDown(KeyCode.U)) { Undo(); }
         if (Input.GetKeyDown(KeyCode.Escape)) { Reset(); }
         if (Input.GetKeyDown(KeyCode.DownArrow)) { SelectedRow++; }
         if (Input.GetKeyDown(KeyCode.UpArrow)) { SelectedRow--; }
         if (Input.GetKeyDown(KeyCode.RightArrow)) { SelectedColumn++; }
         if (Input.GetKeyDown(KeyCode.LeftArrow)) { SelectedColumn--; }
         if (Input.GetKeyDown(KeyCode.Return))
         {
             if (IsPlaceable(SelectedRow, SelectedColumn, _currentPlayer))
             {
                 var pt = new CellPosition(SelectedRow, SelectedColumn);
                 GoNext(pt);
                 //yield return StartCoroutine(GoNextAnime(pt));
                 UpdatePlaceableCells(_currentPlayer);
                 break;
             }
         }
         yield return null;
     }
 }
Esempio n. 19
0
        /// <summary>
        /// Gets the cell position for a given day.
        /// </summary>
        /// <returns>
        /// The cell position for day, as a CellPosition object.
        /// </returns>
        /// <param name='day'>
        /// Day to get the coordinates for.
        /// </param>
        public CellPosition GetCellPositionForDay(int day)
        {
            var toret = new CellPosition();

            // Check for a valid day
            if ( day < 1
              || day > this.LastDay.Day )
            {
                throw new ArgumentOutOfRangeException(
                    "GetCellPositionForDay(), day: " + Convert.ToString( day ) );
            }

            // Do it -- yep, this is a sequential search
            // Tired of calculating (and always nearly working)
            for(int i = 0; i < this.grdDayGrid.Rows.Count; ++i) {
                var cells = this.grdDayGrid.Rows[ i ].Cells;

                for(int j = 0; j < cells.Count; ++j) {
                    int calDay = 0;
                    string value = (string) cells[ j ].Value;

                    if ( value != null ) {
                        if ( int.TryParse( value.Trim(), out calDay ) ) {
                            if ( day == calDay ) {
                                toret.Row = i;
                                toret.Col = j;
                                break;
                            }
                        }
                    }
                }
            }

            return toret;
        }
Esempio n. 20
0
    private CellPosition SelectCellPosition(CellPosition[] cellPositions)
    {
        if (_currentPlayer == Player.Black)
        {
            var pteList = new List<CellPositionEvaluation>();
            foreach (var pt in cellPositions)
            {
                var ev = Eval(pt, _currentPlayer, true);
                pteList.Add(new CellPositionEvaluation(pt, ev));
            }

            var max = int.MinValue;
            var selectedPt = default(CellPosition);
            foreach (var pte in pteList)
            {
                if (pte.Evaluation > max)
                {
                    max = pte.Evaluation;
                    selectedPt = pte.Position;
                }
            }
            return selectedPt;
        }
        return SelectCellPositionRandom(cellPositions);
    }
Esempio n. 21
0
 /// <summary>
 /// Highlights the cell.
 /// </summary>
 /// <param name='pos'>
 /// Position.
 /// </param>
 protected void HighlightCell(CellPosition pos)
 {
     this.HighlightCell( pos.Row, pos.Col );
 }
Esempio n. 22
0
    private int Eval(CellPosition pt, Player player, bool isRec)
    {
        var result = 0;
        var mr = Rows - 1;
        var mc = Columns - 1;

        if (pt.IsCorner(mr, mc)) { result += 10; }
        else if (pt.IsCornerAdjacent(mr, mc)) { result -= 3; }
        if (pt.IsBorder(mr, mc)) { result += 5; }

        var rpt = GetReversibleCellPositions(pt, player);
        result += rpt.Length;

        var record = "";
        foreach (var r in _record) record += r.ToString();

        if (isRec)
        {
            GoNext(pt);
            foreach (var ppt in GetPlaceableCellPositions(_currentPlayer))
            {
                var ev = Eval(ppt, _currentPlayer, false);
                if (ev > 10) result -= (ev - 8);
            }
        }

        Reset(CellPosition.ParseList(record));
        
        return result;
    }
Esempio n. 23
0
 private CellPosition SelectCellPositionRandom(CellPosition[] cellPositions)
 {
     var index = Random.Range(0, cellPositions.Length);
     return cellPositions[index];
 }
Esempio n. 24
0
     SelectCellPositionCount(CellPosition[] cellPositions, Player player)
 {
     var max = 0;
     var maxCellPosition = cellPositions[0];
     foreach(var pt in cellPositions)
     {
         var cellPts = GetReversibleCellPositions(pt, player);
         if (cellPts.Length > max)
         {
             max = cellPts.Length;
             maxCellPosition = pt;
         }
     }
     return maxCellPosition;
 }
Esempio n. 25
0
 private void GoNext(CellPosition pt)
 {
     _record += pt.ToString();
     Place(pt.Row, pt.Column, _currentPlayer);
     var other = _currentPlayer.GetOtherPlayer();
     if (HasPlaceableCell(other)) { _currentPlayer = other; }
     else { _isGameOver = !HasPlaceableCell(_currentPlayer); }
 }
Esempio n. 26
0
 private void Reset(CellPosition[] record)
 {
     Reset();
     foreach(var pt in record) { GoNext(pt); }
 }
Esempio n. 27
0
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            if (e.Button == MouseButtons.Left)
            {
                if (Control.ModifierKeys != Keys.Control)
                {
                    this.Rows.UnselectAllCells();
                }
                CellPosition position = GetCell(e.Location);
                position.row.SelectCell(position.col.Name);
                this.first_selected = position;
                onNeedResize();
            }

            this.Focus();
        }
Esempio n. 28
0
 private OthelloCellView CreateCell(CellPosition pt)
 {
     var cell = Instantiate(OthelloCell);
     cell.name = OthelloCell.name + "(" + pt.Row + "," + pt.Column + ")";
     cell.transform.Translate(1.05F * pt.Column, 0, -1.05F * pt.Row);
     cell.transform.parent = gameObject.transform;
     return cell.GetComponent<OthelloCellView>();
 }
Esempio n. 29
0
        public void MoveToCell(GameTime time, CellPosition newPos)
        {
            var now = time.TotalGameTime.TotalSeconds;

            if (newPos != c_position)
            {
                if (World.IsPassable(newPos))
                {
                    _transitionFromPosition = Position;
                    _transitionToPosition = newPos.ToPixelPosition();
                    _transitionStartTime = now;

                    c_position = newPos;

                    _lastMoveTime = now;
                    OnMove(time);
                }
            }
        }
Esempio n. 30
0
        public static List<Point> GetArchOutline(CellPosition cpArchLocation)
        {
            if (cpArchLocation == CellPosition.Left)
                return new List<Point>(new Point[] { new Point ( 0, 8 ), new Point ( 4, 0 ), new Point ( 4, 8 ), new Point ( 3, 10 ), new Point ( 3, 4 ), new Point ( 1, 8 ), new Point ( 1, 14 ),
                    new Point ( 0, 16 ), new Point ( 0, 8 ) });

            else if (cpArchLocation == CellPosition.Front)
                return new List<Point>(new Point[] { new Point ( 4,8), new Point ( 4,0), new Point (12, 0), new Point (12, 8), new Point (10,8), new Point (10,2), new Point ( 6, 2),
                    new Point (6, 8), new Point ( 4,8) });

            else if (cpArchLocation == CellPosition.Right)
                return new List<Point>(new Point[] { new Point ( 12, 0 ), new Point ( 16, 8 ), new Point ( 16, 16 ), new Point ( 15, 14 ), new Point ( 15, 8 ), new Point ( 13, 4 ), new Point ( 13, 10 ),
                    new Point ( 12, 8 ), new Point ( 12, 0 ) });

            return new List<Point>(new Point[] { new Point(0, 0) });
        }