using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CheckerScript1 : MonoBehaviour
{
private string checkerId;
public int checkerPos; //index
public bool isKing = false;
private bool isSelecteded = false;
private List<int> possibleMoves;
//private Hashtable hitCheckerList;
private HashTableAlterClass hitCheckerList;
private bool isHitteded;
private int column = 8;
public GameObject scroeKeeper;
public Vector3 movePosition;
// Use this for initialization
//public CheckerScript1(string checkerId, int checkerPos,GameObject scoreKeeper)
//{
// this.checkerId = checkerId;
// this.checkerPos = checkerPos;
// this.scroeKeeper = scroeKeeper;
//}
void Start()
{
// hitCheckerList = new Hashtable();
hitCheckerList = new HashTableAlterClass();
possibleMoves = new List<int>();
checkerId = gameObject.tag;
}
// Update is called once per frame
void Update()
{
}
public bool checkPossibleRectOnBoardUpward(string checkerId, bool clearPossibleMoves)
{
if (clearPossibleMoves)
{
hitCheckerList.Clear();
possibleMoves.Clear();
}
checkUpwardPos(checkerId);
if (isKing)
{
checkDownWordPos(checkerId);
}
if (possibleMoves.Count == 0)
return false;
else
return true;
}
public void checkUpwardPos(string checkerId)
{
int currentPosRowNo = checkerPos / column;
int firstColNoOfRowOfCurrentPos = currentPosRowNo * column;
int lastColNoOfRowOfCurrentPos = firstColNoOfRowOfCurrentPos
+ column - 1;
int possiblePos1 = checkerPos - 9;
int possiblePos2 = checkerPos - 7;
int rowOfPossiblePos1 = possiblePos1 / column;
int rowOfPossiblePos2 = possiblePos2 / column;
if (possiblePos1 >= 0 && possiblePos1 <= 63
&& rowOfPossiblePos1 == currentPosRowNo - 1
&& rowOfPossiblePos1 >= 0)
{
if (scroeKeeper.GetComponent<ScoreKeeperAI>().checkersBoxs[possiblePos1].GetComponent<CheckerBox>().isEmpty == false)
{
possibleMoves.Add(possiblePos1);
Debug.Log("upwardpos1:"+checkerPos);
}
else
{
GameObject checker = scroeKeeper.GetComponent<ScoreKeeperAI>().checkersBoxs[possiblePos1].GetComponent<CheckerBox>().currentChecker;
Debug.Log("upwardjump1:"+checkerPos);
if (!checker.CompareTag(checkerId))
{
// possibleMoves.Clear();
int possiblePos1AfterJump = possiblePos1 - 9;
int rowOfPossiblePos1afterJump = possiblePos1AfterJump
/ column;
if (rowOfPossiblePos1afterJump == rowOfPossiblePos1 - 1
&& rowOfPossiblePos1afterJump >= 0
&& possiblePos1AfterJump >= 0
&& possiblePos1AfterJump <= 63)
{
if (scroeKeeper.GetComponent<ScoreKeeperAI>().checkersBoxs[possiblePos1AfterJump].GetComponent<CheckerBox>().isEmpty == false)
{
//possibleMoves.Clear();
for (int i = 0; i < scroeKeeper.GetComponent<ScoreKeeperAI>().playerOneCheckers.Count; i++)
{
foreach(GameObject gm in scroeKeeper.GetComponent<ScoreKeeperAI>().playerOneCheckers){
possibleMoves.Clear();
}
if (scroeKeeper.GetComponent<ScoreKeeperAI>().playerOneCheckers*.GetComponent<CheckerScript1>().checkerPos == possiblePos1)*
{
-
possibleMoves.Clear();*
-
hitCheckerList.Add(possiblePos1AfterJump, scroeKeeper.GetComponent<ScoreKeeperAI>().playerOneCheckers*
);
}
}
possibleMoves.Add(possiblePos1AfterJump);
}
}
}
* }*
}
if (possiblePos2 >= 0 && possiblePos2 <= 63
&& rowOfPossiblePos2 == currentPosRowNo - 1)
{
* if (scroeKeeper.GetComponent().checkersBoxs[possiblePos2].GetComponent().isEmpty == false)*
{
possibleMoves.Add(possiblePos2);
* Debug.Log(“upwardpos2:”+checkerPos);*
}
else
{
* GameObject checker = scroeKeeper.GetComponent().checkersBoxs[possiblePos2].GetComponent().currentChecker;*
* Debug.Log(“upwardjump2:”+checkerPos);*
if (!checker.CompareTag(checkerId))
{
// possibleMoves.Clear();
int possiblePos2AfterJump = possiblePos2 - 7;
int rowOfPossiblePos1afterJump = possiblePos2AfterJump
/ column;
if (rowOfPossiblePos1afterJump == rowOfPossiblePos2 - 1
&& rowOfPossiblePos1afterJump >= 0
&& possiblePos2AfterJump >= 0
&& possiblePos2AfterJump <= 63)
{
if (scroeKeeper.GetComponent().checkersBoxs[possiblePos2AfterJump].GetComponent().isEmpty == false)
{
* //possibleMoves.Clear();*
for (int i = 0; i < scroeKeeper.GetComponent().playerOneCheckers.Count; i++)
{
* foreach(GameObject gm in scroeKeeper.GetComponent().playerOneCheckers){*
* possibleMoves.Clear();*
* }*
_ if (scroeKeeper.GetComponent().playerOneCheckers*.GetComponent().checkerPos == possiblePos2)
{
possibleMoves.Clear();
hitCheckerList.Add(possiblePos2AfterJump, scroeKeeper.GetComponent().playerOneCheckers);
}
}
possibleMoves.Add(possiblePos2AfterJump);
}
}
}
}
}
}
public bool checkPossibleRectOnBoardDownWard(string checkerId,
bool clearPossibleMoves)
{
if (clearPossibleMoves)
{
hitCheckerList.Clear();
possibleMoves.Clear();
}
checkDownWordPos(checkerId);
if (isKing)
{
checkUpwardPos(checkerId);
}
if (possibleMoves.Count == 0)
return false;
else
return true;
}
public void checkDownWordPos(string checkerId)
{
int currentPosRowNo = checkerPos / column;
int firstColNoOfRowOfCurrentPos = currentPosRowNo * column;
int lastColNoOfRowOfCurrentPos = firstColNoOfRowOfCurrentPos
+ column - 1;
int possiblePos1 = checkerPos + 9;
int possiblePos2 = checkerPos + 7;
int rowOfPossiblePos1 = possiblePos1 / column;
int rowOfPossiblePos2 = possiblePos2 / column;
if (possiblePos1 >= 0 && possiblePos1 <= 63
&& rowOfPossiblePos1 == currentPosRowNo + 1*
&& rowOfPossiblePos1 <= 7)
{
* if (scroeKeeper.GetComponent().checkersBoxs[possiblePos1].GetComponent().isEmpty == false)
{
possibleMoves.Add(possiblePos1);
Debug.Log(“downwardpos1:”+checkerPos);
}
else
{
GameObject checker = scroeKeeper.GetComponent().checkersBoxs[possiblePos1].GetComponent().currentChecker;
Debug.Log(“downwardjump1:”+checkerPos);
if (!checker.CompareTag(checkerId))
{
// possibleMoves.Clear();
int possiblePos1AfterJump = possiblePos1 + 9;
int rowOfPossiblePos1afterJump = possiblePos1AfterJump
/ column;
if (rowOfPossiblePos1afterJump == rowOfPossiblePos1 + 1
&& rowOfPossiblePos1afterJump <= 7*
&& possiblePos1AfterJump >= 0
&& possiblePos1AfterJump <= 63)
{
if (scroeKeeper.GetComponent().checkersBoxs[possiblePos1AfterJump].GetComponent().isEmpty == false)
{
* //possibleMoves.Clear();
for (int i = 0; i < scroeKeeper.GetComponent().playerOneCheckers.Count
; i++)
{
foreach(GameObject gm in scroeKeeper.GetComponent().playerOneCheckers){
possibleMoves.Clear();
}
if (scroeKeeper.GetComponent().playerOneCheckers.GetComponent()
.getCheckerPos() == possiblePos1)
{
possibleMoves.Clear();
hitCheckerList.Add(possiblePos1AfterJump,_
_scroeKeeper.GetComponent().playerOneCheckers);
}
}
possibleMoves.Add(possiblePos1AfterJump);
}
}
}
}
}
if (possiblePos1 >= 0 && possiblePos1 <= 63
&& rowOfPossiblePos2 == currentPosRowNo + 1*
&& rowOfPossiblePos2 <= 7)
{
* if (scroeKeeper.GetComponent().checkersBoxs[possiblePos2].GetComponent().isEmpty == false)
{
possibleMoves.Add(possiblePos2);
Debug.Log(“downwardpos2:”+checkerPos);
}
else
{
GameObject checker = scroeKeeper.GetComponent().checkersBoxs[possiblePos2].GetComponent().currentChecker;
Debug.Log(“downwardjump2:”+checkerPos);
if (!checker.CompareTag(checkerId))
{
// possibleMoves.Clear();
int possiblePos2AfterJump = possiblePos2 + 7;
int rowOfPossiblePos1afterJump = possiblePos2AfterJump
/ column;
if (rowOfPossiblePos1afterJump == rowOfPossiblePos2 + 1
&& rowOfPossiblePos1afterJump <= 7*
&& possiblePos2AfterJump >= 0
&& possiblePos2AfterJump <= 63)
{
if (scroeKeeper.GetComponent().checkersBoxs[possiblePos2AfterJump].GetComponent().isEmpty == false)
{
* //possibleMoves.Clear();
for (int i = 0; i < scroeKeeper.GetComponent().playerOneCheckers.Count
; i++)
{
possibleMoves.Clear();
if (scroeKeeper.GetComponent().playerOneCheckers.GetComponent()
.getCheckerPos() == possiblePos2)
{
possibleMoves.Clear();
hitCheckerList.Add(possiblePos2AfterJump,
scroeKeeper.GetComponent().playerOneCheckers
);
}
}
possibleMoves.Add(possiblePos2AfterJump);
}
}
}
}
}
}
public int getCheckerPos()
{
return checkerPos;
}
public void setCheckerPos(int checkerPos)
{
this.checkerPos = checkerPos;
}
public bool isSelected()
{
return isSelecteded;
}
public void setSelected(bool isSelected)
{
this.isSelecteded = isSelected;
}
public List getPossibleMoves()
{
return possibleMoves;
}
public void setPossibleMoves(List possibleMoves)
{
this.possibleMoves = possibleMoves;
}
//public Hashtable getHitCheckerList()
//{
// return hitCheckerList;
//}
//public void setHitCheckerList(Hashtable hitCheckerList)
//{
// this.hitCheckerList = hitCheckerList;
//}
public HashTableAlterClass getHitCheckerList()
{
return hitCheckerList;
}
public void setHitCheckerList(HashTableAlterClass hitCheckerList)
{
this.hitCheckerList = hitCheckerList;
}
public bool isHitted()
{
return isHitteded;
}
public void setHitted(bool isHitted)
{
this.isHitteded = isHitted;
}
public void moveSelectedChecker(GameObject selectedCheckBox)
{
// float timeForCheckerMovement = 0.0f;
bool isOppositeCheckerHitted = false;
scroeKeeper.GetComponent().checkersBoxs[checkerPos].GetComponent().isEmpty = false;
scroeKeeper.GetComponent().checkersBoxs[checkerPos].GetComponent().currentChecker = null;
// print(“Box id:::” + selectedCheckBox);
scroeKeeper.GetComponent().checkersBoxs[selectedCheckBox.GetComponent().selectedBox.GetComponent().boxId].GetComponent().isEmpty = true;
checkerPos = selectedCheckBox.GetComponent().selectedBox.GetComponent().boxId;
selectedCheckBox.GetComponent().selectedBox.GetComponent().currentChecker = gameObject;
if (scroeKeeper.GetComponent().isSoundOn)
{
Camera.main.SendMessage(“PlayMoveSound”, SendMessageOptions.RequireReceiver);
}
movePosition = selectedCheckBox.transform.position;
StartCoroutine (MoveSmooth ());
// movePosition.z = 4;
// transform.localPosition = movePosition;
// print(“Hitted item size” + hitCheckerList.Count);
if (hitCheckerList.Contains(selectedCheckBox.GetComponent().selectedBox.GetComponent().boxId))
{
isOppositeCheckerHitted = true;
GameObject checker = (GameObject)hitCheckerList.Get(selectedCheckBox.GetComponent().selectedBox.GetComponent().boxId);
scroeKeeper.GetComponent().checkersBoxs[checker.GetComponent().checkerPos].GetComponent().isEmpty = false;
scroeKeeper.GetComponent().checkersBoxs[checker.GetComponent().checkerPos].GetComponent().currentChecker = null;
if (scroeKeeper.GetComponent().isSoundOn)
{
Camera.main.SendMessage(“PlayHitSound”, SendMessageOptions.RequireReceiver);
}
if (scroeKeeper.GetComponent().pinkChip.activeSelf)
{*_
scroeKeeper.GetComponent().player2Score++;
}
else
{
scroeKeeper.GetComponent().player1Score++;
}
scroeKeeper.GetComponent().changeScoreNMoves();
checker.GetComponent().setHitted(true);
checker.GetComponent().checkerPos = -1;
checker.SetActive(false);
checker.transform.localPosition = new Vector3(0, 0, 30);
}
if (!isKing)
{
checkIsKing();
}
if (isOppositeCheckerHitted)
{
if (gameObject.CompareTag(“Pink”))
{
this.checkPossibleRectOnBoardDownWard(“Pink”, true);
}
else
{
this.checkPossibleRectOnBoardUpward(“Blue”, true);
}
if (hitCheckerList.Count >= 1)
{
possibleMoves.Clear();
scroeKeeper.GetComponent().clearPossibleMoves();
for (int i = 0; i < hitCheckerList.Count;i++ )
{
possibleMoves.Add((int)hitCheckerList.GetKey(i));
}
scroeKeeper.GetComponent().isSecondHitPossible = true;
scroeKeeper.GetComponent().showPossibleMoves();
}
else
{
possibleMoves.Clear();
scroeKeeper.GetComponent().clearPossibleMoves();
changeSelectionStatus();
changeTheTurn();
}
}
else
{
possibleMoves.Clear();
scroeKeeper.GetComponent().clearPossibleMoves();
changeSelectionStatus();
changeTheTurn();
}
scroeKeeper.GetComponent().changeTurnSprite();
}
private void changeTheTurn()
{
if (scroeKeeper.GetComponent().isComputerTurn)
{
scroeKeeper.GetComponent().player2Moves++;
}
else
{
scroeKeeper.GetComponent().player1Moves++;
}
scroeKeeper.GetComponent().changeScoreNMoves();
if (!isKing)
{
checkIsKing();
}
scroeKeeper.GetComponent().clearPossibleMoves();
scroeKeeper.GetComponent().isSecondHitPossible = false;
possibleMoves.Clear();
hitCheckerList.Clear();
// changeSelectionStatus();
scroeKeeper.GetComponent().setSelectedChecker(null);
scroeKeeper.GetComponent().isComputerTurn = !scroeKeeper.GetComponent().isComputerTurn;
scroeKeeper.GetComponent().changeTurnSprite();
scroeKeeper.GetComponent().checkNoMovePossible();
if (scroeKeeper.GetComponent().isComputerTurn
&& scroeKeeper.GetComponent().isGameOver == false)
{
if (ApplicationLevelHandler.players == 1)
{
scroeKeeper.GetComponent().performComputerTurn();
}
}
}
public void changeSelectionStatus()
{
isSelecteded = !isSelecteded;
tk2dSpriteAnimator anim = gameObject.GetComponent();
if (isSelecteded)
{
anim.Play();
}
else
{
anim.Stop();
anim.SetFrame(1);
}
}
public void checkIsKing()
{
tk2dSpriteAnimator anim = gameObject.GetComponent();
if (gameObject.CompareTag(“Blue”)
&& checkerPos < 8)
{
isKing = true;
anim.Play(“BKingChecker”);
}
else if (gameObject.CompareTag(“Pink”)
&& checkerPos >= 56)
{
isKing = true;
anim.Play(“PKingChecker”);
}
}
* IEnumerator MoveSmooth(){*
* float t = 0f;*
* movePosition.z = 4;*
* while (t<=1) {*
* t += Time.deltaTime;*
* transform.localPosition = Vector3.Lerp (transform.position, movePosition, t);*
* yield return null;*
* }*
* }*
}
//this is the code