private int pathLength(List <SquareInfo> sqs, SquareInfo source, SquareInfo dest) { var dx = new[] { 0, 1, 0, -1 }; var dy = new[] { -1, 0, 1, 0 }; var q = new Queue <SquareInfo>(); var visited = new HashSet <SquareInfo>(); q.Enqueue(source); source.Distance = 0; while (q.Count > 0) { var item = q.Dequeue(); if (item == dest) { return(item.Distance); } if (!visited.Add(item)) { continue; } for (var dir = 0; dir < 4; dir++) { if (item.Connections[dir]) { var movingTo = sqs.First(sq => sq.X == item.X + dx[dir] && sq.Y == item.Y + dy[dir]); movingTo.Distance = item.Distance + 1; q.Enqueue(movingTo); } } } Debug.LogFormat(@"[Corners #{4}] The breath-first search algorithm did not encounter the destination square. Trying to go from ({0}, {1}) to ({2}, {3})", source.X, source.Y, dest.X, dest.Y, _moduleId); throw new InvalidOperationException(string.Format(@"The breath-first search algorithm did not encounter the destination square. Trying to go from ({0}, {1}) to ({2}, {3})", source.X, source.Y, dest.X, dest.Y)); }
public static PointInt GetPoint(SquareInfo square, long w, int N) { var ddiv = (w / N) - square.MinDiv; var drem = (w % N) - square.MinRem; if (square.Direction == 0) { return(new PointInt((int)(square.Row + ddiv + 1), (int)(square.Col + drem + 1))); } if (square.Direction == 1) { return(new PointInt((int)(square.Row + drem + 1), (int)(square.Col + square.Width - ddiv))); } if (square.Direction == 2) { return(new PointInt((int)(square.Row + square.Width - ddiv), (int)(square.Col + square.Width - drem))); } if (square.Direction == 3) { return(new PointInt((int)(square.Row + square.Width - drem), (int)(square.Col + ddiv + 1))); } return(PointInt.MinusOne); }
public void BoardInitialization_NoMoves_PositionEmptyAt(string position) { Board board = Board.CreateBoard(); SquareInfo emptySquare = board.GetSquareInfo(position); Assert.AreEqual(emptySquare.Piece, Piece.None); }
public void BoardInitialization_NoMoves_BlackPawnAt(string position) { Board board = Board.CreateBoard(); SquareInfo blackPawn = board.GetSquareInfo(position); Assert.AreEqual(PieceColor.Black, blackPawn.PieceColor); }
public void Play_PawnA2A5_PawnRemainsAtA2() { Board board = Board.CreateBoard(); board.Play("A2A5"); SquareInfo a2 = board.GetSquareInfo("A2"); Assert.AreEqual(Piece.Pawn, a2.Piece); }
public void Play_WhitePawnA2B4_WhitePawnRemainsAtA2() { Board board = Board.CreateBoard(); board.Play("A2B4"); SquareInfo a2 = board.GetSquareInfo("A2"); Assert.AreEqual(Piece.Pawn, a2.Piece); Assert.AreEqual(PieceColor.White, a2.PieceColor); }
public static PointInt[] Solve(int N, int[][] commands, long[] K) { var squares = new SquareInfo[commands.Length + 1]; squares[0] = new SquareInfo { Row = 0, Col = 0, Width = N, First = 0, DeltaInRow = 1, DeltaInCol = N, Direction = 0 }; squares[0].CalcExtra(N); for (var i = 1; i <= commands.Length; i++) { var command = commands[i - 1]; var ai = command[0]; var bi = command[1]; var di = command[2]; var prev = squares[i - 1]; var newRow = ai - 1; var newCol = bi - 1; var square = new SquareInfo { Row = newRow, Col = newCol, Width = di + 1, First = prev.First + (newCol - prev.Col) * prev.DeltaInRow + (newRow - prev.Row + di) * prev.DeltaInCol, DeltaInRow = -prev.DeltaInCol, DeltaInCol = prev.DeltaInRow, Direction = i % 4, }; square.CalcExtra(N); squares[i] = square; } var result = new PointInt[K.Length]; for (var i = 0; i < K.Length; i++) { var wi = K[i]; var si = BinarySearchHelper.BinarySearchRightBiased(squares, sq => { var rem = wi % N; var div = wi / N; var contains = sq.MinRem <= rem && rem <= sq.MaxRem && sq.MinDiv <= div && div <= sq.MaxDiv; return(contains ? 0 : 1); }); result[i] = GetPoint(squares[si], wi, N); } return(result); }
public void Play_PawnA2A4_PawnFoundAtA4NotFoundAtA2() { Board board = Board.CreateBoard(); board.Play("A2A4"); SquareInfo a4 = board.GetSquareInfo("A4"); SquareInfo a2 = board.GetSquareInfo("A2"); Assert.AreEqual(Piece.Pawn, a4.Piece); Assert.AreEqual(Piece.None, a2.Piece); }
public void Play_WhitePawnCapturesA2B3_WhitePawnAtB3() { Board board = Board.CreateBoard(); board.PlacePiece(Piece.Pawn, PieceColor.Black, "B3"); board.Play("A2B3"); SquareInfo b3 = board.GetSquareInfo("B3"); Assert.AreEqual(Piece.Pawn, b3.Piece); Assert.AreEqual(PieceColor.White, b3.PieceColor); }
public void Play_BlackPawnCapturesA5B4_BlackPawnAtB4() { Board board = Board.CreateBoard(); board.PlacePiece(Piece.Pawn, PieceColor.White, "B4"); board.PlacePiece(Piece.Pawn, PieceColor.Black, "A5"); board.Play("A5B4"); SquareInfo b4 = board.GetSquareInfo("B4"); Assert.AreEqual(Piece.Pawn, b4.Piece); Assert.AreEqual(PieceColor.Black, b4.PieceColor); }
public void Play_KnightD5D6_NotAllowedKnightRemainsAtD5() { Board board = Board.CreateBoard(); board.PlacePiece(Piece.Knight, PieceColor.White, "D5"); board.Play("D5D6"); SquareInfo d6 = board.GetSquareInfo("D6"); SquareInfo d5 = board.GetSquareInfo("D5"); Assert.AreEqual(Piece.None, d6.Piece); Assert.AreEqual(PieceColor.White, d5.PieceColor); Assert.AreEqual(Piece.Knight, d5.Piece); }
public void Play_KnightD5F6_KnightFoundAtF6AndD5RemainsEmpty() { Board board = Board.CreateBoard(); board.PlacePiece(Piece.Knight, PieceColor.White, "D5"); board.Play("D5F6"); SquareInfo f6 = board.GetSquareInfo("F6"); SquareInfo d5 = board.GetSquareInfo("D5"); Assert.AreEqual(Piece.Knight, f6.Piece); Assert.AreEqual(PieceColor.White, f6.PieceColor); Assert.AreEqual(PieceColor.None, d5.PieceColor); }
public void Play_KnightD5E3_KnightFoundAtE3AndD5RemainsEmpty() { Board board = Board.CreateBoard(); board.PlacePiece(Piece.Knight, PieceColor.White, "D5"); board.Play("D5E3"); SquareInfo e4 = board.GetSquareInfo("E3"); SquareInfo d5 = board.GetSquareInfo("D5"); Assert.AreEqual(Piece.Knight, e4.Piece); Assert.AreEqual(PieceColor.White, e4.PieceColor); Assert.AreEqual(PieceColor.None, d5.PieceColor); }
public void Play_KnightD5C7_KnightFoundAtC7AndD5RemainsEmpty() { Board board = Board.CreateBoard(); board.PlacePiece(Piece.Knight, PieceColor.White, "D5"); board.Play("D5C7"); SquareInfo c7 = board.GetSquareInfo("C7"); SquareInfo d5 = board.GetSquareInfo("D5"); Assert.AreEqual(Piece.Knight, c7.Piece); Assert.AreEqual(PieceColor.White, c7.PieceColor); Assert.AreEqual(PieceColor.None, d5.PieceColor); }
public void Play_PawnA4A2_PawnRemainsAtA4AndA2RemainsEmpty() { Board board = Board.CreateBoard(); board.PlacePiece(Piece.Pawn, PieceColor.White, "A4"); board.PlacePiece(Piece.None, PieceColor.None, "A2"); board.Play("A4A2"); SquareInfo a4 = board.GetSquareInfo("A4"); SquareInfo a2 = board.GetSquareInfo("A2"); Assert.AreEqual(Piece.Pawn, a4.Piece); Assert.AreEqual(Piece.None, a2.Piece); }
private void QueueFlagMoves(Square analyzedSquare) { if ((int)analyzedSquare.type >= 1) { SquareInfo info = GetAdjacentSquaresInfo(analyzedSquare.x, analyzedSquare.y); if ((int)analyzedSquare.type == (info.adjacentHiddenCount + info.adjacentFlagCount)) { List <Square> adjacentHiddenSquares = GetAdjacentSquares(analyzedSquare.x, analyzedSquare.y, SquareType.Hidden); foreach (Square sq in adjacentHiddenSquares) { if (!moveQueue.Contains(new Move { X = sq.x, Y = sq.y, Type = MoveType.Flag })) { moveQueue.Enqueue(new Move { X = sq.x, Y = sq.y, Type = MoveType.Flag }); } } } if ((((int)analyzedSquare.type - info.adjacentFlagCount) == 2) && (info.adjacentHiddenCount == 3)) { List <Square> adjacentCrossSquares = GetAdjacentCrossSquares(analyzedSquare.x, analyzedSquare.y); foreach (Square adjacentSquare in adjacentCrossSquares) { SquareInfo infoAboutAdjacent = GetAdjacentSquaresInfo(adjacentSquare.x, adjacentSquare.y); if ((((int)adjacentSquare.type - infoAboutAdjacent.adjacentFlagCount) == 1) && (infoAboutAdjacent.adjacentHiddenCount == 3)) { List <Square> hiddenAroundAnalyzed = GetAdjacentSquares(analyzedSquare.x, analyzedSquare.y, SquareType.Hidden); List <Square> hiddenAroundAdjacent = GetAdjacentSquares(adjacentSquare.x, adjacentSquare.y, SquareType.Hidden); foreach (Square sq in hiddenAroundAnalyzed) { if (!hiddenAroundAdjacent.Contains(sq)) { if (!moveQueue.Contains(new Move { X = sq.x, Y = sq.y, Type = MoveType.Expose })) { moveQueue.Enqueue(new Move { X = sq.x, Y = sq.y, Type = MoveType.Flag }); } } } } } } } }
public void Play_BlackPawnCapturesA5C3_NotAllowedBlackPawnRemainsAtA3() { Board board = Board.CreateBoard(); board.PlacePiece(Piece.Pawn, PieceColor.Black, "A5"); board.PlacePiece(Piece.Pawn, PieceColor.White, "C3"); board.Play("A5C3"); SquareInfo a5 = board.GetSquareInfo("A5"); SquareInfo c3 = board.GetSquareInfo("C3"); Assert.AreEqual(Piece.Pawn, a5.Piece); Assert.AreEqual(PieceColor.Black, a5.PieceColor); Assert.AreEqual(Piece.Pawn, c3.Piece); Assert.AreEqual(PieceColor.White, c3.PieceColor); }
public void Play_BlackPawnCapturesA3B4_NotAllowedBlackPawnRemainsAtA3() { Board board = Board.CreateBoard(); board.PlacePiece(Piece.Pawn, PieceColor.Black, "A3"); board.PlacePiece(Piece.Pawn, PieceColor.White, "B4"); board.Play("A3B4"); SquareInfo a3 = board.GetSquareInfo("A3"); SquareInfo b4 = board.GetSquareInfo("B4"); Assert.AreEqual(Piece.Pawn, a3.Piece); Assert.AreEqual(PieceColor.Black, a3.PieceColor); Assert.AreEqual(Piece.Pawn, b4.Piece); Assert.AreEqual(PieceColor.White, b4.PieceColor); }
private SquareInfo GetAdjacentSquaresInfo(int X, int Y) { SquareInfo info = new SquareInfo(); List <Square> adjacentSquares = GetAdjacentSquares(X, Y); info.adjacentFlagCount = 0; info.adjacentHiddenCount = 0; foreach (Square sq in adjacentSquares) { if (sq.type == SquareType.Flag) { info.adjacentFlagCount++; } else if (sq.type == SquareType.Hidden) { info.adjacentHiddenCount++; } } return(info); }
void Start() { _moduleId = _moduleIdCounter++; _colorblind = ColorblindMode.ColorblindModeActive; // RULE SEED var rnd = RuleSeedable.GetRNG(); Debug.LogFormat(@"[Corners #{0}] Using rule seed: {1}", _moduleId, rnd.Seed); var skip = rnd.Next(0, 70); for (var i = 0; i < skip; i++) { rnd.Next(); } var snDigits = rnd.ShuffleFisherYates(Enumerable.Range(0, 10).ToList()); snDigits.AddRange(rnd.ShuffleFisherYates(Enumerable.Range(0, 10).ToArray())); var cornerColors = rnd.ShuffleFisherYates(Enumerable.Range(0, 16).ToArray()); var todo = new List <SquareInfo>(); var processed = new List <SquareInfo>(); var done = new List <SquareInfo>(); for (var i = 0; i < 16; i++) { done.Add(new SquareInfo { X = i % 4, Y = i / 4, SnDigit = snDigits[i], CornerColor = cornerColors[i] }); } var startX = rnd.Next(0, 4); var startY = rnd.Next(0, 4); var dx = new[] { 0, 1, 0, -1 }; var dy = new[] { -1, 0, 1, 0 }; for (var directionality = 0; directionality < 2; directionality++) { todo.AddRange(done); done.Clear(); var startIx = todo.IndexOf(sq => sq.X == startX && sq.Y == startY); processed.Add(todo[startIx]); todo.RemoveAt(startIx); while (todo.Count > 0) { var pIx = rnd.Next(0, processed.Count); var x = processed[pIx].X; var y = processed[pIx].Y; var availableConnections = new List <DirectionInfo>(); int destIx; for (var dir = 0; dir < 4; dir++) { if ((destIx = todo.IndexOf(sq => sq.X == x + dx[dir] && sq.Y == y + dy[dir])) != -1) { availableConnections.Add(new DirectionInfo { Direction = dir, DestIx = destIx }); } } if (availableConnections.Count == 0) { done.Add(processed[pIx]); processed.RemoveAt(pIx); } else { var cn = availableConnections[rnd.Next(0, availableConnections.Count)]; if (directionality == 0) { processed[pIx].Connections[cn.Direction] = true; } else { todo[cn.DestIx].Connections[(cn.Direction + 2) % 4] = true; } processed.Add(todo[cn.DestIx]); todo.RemoveAt(cn.DestIx); } } done.AddRange(processed); processed.Clear(); } // END RULE SEED // GENERATE PUZZLE tryAgain: var availableCombs = Enumerable.Range(0, 16).ToList(); var combinations = new List <int>(); SquareInfo startingSq = null; for (var i = 0; i < 4; i++) { int comb; if (i == 0) { var snLD = Bomb.GetSerialNumberNumbers().Last(); startingSq = done.Where(sq => sq.SnDigit == snLD).PickRandom(); comb = startingSq.CornerColor; availableCombs.RemoveAll(cmb => done.Any(sq => sq.SnDigit == snLD && sq.CornerColor == cmb)); } else { comb = availableCombs.PickRandom(); } combinations.Add(comb); availableCombs.RemoveAll(cmb => cmb % 4 == comb % 4); } // We now know which corner is the first to click and which square this corresponds to in the diagram. // Test all permutations of the remaining three and calculate their path lengths. var remainingSquares = done.Where(sq => combinations.Skip(1).Contains(sq.CornerColor)).ToList(); var shortestLength = int.MaxValue; var tie = false; foreach (var permutation in new[] { new[] { remainingSquares[0], remainingSquares[1], remainingSquares[2] }, new[] { remainingSquares[0], remainingSquares[2], remainingSquares[1] }, new[] { remainingSquares[1], remainingSquares[0], remainingSquares[2] }, new[] { remainingSquares[1], remainingSquares[2], remainingSquares[0] }, new[] { remainingSquares[2], remainingSquares[0], remainingSquares[1] }, new[] { remainingSquares[2], remainingSquares[1], remainingSquares[0] } }) { var totalLength = pathLength(done, startingSq, permutation[0]) + pathLength(done, permutation[0], permutation[1]) + pathLength(done, permutation[1], permutation[2]); if (totalLength < shortestLength) { shortestLength = totalLength; tie = false; _solution = new[] { startingSq }.Concat(permutation).Select(sq => sq.CornerColor % 4).ToArray(); } else if (totalLength == shortestLength) { tie = true; } } if (tie) { goto tryAgain; } for (var i = 0; i < 4; i++) { Clamps[combinations[i] % 4].material.color = CornerColors[combinations[i] / 4]; _clampColors[combinations[i] % 4] = combinations[i] / 4; // for Souvenir Debug.LogFormat(@"[Corners #{0}] {1} corner is {2}.", _moduleId, _cornerNames[combinations[i] % 4], new[] { "Red", "Green", "Blue", "Yellow" }[combinations[i] / 4]); Corners[i].OnInteract += CornerClickHandler(i); Colorblind[combinations[i] % 4].text = "RGBY"[combinations[i] / 4].ToString(); Colorblind[combinations[i] % 4].gameObject.SetActive(_colorblind); } Debug.LogFormat(@"[Corners #{0}] Solution is: {1}", _moduleId, _solution.Select(c => _cornerNames[c]).Join(", ")); }
void LoadLevel(int _level) { m_numTiles = 0; m_numTilesJammed = 0; if(_level < 0 || LevelFiles.Count <= _level) { Debug.Log("GridRenderer.Loadlevel: _level out of bounds."); return; } var file = LevelFiles[_level].text; var lines = file.Split ('\n'); bool readingMap = true; m_info = new Dictionary<char, SquareInfo> (); m_indexToInfo = new Dictionary<int, SquareInfo>(); m_map = new int[kGridHeight, kGridWidth]; m_jammed = new bool[kGridHeight, kGridWidth]; int y = 0; foreach (string line in lines) { if (readingMap) { if (line.Length == 0) { readingMap = false; continue; } else if (line.Length == kGridWidth) { // Processing a line of the map for (int i = 0; i < kGridWidth; ++i) { if (line[i] == '.') { // Gap in the grid m_map[y, i] = 0; } else { int id = 0; // Grab the id for the square if (m_info.ContainsKey(line[i])) { id = m_info[line[i]].id; } else { SquareInfo info = new SquareInfo(); info.id = m_nextId++; m_info.Add(line[i], info); m_indexToInfo.Add(info.id, info); id = info.id; info.colour_id = 0; } m_map[y, i] = id; ++m_numTiles; } } } else { // Invalid line } } else { // We are reading the codes string[] codes = line.Trim().Split(' '); if ((codes.Length == 2 || codes.Length == 3) && codes[0].Length == 1) { char ch = codes[0][0]; if (m_info.ContainsKey(ch)) { var info = m_info[ch]; info.name = codes[1]; if (codes.Length == 3) { info.colour_id = m_nextId++; info.colour_name = codes[2]; } m_info[ch] = info; } } } // readingMap? ++y; } // foreach line // Create prefabs m_prefabs = new GameObject[m_nextId-1]; foreach (var infoPair in m_info) { SquareInfo info = infoPair.Value; CreatePrefabTileSprite(info.id, info.name); if (info.colour_id != 0) { CreatePrefabTileSprite(info.colour_id, info.colour_name); } } }
void createShape() { int x = heightMap.GetLength(0) - 1; int y = heightMap.GetLength(1) - 1; vertices = new Vector3[(x + 1) * (y + 1)]; string outputInput = GameObject.FindGameObjectWithTag("output"). GetComponent <UnityEngine.UI.InputField>().text; float output = float.Parse(outputInput); for (int k = 0, i = 0; i < y + 1; i++) { for (int j = 0; j < x + 1; j++) { vertices[k] = new Vector3(j, heightMap[j, i], i); k++; } } colors = new Color[vertices.Length]; for (int i = 0; i < vertices.Length; i++) { colors[i] = gradient.Evaluate(vertices[i].y); } for (int i = 0; i < vertices.Length; i++) { vertices[i].y = vertices[i].y * output; } triangles = new int[x * y * 6]; int vert = 0; SquareInfo[] data = new SquareInfo[x * y]; for (int i = 0; i < y; i++) { for (int j = 0; j < x; j++) { data[i * x + j] = new SquareInfo(vert, x); vert++; } vert++; } ComputeBuffer buffer = new ComputeBuffer(data.Length, 8); buffer.SetData(data); ComputeBuffer outputBuffer = new ComputeBuffer(triangles.Length, 4); outputBuffer.SetData(triangles); int kernel = shader.FindKernel("generateMesh"); shader.SetBuffer(kernel, "dataBuffer", buffer); shader.SetBuffer(kernel, "output", outputBuffer); shader.Dispatch(kernel, data.Length / 32, 1, 1); outputBuffer.GetData(triangles); buffer.Dispose(); outputBuffer.Dispose(); uvs = new Vector2[vertices.Length]; for (int i = 0, k = 0; i < y; i++) { for (int j = 0; j < x; j++) { uvs[k] = new Vector2((float)j / (x * 0.01f), (float)i / (y * 0.01f)); k++; } } if (GameObject.FindGameObjectWithTag("debugtoggle"). GetComponent <UnityEngine.UI.Toggle>().isOn) { print("vert: " + vert); } }
public void Read(TProtocol iprot) { iprot.IncrementRecursionDepth(); try { TField field; iprot.ReadStructBegin(); while (true) { field = iprot.ReadFieldBegin(); if (field.Type == TType.Stop) { break; } switch (field.ID) { case 1: if (field.Type == TType.Struct) { CategoryItem = new CategoryItem(); CategoryItem.Read(iprot); } else { TProtocolUtil.Skip(iprot, field.Type); } break; case 2: if (field.Type == TType.Struct) { SpotItem = new SpotItem(); SpotItem.Read(iprot); } else { TProtocolUtil.Skip(iprot, field.Type); } break; case 3: if (field.Type == TType.Struct) { ProductItem = new ProductSearchSummary(); ProductItem.Read(iprot); } else { TProtocolUtil.Skip(iprot, field.Type); } break; case 4: if (field.Type == TType.Struct) { ServiceItem = new ServiceItem(); ServiceItem.Read(iprot); } else { TProtocolUtil.Skip(iprot, field.Type); } break; case 5: if (field.Type == TType.Struct) { YellowpageItem = new YellowpageItem(); YellowpageItem.Read(iprot); } else { TProtocolUtil.Skip(iprot, field.Type); } break; case 6: if (field.Type == TType.Struct) { OaItem = new BuddySearchResult(); OaItem.Read(iprot); } else { TProtocolUtil.Skip(iprot, field.Type); } break; case 7: if (field.Type == TType.Struct) { GeoAddressItem = new GeoAddressItem(); GeoAddressItem.Read(iprot); } else { TProtocolUtil.Skip(iprot, field.Type); } break; case 8: if (field.Type == TType.Struct) { ShortcutItem = new ShortcutItem(); ShortcutItem.Read(iprot); } else { TProtocolUtil.Skip(iprot, field.Type); } break; case 9: if (field.Type == TType.Struct) { SquareItem = new SquareInfo(); SquareItem.Read(iprot); } else { TProtocolUtil.Skip(iprot, field.Type); } break; case 10: if (field.Type == TType.Struct) { SquareCategoryItem = new SQCat(); SquareCategoryItem.Read(iprot); } else { TProtocolUtil.Skip(iprot, field.Type); } break; default: TProtocolUtil.Skip(iprot, field.Type); break; } iprot.ReadFieldEnd(); } iprot.ReadStructEnd(); } finally { iprot.DecrementRecursionDepth(); } }