Unity Color Matching Game - c#

I have searched the similar games on forum and google but i could not find exactly.
I am making a puzzle game. and user can get point if the nodes (horizontal sticks) are same color then he can get.
when they are in same direction it says colors matched but in generated node whenever i rotate the sticks it says also same.
Can you take a look? and tell me how to fix. Also if you have better idea about this matching I will be appreciated.
---------
void Update()
{
if (Input.GetMouseButtonDown(0))
{
clickTime = Time.time;
rayhit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero, Mathf.Infinity, selectableObjLayerMask);
}
else if (Input.GetMouseButtonUp(0))
{
if (rayhit)
{
if (Time.time - clickTime < .2f)
{
Node node = rayhit.transform.GetComponent<Node>();
if (node != null)
{
for (int i = 0; i < node.sticks.Count; i++)
{
Vector3 newAngles = new Vector3(0, 0, (node.sticks[i].transform.localEulerAngles.z - 45));
newAngles.z = newAngles.z < 0 ? newAngles.z + 180 : newAngles.z;
newAngles.z = newAngles.z >180 ? newAngles.z - 180 : newAngles.z;
node.sticks[i].transform.localEulerAngles = newAngles;
node.sticks[i].degree = (int)newAngles.z;
//******** HERE IS COLOR MATCHING*******
if (node.transform.parent.name=="Node1" && node.sticks[i].degree == 90)
{
colorMatch[1] = node.sticks[i].color;
Debug.Log("COLOR 1___"+ colorMatch[1]);
//Debug.Log(colorMatch1);
}
if (node.transform.parent.name == "Node2" && node.sticks[i].degree == 90)
{
colorMatch[2] = node.sticks[i].color;
Debug.Log("COLOR 2___" + colorMatch[2]);
}
if (node.transform.parent.name == "Node3" && node.sticks[i].degree == 90)
{
colorMatch[3] = node.sticks[i].color;
Debug.Log("COLOR 3___" + colorMatch[3]);
//if (colorMatch[1] == colorMatch[2] && colorMatch[2] == colorMatch[3])
//{
// Debug.Log("COLORS MATCHED : " + colorMatch[1]);
//}
}
if (colorMatch[1]==colorMatch[2] && colorMatch[2]==colorMatch[3])
{
Debug.Log("COLOR MATCHED");
}
}
}
}
else
{
Node currNode = rayhit.transform.GetComponent<Node>();
if(currNode.isMoved == false)
{
smallestId = 0;
smallestDistance = 999;
for (int i = 0; i < nodes.Length; i++)
{
float distance = Vector2.Distance(rayhit.transform.position, nodes[i].transform.position);
if (smallestDistance > distance)
{
smallestDistance = distance;
smallestId = i;
}
}
rayhit.transform.position = nodes[smallestId].transform.position;
if (rayhit.transform.parent != nodes[smallestId].transform)
{
if (nodes[smallestId].transform.childCount > 0 && nodes[smallestId].transform != rayhit.transform.parent)
{
if (currNode != null)
{
for (int i = 0; i < currNode.sticks.Count; i++)
{
nodes[smallestId].transform.GetChild(0).GetComponent<Node>().sticks.Add(currNode.sticks[i]);
currNode.sticks[i].transform.SetParent(nodes[smallestId].transform.GetChild(0));
}
Destroy(rayhit.transform.gameObject);
}
}
else
{
if (currNode != null)
{
currNode.isMoved = true;
}
rayhit.transform.SetParent(nodes[smallestId].transform);
}
}
}
}
}
rayhit = new RaycastHit2D();
}
else if (Input.GetMouseButton(0))
{
if(rayhit.transform != null)
{
Node currNode = rayhit.transform.GetComponent<Node>();
if(currNode != null)
if (currNode.isMoved == false)
{
if (Time.time - clickTime >= 0.2f)
{
Vector2 newPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
rayhit.transform.position = newPos;
}
}
}
}
}

