Hola todos, soy nuevo en el foro pero hace tiempo lo visito y me a servido mucho.
llendo al grano actualmente estoy haciendo un juego de tipo connect 4 o 4 in a row; tengo hecho el script y ya logre que cuando haya 4 esferas de un mismo color en fila o columna me de un mensaje de ganador pero no tengo la mas minima idea de como lograr que chequee o verifique si hay 4 en diagonal hacia arriba/abajo de un ganador tambien, alguien me podria ayudar/explicar un poco, les agradeceria mucho.
me siento algo frustrado xDdejo el codigo completo aqui:
using UnityEngine;
using System.Collections;
public class Grid2D : MonoBehaviour {
public int width;
public int height;
public GameObject puzzlePiece;
private GameObject[,] grid;
public int cont;
public int cont2;
public int cont3;
public int cont4;
public int cont5;
public int cont6;
public int cont7;
Color col = Color.white;
void Start ()
{
grid = new GameObject[width =10, height=10];
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
GameObject go = GameObject.Instantiate (puzzlePiece) as GameObject;
Vector3 position = new Vector3(x, y, 0);
go.transform.position = position;
grid [x, y] = go;
}
}
}
void Update ()
{
Vector3 mPosition = Camera.main.ScreenToWorldPoint
(Input.mousePosition);
UpdatePickedPiece(mPosition);
}
void UpdatePickedPiece(Vector3 position)
{
int x = (int)(position.x + 0.5f);
int y = (int)(position.y + 0.5f);
if (x >= 0 && y >= 0 && x < width && y < height)
{
GameObject go = grid [x, y];
if (Input.GetMouseButtonDown (0))
{
if (go.GetComponent<Renderer>().material.color == col)
{
cont++;
if (cont % 2 == 0)
{
go.GetComponent<Renderer> ().material.SetColor ("_Color", Color.red);
filas (x,y,Color.red);
columnas (x,y,Color.red);
}
else
{
go.GetComponent<Renderer> ().material.SetColor ("_Color", Color.black);
filas2 (x,y,Color.black);
columnas2 (x,y,Color.black);
}
}
else
{
cont--;
}
}
}
}
void filas(int x, int y, Color col)
{
for(int _x = 0; _x < width; _x++)
{
if(grid[_x,y].GetComponent<Renderer>().material.color == col)
{
cont3++;
if(cont3 == 5)
{
print("gano rojo");
}
}
else
{
cont3 = 1;
}
}
}
void filas2(int x, int y, Color col)
{
for(int _x = 0; _x < width; _x++)
{
if(grid[_x,y].GetComponent<Renderer>().material.color == col)
{
cont4++;
if(cont4 == 5)
{
print("gano negro");
}
}
else
{
cont4 = 1;
}
}
}
void columnas(int x, int y, Color col)
{
for(int _y = 0; _y < height; _y++)
{
if(grid[x,_y].GetComponent<Renderer>().material.color == col)
{
cont5++;
if(cont5 == 5)
{
print("gano rojo");
}
}
else
{
cont5 = 1;
}
}
}
void columnas2(int x, int y, Color col)
{
for(int _y = 0; _y < height; _y++)
{
if(grid[x,_y].GetComponent<Renderer>().material.color == col)
{
cont6++;
if(cont6 == 5)
{
print("gano negro");
}
}
else
{
cont6 = 1;
}
}
}
}