Facebook
From me, 4 Weeks ago, written in C#.
Embed
Download Paste or View Raw
Hits: 90
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Unity.Collections;
  4. using Unity.Mathematics;
  5. using UnityEngine;
  6. using UnityEngine.SceneManagement;
  7. using UnityEngine.UI;
  8.  
  9. public class Game : MonoBehaviour
  10. {
  11.     public GameObject chesspiece;
  12.  
  13.     // Position and team for each piece
  14.     private GameObject[,] positions = new GameObject[8, 8];
  15.     public string[,] board = new string[8, 8];
  16.     private GameObject[] playerBlack = new GameObject[16];
  17.     private GameObject[] playerWhite = new GameObject[16];
  18.     // private GameObject[] playerBlackKing = new GameObject[1];
  19.     // private GameObject[] playerWhiteKing = new GameObject[1];
  20.  
  21.     private string currentPlayer = "white";
  22.     private bool gameOver = false;
  23.  
  24.     // Start is called before the first frame update
  25.     void Start()
  26.     {
  27.         // Functions ()
  28.         playerWhite = new GameObject[] {
  29.             Create("white_rook", 0,0), Create("white_knight", 1,0),
  30.             Create("white_bishop", 2,0), Create("white_queen", 3,0),
  31.             Create("white_king", 4,0), Create("white_bishop", 5,0),
  32.             Create("white_knight", 6,0), Create("white_rook", 7,0),
  33.             Create("white_pawn", 0,1), Create("white_pawn", 1,1),
  34.             Create("white_pawn", 2,1), Create("white_pawn", 3,1),
  35.             Create("white_pawn", 4,1), Create("white_pawn", 5,1),
  36.             Create("white_pawn", 6,1), Create("white_pawn", 7,1),
  37.         };
  38.  
  39.         playerBlack = new GameObject[] {
  40.             Create("black_rook", 0,7), Create("black_knight", 1,7),
  41.             Create("black_bishop", 2,7), Create("black_queen", 3,7),
  42.             Create("black_king", 4,7), Create("black_bishop", 5,7),
  43.             Create("black_knight", 6,7), Create("black_rook", 7,7),
  44.             Create("black_pawn", 0,6), Create("black_pawn", 1,6),
  45.             Create("black_pawn", 2,6), Create("black_pawn", 3,6),
  46.             Create("black_pawn", 4,6), Create("black_pawn", 5,6),
  47.             Create("black_pawn", 6,6), Create("black_pawn", 7,6),
  48.         };
  49.        
  50.         // Set all piece positions on position board
  51.         for (int i = 0; i < 16; i++){
  52.             SetPosition(playerBlack[i]);
  53.             SetPosition(playerWhite[i]);
  54.         }
  55.  
  56.         UpdateBoardState();
  57.  
  58.     }
  59.  
  60.     public GameObject Create(string name, int x, int y)
  61.     {
  62.         GameObject obj = Instantiate(chesspiece, new Vector3(0,0,-1), Quaternion.identity);
  63.         Chessman cm = obj.GetComponent<Chessman>();
  64.         cm.name = name;
  65.         cm.SetXBoard(x);
  66.         cm.SetYBoard(y);
  67.         cm.Activate();
  68.         return obj;
  69.     }
  70.  
  71.     public void SetPosition(GameObject obj)
  72.     {
  73.         Chessman cm = obj.GetComponent<Chessman>();
  74.         positions[cm.GetXBoard(), cm.GetYBoard()] = obj;
  75.     }
  76.  
  77.     public void SetPositionEmpty(int x, int y)
  78.     {
  79.         positions[x, y] = null;
  80.     }
  81.  
  82.     public GameObject GetPosition(int x, int y)
  83.     {
  84.         return positions[x,y];
  85.     }
  86.  
  87.     public bool PositionOnBoard(int x, int y)
  88.     {
  89.         if (x < 0 || y < 0 || x >= positions.GetLength(0) || y >= positions.GetLength(1)) return false;
  90.         return true;
  91.     }
  92.    
  93.     public string GetCurrentPlayer()
  94.     {
  95.         return currentPlayer;
  96.     }
  97.  
  98.     public bool IsGameOver()
  99.     {
  100.         return gameOver;
  101.     }
  102.  
  103.     public void NextTurn()
  104.     {
  105.         if (currentPlayer == "white"){
  106.             currentPlayer = "black";
  107.         }
  108.         else {
  109.             currentPlayer = "white";
  110.         }
  111.     }
  112.  
  113.     public void Update(){
  114.         if (gameOver == true && Input.GetMouseButtonDown(0)){
  115.             gameOver = false;
  116.  
  117.             SceneManager.LoadScene("Game");
  118.         }
  119.     }
  120.  
  121.     public void Winner(string playerWinner)
  122.     {
  123.         gameOver = true;
  124.  
  125.         GameObject.FindGameObjectWithTag("WinnerText").GetComponent<Text>().enabled = true;
  126.         GameObject.FindGameObjectWithTag("WinnerText").GetComponent<Text>().text = playerWinner + " wins!";
  127.  
  128.         GameObject.FindGameObjectWithTag("RestartText").GetComponent<Text>().enabled = true;
  129.  
  130.  
  131.     }
  132.  
  133.     // Print board state
  134.     public void UpdateBoardState() {
  135.         for (int x = 0; x < positions.GetLength(0); x++) {
  136.             for (int y = 0; y < positions.GetLength(1); y++) {
  137.                 if (positions[x,y] != null) {
  138.                     board[x,y] = positions[x,y].name;
  139.                 }
  140.             }
  141.         }
  142.     }
  143.  
  144.    
  145. }
  146.