public value GenerateMove( opponent op, ref int bestRow, ref int bestColumn, value alpha, value beta) { value state, results; slotState owner; opponent opposite; int notNeeded=0; // ttt_count++; // See if we have a win if ((state = evaluate()) != value.unclear ) { // depth++; return state; } // Get the opposite player for the recursive call if (op == opponent.X) { owner = slotState.ownedByX ; opposite = opponent.O; state = alpha; } else { owner = slotState.ownedByO; opposite = opponent.X; state = beta; } int x, y; for (x=0; x < NumOfRows;x++) { for (y=0; y < NumOfCols; y++) { if (gameBoardSlots[x,y] == slotState.ownedByNull) { SetBoardPositionToOwner(x, y, owner); results = GenerateMove(opposite, ref notNeeded, ref notNeeded, alpha, beta); SetBoardPositionToOwner(x, y, slotState.ownedByNull); if ((op == opponent.X && results > state)|| (op == opponent.O && results < state)) { if (op == opponent.X) { alpha = state = results; } else { beta = state = results; } bestRow = x; bestColumn = y; if (alpha >= beta) { return state; } } } } } return state; }
value evaluateHelper(opponent p) { int x, y; int threeInARow, threeInACol; slotState s; if (p == opponent.X) s = slotState.ownedByX; else s = slotState.ownedByO; // See if there is a win in the rows for (x = 0; x < NumOfRows; x++) { threeInARow = 0; threeInACol = 0; for (y = 0; y < NumOfCols; y++) { if (gameBoardSlots[x, y] == s) { ++threeInARow; } if (gameBoardSlots[y, x] == s) { ++threeInACol; } } // Do we have a winner? if (threeInARow == NumOfRows || threeInACol == NumOfCols) { return value.winner; } } // Now check the diagonals // (0,0, 1,1, 2,2) or (0,2 1,1 2,0) if (gameBoardSlots[1,1] == s) { if ((gameBoardSlots[0,0] == s && gameBoardSlots[2,2] == s) || (gameBoardSlots[0,2] == s && gameBoardSlots[2,0] == s)) { return value.winner; } } return value.unclear; }
override public void MyTurn() { turnCount++; int[] playerList = banker.getPlayerList(this); int myMoney = banker.balanceInquiry(this); //if (banker.inJail(this) && turnCount > STAY_IN_JAIL) //{ // bail = true; //} List <opponent> opponents = new List <opponent>(); foreach (int p in playerList) { opponent t = new opponent(); t.tilesOwned = banker.getPlayerPropertyInfo(p); t.funds = banker.playerMoneyLookup(p); opponents.Add(t); } List <Game_Tile_Safe> myProperties = banker.getPropertiesOwned(this); int assetsAtThisTile = banker.checkCurrentTilesAssets(this); int position = banker.getCurrentTile(this).index; int assetPrices = banker.getCurrentTile(this).assetPrice; //try to mortgage first if money is below the reserve threshold if (myMoney < MORTGAGE_RESERVE) { int[] preferredMortgage = PropertyHelper.prefferedMortgage(); if (preferredMortgage.Length > 0) { banker.mortgageProperty(this, preferredMortgage[0]); PropertyHelper.mortgage(preferredMortgage[0]); myMoney = banker.balanceInquiry(this); //Console.WriteLine(myMoney); } } else //unmortgage a property if we're above the threshold { int[] mortgagedProperties = PropertyHelper.mortgagedProperties(); for (int i = 0; i < mortgagedProperties.Length; i++) { int index = mortgagedProperties[i]; int UMPrice = banker.getPropertyInfo(index).price + (int)((double)banker.getPropertyInfo(index).price * 0.1); if (myMoney + MORTGAGE_RESERVE > UMPrice) { banker.unmortageProperty(this, index); PropertyHelper.unMortgage(index); myMoney = banker.balanceInquiry(this); } } } //then try to buy if (banker.canBuy(this) && myMoney > BUY_RESERVE) { banker.buyProperty(this); PropertyHelper.buyProperty(position); myMoney = banker.balanceInquiry(this); } //then attempt to develop if we have monopoly(s) int[] canDevelop = PropertyHelper.canDevelop(); foreach (int p in canDevelop) { int pos = p; int price = banker.getPropertyInfo(p).assetPrice; if ((myMoney + ASSET_RESERVE) > price) { if (PropertyHelper.buyAsset(pos, 1)) { } else { continue; } if (banker.buyAssets(this, banker.getPropertyInfo(pos))) { /*Console.WriteLine(banker.getPropertyInfo(pos).name);*/ } } } }