public void PutCell(OthelloCell cell, bool isHuman = true) { if (isHuman) { // 人間が打った時 if (CurrentTurn == 0) { // 0 (白)のときは無効にする return; } } // どれかが反転されたかどうか オーバーロード(多重定義で実現している) bool isReverse = CheckAndReverse(OthelloCells, cell, CurrentTurn, true); if (isReverse) { int x = (int)cell.Location.x; int y = (int)cell.Location.y; char xPos = (char)('A' + x); string yPos = (8 - y).ToString(); history.Add(xPos + yPos); TurnEnd(); } }
private void ChangeOwnerBetween(OthelloCell from, OthelloCell to, Vector2 directionVector) { for (Vector2 location = from.Location + directionVector; location != to.Location; location += directionVector) { OthelloCells[(int)location.x, (int)location.y].OwnerID = CurrentTurn; } }
void Start() { currentField = new OthelloCell[BoardSize, BoardSize]; for (int i = 0; i < BoardSize; i++) { for (int j = 0; j < BoardSize; j++) { currentField[i, j] = new OthelloCell(); } } instance = this; OthelloBoardIsSquareSize(); OthelloCells = new OthelloCell[BoardSize, BoardSize]; float cellAnchorSize = 1.0f / BoardSize; for (int y = 0; y < BoardSize; y++) { for (int x = 0; x < BoardSize; x++) { CreateNewCell(x, y, cellAnchorSize); } } // Nextを出す CreateNewCell(10, 6, cellAnchorSize, false); ScoreBoard.GetComponent <RectTransform>().SetSiblingIndex(BoardSize * BoardSize + 1); GameObject.Destroy(Template); InitializeGame(); }
internal void PlaceHere(OthelloCell othelloCell) { for (int direction = 0; direction < DirectionList.Count; direction++) { Vector2 directionVector = DirectionList[direction]; OthelloCell onOtherSide = FindAllyChipOnOtherSide(directionVector, othelloCell.Location, false); if (onOtherSide != null) { ChangeOwnerBetween(othelloCell, onOtherSide, directionVector); } } OthelloCells[(int)othelloCell.Location.x, (int)othelloCell.Location.y].OwnerID = CurrentTurn; OthelloCells[(int)othelloCell.Location.x, (int)othelloCell.Location.y].TurnNumber++; if (CurrentTurn == 0) { WhiteChipNumber--; } else { BlackChipNumber--; } WhiteChipNumberText.text = "Stones\n\n" + WhiteChipNumber; BlackChipNumberText.text = "Stones\n\n" + BlackChipNumber; }
bool CheckAndReverse(OthelloCell[,] field, OthelloCell cell, int turn, bool doReverse) { bool isReverse = false; if (cell.OwnerID != -1) { // すでに置かれていたところは置けない return(false); } for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { if (i == 0 && j == 0) { continue; } isReverse |= CheckAndReverse(field, cell, i, j, turn, doReverse); } } return(isReverse); }
private void CreateNewCell(int x, int y, float cellAnchorSize) { GameObject go = GameObject.Instantiate(Template, this.transform); RectTransform r = go.GetComponent <RectTransform>(); r.anchorMin = new Vector2(x * cellAnchorSize, y * cellAnchorSize); r.anchorMax = new Vector2((x + 1) * cellAnchorSize, (y + 1) * cellAnchorSize); OthelloCell oc = go.GetComponent <OthelloCell>(); OthelloCells[x, y] = oc; oc.Location.x = x; oc.Location.y = y; }
internal void PlaceHere(OthelloCell othelloCell) { for (int direction = 0; direction < DirectionList.Count; direction++) { Vector2 directionVector = DirectionList[direction]; OthelloCell onOtherSide = FindAllyChipOnOtherSide(directionVector, othelloCell.Location, false); if (onOtherSide != null) { ChangeOwnerBetween(othelloCell, onOtherSide, directionVector); } } OthelloCells[(int)othelloCell.Location.x, (int)othelloCell.Location.y].OwnerID = CurrentTurn; }
async Task PositionDiscToCell(OthelloDisc odisc, OthelloCell ocell) { ViewExtensions.CancelAnimations(odisc.View); double jumpHeight = 40; bool cancelled = false; var rect1 = odisc.View.Bounds; if (IsPortrait) { if (odisc.View.InitialColor == OthelloColor.White) { rect1.X += jumpHeight; } else { rect1.X -= jumpHeight; } } else { rect1.Y -= jumpHeight; } var finalRect = new Rectangle(); finalRect.Left = ocell.View.Bounds.Left; finalRect.Top = ocell.View.Bounds.Top; finalRect.Width = ocell.View.Bounds.Width - 0; finalRect.Height = ocell.View.Bounds.Height - 0; if (_debug) { Debug.WriteLine($"PositionDiscToCell disc={odisc.Item} cell={ocell.Item}"); } _layout.RaiseChild(odisc.View); if (!odisc.InUse) { cancelled = await odisc.View.LayoutTo(rect1, 200, Easing.Linear); } odisc.View.IsFlat = false; cancelled = await odisc.View.LayoutTo(finalRect, 600, Easing.SpringOut); if (!cancelled) { AbsoluteLayout.SetLayoutBounds(odisc.View, finalRect); odisc.InUse = true; } return; }
async Task ProcessCell(OthelloCell ocell) { if (_debug) { Debug.WriteLine($"ProcessCell Start {ocell.Item}"); } ocell.IsProcessing = true; if (IsAnimating) { await Task.Delay(new Random().Next(200)); } BusyCount++; if (ocell.View.Disc != null) { OthelloDisc odisc; if (_discs.TryGetValue(ocell.View.Disc, out odisc)) { if (ocell.OthelloDisc != odisc) { if (ocell.OthelloDisc != null) { ocell.OthelloDisc.IsProcessing = true; } await MoveDiscFromCellToStack(ocell); if (ocell.OthelloDisc != null) { ocell.IsProcessing = false; } } odisc.IsProcessing = true; await MoveDiscToCell(odisc, ocell); odisc.IsProcessing = false; } } // might need to remove ocell.InUse from here else if (ocell.View.Disc == null) // && ocell.InUse && ocell.OthelloDisc!=null) { await MoveDiscFromCellToStack(ocell); } BusyCount--; ocell.IsProcessing = false; if (_debug) { Debug.WriteLine($"ProcessCell End {ocell.Item}"); } }
async Task MoveDiscToCell(OthelloDisc odisc, OthelloCell ocell) { if (_debug) { Debug.WriteLine($"MoveDiscToCell Started Cell={ocell.Item} Disc={odisc.Item}"); } odisc.View.ActualColor = odisc.View.DiscColor; ocell.InUse = true; ocell.OthelloDisc = odisc; await PositionDiscToCell(odisc, ocell); if (_debug) { Debug.WriteLine($"MoveDiscToCell Finished Cell={ocell.Item} Disc={odisc.Item}"); } }
public void Click(OthelloCell clicked) { if (state != GameState.WaitingPlayer) { return; } if (!myInfo.turn) { return; } if (clicked.reversible.Max() != 0) { Flip(clicked.x, clicked.y, counter.isBlack); Core.Tell(encounterInfo.pName, string.Format("{0},{1}", clicked.x, clicked.y)); myInfo.ChangeTurn(); encounterInfo.ChangeTurn(); } Debug.Log(clicked.reversible.Max()); }
async Task MoveDiscFromCellToStack(OthelloCell ocell) { if (_debug) { Debug.WriteLine($"MoveDiscFromCellToStack Started Cell={ocell.Item} OthelloDisc={ocell.OthelloDisc}"); } if (ocell.OthelloDisc == null) { return; } ocell.InUse = false; await MoveDiscToStack(ocell.OthelloDisc); ocell.OthelloDisc = null; if (_debug) { Debug.WriteLine($"MoveDiscFromCellToStack Finished Cell={ocell.Item} OthelloDisc={ocell.OthelloDisc}"); } }
void BuildLayout() { if (CellItemsSource == null || DiscItemsSource == null) { return; } _layout.Children.Clear(); _layout.Children.Add(_gridBox); foreach (var item in CellItemsSource) { CellView view = new CellView(); if (view != null) { view.BindingContext = item; _layout.Children.Add(view); var cell = new OthelloCell(view, ProcessCell); cell.Item = item; _cells.Add(item, cell); view.PropertyChanged += CellView_PropertyChanged; var tap = new TapGestureRecognizer(); tap.Command = new Command(v => { CellTappedCommand?.Execute(v); return; }); tap.CommandParameter = item; view.GestureRecognizers.Add(tap); } } foreach (var item in DiscItemsSource) { var view = new DiscView(); view.BindingContext = item; _layout.Children.Add(view); var disc = new OthelloDisc(view, ProcessDisc); disc.Item = item; _discs.Add(item, disc); view.PropertyChanged += DiscView_PropertyChanged; } }
// ひっくり返せたら trueを返す ひっくり返せなかったらfalseを返す bool CheckAndReverse(OthelloCell[,] field, OthelloCell cell, int dx, int dy, int turn, bool doReverse) { // 一つ右横がOwnerIDが違うかどうか x,yは確認したいセル // 添字はint 型しか使えないので キャスト というもので変換している。 int x = (int)cell.Location.x; int y = (int)cell.Location.y; // 相手の駒を見たかどうかの変数 bool isChecked = false; while (true) { // 1つとなりにする。 x += dx; y += dy; if (x >= 0 && x < BoardSize && y >= 0 && y < BoardSize) { // 何もしない } else { // ゲームの範囲外だったら終了 return(false); } if (field[x, y].OwnerID == -1) { // 何も置かれていないので終了 return(false); } else if (field[x, y].OwnerID != turn) { // 相手の駒が置いてある時 「相手の駒を見た」というフラグをたてる isChecked = true; } else if (field[x, y].OwnerID == turn) { // 自分の色の場合 // すでに相手を見ていた場合 if (isChecked) { if (doReverse) { // 囲めるので置いたマスから一つ左のマスを自分の色にする int sx = (int)cell.Location.x; int sy = (int)cell.Location.y; while (sx != x || sy != y) { // [x,y]じゃないのに注意 field[sx, sy].OwnerID = turn; sx += dx; sy += dy; } } // ひっくり返せるときはtrueにして関数を抜ける return(true); } else { // フラグが立っていなかったら、囲めないので処理終了 return(false); } } } }