public void AddTower(TowerBlock towerBlock) { if (GetTowerCount() >= GetMaxAllowedCount()) { print("Dequeing a new Tower!!"); Tower newTower = towers.Dequeue(); newTower.ReleaseTowerblock(); newTower.OccupyTowerBlock(towerBlock); newTower.transform.position = GetDefaultBaseV3(towerBlock); towers.Enqueue(newTower); } else { print("Enqueing a new Tower!!"); towerCount++; Vector3 towerPos = GetDefaultBaseV3(towerBlock); Tower newTower = Instantiate( towerPrefab, towerPos, Quaternion.identity ); newTower.transform.parent = towerBlock.transform; newTower.OccupyTowerBlock(towerBlock); towers.Enqueue(newTower); } }
internal static void Complete(TowerBlock towerBlock) { int newScore = Mathf.RoundToInt(towerBlock.transform.position.y * 10); if (instance.scoreValue < newScore) { instance.scoreValue = newScore; instance.score.text = " " + instance.scoreValue; } // throw new NotImplementedException(); }
static public void Populate(float x, float y, int i) { TowerBlock block = WorldGetBlock(x, y); if (block == null) { block = Instantiate(instance.blockPrefab, new Vector3(x, y, 0.0f), Quaternion.identity) as TowerBlock; block.hpos = i; instance.towerBlocks.Add(block); instance.highest = y; CameraDirector.SetNewMax(instance.highest); } }
static public TowerBlock WorldGetBlock(float x, float y) { RaycastHit2D hit = Physics2D.Raycast(new Vector3(x, y, 10), Vector2.zero); if (hit.collider != null) { TowerBlock b = hit.collider.GetComponent <TowerBlock>(); if (b != null) { return(b); } } return(null); }
static public TowerBlock ViewGetBlock(float x, float y) { Vector3 v = Camera.main.ScreenToWorldPoint(new Vector3(x, y, 10)); RaycastHit2D hit = Physics2D.Raycast(v, Vector2.zero); if (hit.collider != null) { TowerBlock b = hit.collider.GetComponent <TowerBlock>(); if (b != null) { return(b); } } return(null); }
void OnCollisionEnter2D(Collision2D col) { if (bdone) { return; } bdone = true; TowerBlock block = col.collider.gameObject.GetComponent <TowerBlock>(); if (block != null) { block.Damage(); } Instantiate(corpse, transform.position, Quaternion.identity); Destroy(gameObject); }
// Start a new game private void NewGame(int towerHeight = -1) { // Hide the win message WinTextBlock.Opacity = 0; // Disable the SolutionStep Buttons, can only be used after GenerateSolution() is called SolutionStepFirstButton.IsEnabled = false; SolutionStepBackButton.IsEnabled = false; SolutionStepForwardButton.IsEnabled = false; SolutionStepLastButton.IsEnabled = false; // Ensure a reasonable tower height if (towerHeight < TowerHeightMinimum || towerHeight > TowerHeightMaximum) { towerHeight = TowerHeightDefault; } // Set TowerHeight a class-scope variable, to be available to other methods TowerHeight = towerHeight; // Set the starting state of Gameboard Gameboard = new TowerBlock[3, towerHeight]; for (int level = 0; level < Gameboard.GetLength(1); level++) { // Set the tower on the first column Gameboard[0, level] = new TowerBlock(level + 1, BlockState.Block); // Set all other elements to 0 Gameboard[1, level] = new TowerBlock(0, BlockState.Empty); Gameboard[2, level] = new TowerBlock(0, BlockState.Empty); // Debug.WriteLine(string.Format("TowerOfHanoi: {0} {1} {2}", Gameboard[0, level].Width, Gameboard[1, level].Width, Gameboard[2, level].Width)); } // Build the on-screen towers BuildTowers(Gameboard); // Set the top block of the first tower as Clickable SetBlockIsClickable(0, 0, true); // Allow the user to let the tower be solve by the computer SolveButton.IsEnabled = true; }
public bool NewPosition(float x, float y) { Debug.Log(x + " : " + y); TowerBlock block = Tower.ViewGetBlock(x, y); if (block != null) { spriteRenderer.enabled = true; if (block != currentBlock) { currentBlock = block; isVisible = true; transform.position = new Vector2(currentBlock.transform.position.x, currentBlock.transform.position.y); if (currentBlock.buildStatus == block.buildMax) { spriteRenderer.sprite = swipes[4]; } else { if (currentBlock.damageState) { spriteRenderer.sprite = swipes[currentBlock.buildSequence[currentBlock.repairStatus]]; } else { spriteRenderer.sprite = swipes[currentBlock.buildSequence[currentBlock.buildStatus]]; } } } return(true); } else { spriteRenderer.enabled = false; return(false); } }
internal void CheckBuildPlatform() { int valid = 0; TowerBlock tblock_l = Tower.WorldGetBlock(transform.position.x + blockwidth, transform.position.y); TowerBlock tblock_r = Tower.WorldGetBlock(transform.position.x - blockwidth, transform.position.y); TowerBlock tblock_d = Tower.WorldGetBlock(transform.position.x, transform.position.y - blockheight); if (tblock_l != null && tblock_l.buildStatus == tblock_l.buildMax) { valid++; } if (tblock_r != null && tblock_r.buildStatus == tblock_r.buildMax) { valid++; } if (tblock_d != null && tblock_d.buildStatus == tblock_d.buildMax) { valid++; } if (valid == 0) { Tower.BlockFullDelete(this); } }
internal static void BlockFullDelete(TowerBlock towerBlock) { instance.towerBlocks.Remove(towerBlock); Destroy(towerBlock.gameObject); }
internal static void BlockDelete(TowerBlock towerBlock) { { TowerBlock tblock_l = WorldGetBlock(towerBlock.transform.position.x + blockwidth, towerBlock.transform.position.y); TowerBlock tblock_r = WorldGetBlock(towerBlock.transform.position.x - blockwidth, towerBlock.transform.position.y); TowerBlock tblock_u = WorldGetBlock(towerBlock.transform.position.x, towerBlock.transform.position.y + blockheight); TowerBlock tblock_d = WorldGetBlock(towerBlock.transform.position.x, towerBlock.transform.position.y - blockheight); if (tblock_l != null && tblock_l.buildStatus == 0) { tblock_l.CheckBuildPlatform(); } if (tblock_r != null && tblock_r.buildStatus == 0) { tblock_r.CheckBuildPlatform(); } if (tblock_u != null && tblock_u.buildStatus == 0) { tblock_u.CheckBuildPlatform(); } if (tblock_d != null && tblock_d.buildStatus == 0) { tblock_d.CheckBuildPlatform(); } } Queue <TowerBlock> integrityQue = new Queue <TowerBlock>(); List <TowerBlock> checkList = new List <TowerBlock>(); int needed = 0; foreach (TowerBlock towerBlock1 in instance.towerBlocks) { if (towerBlock1.buildStatus == towerBlock1.buildMax) { needed++; } } integrityQue.Enqueue(instance.towerBlocks[0]); while (integrityQue.Count > 0) { TowerBlock tblock = integrityQue.Dequeue(); TowerBlock tblock_l = WorldGetBlock(tblock.transform.position.x + blockwidth, tblock.transform.position.y); TowerBlock tblock_r = WorldGetBlock(tblock.transform.position.x - blockwidth, tblock.transform.position.y); TowerBlock tblock_u = WorldGetBlock(tblock.transform.position.x, tblock.transform.position.y + blockheight); TowerBlock tblock_d = WorldGetBlock(tblock.transform.position.x, tblock.transform.position.y - blockheight); if (tblock_l != null && tblock_l.buildStatus == tblock_l.buildMax && !checkList.Contains(tblock_l)) { integrityQue.Enqueue(tblock_l); } if (tblock_r != null && tblock_r.buildStatus == tblock_r.buildMax && !checkList.Contains(tblock_r)) { integrityQue.Enqueue(tblock_r); } if (tblock_u != null && tblock_u.buildStatus == tblock_u.buildMax && !checkList.Contains(tblock_u)) { integrityQue.Enqueue(tblock_u); } if (tblock_d != null && tblock_d.buildStatus == tblock_d.buildMax && !checkList.Contains(tblock_d)) { integrityQue.Enqueue(tblock_d); } checkList.Add(tblock); } if (checkList.Count < needed) { GameOver(); } }
public void OccupyTowerBlock(TowerBlock towerBlock) { this.towerBlock = towerBlock; towerBlock.OccupyBlock(); }
public void ReleaseTowerblock() { towerBlock.ReleaseBlock(); this.towerBlock = null; }
/// <summary> /// Update a single block in the tower collections on-screen, to its state in Gameboard /// </summary> /// <param name="tower">The tower where the block is located at</param> /// <param name="level">The level where the block is located at</param> private void UpdateBlock(int tower, int level) { if (tower < 0 || tower > 2 || level < 0 || level > TowerHeight - 1) { Debug.WriteLine(string.Format("TowerOfHanoi: UpdateBlock() revieced invalid input. tower={0}, level={1}", tower, level)); return; } TowerBlock newBlock = Gameboard[tower, level]; switch (newBlock.State) { case BlockState.Empty: { switch (tower) { case 0: { Tower0.ElementAt((TowerHeight - 1) - level).SetStateEmpty(); break; } case 1: { Tower1.ElementAt((TowerHeight - 1) - level).SetStateEmpty(); break; } case 2: { Tower2.ElementAt((TowerHeight - 1) - level).SetStateEmpty(); break; } } break; } case BlockState.Available: { switch (tower) { case 0: { Tower0.ElementAt((TowerHeight - 1) - level).SetStateAvailable(BlockWidths[Gameboard[SelectedBlock[0], SelectedBlock[1]].Width - 1]); break; } case 1: { Tower1.ElementAt((TowerHeight - 1) - level).SetStateAvailable(BlockWidths[Gameboard[SelectedBlock[0], SelectedBlock[1]].Width - 1]); break; } case 2: { Tower2.ElementAt((TowerHeight - 1) - level).SetStateAvailable(BlockWidths[Gameboard[SelectedBlock[0], SelectedBlock[1]].Width - 1]); break; } } break; } case BlockState.Block: { switch (tower) { case 0: { Tower0.ElementAt((TowerHeight - 1) - level).SetStateBlock(BlockWidths[newBlock.Width - 1], BlockColours.ElementAt((newBlock.Width - 1 + BlockColours.Length) % BlockColours.Length)); break; } case 1: { Tower1.ElementAt((TowerHeight - 1) - level).SetStateBlock(BlockWidths[newBlock.Width - 1], BlockColours.ElementAt((newBlock.Width - 1 + BlockColours.Length) % BlockColours.Length)); break; } case 2: { Tower2.ElementAt((TowerHeight - 1) - level).SetStateBlock(BlockWidths[newBlock.Width - 1], BlockColours.ElementAt((newBlock.Width - 1 + BlockColours.Length) % BlockColours.Length)); break; } } break; } case BlockState.Selected: { // unset any previously possible moves int x = GetLowestEmptyBlock(0); if (x > -1) { Tower0.ElementAt((TowerHeight - 1) - x).SetStateEmpty(); } x = GetLowestEmptyBlock(1); if (x > -1) { Tower1.ElementAt((TowerHeight - 1) - x).SetStateEmpty(); } x = GetLowestEmptyBlock(2); if (x > -1) { Tower2.ElementAt((TowerHeight - 1) - x).SetStateEmpty(); } switch (tower) { case 0: { Tower0.ElementAt((TowerHeight - 1) - level).SetStateSelected(); break; } case 1: { Tower1.ElementAt((TowerHeight - 1) - level).SetStateSelected(); break; } case 2: { Tower2.ElementAt((TowerHeight - 1) - level).SetStateSelected(); break; } } break; } default: { Debug.WriteLine(string.Format("TowerOfHanoi: ChangeBlock() did not recognise the given state, {0}", newBlock.State)); break; } } ReloadTower(tower); }
/// <summary> /// Automatically solve the current tower, resulting in the array SolutionMoves being filled with the required moves, which the user can then step through /// </summary> private void GenerateSolution() { // https://en.wikipedia.org/wiki/Tower_of_Hanoi, look under section Solution, there are also many more examples, explanations and solutions online // Ensure the Gameboard is in a 'clean' state, ie no blocks are in BlockState Available or Selected NewGame(TowerHeight); // Unable any user input on the gameboard (comment this out, as well as the enabling of SolutionStep buttons, if you would like to follow the steps manually) SetBlockIsClickable(0, 0, false); // Copy the starting and end state of the Gameboard for future reference StartBoard = new TowerBlock[Gameboard.GetLength(0), Gameboard.GetLength(1)]; StartBoard = Gameboard; // Generate the end state of the Gameboard for future reference EndBoard = new TowerBlock[Gameboard.GetLength(0), Gameboard.GetLength(1)]; for (int bl = 0; bl < EndBoard.GetLength(1); bl++) { EndBoard[0, bl] = new TowerBlock(); EndBoard[1, bl] = new TowerBlock(); EndBoard[2, bl] = Gameboard[0, bl]; } // Create the array that will contain all required moves, and set the index counter to 0 SolutionMoves = new int[(int)(Math.Pow(2, TowerHeight) - 1), 4]; SolutionCurrentMove = 0; // Create three Stack<int>'s, one for each of the towers, filled with the block widths on the Gameboard Stack <int> tower0 = new Stack <int>(); Stack <int> tower1 = new Stack <int>(); Stack <int> tower2 = new Stack <int>(); for (int level = (Gameboard.GetLength(1) - 1); level > -1; level--) { if (Gameboard[0, level].State == BlockState.Block) { tower0.Push(Gameboard[0, level].Width); } if (Gameboard[1, level].State == BlockState.Block) { tower1.Push(Gameboard[0, level].Width); } if (Gameboard[2, level].State == BlockState.Block) { tower2.Push(Gameboard[0, level].Width); } } // Generate the solution SolveRecursively(TowerHeight, tower0, 0, tower2, 2, tower1, 1); Debug.WriteLine($"TowerOfHanoi: GenerateSolution() checking, SolutionMoves.Length == {SolutionMoves.GetLength(0)}, SolutionCurrentMove == {SolutionCurrentMove}"); // Enable the step-through buttons SolutionStepFirstButton.IsEnabled = true; SolutionStepBackButton.IsEnabled = true; SolutionStepForwardButton.IsEnabled = true; SolutionStepLastButton.IsEnabled = true; // Reset solutionCurrentMove to 0, so it can be used to step through the generated solution SolutionCurrentMove = 0; // Display the current step and total number of steps found on screen SolutionCurrentStepTextBlock.Text = SolutionCurrentMove.ToString(); SolutionTotalStepsTextBlock.Text = SolutionMoves.GetLength(0).ToString(); }
private static Vector3 GetDefaultBaseV3(TowerBlock towerBlock) { return(new Vector3(towerBlock.transform.position.x, 0f, towerBlock.transform.position.z)); }