void Update() { //if it's my turn if (igameState.isWhiteTurn) { UpdateMouseOver(); int x = (int)mouseOver.x; int y = (int)mouseOver.y; if (igameState.selectedPiece != null) { UpdatePieceDrag(igameState.selectedPiece); } if (Input.GetMouseButtonDown(0)) { SelectPieces(x, y); } if (Input.GetMouseButtonUp(0)) { igameState.Moving((int)igameState.startDrag.x, (int)igameState.startDrag.y, x, y); } } else { // test.GetCurrentCheckerBoard(igameState); // test.selectAction(); aiMove.getAIMove(igameState, mblackPiecePrefab, mwhitePiecePrefab); } }
private Minimax makeDescisionTree(CheckerBoard board, GameObject b, GameObject w) { Minimax mainTree = gameObject.AddComponent <Minimax>(); mainTree.NewMinimax(null, score(board)); List <Move> moves = new List <Move>(); // get alls move for AI moves = getAllLegalMovesFor(false, board); foreach (Move move in moves) { // Make second row CheckerBoard secondstate = gameObject.AddComponent <CheckerBoard>(); copyBoardToBoard(board, secondstate, b, w); secondstate.Moving(move.currRow, move.currCol, move.movRow, move.movCol); Minimax firstLayer = gameObject.AddComponent <Minimax>(); firstLayer.NewMinimax(move, score(secondstate)); // get alls move for human List <Move> secondMoves = getAllLegalMovesFor(true, secondstate); //deleteFunction(secondstate); foreach (Move sMove in secondMoves) { // Make third row CheckerBoard thirdstate = gameObject.AddComponent <CheckerBoard>(); copyBoardToBoard(secondstate, thirdstate, b, w); thirdstate.Moving(sMove.currRow, sMove.currCol, sMove.movRow, sMove.movCol); Minimax secondLayer = gameObject.AddComponent <Minimax>(); secondLayer.NewMinimax(sMove, score(secondstate)); // get alls move for AI List <Move> thirdMoves = getAllLegalMovesFor(false, thirdstate); //deleteFunction(thirdstate); foreach (Move tMove in thirdMoves) { // Make fourth row CheckerBoard fourthstate = gameObject.AddComponent <CheckerBoard>(); // fourthstate.gameObject.SetActive(false); copyBoardToBoard(thirdstate, fourthstate, b, w); fourthstate.Moving(tMove.currRow, tMove.currCol, tMove.movRow, tMove.movCol); Minimax thirdLayer = gameObject.AddComponent <Minimax>(); thirdLayer.NewMinimax(tMove, score(fourthstate)); //deleteFunction(fourthstate); secondLayer.addChild(thirdLayer); } firstLayer.addChild(secondLayer); } mainTree.addChild(firstLayer); } return(mainTree); }
public void selectAction() { LinkedList <Node> visited = new LinkedList <Node>(); Node cur = this; visited.AddLast(this); // if not win and cur is a leaf if (gameState.CheckVictory(state) == -9) { cur.expand(); Node newNode = cur.select(); gameState.Moving((int)newNode.state[65], (int)newNode.state[66], (int)newNode.state[67], (int)newNode.state[68]); if (newNode != null) { //newNode.GetCurrentCheckerBoard(); visited.AddLast(newNode); // caculate value of all child node double value = rollOut(newNode); foreach (Node node in visited) { node.updateStats(value); } } else { Debug.LogError("select action selected a null node"); } } else { double value = 0; switch (cur.gameState.CheckVictory(state)) { case 8: value = 1.0; break; case -8: value = 0.0; break; default: break; } foreach (Node node in visited) { node.updateStats(value); } } //return }
public void getAIMove(CheckerBoard board, GameObject b, GameObject w) { descisionTree = makeDescisionTree(board, b, w); lastMove = pickMove(); board.Moving(lastMove.currRow, lastMove.currCol, lastMove.movRow, lastMove.movCol); }