override public bool makeMove(int i, int j, int val) { BoardGame.Print(" Current Player " + val); int index = BoardGame.GetIndex(i, j); bool moveable = (index >= 0 && index < dim * dim && tiles [index] == vacantVal); if (!moveable) { return(false); } tiles[index] = val; // Remove from vacant space; if (vacantSpaces.ContainsKey(i)) { vacantSpaces[i].Remove(j); if (vacantSpaces[i].Count == 0) { vacantSpaces.Remove(i); } } prevMoveCol = lastMoveCol; prevMoveRow = lastMoveRow; lastMoveCol = i; lastMoveRow = j; //BoardGame.Print (" LastMoveCol " + lastMoveCol + " LastMoveRow " + lastMoveRow); postChecks(); Players.Instance.advancePlayer(); drawBoard(); pubInstance.NotifyListeners(val == TicTacToe.CrossVal ? "AssignedCross" : "AssignedNot"); return(true); }
public int GetVal(int col, int row) { if (col < dim && row < dim && col >= 0 && row >= 0) { return(tiles [BoardGame.GetIndex(col, row)]); } return(defValue); }
public int sumDiagRL(bool abs = true) { int sum = 0; for (int i = 0; i < dim; i++) { int index = BoardGame.GetIndex(dim - 1 - i, i); sum += tiles [index]; } return(abs ? Mathf.Abs(sum) : sum); }
public int sumRow(int rowIndex, bool abs = true) { int sum = 0; for (int col = 0; col < dim; col++) { int index = BoardGame.GetIndex(col, rowIndex); sum += tiles [index]; } return(abs ? Mathf.Abs(sum) : sum); }
public int sumCol(int colIndex, bool abs = true) { int sum = 0; for (int row = 0; row < dim; row++) { int index = BoardGame.GetIndex(colIndex, row); sum += tiles [index]; } return(abs ? Mathf.Abs(sum) : sum); }
override public int predictWinner(int potentialCol, int potentialRow, int val) { int saved = tiles[BoardGame.GetIndex(potentialCol, potentialRow)]; int index = BoardGame.GetIndex(potentialCol, potentialRow); tiles[index] = val; int localWinner = checkWinner(); tiles[index] = saved; return(localWinner); }
public void drawBoard(bool printAnyway = false) { //return; BoardGame.Print("Board:", printAnyway); for (int row = 0; row < dim; row++) { string rowStr = ""; for (int col = 0; col < dim; col++) { int index = BoardGame.GetIndex(col, row); rowStr = rowStr + tiles[index] + " "; } BoardGame.Print(rowStr, printAnyway); } }
// For debug void drawStates(List <float> tiles) { //return; // printing has to be done in row string rowStr = "Board States: " + TicTacToe.Instance.getCurPlayerVal(); BoardGame.Print(rowStr, true); rowStr = ""; int dim = TicTacToe.Instance.Dim; for (int col = 0; col < dim; col++) { for (int row = 0; row < dim; row++) { int index = BoardGame.GetIndex(col, row); rowStr = rowStr + tiles[index] + " "; } } BoardGame.Print(rowStr, true); }
int getVacantPos() { // Still proceed int col = -1, row = -1; TicTacToe game = TicTacToe.Instance; Dictionary <int, List <int> > vacantTiles = game.getAllowedMoves(); int colIndex = UnityEngine.Random.Range(0, vacantTiles.Count); int i = 0; foreach (KeyValuePair <int, List <int> > entry in vacantTiles) { if (i == colIndex) { col = entry.Key; List <int> list = entry.Value; int rowIndex = UnityEngine.Random.Range(0, list.Count); row = list [rowIndex]; break; } i++; } return(BoardGame.GetIndex(col, row)); }
override public int checkWinner() { int sum = 0; // Go thru all cols, see if they add up for (int col = 0; col < dim; col++) { sum = 0; for (int row = 0; row < dim; row++) { sum += tiles[BoardGame.GetIndex(col, row)]; } if (sum == dim) { return(1); } if (sum == -1 * dim) { return(-1); } } sum = 0; // Go thru all cols, see if they add up for (int row = 0; row < dim; row++) { sum = 0; for (int col = 0; col < dim; col++) { sum += tiles[BoardGame.GetIndex(col, row)]; } if (sum == dim) { return(1); } if (sum == -1 * dim) { return(-1); } } // l_R sum = 0; for (int col = dim - 1; col >= 0; col--) { for (int row = 0; row < dim; row++) { if (row == col) { sum += tiles[BoardGame.GetIndex(col, row)]; } } } if (sum == dim) { return(1); } if (sum == -1 * dim) { return(-1); } sum = 0; //R-L for (int col = dim - 1; col >= 0; col--) { for (int row = 0; row < dim; row++) { if (row == dim - 1 - col) { sum += tiles[BoardGame.GetIndex(col, row)]; } } } if (sum == dim) { return(1); } if (sum == -1 * dim) { return(-1); } return(0); }
override public int GetState(int potentialCol = -1, int potentialRow = -1, bool backTrack = false, int val = 0) { int k = 0; float h = 0f; bool potentialMove = potentialCol >= 0 && potentialCol < dim && potentialRow >= 0 && potentialRow < dim; for (int col = 0; col < dim; col++) { int v = 0; for (int row = 0; row < dim; row++) { int valueOfIntrest = tiles[BoardGame.GetIndex(col, row)]; if (potentialMove) { if (col == potentialCol && row == potentialRow) { if (!backTrack) { // Note u need to switch the players as you are calculating the next potential move valueOfIntrest = val; } else { valueOfIntrest = 0; } } } switch (valueOfIntrest) { case 1: v = 1; break; case -1: v = 2; break; default: v = 0; break; } h += Mathf.Pow(3, k) * v; k++; } } /* * if ( potentialMove ) { * BoardGame.Print ( "Potential Move "+ potentialCol + " " + potentialRow + " h " + (int)Mathf.Ceil(h) ); * } */ return((int)Mathf.Ceil(h)); }