Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

141 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

instantiated gameobject position is not working 0 Answers

Not all code paths return a value 1 Answer

this vs gameobject 2 Answers

How to find the transform position of another gameobject then move a gameobject to that position? 1 Answer

How get position from a game object? 5 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges