public static bool IsCheckMated(GameState game, eTeam team) { foreach (GameState gs in AI_Script.NextStates(game, team)) { if (!IsChecked(gs, team)) { return(false); } } return(true); }
public static bool IsChecked(GameState game, eTeam team) { // apply RookPath to King List <Pairii> KingOnRook; if (team == eTeam.WHITE) { for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (game.Pieces[i][j].Team == NextTeam(team) && AI_Script.Path(game.Pieces[i][j], game, i, j).Count > 0) { for (int k = 0; k < AI_Script.Path(game.Pieces[i][j], game, i, j).Count; k++) { int y = AI_Script.Path(game.Pieces[i][j], game, i, j)[k].Key; int x = AI_Script.Path(game.Pieces[i][j], game, i, j)[k].Value; if (game.Pieces[y][x].pType == eType.KING) { Debug.Log("CHECKED"); return(true); } } } } } } else { for (int i = 7; i >= 0; i--) { for (int j = 7; j >= 0; j--) { if (game.Pieces[i][j].Team == NextTeam(team) && AI_Script.Path(game.Pieces[i][j], game, i, j).Count > 0) { for (int k = AI_Script.Path(game.Pieces[i][j], game, i, j).Count - 1; k >= 0; k--) { int y = AI_Script.Path(game.Pieces[i][j], game, i, j)[k].Key; int x = AI_Script.Path(game.Pieces[i][j], game, i, j)[k].Value; if (game.Pieces[y][x].pType == eType.KING) { Debug.Log("CHECKED"); return(true); } } } } } } return(false); }
public void SetPositions(int x, int z, AI_Script pathfinder) { positionX = x; positionZ = z; aiPathfinder = pathfinder; }
private void Update() { #region Timer second += Time.deltaTime; if (second >= t + 1 && isEnd == false) { if (second >= 60) { second = 0; min += 1; t = 0; if (min < 10) { timeText.text = "0" + min.ToString() + ":" + "0" + ((int)second).ToString(); } else { timeText.text = min.ToString() + ":" + "0" + ((int)second).ToString(); } } else { if (second < 10) { t += 1;; if (min < 10) { timeText.text = "0" + min.ToString() + ":" + "0" + ((int)second).ToString(); } else { timeText.text = min.ToString() + ":" + "0" + ((int)second).ToString(); } } else { t += 1;; if (min < 10) { timeText.text = "0" + min.ToString() + ":" + ((int)second).ToString(); } else { timeText.text = min.ToString() + ":" + ((int)second).ToString(); } } } } #endregion #region Clicking if (Input.GetMouseButtonUp(0)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit) && isEnd == false) { if (hit.collider.gameObject.GetComponent <Cell>().State == eState.FREE && selected != null) { thisGame.GetComponent <Board>().HidePath(selected); selected = null; } if (currentTeam == hit.collider.gameObject.GetComponent <Cell>().Team || hit.collider.gameObject.GetComponent <Cell>().State == eState.TARGET || hit.collider.gameObject.GetComponent <Cell>().State == eState.KILLABLE) { if (hit.collider.gameObject.GetComponent <Cell>().State == eState.SELECTABLE) { if (selected == null) // CHUA BAM CAI GI { selected = hit.collider.gameObject; thisGame.GetComponent <Board>().SetSelectedColor(selected, true); thisGame.GetComponent <Board>().ShowPath(selected); } else // DA BAM ROI, GIO BAM CAI KHAC { thisGame.GetComponent <Board>().HidePath(selected); selected = hit.collider.gameObject; thisGame.GetComponent <Board>().SetSelectedColor(selected, true); thisGame.GetComponent <Board>().ShowPath(selected); } } else { if (hit.collider.gameObject.GetComponent <Cell>().State == eState.TARGET || hit.collider.gameObject.GetComponent <Cell>().State == eState.KILLABLE) { GameState gameState = new GameState(); gameState = GetGameState(thisGame); GameObject A = selected; GameObject B = hit.collider.gameObject; int m = B.GetComponent <Cell>().y; int n = B.GetComponent <Cell>().x; int y = A.GetComponent <Cell>().y; int x = A.GetComponent <Cell>().x; GameState nextState = AI_Script.ChangeState(gameState, y, x, m, n); //Debug.Log(nextState.Cells[0][4].pType + " ... " + nextState.Cells[7][4].pType); if (!IsChecked(nextState, currentTeam)) { thisGame.GetComponent <Board>().HidePath(selected); Moves.Add(new KeyValuePair <Pairii, Pairii>(new Pairii(y, x), new Pairii(m, n))); Save.Add(new KeyValuePair <PairTeamType, PairTeamType>( new PairTeamType(A.GetComponent <Cell>().Team, A.GetComponent <Cell>().pType), new PairTeamType(B.GetComponent <Cell>().Team, B.GetComponent <Cell>().pType))); CounterUpdate(B.GetComponent <Cell>().pType, B.GetComponent <Cell>().Team, 1); thisGame.GetComponent <Board>().Move(A, B); moves++; gameState = new GameState(); gameState = GetGameState(thisGame); if (Mode == true) { if (IsCheckMated(gameState, NextTeam(currentTeam))) { EndGame.image.sprite = currentTeam == eTeam.BLACK ? S_BlackWin : S_WhiteWin; EndGame.gameObject.SetActive(true); isEnd = true; } } else { if (IsCheckMated(gameState, NextTeam(currentTeam))) { EndGame.image.sprite = S_YouWin; EndGame.gameObject.SetActive(true); isEnd = true; } } prev = null; AI_turn = true; if (Mode == true) { currentTeam = NextTeam(currentTeam); } //Debug.Log(AI_Script.NumOfLegalMoves(gameState, currentTeam)); selected = null; } } } } } } #endregion else { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit) && isEnd == false) { if (hit.collider.gameObject.GetComponent <Cell>().Team == currentTeam && (AI_turn == false || Mode == true)) { if (prev == null) { prev = hit.collider.gameObject; prev.GetComponent <SpriteRenderer>().color = new Color32(40, 110, 200, 255); foreach (GameObject go in thisGame.GetComponent <Board>().Path(prev)) { if (Board.Enemy(go, prev)) { Board.Highlight(go, "KillMask", true); } else { Board.Highlight(go, "CellMask", true); } } } else if (prev != hit.collider.gameObject && selected == null) { prev.GetComponent <SpriteRenderer>().color = prev.GetComponent <Cell>().TeamColor(prev.GetComponent <Cell>().Team); foreach (GameObject go in thisGame.GetComponent <Board>().Path(prev)) { if (Board.Enemy(go, prev)) { Board.Highlight(go, "KillMask", false); } else { Board.Highlight(go, "CellMask", false); } } prev = hit.collider.gameObject; prev.GetComponent <SpriteRenderer>().color = new Color32(40, 110, 200, 255); foreach (GameObject go in thisGame.GetComponent <Board>().Path(prev)) { if (Board.Enemy(go, prev)) { Board.Highlight(go, "KillMask", true); } else { Board.Highlight(go, "CellMask", true); } } } } else if (hit.collider.gameObject.GetComponent <Cell>().Team != currentTeam && selected == null) { if (prev != null) { prev.GetComponent <SpriteRenderer>().color = prev.GetComponent <Cell>().TeamColor(prev.GetComponent <Cell>().Team); foreach (GameObject go in thisGame.GetComponent <Board>().Path(prev)) { if (Board.Enemy(go, prev)) { Board.Highlight(go, "KillMask", false); } else { Board.Highlight(go, "CellMask", false); } } prev = null; } } } } if (AI_turn == true && Mode == false && isEnd == false) { GameState gameState = new GameState(); // A[y][x]; gameState = GetGameState(thisGame); KeyValuePair <Pairii, Pairii> move = thisGame.GetComponent <AI_Script>().FindMove(gameState); GameObject A = thisGame.GetComponent <Board>().AllPieces[move.Key.Key][move.Key.Value]; GameObject B = thisGame.GetComponent <Board>().AllPieces[move.Value.Key][move.Value.Value]; Moves.Add(new KeyValuePair <Pairii, Pairii>(new Pairii(A.GetComponent <Cell>().y, A.GetComponent <Cell>().x), new Pairii(B.GetComponent <Cell>().y, B.GetComponent <Cell>().x))); Save.Add(new KeyValuePair <PairTeamType, PairTeamType>( new PairTeamType(A.GetComponent <Cell>().Team, A.GetComponent <Cell>().pType), new PairTeamType(B.GetComponent <Cell>().Team, B.GetComponent <Cell>().pType))); CounterUpdate(B.GetComponent <Cell>().pType, B.GetComponent <Cell>().Team, 1); thisGame.GetComponent <Board>().Move(A, B); moves++; gameState = new GameState(); gameState = GetGameState(thisGame); if (IsCheckMated(gameState, currentTeam)) { EndGame.image.sprite = S_YouLose; EndGame.gameObject.SetActive(true); isEnd = true; } AI_turn = false; } }