Need Help with the HorseJump Algorithm

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…

Hi

Your wait coroutine need a StartCoroutine to start, try this

StartCoroutine( wait() );

Instead

wait();

Thx Marata, I totally missed that but I tried it and it didn’t work either…I’m guessing it is because I call the wait() in a function called on OnGUI()

I’l try to do this tomorrow, any tips on doing this will be greatly appreciated.

Thank you all and I’ll post the whole code once I finish it :wink:

mmm I see the problem, you want to use your wait function like a Sleep function on a procedural programming language, the problem here is that your code, with the StartCoroutine fix is working, but not the way you want.

Your coroutine must be any function you want to delay in it, for example:

   void OnGUI( )
   {
      HelperFunction( );
   }

   void HelperFunction( )
   {
      StartCoroutine( SampleFunction( ) );
   }
 
   IEnumerator SampleFunction( )
   {
           //DO FIRST
            //Wait n seconds
            yield return new WaitForSeconds( n );
           //DO SECOND
           //Wait m millis
           yield return new WaitForSeconds( m );
   }

Here is about implementing coroutines in OnGui functions, hope this helps to you. :wink:

http://forum.unity3d.com/threads/29804-Best-practices-OnGUI-and-coroutines

Hey Marata, Thx A lot for your help

Here’s the code, though it doesn’t work as intended yet…it fills 2 slots at once anyhow but not always, if u run it you’ll see what I mean…Imma work on this tomorrow, the university is killing my time :stuck_out_tongue:

TY Again

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 = false;
	
	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 Update()
	{
		if(begin)
			StartCoroutine(MoveHorse(ChessBoard, inix, iniy));
	}
	
	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), "");
			
			}
						
		}
		

		
		
				
					
	}
	
	
	IEnumerator MoveHorse(slot[,] ChessBoard, int posx, int posy)
	{
		int Jump=9;
		moves++;
		
		//StartCoroutine(wait(2.0F));
		Jump=possibility(ChessBoard, posx, posy);
		
	
		yield return new WaitForSeconds(2);
		
		Debug.Log(posx + " " + posy);
		
		switch(Jump)
		{
		case 1:
			ChessBoard[posx-1,posy-2]=CurrentSlot;
			//if(moves<64  begin)
				yield return StartCoroutine(MoveHorse(ChessBoard, posx-1, posy-2));
						
			break;
		case 2:
			ChessBoard[posx+1,posy-2]=CurrentSlot;
			//if(moves<64  begin)
				yield return StartCoroutine(MoveHorse(ChessBoard, posx+1, posy-2));
			
			break;
		case 3:
			ChessBoard[posx-1,posy+2]=CurrentSlot;
			//if(moves<64  begin)
				yield return StartCoroutine(MoveHorse(ChessBoard, posx-1, posy+2));
			
			
			break;
		case 4:
			ChessBoard[posx+1,posy+2]=CurrentSlot;
			//if(moves<64  begin)
				yield return StartCoroutine(MoveHorse(ChessBoard, posx+1, posy+2));
			
			
			break;
		case 5:
			ChessBoard[posx-2,posy-1]=CurrentSlot;
			//if(moves<64  begin)
				yield return StartCoroutine(MoveHorse(ChessBoard, posx-2, posy-1));
			
			
			break;
		case 6:
			ChessBoard[posx-2,posy+1]=CurrentSlot;
			//if(moves<64  begin)
				yield return StartCoroutine(MoveHorse(ChessBoard, posx-2, posy+1));
			
			
			break;
		case 7:
			ChessBoard[posx+2,posy-1]=CurrentSlot;
			//if(moves<64  begin)
				yield return StartCoroutine(MoveHorse(ChessBoard, posx+2, posy-1));
			
			
			break;
		case 8:
			ChessBoard[posx+2,posy+1]=CurrentSlot;
			//if(moves<64  begin)
				yield return StartCoroutine(MoveHorse(ChessBoard, posx+2, posy+1));
			
			
			break;
		}
		
		
		
		
	}
	
	int possibility(slot[,] ChessBoard, int posx, int posy)
	{
		int Best=9;
		int temp=0;
		int Option=0;

		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)
			{
				Best=temp;
				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)
			{
				Best=temp;
				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)
			{
				Best=temp;
				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)
			{
				Best=temp;
				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)
			{
				Best=temp;
				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)
			{
				Best=temp;
				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)
			{
				Best=temp;
				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)
			{
				Best=temp;
				Option=8;
			}
		}
		Debug.Log(temp);
		return Option;	
	}
	
	int verify(int x, int y)
	{
		int cont=0;
		if((x-1>=0  x-1<8)  (y-2>=0  y-2<8)  ChessBoard[(x-1),(y-2)]!=CurrentSlot)
				cont++;
		
		if(x+1>=0  x+1<8  y-2>=0  y-2<8  ChessBoard[(x+1),(y-2)]!=CurrentSlot)
				cont++;
		
		if(x-1>=0  x-1<8  y+2>=0  y+2<8  ChessBoard[(x-1),(y+2)]!=CurrentSlot)
				cont++;
		
		if((x+1>=0  x+1<8)  (y+2>=0  y+2<8)  ChessBoard[(x+1),(y+2)]!=CurrentSlot)
				cont++;
		
		if(x-2>=0  x-2<8  y-1>=0  y-1<8  ChessBoard[(x-2),(y-1)]!=CurrentSlot)
				cont++;
		
		if(x-2>=0  x-2<8  y+1>=0  y+1<8  ChessBoard[(x-2),(y+1)]!=CurrentSlot)
				cont++;
		
		if(x+2>=0  x+2<8  y-1>=0  y-1<8  ChessBoard[(x+2),(y-1)]!=CurrentSlot)
				cont++;
		
		if(x+2>=0  x+2<8  y+1>=0  y+1<8  ChessBoard[(x+2),(y+1)]!=CurrentSlot)
				cont++;
		
		return cont;
	}
	
	IEnumerator wait(float waitTime)
	{
		yield return new WaitForSeconds(waitTime);
		
	}
}

