public bool CheckWinner(GameControl.Players player) { bool flag; for (int i = 0; i < lines.Count; i++) { flag = true; for (int j = 0; j < lines[i].Length; j++) { if (!nodes[lines[i][j]].chess) { flag = false; break; } else if (nodes[lines[i][j]].chess.GetComponent <Chess>().belongTo != player) { flag = false; break; } } if (flag) { return(true); } } return(false); }
public void Prompt(string text, GameControl.Players player) { Color color = player == GameControl.Players.Player1 ? Color.red : Color.blue; this.gameObject.GetComponent <Text>().text = text; this.gameObject.GetComponent <Text>().color = color; }
public void ShowWinner(GameControl.Players player) { Color color = player == GameControl.Players.Player1 ? Color.red : Color.blue; string c = player == GameControl.Players.Player1 ? "Red side" : "Blue side"; string text = "- - - -" + c + " is Winner" + "- - - -"; this.gameObject.GetComponent <Text>().text = text; this.gameObject.GetComponent <Text>().color = color; }
private GameObject SelectUnboardedChess(GameControl.Players player, GameObject[] chesses) { for (int i = 0; i < chesses.Length; i++) { if (chesses[i].GetComponent <Chess>().node_index == -1 && chesses[i].GetComponent <Chess>().belongTo == player) { return(chesses[i]); } } return(null); }
private int EvaluateBoard(Node[] board, GameControl.Players player) { int LCM = 3 * 5 * 7; // common multiple of inserction numbers GameControl.Players compititor = player == GameControl.Players.Player1 ? GameControl.Players.Player2 : GameControl.Players.Player1; int playerCount = 0, compititorCount = 0; int p_num; // the number of chess on a line belonging to player int c_num; // the number of chess on a line belonging to compititor int line_length; // the total inserction of this line for (int i = 0; i < lines.Count; i++) { p_num = 0; c_num = 0; line_length = lines[i].Length; for (int j = 0; j < line_length; j++) { if (!nodes[lines[i][j]].chess) { continue; } if (nodes[lines[i][j]].chess.GetComponent <Chess>().belongTo == player) { p_num++; } else { c_num++; } } // compititor owns at least one chess on this line, so that the player can't got win via it if (c_num > 0) { playerCount += 0; // same as player count if (p_num > 0) { compititorCount += 0; } else { compititorCount += (c_num * LCM / line_length) * (c_num * LCM / line_length); } } // player may get win on this line, incremental marginal benefit else { if (p_num == line_length) { playerCount += 100000000; } playerCount += (p_num * LCM / line_length) * (p_num * LCM / line_length); } } return(playerCount - compititorCount); }
public void AIOperation(GameControl.Players player) { GameObject[] chesses = GameObject.FindGameObjectsWithTag("Chess"); GameObject selectChess; int[] availableNodes; // evaluation of assumption board float assumptionEva; // records of best choice GameObject bestChess = null; int bestTarget = -1; float bestEva = -10000; if (!AllChesesOnBoard()) { selectChess = SelectUnboardedChess(player, chesses); availableNodes = FindAvailableNode(selectChess); if (availableNodes.Length == 0) { return; } for (int i = 0; i < availableNodes.Length; i++) { // try to move a chess and evaluate the board int origin_index = selectChess.GetComponent <Chess>().node_index; Vector3 origin_pos = selectChess.GetComponent <Chess>().transform.position; nodes = AIMoveChess(selectChess, nodes, availableNodes[i]); assumptionEva = EvaluateBoard(nodes, player); // undo the movement nodes = UndoMoveChess(selectChess, nodes, origin_index, origin_pos); // record the choice if it is better than current record if (assumptionEva > bestEva) { bestChess = selectChess; bestTarget = availableNodes[i]; bestEva = assumptionEva; } } } else { for (int j = 0; j < chesses.Length; j++) { if (chesses[j].GetComponent <Chess>().belongTo != player) { continue; } else { selectChess = chesses[j]; availableNodes = FindAvailableNode(selectChess); if (availableNodes.Length == 0) { continue; } for (int i = 0; i < availableNodes.Length; i++) { // try to move a chess and evaluate the board int origin_index = selectChess.GetComponent <Chess>().node_index; Vector3 origin_pos = selectChess.GetComponent <Chess>().transform.position; nodes = AIMoveChess(selectChess, nodes, availableNodes[i]); assumptionEva = EvaluateBoard(nodes, player); // undo the movement nodes = UndoMoveChess(selectChess, nodes, origin_index, origin_pos); // record the choice if it is better than current record if (assumptionEva > bestEva) { bestChess = selectChess; bestTarget = availableNodes[i]; bestEva = assumptionEva; } } } } } if (!bestChess || bestTarget == -1) { Debug.Log("Logic Error"); return; } AIMoveChess(bestChess, nodes, bestTarget); gc.GetComponent <GameControl>().ChangeTurn(); }