Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CheckerBoard : MonoBehaviour {
    
    public Piece[,] pieces = new Piece[8, 8];
    public GameObject whitePiece;
    public GameObject blackPiece;

    private Vector3 boardoffset = new Vector3(-4.0f, 0, -4.0f);
    private Vector3 pieceoffset = new Vector3(0.5f, 0, 0.5f);

    private Piece SelectedPiece;
    private List<piece> forcedPieces; 

    public bool isWhite;
    private bool isWhiteTurn;
    private bool hasKilled;


    private Vector2 mouseover;
    private Vector2 startDrag;
    private Vector2 endDrag;
    

    private void Start()
    {
        isWhiteTurn = true;
        isWhite = true;
        forcedPieces = new List<piece>();
        GenerateBoard();
    }

    private void Update()
    {
        UpdateMouseOver();

        //if it is my turn
        if((isWhite)?isWhiteTurn:!isWhiteTurn)
        {
            int x = (int)mouseover.x;
            int y = (int)mouseover.y;

            if (SelectedPiece != null)
            {
                UpdatePieceDrag(SelectedPiece);
            }
            if (Input.GetMouseButtonDown(0))
            {
                SelectPiece(x, y);
            }

            if (Input.GetMouseButtonUp(0))
                TryMove((int)startDrag.x, (int)startDrag.y, x, y);
        }
        
    }

    private void UpdateMouseOver()
    {
        //if it is my turn
        if(!Camera.main)
        {
            Debug.Log("Unable to find main camera");
            return;
        }
        RaycastHit hit;
        if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),out hit,25.0f,LayerMask.GetMask("Board")))
        {
            mouseover.x = (int)(hit.point.x - boardoffset.x);
            mouseover.y = (int)(hit.point.z - boardoffset.z);
        }
        else
        {
            mouseover.x = -1;
            mouseover.y = -1;
        }
    }

    private void GenerateBoard()
    {
        // generate white pieces
        for(int y=0; y<3; y++)
        {
            bool oddRow = (y % 2 == 0);
            for(int x=0; x<8; x+=2)
            {
                //generate our pieces
                GeneratePiece((oddRow) ? x : x +1, y);
            }
        }

        //generate black pieces
        for (int y = 7; y > 4; y--)
        {
            bool oddRow = (y % 2 == 0);
            for (int x = 0; x < 8; x += 2)
            {
                //generate thier pieces
                GeneratePiece((oddRow) ? x : x + 1, y);
            }
        }
    }

    private void GeneratePiece(int x, int y)
    {
        bool isPieceWhite = (y > 3) ? false : true;
        GameObject go = Instantiate(isPieceWhite?whitePiece:blackPiece) as GameObject;
        go.transform.SetParent(transform);
        Piece p = go.GetComponent<piece>();
        pieces[x, y] = p;
        MovePiece(p, x, y);
    }

    private void MovePiece(Piece p, int x, int y)
    {
        p.transform.position = (Vector3.right * x) + (Vector3.forward * y) + boardoffset + pieceoffset;
    }

    private void SelectPiece(int x, int y)
    {
        //Out of bound
        if (x < 0 || x >= 8 || y < 0 || y >= 8)
            return;
        Piece p = pieces[x, y];
        if(p != null && p.isWhite == isWhite)
        {
            if (forcedPieces.Count == 0)
            {
                SelectedPiece = p;
                startDrag = mouseover;
            }

            else
            {
                //look for the piece under our forced pieces list
                if (forcedPieces.Find(fp => fp == p) == null)
                    return;

                SelectedPiece = p;
                startDrag = mouseover;
            }
        }
    }

    private void TryMove(int x1,int y1,int x2,int y2)
    {

        forcedPieces = ScanForPossibleMove();

        //Multiplayer Support
        startDrag = new Vector2(x1, y1);
        endDrag = new Vector2(x2, y2);
        SelectedPiece = pieces[x1, y1];

        //error
        if(x2 < 0 || x2 >= 8 || y2 < 0 || y2 >= 8)
        {
            if (SelectedPiece != null)
            {
                MovePiece(SelectedPiece, x1,y1);
            }
            startDrag = Vector2.zero;
            SelectedPiece = null;
            return;
        }

        if(SelectedPiece != null)
        {
            //if it has not moved
            if( endDrag == startDrag)
            {
                MovePiece(SelectedPiece, x1, y1);
                startDrag = Vector2.zero;
                SelectedPiece = null;
                return;
            }
            //check if its a valid move
            if(SelectedPiece.validMove(pieces, x1, y1, x2, y2))
            {
                //did we kill anything
                //if this is a jump
                if(Mathf.Abs(x2 - x1) == 2)
                {
                    Piece p = pieces[(x1 + x2) / 2, (y1 + y2) / 2];
                    if(p != null)
                    {
                        pieces[(x1 + x2) / 2, (y1 + y2) / 2] = null;
                        Destroy(p.gameObject);
                        hasKilled = true;
                    }
                }

                //we are supposed to killaa anything?

                if(forcedPieces.Count != 0 && !hasKilled)
                {
                    MovePiece(SelectedPiece, x1, y1);
                    startDrag = Vector2.zero;
                    SelectedPiece = null;
                    return;
                }

                pieces[x2, y2] = SelectedPiece;
                pieces[x1, y1] = null;
                MovePiece(SelectedPiece,x2, y2);
                EndTurn();
            }
            else
            {
                MovePiece(SelectedPiece, x1, y1);
                startDrag = Vector2.zero;
                SelectedPiece = null;
                return;
            }
        }
 }

    private void EndTurn()
    {
        int x = (int)endDrag.x;
        int y = (int)endDrag.y;

        //promotions
        if(SelectedPiece != null)
        {
            if(SelectedPiece.isWhite && !SelectedPiece.isKing && y == 7)
            {
                SelectedPiece.isKing = true;
                SelectedPiece.transform.Rotate(Vector3.right * 180);
            }

            else if(!SelectedPiece.isWhite && !SelectedPiece.isKing && y == 0)
            {
                SelectedPiece.isKing = true;
                SelectedPiece.transform.Rotate(Vector3.right * 180);
            }
             
        }
        SelectedPiece = null;
        startDrag = Vector2.zero;

        if (ScanForPossibleMove(SelectedPiece, x, y).Count != 0 && hasKilled)
            return;


        isWhiteTurn = !isWhiteTurn;
        isWhite = !isWhite;
        hasKilled = false;
        CheckVictory();
    }

    private void CheckVictory()
    {
        var ps = FindObjectsOfType<piece>();
        bool hasWhite = false, hasBlack = false;
        for(int i = 0; i < ps.Length; i++)
        {
            if (ps[i].isWhite)
                hasWhite = true;
            else
                hasBlack = true;

        }

        if (!hasWhite)
            Victory(false);
        if (!hasBlack)
            Victory(true);
    }

    public void Victory(bool isWhite)
    {
        if (isWhite)
            Debug.Log("White player has won");
        else
            Debug.Log("Black player has won");

    }

    private List<piece> ScanForPossibleMove()
    {
        forcedPieces = new List<piece>();

        //check all the pieces
        for (int i = 0; i < 8; i++)
            for (int j = 0; j < 8; j++)
                if (pieces[i, j] != null && pieces[i, j].isWhite == isWhiteTurn)
                    if (pieces[i, j].IsForceToMove(pieces, i, j))
                        forcedPieces.Add(pieces[i, j]);
        return forcedPieces;
    }

    private List<piece> ScanForPossibleMove(Piece p, int x, int y)
    {
        forcedPieces = new List<piece>();

        if (pieces[x, y].IsForceToMove(pieces, x, y))
            forcedPieces.Add(pieces[x, y]);

        return forcedPieces;
    }

    private void UpdatePieceDrag(Piece p)
    {
        //if it is my turn
        if (!Camera.main)
        {
            Debug.Log("Unable to find main camera");
            return;
        }
        RaycastHit hit;
        if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 25.0f, LayerMask.GetMask("Board")))
        {
            p.transform.position = hit.point + Vector3.up;
        }
    }

}


[edit]Code block added - OriginalGriff[/edit]

What I have tried:

i had tried to start from the mouse over but it didnt work
Posted
Updated 23-Apr-17 0:58am
v2

1 solution

Either you wrote it - in which case you already know what to do - or you found it on the internet and don't understand how it works.
And since this is your homework, you would be better off writing your own so you do understand how it works, and what you need to do.

Either way, just dumping your whole code on us and saying "what do I need to change?" isn't polite and won't make you many friends here.

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
 
Share this answer
 
Comments
Member 13078866 23-Apr-17 7:17am    
tell me where to start?
OriginalGriff 23-Apr-17 7:27am    
With what? You haven't done anything except find a program you hope will do what you want on the internet, find it doesn't, and ask us how to fix that!

The idea of "development" is as the word suggests: "The systematic use of scientific and technical knowledge to meet specific objectives or requirements." (BusinessDictionary.com)

That's not the same thing as "have a quick google and give up if I can't find exactly the right code".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900