Advertisement
Guest User

Untitled

a guest
Mar 21st, 2021
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.04 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using Photon.Pun;
  7.  
  8. public class Board : MonoBehaviourPunCallbacks
  9. {
  10.     [SerializeField]
  11.     int boardSize = 8;
  12.  
  13.     public Vector2[,] boardGrid;
  14.     public ChessPiece[,] pieces;
  15.     [SerializeField]
  16.     Transform chessPieceContainer;
  17.     [SerializeField]
  18.     float spaceBetweenPieces = 1;
  19.     [SerializeField]
  20.     GameObject chessPiece, capturedChessPiece;
  21.     TeamColor currentTeam, onePlayerTeam, twoPlayerTeam, computerTeam;
  22.  
  23.     ChessPiece selectedPiece;
  24.     Vector2 mousePos;
  25.     Camera cam;
  26.  
  27.     [SerializeField]
  28.     GameObject highlighter, movementBox;
  29.     List<GameObject> boxes;
  30.     SpeechBubble bubble;
  31.     [SerializeField]
  32.     RectTransform[] onePlayerPiecesGraveyard, twoPlayerPiecesGraveyard;
  33.  
  34.     int numCapturedWhite, numCapturedBlack;
  35.  
  36.     bool gameOver, blackKingDown,blackQueenDown, firstpawnBlack,firstPawnWhite, enemyTurn;
  37.  
  38.     PhotonView view;
  39.  
  40.     private void Awake()
  41.     {
  42.         view = GetComponent<PhotonView>();
  43.         bubble = FindObjectOfType<SpeechBubble>();
  44.         boxes = new List<GameObject>();
  45.         cam = Camera.main;
  46.         currentTeam = TeamColor.White;
  47.         boardGrid = new Vector2[boardSize, boardSize];
  48.         pieces = new ChessPiece[boardSize, boardSize];
  49.  
  50.  
  51.         InitiateBoard();
  52.        
  53.         onePlayerTeam = GameManager.team;
  54.         if (onePlayerTeam == TeamColor.Black)
  55.         {
  56.            
  57.             if (GameManager.isSinglePlayer)
  58.             {
  59.                 computerTeam = TeamColor.White;
  60.                 StartCoroutine(ComputerTurn());
  61.             }
  62.             else
  63.             {
  64.                 twoPlayerTeam = TeamColor.White;
  65.             }
  66.             bubble.SpeechBubbleDisplay(13);
  67.         }
  68.         else
  69.         {
  70.             bubble.SpeechBubbleDisplay(12);
  71.             if (GameManager.isSinglePlayer)
  72.             {
  73.                 computerTeam = TeamColor.Black;
  74.             }
  75.             else
  76.             {
  77.                 twoPlayerTeam = TeamColor.Black;
  78.             }
  79.            
  80.         }
  81.        
  82.     }
  83.  
  84.     [PunRPC]
  85.     void InitiateBoard()
  86.         {
  87.         GameManager.boardInitiated = true;
  88.         for (int x = 0; x < boardGrid.GetLength(0); x++)
  89.         {
  90.  
  91.             for (int y = 0; y < boardGrid.GetLength(1); y++)
  92.             {
  93.  
  94.                     boardGrid[x, y] = new Vector3(chessPieceContainer.position.x + x * spaceBetweenPieces, chessPieceContainer.position.y + y * spaceBetweenPieces, 0);
  95.  
  96.                     if (y < 2 || y > 5)
  97.                     {
  98.  
  99.                         GameObject spawnedPiece = PhotonNetwork.InstantiateRoomObject("Prefabs/Piece", boardGrid[x, y], transform.rotation);
  100.                         pieces[x, y] = spawnedPiece.GetComponent<ChessPiece>();
  101.                         pieces[x, y].posOnBoard = new Vector2Int(x, y);
  102.                     }
  103.                
  104.             }
  105.         }
  106.         CheckAllLegalMoves();
  107.         }
  108.  
  109.  
  110.  
  111.     private void Update()
  112.     {
  113.  
  114.         if(selectedPiece == null && highlighter.activeInHierarchy)
  115.         {
  116.             highlighter.SetActive(false);
  117.         }
  118.         if (Input.GetMouseButtonDown(0))
  119.         {
  120.             OnMouse();
  121.         }
  122.     }
  123.  
  124.     void OnMouse()
  125.     {
  126.         mousePos = Input.mousePosition;
  127.         RemoveSelectionBoxes();
  128.         if (!gameOver)
  129.         {
  130.             if (onePlayerTeam == currentTeam)
  131.             {
  132.                 if (CheckIfCoordsAreOnGrid(mousePos))
  133.                 {
  134.                     if (GetPieceAtCoords(mousePos) != null && selectedPiece == null && pieces[CheckGridPosAtCoords(mousePos).x, CheckGridPosAtCoords(mousePos).y].team == currentTeam)
  135.                     {
  136.                         Vector2Int pieceToSelectIndex = CheckGridPosAtCoords(mousePos);
  137.                         SelectPiece(pieces[pieceToSelectIndex.x, pieceToSelectIndex.y]);
  138.                     }
  139.                     else if (selectedPiece != null)
  140.                     {
  141.                         if (pieces[CheckGridPosAtCoords(mousePos).x, CheckGridPosAtCoords(mousePos).y] == null)
  142.                         {
  143.                             MovePiece(CheckGridPosAtCoords(mousePos), selectedPiece);
  144.                         }
  145.  
  146.                         else if (GetPieceAtCoords(mousePos).team == selectedPiece.team)
  147.                         {
  148.                             SelectPiece(GetPieceAtCoords(mousePos));
  149.                         }
  150.                         else if (selectedPiece != null)
  151.                         {
  152.                             CapturePiece(GetPieceAtCoords(mousePos));
  153.                         }
  154.                     }
  155.                 }
  156.             }
  157.         }
  158.     }
  159.     void SwitchActiveTeam()
  160.     {
  161.         if(currentTeam == computerTeam)
  162.         {
  163.             currentTeam = onePlayerTeam;
  164.         }
  165.         else if(GameManager.isSinglePlayer)
  166.         {
  167.             currentTeam = computerTeam;
  168.         }
  169.         else
  170.         {
  171.             currentTeam = twoPlayerTeam;    
  172.         }
  173.     }
  174.  
  175.     ChessPiece GetPieceAtCoords(Vector2 coords)
  176.     {
  177.         return pieces[CheckGridPosAtCoords(coords).x, CheckGridPosAtCoords(coords).y];
  178.     }
  179.  
  180.     Vector2Int CheckGridPosAtCoords(Vector2 coords)
  181.     {
  182.         float distance = 2;
  183.         Vector2Int pieceInd = Vector2Int.one * -1;
  184.         for (int x = 0; x < boardGrid.GetLength(0); x++)
  185.         {
  186.  
  187.             for (int y = 0; y < boardGrid.GetLength(1); y++)
  188.             {
  189.                 if (Vector2.Distance(cam.ScreenToWorldPoint(coords), boardGrid[x, y]) < distance)
  190.                 {
  191.                     distance = Vector2.Distance(cam.ScreenToWorldPoint(coords), boardGrid[x, y]);
  192.                     pieceInd = new Vector2Int(x, y);
  193.                 }
  194.             }
  195.  
  196.         }if(pieceInd == Vector2Int.one * -1)
  197.         {
  198.             RemoveSelectionBoxes();
  199.             print("Something when wrong in selecting?");
  200.            
  201.         }
  202.         return new Vector2Int(pieceInd.x,pieceInd.y);
  203.     }
  204.  
  205.     void CheckAllLegalMoves()
  206.     {
  207.         for (int i = 0; i < pieces.GetLength(0); i++)
  208.         {
  209.             for (int j = 0; j < pieces.GetLength(1); j++)
  210.             {
  211.                 if (pieces[i, j] != null)
  212.                 {
  213.                     pieces[i, j].CalculateLegalMoves();
  214.                 }
  215.             }
  216.         }
  217.     }
  218.  
  219.     void MovePiece(Vector2Int pos, ChessPiece pieceToMove)
  220.     {
  221.         if (pieces[pos.x, pos.y] == null && pieceToMove.legalMoves.Contains(pos))
  222.         {
  223.             Vector2Int previousPos = pieceToMove.posOnBoard;
  224.             if (pieceToMove.firstMove)
  225.             {
  226.                 pieceToMove.firstMove = false;
  227.             }
  228.             pieces[previousPos.x, previousPos.y] = null;
  229.             pieceToMove.posOnBoard = pos;
  230.             pieceToMove.transform.position = boardGrid[pos.x,pos.y];
  231.             pieces[pos.x,pos.y] = pieceToMove;
  232.             DeselectPiece();
  233.  
  234.             StartNewTurn();
  235.  
  236.         }else if(pieces[pos.x, pos.y] != null && pieces[pos.x, pos.y].team != selectedPiece.team)
  237.         {
  238.  
  239.             CapturePiece(pieces[pos.x, pos.y]);
  240.  
  241.         }
  242.         else if(!selectedPiece.legalMoves.Contains(pos))
  243.         {
  244.             print("Something when wrong in moving?");
  245.         }
  246.     }
  247.  
  248.     void CapturePiece(ChessPiece pieceToCapture)
  249.     {
  250.         Vector2Int posToMoveTo = Vector2Int.zero;
  251.         if(pieceToCapture.team == TeamColor.White)
  252.         {
  253.             ChessPieceEvent(pieceToCapture.pieceIndex,pieceToCapture.GetComponent<SpriteRenderer>().sprite);
  254.         }
  255.         else
  256.         {
  257.             ChessPieceEvent(pieceToCapture.pieceIndex + 6, pieceToCapture.GetComponent<SpriteRenderer>().sprite);
  258.         }
  259.         for (int x = 0; x < pieces.GetLength(0); x++)
  260.         {
  261.             for (int y = 0; y < pieces.GetLength(1); y++)
  262.             {
  263.                 if(pieces[x,y] == pieceToCapture)
  264.                 {
  265.                     posToMoveTo = pieces[x, y].posOnBoard;
  266.                     pieces[x, y] = null;
  267.                     Destroy(pieceToCapture.gameObject);
  268.                 }
  269.             }
  270.         }
  271.  
  272.         Destroy(pieceToCapture.gameObject);
  273.         MovePiece(posToMoveTo,selectedPiece);
  274.     }
  275.  
  276.     public int PosX(Vector2Int coords)
  277.     {
  278.         return CheckGridPosAtCoords(coords).x;
  279.     }
  280.  
  281.     public int PosY(Vector2Int coords)
  282.     {
  283.         return CheckGridPosAtCoords(coords).y;
  284.     }
  285.  
  286.  
  287.     public void SelectPiece(ChessPiece piece)
  288.     {
  289.         if(selectedPiece == null)
  290.         {
  291.             RemoveSelectionBoxes();
  292.         }
  293.         selectedPiece = piece;
  294.         HighlightSelectedPiece();
  295.         for (int i = 0; i < selectedPiece.legalMoves.Count; i++)
  296.         {
  297.             boxes.Add(Instantiate(movementBox, boardGrid[selectedPiece.legalMoves[i].x, selectedPiece.legalMoves[i].y], transform.rotation));
  298.         }
  299.     }
  300.  
  301.     void HighlightSelectedPiece()
  302.     {
  303.         highlighter.SetActive(true);
  304.         highlighter.transform.position = selectedPiece.transform.position;
  305.     }
  306.  
  307.     public void DeselectPiece()
  308.     {
  309.         selectedPiece = null;
  310.         RemoveSelectionBoxes();
  311.     }
  312.  
  313.     void RemoveSelectionBoxes()
  314.     {
  315.         for (int i = 0; i < boxes.Count; i++)
  316.         {
  317.             Destroy(boxes[i]);
  318.         }
  319.     }
  320.  
  321.     public bool CheckIfArrayIsOutOfBounds(Vector2Int pos)
  322.     {
  323.         return pos.x > -1 && pos.x < 8 && pos.y > -1 && pos.y < 8;
  324.     }
  325.    
  326.     public bool CheckIfCoordsAreOnGrid(Vector2 coords)
  327.     {
  328.  
  329.         float distance = 2;
  330.         Vector2Int pieceInd = Vector2Int.one * -1;
  331.         for (int x = 0; x < boardGrid.GetLength(0); x++)
  332.         {
  333.  
  334.             for (int y = 0; y < boardGrid.GetLength(1); y++)
  335.             {
  336.                 if (Vector2.Distance(cam.ScreenToWorldPoint(coords), boardGrid[x, y]) < distance)
  337.                 {
  338.                     distance = Vector2.Distance(cam.ScreenToWorldPoint(coords), boardGrid[x, y]);
  339.                     pieceInd = new Vector2Int(x, y);
  340.                     return true;
  341.                 }
  342.             }
  343.  
  344.         }
  345.         if (pieceInd == Vector2Int.one * -1)
  346.         {
  347.             return false;
  348.  
  349.         }
  350.  
  351.         return false;
  352.     }
  353.  
  354.     void StartNewTurn()
  355.     {
  356.         SwitchActiveTeam();
  357.  
  358.         for (int i = 0; i < pieces.GetLength(0); i++)
  359.         {
  360.             for (int j = 0; j < pieces.GetLength(1); j++)
  361.             {
  362.                 if(pieces[i,j] != null)
  363.                 {
  364.                     pieces[i, j].CalculateLegalMoves();
  365.                 }
  366.             }
  367.         }
  368.         if(currentTeam != onePlayerTeam && !enemyTurn && GameManager.isSinglePlayer)
  369.         {
  370.             StopAllCoroutines();
  371.             StartCoroutine(ComputerTurn());
  372.         }
  373.     }
  374.  
  375.     IEnumerator ComputerTurn()
  376.     {
  377.         if (!gameOver)
  378.         {
  379.             enemyTurn = true;
  380.             float random = UnityEngine.Random.Range(1f, 5f);
  381.             yield return new WaitForSeconds(random);
  382.             CheckAllLegalMoves();
  383.  
  384.             ComputerRandomMove();
  385.             enemyTurn = false;
  386.         }
  387.     }
  388.  
  389.     public void ComputerRandomMove()
  390.     {
  391.         ChessPiece pieceToMove = GetRandomPieceOfTeam();
  392.         int highestNumber = 1;
  393.         for (int i = 0; i < 500; i++)
  394.         {
  395.             pieceToMove = GetRandomPieceOfTeam();
  396.             if ( pieceToMove.legalMoves.Count > 0)
  397.             {
  398.                 break;
  399.             }
  400.             highestNumber = i + 1;
  401.         }
  402.         bool moving = false; ;
  403.         List<Vector2Int> moves = new List<Vector2Int>();
  404.  
  405.         for (int i = 0; i < pieceToMove.legalMoves.Count; i++)
  406.         {
  407.             moves.Add(pieceToMove.legalMoves[i]);
  408.             if(pieces[pieceToMove.legalMoves[i].x, pieceToMove.legalMoves[i].y] != null)
  409.             {
  410.                 SelectPiece(pieceToMove);
  411.                 moving = true;
  412.                 CapturePiece(pieces[pieceToMove.legalMoves[i].x, pieceToMove.legalMoves[i].y]);
  413.                 break;
  414.             }
  415.         }
  416.         if (!moving)
  417.         {
  418.  
  419.             int random = UnityEngine.Random.Range(0, moves.Count);
  420.             SelectPiece(pieceToMove);
  421.             MovePiece(pieceToMove.legalMoves[random], selectedPiece);
  422.         }
  423.     }
  424.  
  425.     ChessPiece GetRandomPieceOfTeam()
  426.     {
  427.         List<ChessPiece> piecesToCycle = new List<ChessPiece>();
  428.         for (int i = 0; i < pieces.GetLength(0); i++)
  429.         {
  430.             for (int j = 0; j < pieces.GetLength(1); j++)
  431.             {
  432.                     if (pieces[i, j] != null && pieces[i, j].team != onePlayerTeam)
  433.                     {
  434.                         piecesToCycle.Add(pieces[i, j]);
  435.                     }
  436.             }
  437.         }
  438.         int random = UnityEngine.Random.Range(0, piecesToCycle.Count);
  439.        
  440.         return piecesToCycle[random];
  441.     }
  442.  
  443.     void ChessPieceEvent(int pieceIndex, Sprite sprite)
  444.     {
  445.         if (pieceIndex < 5) {
  446.             numCapturedWhite++;
  447.             GameObject deadPiece = PhotonNetwork.Instantiate("Prefabs/CapturedPiece" ,onePlayerPiecesGraveyard[numCapturedWhite % 2].position,Quaternion.identity);
  448.             deadPiece.GetComponent<Image>().sprite = sprite;
  449.             if(pieceIndex == 0)
  450.             {
  451.                 EndGame(TeamColor.Black);
  452.             }
  453.         }
  454.         else
  455.         {
  456.             numCapturedBlack++;
  457.             GameObject deadPiece = PhotonNetwork.Instantiate("Prefabs/CapturedPiece", twoPlayerPiecesGraveyard[numCapturedBlack % 2].position, Quaternion.identity);
  458.             deadPiece.GetComponent<Image>().sprite = sprite;
  459.             if(pieceIndex == 6)
  460.             {
  461.                 blackKingDown = true;
  462.             }
  463.             if(pieceIndex == 11)
  464.             {
  465.                 blackQueenDown = true;
  466.             }
  467.             if(blackKingDown && blackQueenDown)
  468.             {
  469.                 EndGame(TeamColor.White);
  470.             }
  471.         }
  472.         bubble.SpeechBubbleDisplay(pieceIndex);
  473.     }
  474.  
  475.     public void EndGame(TeamColor victor)
  476.     {
  477.         print("Game over, man");
  478.         gameOver = true;
  479.     }
  480.  
  481.  
  482. }
  483.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement