Question by
digdown · Aug 03, 2017 at 08:09 AM ·
gameobjectpositionreturnthis
Minesweeper - Surrounding Objects - C#
I am having trouble finding the neighbors in a simple classic minesweeper game.
the idea is: if this gameobject, located at (x,y,0).mineBool == true return true;
leaving the function nextDoorMines as static returns that "this" is not valid in a static function, if I remove static it tells me that an object reference is required.
I thought I had a good handle on what is happening, maybe I'm just tired...but I can't figure out how to resolve this problem.
here is the code, if anyone is able to help it would be greatly appreciated:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class remakeGamePlay : MonoBehaviour {
public bool flaggedBool;
public bool mineBool;
public bool uncoveredBool;
public Sprite flaggedTex;
public Sprite mineTex;
public Sprite[] numTexts;
public GameObject[] gamePieces;
public static int w = 9;
public static int h = 6;
public static remakeGamePlay[,] gameplay = new remakeGamePlay[w, h];
void OnMouseUp(){
print (this.transform.position);
if (flaggedBool==false) {
if (mineBool == true) {
print ("you lose");
hitMine ();
} else {
int x = (int)this.transform.position.x;
int y = (int)this.transform.position.y;
// print (x);
//print (y);
floodFillUncover (x, y, new bool[w, h]);
whatTexture (nextDoorMines (x, y));
for (int i = 0; i < gamePieces.Length; i++) {
if (gamePieces [i].GetComponent<remakeGamePlay> ().isUnclicked () && gamePieces [i].GetComponent<remakeGamePlay> ().mineBool == false)
print ("you win");
}
}
} else {
}
}
public static void floodFillUncover(int x, int y, bool[,] visited){
if (x >= 0 && y >= 0 && x < w && y < h) {
if (visited [x, y])
return;
gameplay [x, y].whatTexture (nextDoorMines (x, y));
if (nextDoorMines (x, y) > 0)
return;
visited [x, y] = true;
floodFillUncover (x - 1, y, visited);
floodFillUncover (x + 1, y, visited);
floodFillUncover (x, y -1, visited);
floodFillUncover (x, y +1, visited);
}
}
// public static bool doneYet(){
// //return false;
// for (int i = 0; i < gamePieces.Length; i++) {
// if (gamePieces[i].GetComponent<remakeGamePlay>().isUnclicked () && gamePieces[i].GetComponent<remakeGamePlay>().mineBool == false)
// return false;
//
//
//
// return true;
//
//
// }
//
// }
public static bool mineLoc(int x, int y){
if (x >= 0 && y >= 0 && x < w && y < h)
//return true;
return gameplay [x, y].GetComponent<remakeGamePlay> ().mineBool;
//if (gameplay [x, y].mineBool == true)
//if (gameplay [x, y].GetComponent<GameObject>().GetComponent<remakeGamePlay> ().mineBool == true)
// if (this.transform.position == Vector3(x,y,0) && this.GetComponent<remakeGamePlay> ().mineBool == true)
// if (gameplay[x,y].mineBool == true)
return true;
//return gameplay [x, y].GetComponent<GameObject>().transform.position.x.GetComponent<remakeGamePlay> ().mineBool;
return false;
}
public static int nextDoorMines(int x, int y ){
int count = 0;
if (this.transform.position.x == x && this.transform.position.y == y + 1 && this.mineBool == true)
++count;
// if (mineLoc (x, y + 1))++count;
// if (mineLoc (x+1, y + 1))++count;
// if (mineLoc (x+1, y))++count;
// if (mineLoc (x+1, y - 1))++count;
// if (mineLoc (x, y - 1))++count;
// if (mineLoc (x-1, y - 1))++count;
// if (mineLoc (x-1, y))++count;
// if (mineLoc (x-1, y + 1))++count;
return count;
}
public void hitMine(){
for (int i = 0; i < gamePieces.Length; i++) {
if (gamePieces[i].GetComponent<remakeGamePlay>().mineBool == true)
gamePieces[i].GetComponent<remakeGamePlay>().whatTexture (0);
}
}
public bool isUnclicked(){
return GetComponent<SpriteRenderer> ().sprite.texture.name == "default";
}
public void whatTexture(int surroundingMines){
if (mineBool ==true) {
GetComponent<SpriteRenderer> ().sprite = mineTex;
} else if (flaggedBool) {
GetComponent<SpriteRenderer> ().sprite = flaggedTex;
} else {
GetComponent<SpriteRenderer> ().sprite = numTexts[surroundingMines];
}
}
// Use this for initialization
void Start () {
gamePieces = GameObject.FindGameObjectsWithTag ("gb");
mineBool = Random.value < 0.15;
int x = (int)this.transform.position.x;
int y = (int)this.transform.position.y;
}
// Update is called once per frame
void Update () {
}
}
Comment