/// <summary> /// If a given player can immediately win by moving an a particular place /// </summary> /// <param name="currentState">state of the board</param> /// <param name="who">player who can win</param> /// <returns>-1 if cannot win in one move, corrspoding board position otherwise</returns> private int CanWinByMoving(int[] currentState, MyTicTacToeWorld.PLAYERS who) { int myPlaces, emptyPlace, emptyPos = 0; for (int i = 0; i < MyTicTacToeGame.allLines.GetLength(0); i++) { myPlaces = 0; emptyPlace = 0; for (int j = 0; j < MyTicTacToeGame.allLines.GetLength(1); j++) { if (currentState[MyTicTacToeGame.allLines[i, j]] == 0) { emptyPlace++; emptyPos = MyTicTacToeGame.allLines[i, j]; } else if (currentState[MyTicTacToeGame.allLines[i, j]] == (int)who) { myPlaces++; } if (myPlaces == 2 && emptyPlace == 1) { return(emptyPos); } } } return(-1); }
public bool ApplyAction(int where, MyTicTacToeWorld.PLAYERS who, int[] currentState) { if (currentState[where] != 0) { return(false); } currentState[where] = (int)who; return(true); }
private float EvaluateState(int[] state, MyTicTacToeWorld.PLAYERS player) { float sum = 0; int[] indexes = new int[3]; for (int i = 0; i < MyTicTacToeGame.allLines.GetLength(0); i++) { for (int j = 0; j < indexes.Length; j++) { indexes[j] = MyTicTacToeGame.allLines[i, j]; } sum += EvalSet(state, indexes, player); } return(sum); }
public bool CheckWinner(MyTicTacToeWorld.PLAYERS who, int[] currentState) { for (int i = 0; i < allLines.GetLength(0); i++) { for (int j = 0; j < allLines.GetLength(1); j++) { if (currentState[allLines[i, j]] != (int)who) { break; } if (j == allLines.GetLength(1) - 1) { return(true); } } } return(false); }
/// <summary> /// returns the value of a given game state for given player /// </summary> /// <param name="state">game state to be evaluated</param> /// <param name="indexes">list of 3 indexes (row, column, diagonal)</param> /// <param name="player">COMPUTER or PLAYERS</param> /// <returns>value of the state</returns> private float EvalSet(int[] state, int[] indexes, MyTicTacToeWorld.PLAYERS player) { if (indexes.Length != 3) { MyLog.ERROR.WriteLine("length of indexes has to be 3!"); return(0); } int freeOnes = 0; int myOnes = 0; int oponentOnes = 0; for (int i = 0; i < indexes.Length; i++) { if (state[indexes[i]] == (int)player) { myOnes++; } else if (state[indexes[i]] == 0) { freeOnes++; } else { oponentOnes++; } } if (myOnes == 1 && freeOnes == 2) // good { return(1); } else if (myOnes == 2 && freeOnes == 1) // better { return(10); } else if (myOnes == 3) // win { return(100); } return(0); }