void test() { /* * When turns change: (do the following for the one who's turn has just started) * 1. See if there exists an enemy piece which can move in one of the kings positions * 1.1.0 if yes, prevent the king from moving to that position * 2. See if there exists a friendly piece which when moved makes the king vulnurable * with the board only containing the king (from friendly pieces): * 2.0.0 Check all the possible positions of every enemy piece * 2.0.1 if one piece can attack the king: * * */ GameObject KING; BLACK_ID.TryGetValue(15, out KING); pieceCode KING_ID = KING.GetComponent <pieceCode>(); List <int[]> KING_MOVES = movements.handle_piece(KING_ID.row, KING_ID.col, "king", boardBlack); foreach (var piece in WHITE_ID) { pieceCode code = piece.Value.GetComponent <pieceCode>(); List <int[]> moves = movements.handle_piece(code.row, code.col, code.type, boardWhite); List <int[]> common = Enumerable.Union(moves, KING_MOVES).ToList(); if (common.Count == 0) { continue; } } }
void Update() { if (gameEnded) { if (Time.time - time0 >= TurnChangeWaitTime) { UnityEngine.SceneManagement.SceneManager.LoadScene("menu"); } } if (this.turn_change) { if ((Time.time - time0) >= TurnChangeWaitTime) { this.switch_turns(); turn_change = false; } return; } Vector3 mpos = Input.mousePosition; Ray pos_ray = Camera.main.ScreenPointToRay(mpos); RaycastHit hit; if (Physics.Raycast(pos_ray.origin, pos_ray.direction, out hit, 100.0f)) { Collider collider = hit.collider; pieceCode code = collider.GetComponent <pieceCode>(); if (code) { if (Input.GetMouseButtonDown(0)) { if (collider.tag == turn) { if (collider.gameObject == selected_piece) { this.unSelect(); return; } this.select(collider); return; } else if (collider.tag == "SQUARE") { Tuple <int[], pieceCode, bool, pieceCode, int[]> moveData = this.GetMoveData(collider); if (moveData != null) { this.HandleMoveData(moveData); } } } } } if (Input.GetKeyDown(KeyCode.Escape) && selected) { this.unSelect(); } ; }
void init_squares() { Vector3 scale = new Vector3(3.000685f, 1, 3.0f); Vector3 pos = new Vector3(7.503f, -0.913f, -13.654f); /* Square Indexing is based on black (0, 0) */ for (uint j = 0; j < 8; j++) { Vector3 localPos = pos + unitX * -new Vector3(j, 0, 0); var ROW_TABLE = new Dictionary <int, GameObject>(); SQUARE_ID.Add((int)j, ROW_TABLE); for (uint i = 0; i < 8; i++) { GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); cube.tag = "SQUARE"; cube.transform.position = localPos + 3.0f * new Vector3(0, 0, i); cube.transform.localScale = scale; cube.AddComponent <BoxCollider>(); pieceCode code = cube.AddComponent <pieceCode>(); code.row = (int)j; code.col = (int)i; ROW_TABLE.Add((int)i, cube); var renderer = cube.GetComponent <MeshRenderer>(); renderer.material = SQUARE_TARNSPARENT; } ; } ; }
void init_settings() { NetData.settings = new Dictionary <string, float>() { { "Ambient Occulusion", 0.05f }, { "Bloom Effect", 4f }, { "Depth of Field", 20f }, { "Color Grading", 89f }, }; TestSettings = this.clone(NetData.settings); SettingsPointer = new Dictionary <int, string>(); OPTION_DATA = new GameObject[4]; UnityEngine.UI.Slider[] options = new UnityEngine.UI.Slider[] { OPTION0, OPTION1, OPTION2, OPTION3 }; int i = 0; NetData.fancyGraphics = true; foreach (var option in NetData.settings) { SettingsPointer.Add(i, option.Key); UnityEngine.UI.Slider slider = options[i]; RectTransform s_t = slider.GetComponent <RectTransform>(); TMPro.TextMeshProUGUI text = s_t.GetChild(s_t.childCount - 1).GetComponent <TMPro.TextMeshProUGUI>(); slider.value = option.Value; pieceCode code = slider.gameObject.AddComponent <pieceCode>(); int iCopy = i; slider.onValueChanged.AddListener(delegate { TestSettings[SettingsPointer[iCopy]] = slider.value; }); OPTION_DATA[i] = slider.gameObject; i++; } APPLY_BUTTON.onClick.AddListener(delegate { NetData.settings = clone(TestSettings); }); BUTTON_TOGGLE.onValueChanged.AddListener(delegate { NetData.fancyGraphics = !NetData.fancyGraphics; foreach (var item in options) { item.enabled = NetData.fancyGraphics; } }); }
/* See if the selected point is in the available ones and return it */ int[] get_point(pieceCode target) { if (!selected) { return(null); } int[] TARGET_POINT = null; foreach (int[] point in availableMoves) { bool isEqualToPoint = (point[0] == target.row) && (point[1] == target.col) && (point[2] != 2); if (isEqualToPoint) { TARGET_POINT = point; } } return(TARGET_POINT); }
public bool isEnemy(int row, int col, int[,] boardArray) { int item = boardArray[row, col]; if (item == 0) { return(false); } GameObject boardPiece; WHITE_ID.TryGetValue(item, out boardPiece); if (boardPiece == null) { if (turn == "white") { return(true); } return(false); } pieceCode code = boardPiece.GetComponent <pieceCode>(); int[] check_row = new int[2] { code.row, code.col }; if (turn == "black") { check_row = this.BlackToWhite(code.row, code.col); } if ((check_row[0] == row) && (check_row[1] == col)) { if (turn == "white") { return(false); } else { return(true); } } return(turn == "white" ? true : false); }
void appendGlobalItem(ref GameObject item, int id, int row, int col, string team, string type) { pieceCode code = item.AddComponent <pieceCode>(); code.id = id; code.row = row; code.col = col; if (team == "white") { WHITE_ID.Add(code.id, item); item.tag = "white"; setMaterial(ref item, WHITE_MATERIAL); } else { BLACK_ID.Add(code.id, item); item.tag = "black"; setMaterial(ref item, BLACK_MATERIAL); } code.type = type; }
public void HandleMoveData(Tuple <int[], pieceCode, bool, pieceCode, int[]> moveData) { pieceCode TO = moveData.Item2; pieceCode PIECE_CODE = moveData.Item4; int[] FROM = moveData.Item1; int[] LocalTargetCords = moveData.Item5; if (moveData.Item3) // Is enemy { this.Kill(TO.row, TO.col, turn == "white" ? "black" : "white"); // movedata.TO.row, movedata.To.Col } this.move( piece: selected_piece, row: TO.row, col: TO.col, relativeToWhite: false, update: true, isHorse: PIECE_CODE.type == "horse" ? true : false, fromRow: FROM[0], fromCol: FROM[1] ); MOVE_AUDIO.Play(); PIECE_CODE.row = LocalTargetCords[0]; PIECE_CODE.col = LocalTargetCords[1]; this.unSelect(); turn_change = true; time0 = Time.time; string winner = this.gameWinner(); if (winner != "") { setWinner(winner); } return; }
void PaintAvailableSquares(Collider pieceHitByRay) { pieceCode SEL_PIECE = pieceHitByRay.gameObject.GetComponent <pieceCode>(); List <int[]> bMoves; int[] CURRENT_POINT; print($"TURN is {this.turn}"); if (turn == "white") { Debug.Log("white succeded"); List <int[]> moves = movements.handle_piece(SEL_PIECE.row, SEL_PIECE.col, SEL_PIECE.type, boardWhite); bMoves = this.TransformListToBlack(moves); /* <IMPORTANT> White points are translated to black </IMPORTANT> */ CURRENT_POINT = this.BlackToWhite(SEL_PIECE.row, SEL_PIECE.col); } else { Debug.Log("black succeded"); bMoves = movements.handle_piece(SEL_PIECE.row, SEL_PIECE.col, SEL_PIECE.type, boardBlack); CURRENT_POINT = new int[] { SEL_PIECE.row, SEL_PIECE.col }; } this.toggle_avaiable(bMoves); // Paint point in which the piece is sitting in point blue CURRENT_POINT = new int[3] { CURRENT_POINT[0], CURRENT_POINT[1], 2 }; GameObject current_square = this.getSquare(CURRENT_POINT[0], CURRENT_POINT[1]); this.setMaterial(ref current_square, SQUARE_NEUTRAL); bMoves.Add(CURRENT_POINT); availableMoves = bMoves; }
public Tuple <int[], pieceCode, bool, pieceCode, int[]> GetMoveData(Collider collider) { // The square clicked pieceCode target = collider.gameObject.GetComponent <pieceCode>(); int[] TARGET_POINT = this.get_point(target); if (TARGET_POINT == null) { return(null); } // The piece seleceted pieceCode piece_code = selected_piece.GetComponent <pieceCode>(); int[] selected_piece_cords; // Convert the position of the piece to global cooridantes (black coordinates) if (turn == "white") { selected_piece_cords = this.BlackToWhite(piece_code.row, piece_code.col); } else { selected_piece_cords = new int[] { piece_code.row, piece_code.col } }; // Player tries to move where they are if (selected_piece_cords[0] == target.row && selected_piece_cords[1] == target.col) { return(null); } int[] LocalTargetCords; if (turn == "white") { LocalTargetCords = this.BlackToWhite(target.row, target.col); } else { LocalTargetCords = new int[] { target.row, target.col } }; bool isEnemy = movements.isEnemy( LocalTargetCords[0], LocalTargetCords[1], turn == "white" ? boardWhite : boardBlack); var mData = Tuple.Create <int[], pieceCode, bool, pieceCode, int[]> (selected_piece_cords, target, isEnemy, piece_code, LocalTargetCords); return(mData); } string gameWinner() { if (WHITE_ID.Count == 0) { return("black"); } else if (BLACK_ID.Count == 0) { return("white"); } else { return(""); } } void setWinner(string winner) { gameEnded = true; if (winner == "white") { this.winnerBlack.gameObject.SetActive(false); this.winnerWhite.gameObject.SetActive(true); return; } this.winnerBlack.gameObject.SetActive(true); this.winnerWhite.gameObject.SetActive(false); time0 = Time.time; }
int isEnemyKing( /* * 0: Empty, continue; * 1: Friendly, stop; * 2: Enemy King; * 3: Enemy Piece; (Other) */ int row, int col, int[,] boardArray) { int piece = boardArray[row, col]; if (piece == 0) // Empty { return(0); } GameObject PieceObject; string type; WHITE_ID.TryGetValue(piece, out PieceObject); if (PieceObject == null) // BLACK PIECE { if (turn == "black") // Friendly Object, must stop { return(1); } type = BLACK_ID[piece].GetComponent <pieceCode>().type; } else { pieceCode code = PieceObject.GetComponent <pieceCode>(); int[] localCords; if (turn == "black") // transform black cords to white because code's cords is in white { localCords = this.BlackToWhite(row, col); } else { localCords = new int[2] { row, col } }; Debug.Log($"Piece Info: ({code.row}, {code.col})"); if (code.row == localCords[0] && code.col == localCords[0]) // WHITE PIECE { Debug.Log("White piece found"); if (turn == "white") { return(1); // Stop Because Friendly piece } type = code.type; } else // BLACK PIECE { Debug.Log("Black Piece Found"); if (turn == "black") { return(1); // stop } type = BLACK_ID[piece].GetComponent <pieceCode>().type; } } if (type == "king") { Debug.Log("KING FOUND"); return(2); } Debug.Log($"ENEMY FOUND {type}"); return(3); }
// public bool pinged = true; void Update() { if (!awaiting) { bool ReceiveAsync = client_socket.ReceiveAsync(e); awaiting = true; } if (e.BytesTransferred > 0) { string RECEVIED_STRING = System.Text.Encoding.UTF8.GetString(e.Buffer); int end_index = RECEVIED_STRING.IndexOf("END:END"); if (end_index != -1) // Full message received { IDictionary <string, string> MSG = TCP_SERVER.unpack_message(RECEVIED_STRING); this.handle_message(MSG); e = new SocketAsyncEventArgs(); e.SetBuffer(new byte[512], 0, 512); awaiting = false; } } if (this.turn != 1) { return; } Vector3 mpos = Input.mousePosition; Ray pos_ray = Camera.main.ScreenPointToRay(mpos); RaycastHit hit; if (Physics.Raycast(pos_ray.origin, pos_ray.direction, out hit, 100.0f)) { Collider collider = hit.collider; pieceCode code = collider.GetComponent <pieceCode>(); if (Input.GetMouseButtonDown(0)) { if ((turn == 1) && (collider.tag == this.color_piece)) { if (collider.gameObject == CHESS.selected_piece) { this.CHESS.unSelect(); return; } CHESS.select(collider); return; } else if (collider.tag == "SQUARE") { if (!CHESS.selected) { return; } System.Tuple <int[], pieceCode, bool, pieceCode, int[]> moveData = CHESS.GetMoveData(collider); if (moveData != null) { IDictionary <string, string> MESSAGE = new Dictionary <string, string>() { { "type", "move" }, { "piece", CHESS.selected_piece.GetComponent <pieceCode>().id.ToString() }, // Piece's ID { "to", $"{moveData.Item2.row},{moveData.Item2.col}" }, { "from", $"{moveData.Item1[0]},{moveData.Item1[1]}" }, { "isenemy", $"{moveData.Item3}" } }; this.SendAsyncMessage(TCP_SERVER.pack_message(MESSAGE)); } } } } }
void HandleMoveMessage(IDictionary <string, string> moveMessage) { if (IS_ENEMY) { CHESS.Kill(TO.row, TO.col, team_of_killed: moveMessage["turn"] == "white" ? "black" : "white"); } CHESS.move( piece: selected_piece, row: TO.row, col: TO.col, relativeToWhite: false, update: true, isHorse: selected_piece.GetComponent <pieceCode>().type == "horse" ? true : false, fromRow: FROM.row, fromCol: FROM.col ); CHESS.unSelect(); string hahaha = moveMessage["turn"]; if (moveMessage["turn"] != color_piece) { this.turn = 1; } else { this.turn = 0; } int[] LocalToCords; if (CHESS.turn == "white") { LocalToCords = CHESS.BlackToWhite(TO.row, TO.col); } else { LocalToCords = new int[2] { TO.row, TO.col }; } if (moveMessage["turn"] == "white") { CHESS.setTurn("black"); } else { CHESS.setTurn("white"); } pieceCode code = selected_piece.gameObject.GetComponent <pieceCode>(); code.row = LocalToCords[0]; code.col = LocalToCords[1]; if (started) { MOVE_AUDIO.Play(); } this.handleGameEnd(); }