Hey unity developers.
I’m trying to make this algorithm with unity, I’m implementing it with the Unity GUI but I’ll have to make it 3D once this works.
the first problem I have is that the yield instruction isn’t working, I need it to work in order to check where it’s failing easier.
I need it to wait for 1 or 2 seconds before it calls the MoveHorse function again.
I haven’t used the yield instruction much, so could someone please point me in the right direction or tell me where it is I’m messing up.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Board : MonoBehaviour {
public GUISkin gSkin;
public Rect ChessRect = new Rect(20,20,320,320);
public int moves=1;
int inix, iniy;
public bool begin;
public class slot
{
public int step;
public bool used;
public bool horseOn;
}
public slot CurrentSlot = new slot();
public slot EmptySlot = new slot();
public slot[,] ChessBoard = new slot[8,8];
// Use this for initialization
void Start () {
EmptySlot.step=0;
EmptySlot.used=false;
EmptySlot.horseOn=false;
CurrentSlot.step=1;
EmptySlot.used=false;
EmptySlot.horseOn=false;
for(int i=0;i<8;i++)
{
for (int j=0; j<8; j++)
{
ChessBoard[i,j]=EmptySlot;
}
}
}
void OnGUI()
{
if (gSkin)
GUI.skin = gSkin;
else
Debug.Log("StartMenuGUI: GUI Skin object missing!");
//ChessRect = GUI.Window(0, ChessRect, CreateScene, "Place ships");
CreateScene();
}
void CreateScene()
{
for(int i=0; i<8; i++)
{
for(int j=0; j<8; j++)
{
if(ChessBoard[i,j]!=null)
{
bool it = GUI.Button(new Rect(30*i+2, 30*j+2, 30, 30), ChessBoard[i,j].step.ToString());
if(it begin==false)
{
CurrentSlot.step=1;
ChessBoard[i,j] = CurrentSlot;
inix=i;
iniy=j;
begin=true;
Debug.Log(i + " " + j);
}
}
else
GUI.Button(new Rect(30*i+2, 30*j+2, 30, 30), "");
}
}
if(moves <=64 begin)
MoveHorse(ChessBoard, inix, iniy);
}
void MoveHorse(slot[,] ChessBoard, int posx, int posy)
{
int Jump=9;
moves++;
wait();
Jump=possibility(ChessBoard, posx, posy);
Debug.Log(posx + " " + posy);
switch(Jump)
{
case 1:
ChessBoard[posx-1,posy-2]=CurrentSlot;
//if(moves<64 begin)
MoveHorse(ChessBoard, posx-1, posy-2);
wait();
break;
case 2:
ChessBoard[posx+1,posy-2]=CurrentSlot;
//if(moves<64 begin)
MoveHorse(ChessBoard, posx+1, posy-2);
wait();
break;
case 3:
ChessBoard[posx-1,posy+2]=CurrentSlot;
//if(moves<64 begin)
MoveHorse(ChessBoard, posx-1, posy+2);
break;
case 4:
ChessBoard[posx+1,posy+2]=CurrentSlot;
//if(moves<64 begin)
MoveHorse(ChessBoard, posx+1, posy+2);
wait();
break;
case 5:
ChessBoard[posx-2,posy-1]=CurrentSlot;
//if(moves<64 begin)
MoveHorse(ChessBoard, posx-2, posy-1);
wait();
break;
case 6:
ChessBoard[posx-2,posy+1]=CurrentSlot;
//if(moves<64 begin)
MoveHorse(ChessBoard, posx-2, posy+1);
wait();
break;
case 7:
ChessBoard[posx+2,posy-1]=CurrentSlot;
//if(moves<64 begin)
MoveHorse(ChessBoard, posx+2, posy-1);
wait();
break;
case 8:
ChessBoard[posx+2,posy+1]=CurrentSlot;
//if(moves<64 begin)
MoveHorse(ChessBoard, posx+2, posy+1);
wait();
break;
}
wait();
}
int possibility(slot[,] ChessBoard, int posx, int posy)
{
int Best=9;
int temp=0;
int Option=9;
if((posx-1>=0 posx-1<8) (posy-2>=0 posy-2<8) ChessBoard[(posx-1),(posy-2)]!=CurrentSlot)
{
temp=verify((posx-1),(posy-2));
Option=1;
}
if(posx+1>=0 posx+1<8 posy-2>=0 posy-2<8 ChessBoard[(posx+1),(posy-2)]!=CurrentSlot)
{
temp=verify((posx+1),(posy-2));
if(temp<Best)
Option=2;
}
if(posx-1>=0 posx-1<8 posy+2>=0 posy+2<8 ChessBoard[(posx-1),(posy+2)]!=CurrentSlot)
{
temp=verify((posx-1),(posy+2));
if(temp<Best)
Option=3;
}
if((posx+1>=0 posx+1<8) (posy+2>=0 posy+2<8) ChessBoard[(posx+1),(posy+2)]!=CurrentSlot)
{
temp=verify((posx+1),(posy+2));
if(temp<Best)
Option=4;
}
if(posx-2>=0 posx-2<8 posy-1>=0 posy-1<8 ChessBoard[(posx-2),(posy-1)]!=CurrentSlot)
{
temp=verify((posx-2),(posy-1));
if(temp<Best)
Option=5;
}
if(posx-2>=0 posx-2<8 posy+1>=0 posy+1<8 ChessBoard[(posx-2),(posy+1)]!=CurrentSlot)
{
temp=verify((posx-2),(posy+1));
if(temp<Best)
Option=6;
}
if(posx+2>=0 posx+2<8 posy-1>=0 posy-1<8 ChessBoard[(posx+2),(posy-1)]!=CurrentSlot)
{
temp=verify((posx+2),(posy-1));
if(temp<Best)
Option=7;
}
if(posx+2>=0 posx+2<8 posy+1>=0 posy+1<8 ChessBoard[(posx+2),(posy+1)]!=CurrentSlot)
{
temp=verify((posx+2),(posy+1));
if(temp<Best)
Option=8;
}
return Option;
}
int verify(int x, int y)
{
int cont=0;
if(x-1>=0 y-2>=0)
{
if(ChessBoard[(x-1),(y-2)]==CurrentSlot)
cont++;
}
else if(x+1>=0 y-2>=0)
{
if(ChessBoard[(x+1),(y-2)]==CurrentSlot)
cont++;
}
else if(x-1>=0 y+2>=0)
{
if(ChessBoard[(x-1),(y+2)]==CurrentSlot)
cont++;
}
else if(x+1>=0 y+2>=0)
{
if(ChessBoard[(x+1),(y+2)]==CurrentSlot)
cont++;
}
else if(x-2>=0 y-1>=0)
{
if(ChessBoard[(x-2),(y-1)]==CurrentSlot)
cont++;
}
else if(x-2>=0 y+1>=0)
{
if(ChessBoard[(x-2),(y+1)]==CurrentSlot)
cont++;
}
else if(x+2>=0 y-1>=0)
{
if(ChessBoard[(x+2),(y-1)]==CurrentSlot)
cont++;
}
else if(x+2>=0 y+1>=0)
{
if(ChessBoard[(x+2),(y+1)]==CurrentSlot)
cont++;
}
return cont;
}
IEnumerator wait()
{
yield return new WaitForSeconds(5);
}
}
Thank you All…