//Handles the player clicking a tile. public override void handlePlayerAt(int x, int y) { //Get the staterep location and updating it with the correct value latestStateRep[x * 6 + y] = pieceSelected == 0 ? 2 : 1; //Change the players turn currentPlayersTurn = (currentPlayersTurn + 1) % 2; //Make a move array for the lastest move lastMovePlayed = new int[] { x * 6 + y, pieceSelected == 0 ? 2 : 1 }; //Update the number of moves numbMovesPlayed++; //Set up the last state latestAIState = new OCAIState(playerIndx, null, 0, latestStateRep, lastMovePlayed, numbMovesPlayed); //Find out the result of the board int result = latestAIState.getWinner(); //And the end game as such if (result >= 0) { if (result == 2) { winlose.text = "You drew!"; } else if (result == playerIndx) { winlose.text = "You won!"; } else { winlose.text = "You lost!"; } gamePlaying = false; EndGame.SetActive(true); } }
//Called each time the update loop checks the AI progress public override int checkAI() { //If the AI has not stated if (!ai.started) { //Start it with the current state. AIState currentState = new OCAIState((currentPlayersTurn + 1) % 2, null, 0, latestStateRep, lastMovePlayed, numbMovesPlayed); ai.run(currentState); } //Otherwise if the AI is done else if (ai.done) { //Get the next state (after the AI has moved) OCAIState nextAIState = (OCAIState)ai.next; //Unpack the state latestAIState = nextAIState; latestStateRep = nextAIState == null ? null : nextAIState.stateRep; //Reset the AI ai.reset(); //Switch which player is playing currentPlayersTurn = (currentPlayersTurn + 1) % 2; //Update the graphical rep of the board updateBoard(); //And increment the number of moves numbMovesPlayed++; } //Return who the winner is return(latestAIState.getWinner()); }
//Generates all children (results of all moves) from this state. public override List <AIState> generateChildren() { //List of children children = new List <AIState> (); //If the game is already over there are no children if (getWinner() >= 0) { this.children = children; return(children); } //Swap the player int newPIndx = (playerIndex + 1) % 2; //Increment the number of peices played int newNumbPieces = numbPiecesPlayed + 1; //Loop through all of the board pieces for (int i = 0; i < stateRep.Length; i++) { //if it is 0 (therefore empty) if (stateRep[i] == 0) { //We have a possible peice to play so clone the board int[] newStateRep = (int[])stateRep.Clone(); int[] move = { i, 1 }; //and simululate playing a white piece newStateRep [i] = 1; OCAIState childAIState = new OCAIState(newPIndx, this, depth + 1, newStateRep, move, newNumbPieces); //And add this state as a child children.Add(childAIState); //Then simululate playing a black piece int[] newStateRep2 = (int[])stateRep.Clone(); newStateRep2 [i] = 2; int[] move2 = { i, 2 }; OCAIState childAIState2 = new OCAIState(newPIndx, this, depth + 1, newStateRep2, move2, newNumbPieces); //And add this state as a child children.Add(childAIState2); } } //return it. return(children); }
public OrderAndChaos() { //Make a blank state latestAIState = new OCAIState(); currentPlayersTurn = 0; }