Показать сообщение отдельно
Старый 25.10.2011, 02:15   #4
pax
Unity/C# кодер
 
Аватар для pax
 
Регистрация: 03.10.2005
Адрес: Россия, Рязань
Сообщений: 7,568
Написано 3,006 полезных сообщений
(для 5,323 пользователей)
Ответ: Листинг и сравнение, нужна помощь

Вот
using System.Collections.Generic;
using UnityEngine;

public class 
Numbers : MonoBehaviour
{

    public class 
Button
    
{
        public 
bool opend;
        public 
string contents;
    }

    private List<
Button> _buttons = new List<Button>();

    
void Start()
    {
        for (
int i = 0; i < 8; i++)
        {
            
_buttons.Add(new Button { contents = i.ToString() });
        }

        for (
int i = 0; i < 8; i++)
        {
            
_buttons.Add(new Button { contents = (7-i).ToString() });
        }
    }

    private 
Button _last = null;

    private 
bool _win;

    
void OnGUI()
    {
        if (
_win)
        {
            
GUI.Label(new Rect(10,10,100,30), "You Win!" );
            return;
        }

        for (
int i = 0; i < 4; i++)
        {
            for (
int j = 0; j < 4; j++)
            {
                
int index = i*4 + j;

                var 
rect = new Rect(10 + i*60, 10 + j*60, 50, 50);

                if (
GUI.Button(rect, _buttons[index].opend ? _buttons[index].contents : "?"))
                {
                    if (
_buttons[index].opend) continue;
                    if(
_last== null)
                    {
                        
_buttons[index].opend = true;
                        
_last = _buttons[index];
                    }
                    else if (
_last.contents == _buttons[index].contents)
                    {
                        
_last = null;
                        
_buttons[index].opend = true;

                        
_win = true;
                        foreach (var 
button in _buttons)
                        {
                            if(!
button.opend)
                            {
                                
_win = false;
                                break;
                            }
                        }
                    }
                    else
                    {
                        foreach (var 
button in _buttons)
                        {
                            
button.opend = false;
                        }
                        
_last = null;
                    }
                }

            }
        }

    }
} 


UPD: Вариант с закрытием только последних кнопок
using System.Collections.Generic;
using UnityEngine;

public class 
Numbers : MonoBehaviour
{

    public class 
Button
    
{
        public 
bool opend;
        public 
string contents;
    }

    private List<
Button> _buttons = new List<Button>();

    
void Start()
    {
        for (
int i = 0; i < 8; i++)
        {
            
_buttons.Add(new Button { contents = i.ToString() });
        }

        for (
int i = 0; i < 8; i++)
        {
            
_buttons.Add(new Button { contents = (7-i).ToString() });
        }
    }

    private 
Button _last = null;

    private 
bool _win;

    
void OnGUI()
    {
        if (
_win)
        {
            
GUI.Label(new Rect(10,10,100,30), "You Win!" );
            return;
        }

        for (
int i = 0; i < 4; i++)
        {
            for (
int j = 0; j < 4; j++)
            {
                
int index = i*4 + j;

                var 
rect = new Rect(10 + i*60, 10 + j*60, 50, 50);

                if (
GUI.Button(rect, _buttons[index].opend ? _buttons[index].contents : "?"))
                {
                    if (
_buttons[index].opend) continue;
                    if(
_last== null)
                    {
                        
_buttons[index].opend = true;
                        
_last = _buttons[index];
                    }
                    else if (
_last.contents == _buttons[index].contents)
                    {
                        
_last = null;
                        
_buttons[index].opend = true;

                        
_win = true;
                        foreach (var 
button in _buttons)
                        {
                            if(!
button.opend)
                            {
                                
_win = false;
                                break;
                            }
                        }
                    }
                    else
                    {
                        
_last.opend = false;
                        
_last = null;
                    }
                }

            }
        }

    }
} 
__________________
Blitz3d to Unity Wiki
(Offline)
 
Ответить с цитированием
Эти 4 пользователя(ей) сказали Спасибо pax за это полезное сообщение:
FireOwl (25.10.2011), L-ee-X (25.10.2011), Lestar (25.10.2011), Reks888 (25.10.2011)