Try to test your logic first with debug output, then code the coroutines.

I hope you have it working soon.

Done :smile:, I was busy with the university so I had not have time to finish this…but here it is…Thx a lot Marata for helping me out

it still needs the 3D functionality

//BackTracking Algorithm

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class KnightsTour : MonoBehaviour {
	
	public GUISkin gSkin;
	public Texture2D BackDrop;
	public delegate void GUIMethod(); //Delegates, switching between windows
    public GUIMethod currentGUIMethod; //
	
	private Rect ChessRect = new Rect(370,230,320,320);
	private Rect MainMenuRect = new Rect(390,200,200,200);
	

	int inix, iniy, rows=8, columns=8;
	public bool begin = false;
	public bool running = false;
	private int moves=0;
	
	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 = null;

	// Use this for initialization
	void Start () {
		
		rows=8;
		columns=8;
		EmptySlot.step=0;
		EmptySlot.used=false;
		EmptySlot.horseOn=false;	
		
		CurrentSlot.step=1;
		EmptySlot.used=false;
		EmptySlot.horseOn=false;
			
			
		
		this.currentGUIMethod=MainMenu;
	}
	
	void beginScene()
	{
		for(int i=0;i<rows;i++)
			{
				for (int j=0; j<columns; j++)
				{
					ChessBoard[i,j]=EmptySlot;
				}
			}
	}

	void Update()
	{
		if(begin)
		{
			StartCoroutine(MoveHorse(ChessBoard, inix, iniy));
		}
		begin=false;
	}
	
	void OnGUI()
	{
				 if (gSkin)
					 GUI.skin = gSkin;
				 else
					 Debug.Log("StartMenuGUI: GUI Skin object missing!");
		
		GUIStyle backgroundStyle = new GUIStyle();
		backgroundStyle.normal.background = BackDrop;
		
		GUI.Label ( new Rect( 0, 0, 1024,768), "", backgroundStyle);
			
		//ChessRect = GUI.Window(0, ChessRect, CreateScene, "Place ships");
		//CreateScene();
		
		GUI.Label(new Rect(345,120,100,30), "Knight's Tour", "Titles");
		
		this.currentGUIMethod();
		
	}
	
	void MainMenu()
	{
		MainMenuRect = GUI.Window (0, MainMenuRect, Menu, "MAIN MENU");
	}
	
	void Scene()
	{
		ChessRect = GUI.Window (1, ChessRect, CreateScene, "", "EmptyWindow");
	}
	
	
	void Menu(int WindowID)
	{
		if(GUI.Button(new Rect(50,20,100,30), "Begin 8x8"))
		{
			rows=8;
			columns=8;
			moves=0;
			running=false;
			ChessBoard = new slot[rows,columns];
			beginScene();
			this.currentGUIMethod=Scene;
		}
		if(GUI.Button(new Rect(50,60,100,30), "Begin 6x6"))
		{
			rows=6;
			columns=6;
			moves=0;
			running=false;
			ChessBoard = new slot[rows,columns];
			beginScene();
			this.currentGUIMethod=Scene;
		}
		if(GUI.Button(new Rect(50,100,100,30), "Begin 5x5"))
		{
			rows=5;
			columns=5;
			moves=0;
			running=false;
			ChessBoard = new slot[rows,columns];
			beginScene();
			this.currentGUIMethod=Scene;
		}
	}
	
	void CreateScene(int WindowID)
	{	
		
		for(int i=0; i<rows; i++)
		{
			for(int j=0; j<columns; j++)
			{
				if(ChessBoard[i,j]!=null)
				{
					//Se le asigna cada boton a una variable para poder ser llamado
					bool it = GUI.Button(new Rect(30*i+2, 30*j+2, 30, 30), ChessBoard[i,j].step.ToString()); 
					//Si la casilla es CurrentSlot se le cambia el estilo al boton
					if(ChessBoard[i,j]==CurrentSlot)
						GUI.Button(new Rect(30*i+2, 30*j+2, 30, 30), ChessBoard[i,j].step.ToString(), "Used");
					//sino se deja por defecto
					else
						GUI.Button(new Rect(30*i+2, 30*j+2, 30, 30), ChessBoard[i,j].step.ToString());
					//si se presiona el boton y begin es igual a falso se inicia el algoritmo desde esa posicion
					if(it  begin==false  running==false)
					{
						ChessBoard[i,j] = CurrentSlot; //Se le asigna a la casilla que era EmptySlot por defecto, CurrentSlot
						inix=i; //se asigna la posicion inicial en x
						iniy=j; //se asigna la posicion inicial en y
						begin=true; //begin se pasa a verdadero para comenzar el algoritmo
						running=true; //running se pasa a verdadero para evitar que se pueda ejecutar varias veces
						Debug.Log(i + " " + j); //Para probar las posiciones
					}					
				}			
			}						
		}
		
		if(GUI.Button(new Rect(20, 290, 100, 30), "Back"))
		{
			beginScene();
			this.currentGUIMethod=MainMenu;
		}
	}
	
	
	IEnumerator MoveHorse(slot[,] ChessBoard, int posx, int posy)
	{
		int Jump=9;
		moves++;
		
		//StartCoroutine(wait(2.0F));
		if(ChessBoard[posx,posy].step==1)
		{
			Jump=possibility(ChessBoard, posx, posy);
			Debug.Log(posx + " " + posy);			
		}
		
		yield return new WaitForSeconds(2);
		
		
		
		switch(Jump)
		{
		case 1:
			ChessBoard[posx-1,posy-2]=CurrentSlot;
			Jump=9;
			//if(moves<64  begin)
				yield return StartCoroutine(MoveHorse(ChessBoard, posx-1, posy-2));						
			break;
		case 2:
			ChessBoard[posx+1,posy-2]=CurrentSlot;
			Jump=9;
			//if(moves<64  begin)
				yield return StartCoroutine(MoveHorse(ChessBoard, posx+1, posy-2));			
			break;
		case 3:
			ChessBoard[posx-1,posy+2]=CurrentSlot;
			Jump=9;
			//if(moves<64  begin)
				yield return StartCoroutine(MoveHorse(ChessBoard, posx-1, posy+2));			
			break;
		case 4:
			ChessBoard[posx+1,posy+2]=CurrentSlot;
			Jump=9;
			//if(moves<64  begin)
				yield return StartCoroutine(MoveHorse(ChessBoard, posx+1, posy+2));			
			break;
		case 5:
			ChessBoard[posx-2,posy-1]=CurrentSlot;
			Jump=9;
			//if(moves<64  begin)
				yield return StartCoroutine(MoveHorse(ChessBoard, posx-2, posy-1));			
			break;
		case 6:
			ChessBoard[posx-2,posy+1]=CurrentSlot;
			Jump=9;
			//if(moves<64  begin)
				yield return StartCoroutine(MoveHorse(ChessBoard, posx-2, posy+1));			
			break;
		case 7:
			ChessBoard[posx+2,posy-1]=CurrentSlot;
			Jump=9;
			//if(moves<64  begin)
				yield return StartCoroutine(MoveHorse(ChessBoard, posx+2, posy-1));			
			break;
		case 8:
			ChessBoard[posx+2,posy+1]=CurrentSlot;
			Jump=9;
			//if(moves<64  begin)
				yield return StartCoroutine(MoveHorse(ChessBoard, posx+2, posy+1));			
			break;
		}	
		
		
	}
	
	int possibility(slot[,] ChessBoard, int posx, int posy)
	{
		int Best=9;
		int temp=0;
		int Option=0;

		if((posx-1>=0  posx-1<rows)  (posy-2>=0  posy-2<columns)  ChessBoard[(posx-1),(posy-2)]!=CurrentSlot)
		{
			temp=verify((posx-1),(posy-2));
			if(temp<=Best)
			{
				Best=temp;
				Option=1;
			}
		}
		if(posx+1>=0  posx+1<rows  posy-2>=0  posy-2<columns  ChessBoard[(posx+1),(posy-2)]!=CurrentSlot)
		{
			temp=verify((posx+1),(posy-2));
			if(temp<=Best)
			{
				Best=temp;
				Option=2;
			}
		}
		if(posx-1>=0  posx-1<rows  posy+2>=0  posy+2<columns  ChessBoard[(posx-1),(posy+2)]!=CurrentSlot)
		{
			temp=verify((posx-1),(posy+2));
			if(temp<=Best)
			{
				Best=temp;
				Option=3;
			}
		}
		if((posx+1>=0  posx+1<rows)  (posy+2>=0  posy+2<columns)  ChessBoard[(posx+1),(posy+2)]!=CurrentSlot)
		{
			temp=verify((posx+1),(posy+2));
			if(temp<=Best)
			{
				Best=temp;
				Option=4;
			}
		}
		if(posx-2>=0  posx-2<rows  posy-1>=0  posy-1<columns  ChessBoard[(posx-2),(posy-1)]!=CurrentSlot)
		{
			temp=verify((posx-2),(posy-1));
			if(temp<=Best)
			{
				Best=temp;
				Option=5;
			}
		}
		if(posx-2>=0  posx-2<rows  posy+1>=0  posy+1<columns  ChessBoard[(posx-2),(posy+1)]!=CurrentSlot)
		{
			temp=verify((posx-2),(posy+1));
			if(temp<=Best)
			{
				Best=temp;
				Option=6;
			}
		}
		if(posx+2>=0  posx+2<rows  posy-1>=0  posy-1<columns  ChessBoard[(posx+2),(posy-1)]!=CurrentSlot)
		{
			temp=verify((posx+2),(posy-1));
			if(temp<=Best)
			{
				Best=temp;
				Option=7;
			}
		}
		if(posx+2>=0  posx+2<rows  posy+1>=0  posy+1<columns  ChessBoard[(posx+2),(posy+1)]!=CurrentSlot)
		{
			temp=verify((posx+2),(posy+1));
			if(temp<=Best)
			{
				Best=temp;
				Option=8;
			}
		}
		
		return Option;	
	}
	
	int verify(int x, int y)
	{
		int cont=0;
		if((x-1>=0  x-1<rows)  (y-2>=0  y-2<columns)  ChessBoard[(x-1),(y-2)]!=CurrentSlot)
				cont++;
		
		if(x+1>=0  x+1<rows  y-2>=0  y-2<columns  ChessBoard[(x+1),(y-2)]!=CurrentSlot)
				cont++;
		
		if(x-1>=0  x-1<rows  y+2>=0  y+2<columns  ChessBoard[(x-1),(y+2)]!=CurrentSlot)
				cont++;
		
		if((x+1>=0  x+1<rows)  (y+2>=0  y+2<columns)  ChessBoard[(x+1),(y+2)]!=CurrentSlot)
				cont++;
		
		if(x-2>=0  x-2<rows  y-1>=0  y-1<columns  ChessBoard[(x-2),(y-1)]!=CurrentSlot)
				cont++;
		
		if(x-2>=0  x-2<rows  y+1>=0  y+1<columns  ChessBoard[(x-2),(y+1)]!=CurrentSlot)
				cont++;
		
		if(x+2>=0  x+2<rows  y-1>=0  y-1<columns  ChessBoard[(x+2),(y-1)]!=CurrentSlot)
				cont++;
		
		if(x+2>=0  x+2<rows  y+1>=0  y+1<columns  ChessBoard[(x+2),(y+1)]!=CurrentSlot)
				cont++;
	
		return cont;
	}

}