- Home /
While loop
Hello. I'm trying to write a simple Minesweeper game. I have two scripts. First one scOnCellPress is at prefab, and it contains next lines:
public var isMouseOver:int=0;
public var sGameControl:scGameControl;
public var hasMine:int=0;
function Start () {
var objGameControl=GameObject.Find("GameControl");
sGameControl=objGameControl.GetComponent(scGameControl);
}
function Update () {
if (Input.GetMouseButtonUp(0) && isMouseOver==1){
if (sGameControl.minesSet=="-1"){
sGameControl.minesSet=gameObject.name;
}
}
}
function OnMouseOver(){
isMouseOver=1;
}
function OnMouseExit(){
isMouseOver=0;
}
Second one scGameControl is at empty GameObject named GameControl at the scene, and contains next lines:
var x:float;
var y:float;
var Field:GameObject[,];
var preCell:GameObject;
var cellsNum:int=0;
var totalMines:int;
function Start () {
Field=new GameObject[x,y];
for (var j=0;j<y;j++){
for (var i=0;i<x;i++){
var cellClone=Instantiate(preCell,Vector3(0,0,0),Quaternion.identity);
cellClone.name="Cell"+cellsNum;
cellsNum+=1;
Field[i,j]= cellClone;
}
minesSet="-1";
}
function Update () {
if (minesSet!="-2" && minesSet!="-1" && minesSet!="-3"){
SetMines();
minesSet="-3";
}
}
function SetMines(){
var c=0;
var i:int;
var j:int;
while (c<totalMines){
i=Random.Range(0,x);
j=Random.Range(0,y);
if (Field[i,j].name!=minesSet){
Field[i,j].GetComponent(scOnCellPress).hasMine=1;
c++;
}
}
So mines are generated after first cell at field is opened. totalMines are set by hand at designtime. 10 for example. But at some reason sometimes only 9 mines are generated. I've already tried to rewrite SetMines() like that:
var c=0;
var i:int;
var j:int;
for (c=0;c<totalMines;c++){
i=Random.Range(0,x);
j=Random.Range(0,y);
if (Field[i,j].name!=minesSet){
Field[i,j].GetComponent(scOnCellPress).hasMine=1;
}
if (Field[i,j].name==minesSet){
c--;
}
}
But that didn't solved the problem.
Tried to rewrite Set$$anonymous$$ines() like that:
function Set$$anonymous$$ines(){
var c=0;
var i:int;
var j:int;
while (c<=total$$anonymous$$ines-1){
i=Random.Range(0,x);
j=Random.Range(0,y);
if (Field[i,j].name!=$$anonymous$$esSet){
Field[i,j].GetComponent(scOnCellPress).has$$anonymous$$ine=1;
c++;
}
if (Field[i,j].name==$$anonymous$$esSet){
Debug.Log("Hit");
}
}
}
In case 9 $$anonymous$$es are generated, the log is empty.
And if rewrite Set$$anonymous$$ines() like that:
function Set$$anonymous$$ines(){
var c=0;
var i:int;
var j:int;
while (c<total$$anonymous$$ines){
i=Random.Range(0,x);
j=Random.Range(0,y);
if (Field[i,j].name!=$$anonymous$$esSet){
Field[i,j].GetComponent(scOnCellPress).has$$anonymous$$ine=1;
c++;
}
if (Field[i,j].name==$$anonymous$$esSet){
Debug.Log("Hit");
}
}
Debug.Log("c="+c);
for (i=0;i<x;i++){
for (j=0;j<y;j++){
if (Field[i,j].GetComponent(scOnCellPress).has$$anonymous$$ine==1){Debug.Log(Field[i,j].name+' has $$anonymous$$e!');}
}
}
}
In case when 9 $$anonymous$$es are generated, log tells:
c=10
UnityEngine.Debug:Log(Object)
Cell10 has $$anonymous$$e!
UnityEngine.Debug:Log(Object)
Cell11 has $$anonymous$$e!
UnityEngine.Debug:Log(Object)
Cell24 has $$anonymous$$e!
UnityEngine.Debug:Log(Object)
Cell15 has $$anonymous$$e!
UnityEngine.Debug:Log(Object)
Cell35 has $$anonymous$$e!
UnityEngine.Debug:Log(Object)
Cell6 has $$anonymous$$e!
UnityEngine.Debug:Log(Object)
Cell7 has $$anonymous$$e!
UnityEngine.Debug:Log(Object)
Cell37 has $$anonymous$$e!
UnityEngine.Debug:Log(Object)
Cell57 has $$anonymous$$e!
UnityEngine.Debug:Log(Object)
Nine $$anonymous$$es, and counter variable at ten. I really don't get it.
And when 10 $$anonymous$$es are generated, counter variable is still at 10.
Answer by $$anonymous$$ · May 04, 2015 at 11:22 PM
Seems like it might be a matter of creating a mine where there is already a mine. Note I'm a c# guy so there may be a couple bugs in the code. Try this:
function SetMines(){
var c=0;
var i:int;
var j:int;
// Create 'totalMines' number of mines
for(c=0;c<totalMines;c++)
{
// keep looping till a mine is placed
while(true)
{
// Get a random point
i=Random.Range(0,x);
j=Random.Range(0,y);
// Check if the point is not yet a mine
if (Field[i,j].name!=minesSet)
{
// Set the Field[i,j] to be a mine
Field[i,j].GetComponent(scOnCellPress).hasMine=1;
// Break out of the current loop and move on to the next mine
break;
}
}
}
}
Wow, I really forgot to check if cell already has $$anonymous$$e.
function Set$$anonymous$$ines(){
var c=0;
var i:int;
var j:int;
while (c<total$$anonymous$$ines){
i=Random.Range(0,x);
j=Random.Range(0,y);
if (Field[i,j].name!=$$anonymous$$esSet && Field[i,j].GetComponent(scOnCellPress).has$$anonymous$$ine==0){
Field[i,j].GetComponent(scOnCellPress).has$$anonymous$$ine=1;
c++;
}
}
}
Its all works great now. Thanks!
Answer by siaran · May 04, 2015 at 02:53 PM
On Phone so bit short but youb should use smaller or equal sign instead of smaller or set amount of mines 1 higher then the number you actually want
Correct me if I'm wrong, but itterating from zero to less than number_of_$$anonymous$$es should work fine too. And the point is that at about one launch from five there is nine $$anonymous$$es generated, not ten, but in other four launches there is all ok: ten $$anonymous$$es are generated.
Sorry you v are right it should work didn't v read it properly formatting is bad on phone. $$anonymous$$ost likely there is a problem with the c if condition in your loop have you checked that. ?