if (node.transform.parent.name=="Node1" && node.sticks[i].degree == 90)
{
colorMatch[1] = node.sticks[i].color;
Debug.Log("COLOR 1___"+ colorMatch[1]);
//Debug.Log(colorMatch1);
}
if (node.transform.parent.name == "Node2" && node.sticks[i].degree == 90)
{
colorMatch[2] = node.sticks[i].color;
Debug.Log("COLOR 2___" + colorMatch[2]);
}
if (node.transform.parent.name == "Node3" && node.sticks[i].degree == 90)
{
colorMatch[3] = node.sticks[i].color;
Debug.Log("COLOR 3___" + colorMatch[3]);
if (colorMatch[1] == colorMatch[2] && colorMatch[2] == colorMatch[3])
{
Debug.Log("COLORS MATCHED : " + colorMatch[1]);
}
}
Here is working code. but how i can destroy the matched sticks?

Related

Using Yield to make a function wait until something is true Unity C#

I'm trying to use Yield so my functions wait to spawn more enemies in scene if the max amount has been reached. But now the functions skips all this while cycle in its entirety.
I have never use yield so maybe I'm understanding wrong what it does while reading the documentation.
Also maybe there is a better way to do it.
while ( i < clonesASpawnear.Length)
{
if (j <= endList)
{
if (clonesASpawnear[i] == null)
{
if (sPCurrent == sPMax)
{
sPCurrent = 0;
}
yield return new WaitUntil(() => aliveEnemies < maxAmmoutOfEnemiesOnStage);
clonesASpawnear[i] = Instantiate(enemyTypeList[j], spawnPoints[sPCurrent].transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
clonesASpawnear[i].SetActive(true);
clonesASpawnear[i].GetComponent<EnemyMovement_DCH>().player = Target;
aliveEnemies += 1;
clonesASpawnear[i].GetComponent<EnemyDamageHandler_DCH>().SpawnerEnemies = this;
j++;
i++;
sPCurrent++;
}
}
else
{
j = startList;
}
}
}
Edited as requested: here is the update where the function is called
void Update()
{
if (pdh.playerIsDead == false && roundOver==false)
{
playerAliveTime += Time.deltaTime;
}
if (waveNumer <= 3 )
{
timeForNextSpawn -= Time.deltaTime;
if (timeForNextSpawn <= 0 && aliveEnemies == 0)
{
nextWaveTextUI.text = nextWaveText;
int waitT = (int)waitTimeForNewWave;
StartCoroutine(delayXSeconds(waitT));
timeForNextSpawn = waitTimeForNewWave;
auxWaveThisRound--;
waveNumer++;
spawnEnemies();
}
}
else
{
if(aliveEnemies == 0 && auxWaveThisRound <= 0)
{
clearedRoundTextUI.text = clearedRoundText;
roundOver = true;
StartCoroutine(waiterReset());
}
}
accuracy = successfulProjectiles / projectileFired;
}
And the complete function where the above code is
IEnumerator spawnEnemies()
{
int percentForWave=0;
int percentForType=0;
int TotalEnemies = (int)enemySpawnsThisRound;
if (waveNumer == 1)
{
Debug.Log("Entro al wave 1");
percentForWave = 20;
percentForType = 20;
startList = 0;
}
if (waveNumer == 2)
{
Debug.Log("Entro al wave 2");
percentForWave = 70;
percentForType = 70;
startList = endList;
}
if (waveNumer == 3)
{
Debug.Log("Entro al wave 3");
percentForWave = 10;
percentForType = 10;
startList = endList;
}
int enemiesThisWave = Decimal.ToInt32(Math.Round(TotalEnemies * ((decimal)percentForWave / 100), 1));
int enemiesForType = Decimal.ToInt32(Math.Round(lenghtList * ((decimal)percentForType / 100), 1));
endList = enemiesForType + startList;
clonesASpawnear = new GameObject[enemiesThisWave];
int i = 0;
int j = startList;
while ( i < clonesASpawnear.Length)
{
if (j <= endList)
{
if (clonesASpawnear[i] == null)
{
if (sPCurrent == sPMax)
{
sPCurrent = 0;
}
yield return new WaitUntil(() => aliveEnemies < maxAmmoutOfEnemiesOnStage);
clonesASpawnear[i] = Instantiate(enemyTypeList[j], spawnPoints[sPCurrent].transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
clonesASpawnear[i].SetActive(true);//lo activo
clonesASpawnear[i].GetComponent<EnemyMovement_DCH>().player = Target;
aliveEnemies += 1;
clonesASpawnear[i].GetComponent<EnemyDamageHandler_DCH>().SpawnerEnemies = this;
j++;
i++;
sPCurrent++;
}
}
else
{
j = startList;
}
}
}
Do not use a while loop. Instead, use "Update()".
void Update() {
if (aliveEnemies < maxAmmoutOfEnemiesOnStage && j <= endList)
{
if (clonesASpawnear[i] == null)
{
if (sPCurrent == sPMax)
{
sPCurrent = 0;
}
clonesASpawnear[i] = Instantiate(enemyTypeList[j], spawnPoints[sPCurrent].transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
clonesASpawnear[i].SetActive(true);
clonesASpawnear[i].GetComponent<EnemyMovement_DCH>().player = Target;
aliveEnemies += 1;
clonesASpawnear[i].GetComponent<EnemyDamageHandler_DCH>().SpawnerEnemies = this;
j++;
i++;
sPCurrent++;
}
}
else
{
j = startList;
}
}
This is the idea of the Update frame. Logic in here is hit every frame. It will only continue if the number of enemies is below the max. This method MUST go into a class that extends MonoBehaviour, or else it will not be invoked. Given the intended logic, it makes sense that this would be attached to some master "game monitoring" GameObject that is managing the meta of the game along with win states etc.
EDIT (After more content):
Heres one problem "spawnEnemies();". That is an IEnumerator, but you don't use "StartCoroutine(spawnEnemies())". So the method will literally not execute. There will be no errors, but nothing in the method will occur.

How to make my code evaluate the whole arithmetic expression?

I am trying to make a string calculator, it works fine with two numbers but I always encounter a problem when evaluating multiple operations:
7*2+4=
Also can you help me with my multiplication and division code. I don't understand why it prints 0 even with just two numbers(7*5)
using System;
using System.Text.RegularExpressions;
namespace Sariling_Calcu
{
class Program
{
private static string exp;
private static int[] i = new int[1000];
private static char[] oper = new char[10];
private static int cntr2;
private static int result;
private static int pluscount;
private static int subcount;
private static int mulcount;
private static int divcount;
static void getNum()
{
string[] strNum = (Regex.Split(exp, #"\D+"));
for (int cntr = 0; cntr < exp.Length; cntr++)
{
foreach (string item in strNum)
{
if (!string.IsNullOrEmpty(item))
{
i[cntr] = int.Parse(item);
cntr += 1;
}
}
}
}
static void counter()
{
for (int cntr = 0; cntr < exp.Length; cntr++)
{
if (exp[cntr] == '+')
{
pluscount++;
}
else if (exp[cntr] == '-')
{
subcount++;
}
else if (exp[cntr] == '*')
{
mulcount++;
}
else if (exp[cntr] == '/')
{
divcount--;
}
}
}
static void oprtr()
{
for (int cntr = 0; cntr < exp.Length; cntr++)
{
if (exp[cntr] != '1'
&& exp[cntr] != '2'
&& exp[cntr] != '3'
&& exp[cntr] != '4'
&& exp[cntr] != '5'
&& exp[cntr] != '6'
&& exp[cntr] != '7'
&& exp[cntr] != '8'
&& exp[cntr] != '9')
{
if (exp[cntr] == '+')
{
result += i[cntr2];
cntr2 += 1;
pluscount--;
if (pluscount == 0 && subcount == 0 && mulcount==0 && divcount==0)
{
cntr2 += 3;
result += i[cntr2];
}
}
else if (exp[cntr] == '-')
{
result -= i[cntr2];
cntr2 += 1;
subcount--;
result = -result;
if (pluscount == 0 && subcount == 0 && mulcount == 0 && divcount == 0)
{
cntr2 += 3;
result -= i[cntr2];
}
}
else if (exp[cntr] == '*')
{
if (result == 0)
{
result += 1;
}
result *= i[cntr2];
cntr2 += 1;
mulcount--;
if (pluscount == 0 && subcount == 0 && mulcount == 0 && divcount == 0)
{
cntr2 += 3;
result *= i[cntr2];
}
}
else if (exp[cntr] == '/')
{
if (result == 0)
{
result += 1;
}
result /= i[cntr2];
cntr2 += 1;
divcount--;
if (pluscount == 0 && subcount == 0 && mulcount == 0 && divcount == 0)
{
cntr2 += 3;
result /= i[cntr2];
}
}
}
}
}
static void Main(string[] args)
{
Console.Write("Expression: ");
exp = Console.ReadLine();
counter();
getNum();
oprtr();
Console.Write("Answer: \n" + result);
Console.ReadLine();
}
}
}
you could use LINQ to reduce your code to a few lines but looks like its a school assignment where you would have to go with Arrays and loops.
I have tried to refine your code a bit and did a few fixes, change getNum(), counter() and oprtr() methods as below and let me know if it works, then I would add some comments in the code to explain changes I made.
static void getNum()
{
string[] strNum = (Regex.Split(exp, #"\D+"));
for (int cntr = 0; cntr < strNum.Length; cntr++)
{
if (!string.IsNullOrEmpty(strNum[cntr]))
{
i[cntr] = int.Parse(strNum[cntr]);
}
}
}
static void counter()
{
cntr2 = 0;
for (int cntr = 0; cntr < exp.Length; cntr++)
{
if (exp[cntr] == '+')
{
oper[cntr2] = '+';
cntr2++;
}
else if (exp[cntr] == '-')
{
oper[cntr2] = '-';
cntr2++;
}
else if (exp[cntr] == '*')
{
oper[cntr2] = '*';
cntr2++;
}
else if (exp[cntr] == '/')
{
oper[cntr2] = '/';
cntr2++;
}
}
}
static void oprtr()
{
result = i[0];
cntr2 = 1;
for (int cntr = 0; cntr < oper.Length; cntr++)
{
if (oper[cntr] == '+')
{
result += i[cntr2];
}
else if (oper[cntr] == '-')
{
result -= i[cntr2];
}
else if (oper[cntr] == '*')
{
result *= i[cntr2];
}
else if (oper[cntr] == '/')
{
if (i[cntr2] == 0)
{
throw new DivideByZeroException();
}
result /= i[cntr2];
}
cntr2 += 1;
}
}

Is there a better way of coding this [duplicate]

This question already has an answer here:
Arrays in visual basic Creating a program that deals with driving distance [closed]
(1 answer)
Closed 4 years ago.
So the issue is
"Create a project that looks up the driving distance between two cities. Use two drop-down lists that contain the names of the cities. Label one list Departure and the other Destination. Use a Look Up button to calculate the distance.
Store the distances in a two-dimensional table."
is there a better way of coding this by using a for loop
ok here is the code below
private void lookUpButton_Click(object sender, EventArgs e)
{
int [,] miles = { {0,1004, 1753, 2752, 3017,1520, 1507,609, 3115,448},
{1004,0, 921,1780, 2048, 1397, 919,515 , 2176,709},
{ 1753,921, 0,1230, 1399,1343, 517,1435, 2234,1307},
{ 2752,1780,1230,0 , 272,2570, 1732,2251, 1322,2420},
{3017,2048 , 1399,272, 0,2716, 1858,2523, 1278,2646},
{ 1520,1397, 1343,2570, 2716,0, 860,1494, 3447,1057},
{ 1507,919, 517,1732, 1858,860, 0,1307, 2734,1099},
{ 609,515, 1435,2251, 2523,1494, 1307,0, 2820,571},
{ 3155,2176, 2234,1322, 1278,3447, 2734,2820, 0,2887},
{ 448,709, 1307,2420, 2646,1057, 1099,571,2887,0 }
};
//var distance = miles[txtDeparture.SelectedIndex,txtDeparture.SelectedIndex];
// alot of if statments
if ((txtDeparture.SelectedIndex == 0) && (txtDestination.SelectedIndex == 0))
{
var distance = miles[0, 0];
txtDistancebox.Text = distance.ToString();
}
else if ((txtDeparture.SelectedIndex == 0) && (txtDestination.SelectedIndex == 1))
{
var distance = miles[0, 1];
txtDistancebox.Text = distance.ToString();
}
else if ((txtDeparture.SelectedIndex == 0) && (txtDestination.SelectedIndex == 2))
{
var distance = miles[0, 2];
txtDistancebox.Text = distance.ToString();
}
else if ((txtDeparture.SelectedIndex == 0) && (txtDestination.SelectedIndex == 3))
{
var distance = miles[0, 3];
txtDistancebox.Text = distance.ToString();
}
else if ((txtDeparture.SelectedIndex == 0) && (txtDestination.SelectedIndex == 4))
{
var distance = miles[0, 4];
txtDistancebox.Text = distance.ToString();
}
else if ((txtDeparture.SelectedIndex == 0) && (txtDestination.SelectedIndex == 5))
{
var distance = miles[0, 5];
txtDistancebox.Text = distance.ToString();
}
else if ((txtDeparture.SelectedIndex == 0) && (txtDestination.SelectedIndex == 6))
{
var distance = miles[0, 6];
txtDistancebox.Text = distance.ToString();
}
else if ((txtDeparture.SelectedIndex == 0) && (txtDestination.SelectedIndex == 7))
{
var distance = miles[0, 7];
txtDistancebox.Text = distance.ToString();
}
else if ((txtDeparture.SelectedIndex == 0) && (txtDestination.SelectedIndex == 8))
{
var distance = miles[0, 8];
txtDistancebox.Text = distance.ToString();
}
else if ((txtDeparture.SelectedIndex == 0) && (txtDestination.SelectedIndex == 9))
{
var distance = miles[0, 9];
txtDistancebox.Text = distance.ToString();
}
else if ((txtDeparture.SelectedIndex == 0) && (txtDestination.SelectedIndex == 10))
{
var distance = miles[0, 10];
txtDistancebox.Text = distance.ToString();
}
else if ((txtDeparture.SelectedIndex == 1) && (txtDestination.SelectedIndex == 0))
{
var distance = miles[1, 0];
txtDistancebox.Text = distance.ToString();
}
}
Something like this:
var distance = miles[txtDeparture.SelectedIndex, txtDestination.SelectedIndex];
txtDistancebox.Text = distance.ToString();
I think you can make one method for calculating txtDistancebox.
like as ...Private int CalculateDistace(int DepartureIndex, int DestinationIndex)

Argument out of range error

My code is working intermittently, but sometimes gives me the argument out of range error and I can't figure out why, I've narrowed it down to one function:
void corridorFill()
{
int dir = 0;
//Set initial coords
int rand = Random.Range(0,freeCol.Count);
List<int> freeCorRow = new List<int>();
List<int> freeCorCol = new List<int>();
row = freeRow[rand];
col = freeCol[rand];
int fill = 0;
while(fill < area)
{
//Pick a random direction and go that way
//0 = north, 1 = east, 2 = south, 3 = west
dir = Random.Range(0,4);
if(directionIsSafe(dir, row, col, (int)room.Unreserved, roomType)
&& directionIsSafe(dir, row, col, (int)connect.Empty, connections))
{
//move in direction
moveDirection(dir);
freeCorRow.Add(row);
freeCorCol.Add(col);
if(fill > 0)
{
//place exit to previous tile
addExit(row, col, (dir+2)%4);
//change exits of previous room to connect
addExit(freeCorRow[freeCorRow.Count-2], freeCorCol[freeCorCol.Count-2], dir);
}
fill++;
}
else
{
bool set = false;
while(!set)
{
//direction is not safe therefore start again somewhere else, attached to what we already have
int r = Random.Range(0,freeCorRow.Count);
//check if a tile beside a known tile is free
dir = Random.Range(0,4);
//if the direction is safe, go that way
if(directionIsSafe(dir, freeCorRow[r], freeCorCol[r], (int)room.Unreserved, roomType)
&& directionIsSafe(dir, freeCorRow[r], freeCorCol[r], (int)connect.Empty, connections))
{
addExit(freeCorRow[r], freeCorCol[r], dir);
row = freeCorRow[r];
col = freeCorCol[r];
moveDirection(dir); //move in direction
addExit(row, col, (dir+2)%4); //place exit to previous tile
freeCorRow.Add(row);
freeCorCol.Add(col);
set = true;
}
}
fill++;
}
}
}
which uses the addExit function, though I don't think the problem is here:
//check previous tile corridor configuration and change to match current
void addExit(int row, int col, int dir)
{
//Add northourn exit to room
if(dir == 0)
{
if(connections[row,col] == (int)connect.Empty)
{
connections[row,col] = (int)connect.N;
}
else if(connections[row,col] == (int)connect.E)
{
connections[row,col] = (int)connect.NE;
}
else if(connections[row,col] == (int)connect.S)
{
connections[row,col] = (int)connect.NS;
}
else if(connections[row,col] == (int)connect.W)
{
connections[row,col] = (int)connect.NW;
}
else if(connections[row,col] == (int)connect.SE)
{
connections[row,col] = (int)connect.NES;
}
else if(connections[row,col] == (int)connect.SW)
{
connections[row,col] = (int)connect.SWN;
}
else if(connections[row,col] == (int)connect.EW)
{
connections[row,col] = (int)connect.WNE;
}
else if(connections[row,col] == (int)connect.ESW)
{
connections[row,col] = (int)connect.NESW;
}
}
//Add eastern exit to room
if(dir == 1)
{
if(connections[row,col] == (int)connect.Empty)
{
connections[row,col] = (int)connect.E;
}
else if(connections[row,col] == (int)connect.N)
{
connections[row,col] = (int)connect.NE;
}
else if(connections[row,col] == (int)connect.S)
{
connections[row,col] = (int)connect.SE;
}
else if(connections[row,col] == (int)connect.W)
{
connections[row,col] = (int)connect.EW;
}
else if(connections[row,col] == (int)connect.NW)
{
connections[row,col] = (int)connect.WNE;
}
else if(connections[row,col] == (int)connect.SW)
{
connections[row,col] = (int)connect.ESW;
}
else if(connections[row,col] == (int)connect.NS)
{
connections[row,col] = (int)connect.NES;
}
else if(connections[row,col] == (int)connect.SWN)
{
connections[row,col] = (int)connect.NESW;
}
}
//Add southourn exit to room
if(dir == 2)
{
if(connections[row,col] == (int)connect.Empty)
{
connections[row,col] = (int)connect.S;
}
else if(connections[row,col] == (int)connect.N)
{
connections[row,col] = (int)connect.NS;
}
else if(connections[row,col] == (int)connect.E)
{
connections[row,col] = (int)connect.SE;
}
else if(connections[row,col] == (int)connect.W)
{
connections[row,col] = (int)connect.SW;
}
else if(connections[row,col] == (int)connect.NE)
{
connections[row,col] = (int)connect.NES;
}
else if(connections[row,col] == (int)connect.NW)
{
connections[row,col] = (int)connect.SWN;
}
else if(connections[row,col] == (int)connect.EW)
{
connections[row,col] = (int)connect.ESW;
}
else if(connections[row,col] == (int)connect.WNE)
{
connections[row,col] = (int)connect.NESW;
}
}
//Add western exit to room
if(dir == 3)
{
if(connections[row,col] == (int)connect.Empty)
{
connections[row,col] = (int)connect.W;
}
else if(connections[row,col] == (int)connect.N)
{
connections[row,col] = (int)connect.NW;
}
else if(connections[row,col] == (int)connect.E)
{
connections[row,col] = (int)connect.EW;
}
else if(connections[row,col] == (int)connect.S)
{
connections[row,col] = (int)connect.SW;
}
else if(connections[row,col] == (int)connect.NE)
{
connections[row,col] = (int)connect.WNE;
}
else if(connections[row,col] == (int)connect.SE)
{
connections[row,col] = (int)connect.ESW;
}
else if(connections[row,col] == (int)connect.NS)
{
connections[row,col] = (int)connect.SWN;
}
else if(connections[row,col] == (int)connect.NES)
{
connections[row,col] = (int)connect.NESW;
}
}
}
and the directionIsSafe function:
bool directionIsSafe(int dir, int row, int col, int roomname, int[,] checkType)
{
if(dir == 0 && col+1 < stationHeight)
{
if(checkType[row, col+1] == roomname)
{
return true;
}
else
{
return false;
}
}
else if(dir == 1 && row+1 < stationWidth)
{
if(checkType[row+1,col] == roomname)
{
return true;
}
else
{
return false;
}
}
else if(dir == 2 && col > 0)
{
if(checkType[row,col-1] == roomname)
{
return true;
}
else
{
return false;
}
}
else if(dir == 3 && row > 0)
{
if(checkType[row-1,col] == roomname)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
Reading through the code it seems like it should work, and it does some of the time, but not all of the time. I can't figure out why only sometimes it doesn't work. Appreciate any light you might shed on the problem
EDT
freeCol and freeRow are created using:
if(genType == (int)shapes.cross)
{
//for odd numbers
if(stationWidth%2 == 1)
{
for(row = 0; row < stationWidth; row++)
{
for(col = 0; col < stationHeight; col++)
{
if((row <= stationWidth/2 + (crossArmSize/2.0f + 0.5f) - 1 && row >= stationWidth/2 - (crossArmSize/2.0f + 0.5f) + 1)
|| (col <= stationHeight/2 + (crossArmSize/2.0f + 0.5f) - 1 && col >= stationHeight/2 - (crossArmSize/2.0f + 0.5f) + 1))
{
roomType[row,col] = (int)room.Unreserved;
freeRow.Add(row);
freeCol.Add(col);
}
}
}
}
//for even numbers
else if(stationWidth%2 == 0)
{
for(row = 0; row < stationWidth; row++)
{
for(col = 0; col < stationHeight; col++)
{
if((row < stationWidth/2 + crossArmSize/2 && row >= stationWidth/2 - crossArmSize/2)
|| (col < stationWidth/2 + crossArmSize/2 && col >= stationWidth/2 - crossArmSize/2))
{
roomType[row,col] = (int)room.Unreserved;
freeRow.Add(row);
freeCol.Add(col);
}
}
}
}
corridorFill();
}
I think Random.Range is inclusive. Try int rand = Random.Range(0,freeCol.Count - 1);
Are Width and Height swapped in the last function?
Normally rows go along the height and columns along the width.
OK, so I changed:
row = freeRow[rand];
col = freeCol[rand];
to:
row = freeRow[Mathf.RoundToInt(stationWidth/2)];
col = freeCol[Mathf.RoundToInt(stationHeight/2)];
and it works every time now. I just can't figure out why the former was throwing out weird results and just plain not working, if you can shed light on it I'd love to hear it!

WPF Tree View event

I want to make an application in WPF same as windows application. When i use treeview event in wpf i did not find any event similar to treeview_NodeMouseClick of WIndows Application.
//Windows Application Code
private void tv_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Node != null)
{
GetAllchield(e.Node, e.Node.Level);
}
}
// GetAllchield
public void GetAllchield(TreeNode clickednode, int indexDepth)
{
if (clickednode.Nodes.Count > 0 && !clickednode.IsExpanded)
{
clickednode.Collapse();
return;
}
string[] FullPath = clickednode.FullPath.Split('\\');
string rootnode = FullPath[0].ToString();
//get all market for root event type
int eventTypeID = DictionaryAllActiveEventTypes[rootnode];
string[] allMarkets = GetAllMarketForEventID(eventTypeID);
//selecting unque chield node and populating in tree
for (int i = 0; i < allMarkets.Length; i++)
{
if (allMarkets[i].Contains(clickednode.Text))
{
string[] marketDetails = allMarkets[i].Split('~');
int marketID = Convert.ToInt32(marketDetails[0]);
string MarketName = marketDetails[1].ToString();
string MarketStatus = marketDetails[3].ToString();
string EventHeirarchy = marketDetails[6].ToString();
string Menupath = marketDetails[5].ToString();
string[] Arrmenupath = Menupath.Trim(':').Split('\\');
string chieldText = "";
if (indexDepth == 0)
{
if (rootnode == "Cricket" || rootnode == "Tennis" || rootnode == "Golf" || rootnode == "Rugby")
{
if (Arrmenupath[2].Contains("Group") && Arrmenupath[2].Length == 7)
{
if ((indexDepth + 3) <= Arrmenupath.Length - 1)
{
chieldText = Arrmenupath[indexDepth + 3].ToString();
}
}
else
{
if ((indexDepth + 2) <= Arrmenupath.Length - 1)
chieldText = Arrmenupath[indexDepth + 2].ToString();
}
}
else
if ((indexDepth + 2) <= Arrmenupath.Length - 1)
chieldText = Arrmenupath[indexDepth + 2].ToString();
}
else
{
if (Arrmenupath[Arrmenupath.Length - 1] == clickednode.Text)
chieldText = MarketName;
else
{
if (allMarkets[i].Contains(clickednode.Text) && allMarkets[i].Contains(clickednode.Parent.Text) && allMarkets[i].Contains(rootnode))
{
if (rootnode == "Cricket" || rootnode == "Tennis" || rootnode == "Golf" || rootnode == "Rugby")
{
if (Arrmenupath[2].Contains("Group") && Arrmenupath[2].Length == 7)
{
if ((indexDepth + 3) <= Arrmenupath.Length - 1)
{
chieldText = Arrmenupath[indexDepth + 3].ToString();
}
}
else
{
if ((indexDepth + 2) <= Arrmenupath.Length - 1)
chieldText = Arrmenupath[indexDepth + 2].ToString();
}
}
else
if ((indexDepth + 2) <= Arrmenupath.Length - 1)
chieldText = Arrmenupath[indexDepth + 2].ToString();
}
}
}
//check whether node is already added
//if (chieldText.Contains("MiWay"))
//{ }
if (!string.IsNullOrEmpty(chieldText))
{
if (clickednode.Nodes.Count >= 1)
{
bool doesNodeAlreadyExist = false;
foreach (TreeNode node in clickednode.Nodes)
{
if (node.Text == chieldText)
{
doesNodeAlreadyExist = true;
break;
}
}
if (!doesNodeAlreadyExist)
{
clickednode.Nodes.Add(chieldText);
}
}
else
{
clickednode.Nodes.Add(chieldText);
}
}
}
}
clickednode.Expand();
}
I want to use same as in WPF. Please help me or if you get it.
You can use MouseLeftButtonDown and check sender argument or use SelectedItemChanged

Categories

Resources