private static void CountDir(Piece target, int x, int y, ref int count, Piece[,] source, int dirX, int dirY, ref bool[,] map) { //範囲外や捜査済みなら終了 if (x < 0 || x >= source.GetLength(0) || y < 0 || y >= source.GetLength(1)) { return; } if (map[x, y]) { return; } map[x, y] = true; //自分の位置に対象の駒がおいてあれば+、そうでなければ捜査終了 if (source[x, y] == target) { count++; } else { return; } CountDir(target, x - dirX, y - dirY, ref count, source, dirX, dirY, ref map); CountDir(target, x + dirX, y + dirY, ref count, source, dirX, dirY, ref map); }
public void DrawBoard(Player player) { for (int y = board.GetLength(1) - 1; y >= 0; y--) { Console.Write("{0} ", y); for (int x = 0; x < board.GetLength(0); x++) { if (board[x, y] != null) { Console.Write(board[x, y] + " "); } else { Console.Write("* "); } } Console.WriteLine(); } Console.WriteLine(); Console.Write(" "); for (int i = 0; i < board.GetLength(0); i++) { Console.Write("{0} ", i); } Console.WriteLine(); }
private Vector2 GetScore(Piece[,] board) { Vector2 scores = new Vector2(); int white = 0; int black = 0; for (int i = 0; i < board.GetLength(0); i++) { for (int j = 0; j < board.GetLength(1); j++) { if (board[i, j] && board[i, j].isWhite) { white++; } else if (board[i, j] && !board[i, j].isWhite) { black++; } } } whiteScore = white; blackScore = black; scores.x = whiteScore; scores.y = blackScore; if (whiteScore == 0 || blackScore == 0 || whiteScore + blackScore == 64) { EndGame(); return(scores); } return(scores); }
public static void ExtendedNaive(ref Piece[,] states) { int row = states.GetLength(0); int column = states.GetLength(1); for (int z = 0; z <= rnd.Next(maxRnd); z++) { for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { int randomRow = rnd.Next(row); int randomColumn = rnd.Next(column); Piece temp = states[randomRow, randomColumn]; states[randomRow, randomColumn] = states[i, j]; states[i, j] = temp; } } for (int i = 0; i < column; i++) { for (int j = 0; j < row; j++) { int randomRow = rnd.Next(row); int randomColumn = rnd.Next(column); Piece temp = states[randomRow, randomColumn]; states[randomRow, randomColumn] = states[j, i]; states[j, i] = temp; } } } }
public override List <byte[][]> GetAttack(Piece[,] board) { // Assume cannot move List <byte[][]> moves = new List <byte[][]>(); // Find piece byte[] pos = GetPos(board); // Elephant movement for (byte seed = 0; seed < 8; seed++) { sbyte[] dir = { (sbyte)(((seed % 2) * 2) - 1), (sbyte)((((byte)(seed / 4)) * 2) - 1) }; sbyte mag = (sbyte)(((seed / 2) % 2) + 2); byte[] finalDir = { (byte)(pos[0] + (dir[0] * mag)), (byte)(pos[1] + (dir[1] * mag)) }; if (finalDir[0] < 0 || finalDir[0] >= board.GetLength(0) || finalDir[1] < 0 || finalDir[1] >= board.GetLength(1)) { continue; } if (board[finalDir[0], finalDir[1]] != null) { if (board[finalDir[0], finalDir[1]].team != this.team) { byte[][] steps = new byte[3][]; steps[0] = new byte[] { finalDir[0], finalDir[1], byte.MaxValue, byte.MaxValue }; steps[1] = new byte[] { finalDir[0], finalDir[1], pos[0], pos[1] }; steps[2] = new byte[] { pos[0], pos[1], byte.MaxValue, byte.MaxValue - 1 }; moves.Add(steps); } } } // Machine's movement for (byte seed = 0; seed < 8; seed++) { sbyte[] dir = { (sbyte)(((seed / 4) + 1) % 2), (sbyte)(seed / 4) }; dir[0] *= (sbyte)(dir[0] * (((seed / 2) % 2) * 2) - 1); dir[1] *= (sbyte)(dir[1] * ((((seed / 2) + 1) % 2) * 2) - 1); sbyte mag = (sbyte)((seed % 2) + 2); byte[] finalDir = { (byte)(pos[0] + (dir[0] * mag)), (byte)(pos[1] + (dir[1] * mag)) }; if (finalDir[0] < 0 || finalDir[0] >= board.GetLength(0) || finalDir[1] < 0 || finalDir[1] >= board.GetLength(1)) { continue; } if (board[finalDir[0], finalDir[1]] != null) { if (board[finalDir[0], finalDir[1]].team != this.team) { byte[][] steps = new byte[3][]; steps[0] = new byte[] { finalDir[0], finalDir[1], byte.MaxValue, byte.MaxValue }; steps[1] = new byte[] { finalDir[0], finalDir[1], pos[0], pos[1] }; steps[2] = new byte[] { pos[0], pos[1], byte.MaxValue, byte.MaxValue - 1 }; moves.Add(steps); } } } return(moves); }
public static void FisherYates(ref Piece[,] states) { int row = states.GetLength(0); int column = states.GetLength(1); for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { int randomRow = i + (int)(rnd.NextDouble() * (row - i)); int randomColumn = j + (int)(rnd.NextDouble() * (column - j)); /*Console.WriteLine("sebelum berubah"); * Console.WriteLine(i.ToString()+","+j.ToString()); * Console.WriteLine(states[randomRow, randomColumn].index); * Console.WriteLine(states[i, j].index);*/ Piece temp = states[randomRow, randomColumn]; states[randomRow, randomColumn] = states[i, j]; states[i, j] = temp; /*Console.WriteLine("sesudah berubah"); * Console.WriteLine(states[randomRow, randomColumn].index); * Console.WriteLine(states[i, j].index);*/ } } }
void CreateTiles(Piece[,] piece) { for (int j = 0; j < piece.GetLength(0); ++j) { for (int i = 0; i < piece.GetLength(1); ++i) { GameObject tile = Instantiate(tileMesh); tile.transform.parent = tilesContainer.transform; tile.name = i + "_" + j + "_tile"; float width = tileSize.x; float height = tileSize.y; float offset = 0; if (i % 2 == 1) { offset = -height / 2; } tile.transform.localPosition = new Vector3(width * i, 0, -height * j + offset); tile.transform.localRotation = Quaternion.Euler(new Vector3(90, 0, 0)); tile.GetComponent <Tile>().SetShape(piece [j, i].Shape); tile.GetComponent <Tile>().SetEnable(false); tile.GetComponent <Tile>().SetUsed(false); //Cubes.Add (tile); cubes.Add(tile.name, tile); } } }
public void ExecuteSolution() { editorManager.SetGameState(EditorManager.GameState.Solving); pieceBoard = boardManager.pieceBoard; PieceType[] board = new PieceType[pieceBoard.Length]; int k = 0; for (int i = pieceBoard.GetLength(1) - 1; i >= 0; i--) { for (int j = 0; j < pieceBoard.GetLength(0); j++) { Piece x = pieceBoard[j, i]; board[k++] = x && x.GetPieceType() != PieceType.objective ? x.GetPieceType() : PieceType.empty; } } LevelSolver newLevelSolver = new LevelSolver(pieceBoard.GetLength(0), pieceBoard.GetLength(1), board); newLevelSolver.SetGoals(new Tuple <int, int>(pieceBoard.GetLength(0) - (int)boardManager.GetObjectivePos().y - 1, (int)boardManager.GetObjectivePos().x)); List <InputHandler.MoveDirection> moveDirections = newLevelSolver.Solve(); if (moveDirections.Count > 0) { print(String.Join(" ", moveDirections.Select(x => x.ToString()))); SaveLevelTemp(moveDirections); } else { print("Nao tem solução!"); } editorManager.SetGameState(EditorManager.GameState.InGame); }
public override List <Coordinates> GetPossibleMoves(Piece[,] GameBoard, Coordinates Coords) { List <Coordinates> CoordList = new List <Coordinates>(); Color OppositeColor; if (PieceColor == Color.Black) { OppositeColor = Color.White; } else { OppositeColor = Color.Black; } /*Si aucune pièce n'est présente en [X, Y] ou qu'une pièce peut être mangée, * et que le roi ne se trouve pas au bord du plateau, * ajouter le mouvement à la liste*/ foreach (Coordinates Move in Moves) { if ((Coords.X + Move.X >= 0 && Coords.X + Move.X < GameBoard.GetLength(0)) && (Coords.Y + Move.Y >= 0 && Coords.Y + Move.Y < GameBoard.GetLength(1)) && (GameBoard[Coords.X + Move.X, Coords.Y + Move.Y] == null || GameBoard[Coords.X + Move.X, Coords.Y + Move.Y].PieceColor == OppositeColor)) { CoordList.Add(new Coordinates(Coords.X + Move.X, Coords.Y + Move.Y)); } } return(CoordList); }
public void CreateRandomLevel() { boardManager.CleanEverything(); boardManager.CreateBoard(6); pieceBoard = boardManager.pieceBoard; //creating random movable objects for (int i = 0; i < (int)pieceBoard.GetLength(0) / 2; i++) { Vector2 randomPos = new Vector2((int)UnityEngine.Random.Range(0, pieceBoard.GetLength(0)), (int)UnityEngine.Random.Range(0, pieceBoard.GetLength(0))); while (pieceBoard[(int)randomPos.x, (int)randomPos.y] != null) { randomPos = new Vector2((int)UnityEngine.Random.Range(0, pieceBoard.GetLength(0)), (int)UnityEngine.Random.Range(0, pieceBoard.GetLength(0))); } pieceBoard[(int)randomPos.x, (int)randomPos.y] = boardManager.InstantiateGameObject(PieceType.normal, new Vector2((int)randomPos.x, (int)randomPos.y)); } //creating random static objects for (int j = 0; j < (int)pieceBoard.GetLength(0) / 2; j++) { Vector2 randomPos = new Vector2((int)UnityEngine.Random.Range(0, pieceBoard.GetLength(0)), (int)UnityEngine.Random.Range(0, pieceBoard.GetLength(0))); while (pieceBoard[(int)randomPos.x, (int)randomPos.y] != null) { randomPos = new Vector2((int)UnityEngine.Random.Range(0, pieceBoard.GetLength(0)), (int)UnityEngine.Random.Range(0, pieceBoard.GetLength(0))); } pieceBoard[(int)randomPos.x, (int)randomPos.y] = boardManager.InstantiateGameObject(PieceType.statice, new Vector2((int)randomPos.x, (int)randomPos.y)); } //creating goal piece { Vector2 randomPos = new Vector2((int)UnityEngine.Random.Range(0, pieceBoard.GetLength(0)), (int)UnityEngine.Random.Range(0, pieceBoard.GetLength(0))); while (pieceBoard[(int)randomPos.x, (int)randomPos.y] != null) { randomPos = new Vector2((int)UnityEngine.Random.Range(0, pieceBoard.GetLength(0)), (int)UnityEngine.Random.Range(0, pieceBoard.GetLength(0))); } pieceBoard[(int)randomPos.x, (int)randomPos.y] = boardManager.InstantiateGameObject(PieceType.goal, new Vector2((int)randomPos.x, (int)randomPos.y)); } //Create the main piece { Vector2 randomPos = new Vector2((int)UnityEngine.Random.Range(0, pieceBoard.GetLength(0)), (int)UnityEngine.Random.Range(0, pieceBoard.GetLength(0))); while (pieceBoard[(int)randomPos.x, (int)randomPos.y] != null) { randomPos = new Vector2((int)UnityEngine.Random.Range(0, pieceBoard.GetLength(0)), (int)UnityEngine.Random.Range(0, pieceBoard.GetLength(0))); } pieceBoard[(int)randomPos.x, (int)randomPos.y] = boardManager.InstantiateGameObject(PieceType.objective, new Vector2((int)randomPos.x, (int)randomPos.y)); boardManager.SetGoalPiecePos(new Vector2((int)randomPos.x, (int)randomPos.y)); } Invoke("ExecuteSolution", 0.1f); }
private IEnumerable <Location> AllBoard() { return (from y in Enumerable.Range(0, cells.GetLength(0)) from x in Enumerable.Range(0, cells.GetLength(1)) select new Location(x, y)); }
public bool CheckForCheckmate(Piece king) { //Check for check if (!CheckForCheck(king)) { return(false); } //Check for mate List <Piece> team = king.Color == "black" ? blackTeam : whiteTeam; Vector2Int to = new Vector2Int(0, 0); for (int i = 0; i < team.Count; i++) { //Go through all board squares //check CheckMove for each square and piece for (int j = 0; j < boardState.GetLength(0); j++) { for (int k = 0; k < boardState.GetLength(1); k++) { to.x = j; to.y = k; if (MovePieceIsLegal(GetPiecePosition(team[i]), to)) { //not checkmate return(false); } } } } //checkmate return(true); }
/// <summary> /// Displays all available pieces that are available for movement. /// </summary> /// <param name="currentPlayer">State to check for.</param> public void ShowAvailableStates(State currentPlayer) { int number = 0; // This will run through the board and // check which ones can move. Printing // out the relevant pieces. Console.WriteLine("\nAvailable pieces for movement:"); for (int x = 0; x < board.GetLength(0); x++) { for (int y = 0; y < board.GetLength(1); y++) { Position position = new Position(x, y); number++; if (IsOccupied(position)) { if (GetPiece(position).State == currentPlayer) { if (CanMoveAtAll(position)) { Console.WriteLine($"({number}) - {x}, {y}"); } } } } } Console.WriteLine(); }
public static bool InBounds(Piece[,] board, Vector2 p) { var maxX = board.GetLength(0); var maxY = board.GetLength(1); return(p.x >= 0 && p.x < maxX && p.y >= 0 && p.y < maxY); }
public override List <byte[][]> GetMove(Piece[,] board) { // Assume cannot move List <byte[][]> move = new List <byte[][]>(); byte[] pos = GetPos(board); for (byte seed = 0; seed < 4; seed++) { sbyte[] delta = { (sbyte)(seed / 2), (sbyte)((((seed / 2)) + 1) % 2) }; delta[0] = (sbyte)((((seed % 2) * 2) - 1) * delta[0]); delta[1] = (sbyte)((((seed % 2) * 2) - 1) * delta[1]); for (int i = 1; i < board.GetLength(0) + board.GetLength(1); i++) { byte[] newPos = new byte[] { ((byte)(pos[0] + (i * delta[0]))), ((byte)(pos[1] + (i * delta[1]))) }; if (!(newPos[0] < 0 || board.GetLength(0) <= newPos[0] || newPos[1] < 0 || board.GetLength(1) <= newPos[1])) { if (board[newPos[0], newPos[1]] != null) { if (board[newPos[0], newPos[1]].team != this.team) { byte[][] spot = new byte[3][]; spot[0] = new byte[] { newPos[0], newPos[1], byte.MaxValue, byte.MaxValue }; spot[1] = new byte[] { newPos[0], newPos[1], pos[0], pos[1] }; spot[2] = new byte[] { pos[0], pos[1], byte.MaxValue, byte.MaxValue - 1 }; move.Add(spot); break; } else { break; } } else { byte[][] spot = new byte[2][]; spot[0] = new byte[] { newPos[0], newPos[1], pos[0], pos[1] }; spot[1] = new byte[] { pos[0], pos[1], byte.MaxValue, byte.MaxValue - 1 }; move.Add(spot); } } } } // Castling if (!Game1.KingInCheck(team, board)) { byte[][] castleMove = GetCastle(board); if (castleMove != null) { move.Add(castleMove); } } return(move); }
public static void ClearLegalMoves(Piece[,] Board) { for (int x = 0; x < Board.GetLength(0); x++) { for (int y = 0; y < Board.GetLength(1); y++) { Board[x, y].legalMove = false; } } }
public Piece GetPieceAt(int gridx, int gridy) { if (gridx >= 0 && gridy >= 0 && gridx < m_pieces.GetLength(0) && gridy < m_pieces.GetLength(1)) { return(m_pieces[gridx, gridy]); } else { return(null); } }
internal static Piece[,] CopyBoard(Piece[,] copy) { Piece[,] newB = new Piece[copy.GetLength(0), copy.GetLength(1)]; for (int x = 0; x < copy.GetLength(0); x++) { for (int y = 0; y < copy.GetLength(1); y++) { newB[x, y] = copy[x, y]; } } return(newB); }
private Vector2 SelectSquare(int x, int y, Piece[,] board) { //Out of bounds if (x < 0 || x >= board.GetLength(0) || y < 0 || y >= board.GetLength(1)) { return(new Vector2()); } Vector2 chosenSquare = new Vector2(x, y); return(chosenSquare); }
public static string WriteGameBoard(Piece[,] pieces) { string result = ""; for (int y = 0; y < pieces.GetLength(1); y++) { for (int x = 0; x < pieces.GetLength(0); x++) { result += WritePiece(pieces[x, y]) + " "; } } return("H" + " " + result.Substring(0, result.Length - 1) + "|"); }
public override IEnumerable <BoardGames.Piece <Vector2i> > GetPieces() { for (int y = 0; y < theBoard.GetLength(1); ++y) { for (int x = 0; x < theBoard.GetLength(0); ++x) { if (theBoard[x, y] != null) { yield return(theBoard[x, y]); } } } }
/// <summary> /// fill the grid with blank spaces /// </summary> private void InstantiateGrid() { Grid = new Piece[6, 7]; for (int r = 0; r < Grid.GetLength(0); r++) { for (int c = 0; c < Grid.GetLength(1); c++) { Grid[r, c] = new Piece { Id = 0, Icon = 'O', Color = ConsoleColor.Red }; } } }
private Piece[,] CopyPieces() { Piece[,] tar; tar = new Piece[pieces.GetLength(0), pieces.GetLength(1)]; for (int i = 0; i < pieces.GetLength(0); ++i) { for (int j = 0; j < pieces.GetLength(1); j++) { tar[i, j] = pieces[i, j].Clone(); } } return(tar); }
public override Game1.GameState Move(Piece[,] board, int code) { byte[] currPos = GetPos(board); // X == 0 || X == board.GetLength(0)-1 byte supposeY = (byte)(((team + 1) % 2) * (board.GetLength(1) - 1)); if (currPos[1] == supposeY && (currPos[0] == 0 || currPos[0] == (board.GetLength(0) - 1))) { listening = true; return(Game1.GameState.prom); } return(Game1.GameState.blank); }
private void SelectPiece(int x, int y) { // Out of bounds if (x < 0 || x >= pieces.GetLength(0) || y < 0 || y >= pieces.GetLength(1)) { return; } Piece p = pieces[x, y]; if (p != null && p.isWhite == isWhite) { if (forcedPieces.Count == 0) { selectedPiece = p; startDrag = mouseOver; } else { // look for the piece under our forced pieces list if (forcedPieces.Find(fp => fp == p) == null) { return; } selectedPiece = p; startDrag = mouseOver; } } }
/// <summary> /// 指定した場所に駒を置く /// </summary> /// <returns>設置できたかどうか</returns> public bool SetPiece(int x, int y, ref Piece[,] board) { if (x < 0 || x >= board.GetLength(0) || y < 0 || y >= board.GetLength(1)) { return(false); } if (board[x, y] != Piece.Null) { return(false); } board[x, y] = Turn; return(true); }
private int[] GetKingLocation() { int[] cord = new int[2]; int cond = (playerSwitch) ? 0 : 1; for (int i = 0; i < BoardArray.GetLength(0); i++) { for (int x = 0; x < BoardArray.GetLength(0); x++) { if (BoardArray[i, x] != null) { if (playerSwitch && BoardArray[i, x].Color == cond) { if ((string)BoardDisplay[i, x].Content == "K") { cord[0] = i; cord[1] = x; } } else if (!playerSwitch && BoardArray[i, x].Color == cond) { if ((string)BoardDisplay[i, x].Content == "k") { cord[0] = i; cord[1] = x; } } } } } return(cord); }
static void PawnCheck(Piece selectedPiece, int currentPlayer, Piece[,] Board, bool enableMove) { if (selectedPiece.player == 2 && currentPlayer == 2) //Moving conditions for player 2 { if (Board[selectedPiece.x, selectedPiece.y + 1].pieceType == PieceType.None && enableMove == true) { Board[selectedPiece.x, selectedPiece.y + 1].legalMove = enableMove; if (selectedPiece.y == 1) { if (Board[selectedPiece.x, selectedPiece.y + 2].pieceType == PieceType.None) { Board[selectedPiece.x, selectedPiece.y + 2].legalMove = enableMove; } } } //Attack diagonally if (selectedPiece.x < Board.GetLength(0) - 1 && (Board[selectedPiece.x + 1, selectedPiece.y + 1].player == 1 || enableMove == false)) { Board[selectedPiece.x + 1, selectedPiece.y + 1].legalMove = enableMove; } if (selectedPiece.x > 0 && (Board[selectedPiece.x - 1, selectedPiece.y + 1].player == 1 || enableMove == false)) { Board[selectedPiece.x - 1, selectedPiece.y + 1].legalMove = enableMove; } } // Moving conditions for player 1 else if (selectedPiece.player == 1 && currentPlayer == 1) { if (Board[selectedPiece.x, selectedPiece.y - 1].pieceType == PieceType.None && enableMove == true) { Board[selectedPiece.x, selectedPiece.y - 1].legalMove = enableMove; if (selectedPiece.y == 6) { if (Board[selectedPiece.x, selectedPiece.y - 2].pieceType == PieceType.None) { Board[selectedPiece.x, selectedPiece.y - 2].legalMove = enableMove; } } } // Attack diagonally if (selectedPiece.x < Board.GetLength(0) - 1 && (Board[selectedPiece.x + 1, selectedPiece.y - 1].player == 2 || enableMove == false)) { Board[selectedPiece.x + 1, selectedPiece.y - 1].legalMove = enableMove; } if (selectedPiece.x > 0 && (Board[selectedPiece.x - 1, selectedPiece.y - 1].player == 2 || enableMove == false)) { Board[selectedPiece.x - 1, selectedPiece.y - 1].legalMove = enableMove; } } }
private void intial() { for (int i = 0; i < board.GetLength(0); i++) { for (int j = 0; j < board.GetLength(1); j++) { board[i, j] = new Piece(j * 100, i * 100); board[i, j].Click += play; board[i, j].Cursor = Cursors.Hand; Controls.Add(board[i, j]); } } drawScore(); }
public bool IsValidPlacement(Vec2 position) { if (turnCount == 2) { Vec2 relativePosition = position - (new Vec2(Grid.GetLength(0), Grid.GetLength(1)) / 2); if (relativePosition.x < 3 && relativePosition.x > -3 && relativePosition.y < 3 && relativePosition.y > -3) { return(false); } } return(Grid.Get(position).Equals(Piece.EMPTY)); }