/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { // initialization logic initPosition(); currentBoardVsGoat = new Board(); currentBoardVsTiger = new Board(); currentBoardTwoPlayer = new Board(); fetchSavedState(); fetchSavedSettings(); currentBoard = currentBoardVsGoat; goatsCaptured = currentBoard.mGoatsIntoBoard - getGoatCount(); gameStateVsGoat = new GameState(); gameStateVsTiger = new GameState(); gameStateTwoPlayer = new GameState(); winner = nodeState.none; newMoveDone = false; puckTouched = false; currentMode = gameMode.vsTiger; currentScreen = gameScreens.mainMenuScreen; //currentMode = gameMode.vsGoat; helpSection = new ScrollContainer(); creditsection = new CreditsSection(); onePlayerBtnGoat = new gButton(470, 97); onePlayerBtnTiger = new gButton(485, 197); twoPlayerBtn = new gButton(475,297); settingsBtn = new gButton(555,20); helpBtn = new gButton(480, 20); creditsBtn = new gButton(630, 20); rateBtn = new gButton(465, 400); sfxOnBtn = new gButton(380,175); sfxOffBtn = new gButton(380,175); undoBtn = new gButton(648, 403); //level levelBtn1 = new gButton(378, 265); levelBtn2 = new gButton(458, 265); levelBtn3 = new gButton(538, 265); menuBtn = new gButton(280,240); newGameBtn = new gButton(440, 240); resumeBtn = new gButton(440,240); okBtn = new gButton(552,320); //settings level switch (level) { case 1: levelBtn1.pressed = true; break; case 2: levelBtn2.pressed = true; break; case 3: levelBtn3.pressed = true; break; default: levelBtn1.pressed = true; break; } base.Initialize(); }
void resetCurrentGame() { currentBoard = new Board(); goatsCaptured = currentBoard.mGoatsIntoBoard - getGoatCount(); gameState.positionslist.Clear(); puckTouched = false; gameState.mGoatsIntoBoardList.Clear(); DeleteCurrentSavedFile(); }
void pieceTransitionStarter(Board next) { currentPiece = currentBoard.mTurnForPlayer ?nodeState.tiger:nodeState.goat; pieceTo.X = -1; pieceTo.Y = -1; capGoat.X = -1; capGoat.Y = -1; dropGoat.Y = -1; if (currentBoard.mGoatsIntoBoard < 15 && !currentBoard.mTurnForPlayer) { System.Diagnostics.Debug.WriteLine("goats turn-------------------"); //find diff of position for (int i = 0; i < 5; i++) { for (int j = 0; j < 6; j++) { if (currentBoard.mValues[i, j] == next.mValues[i, j]) { } else { System.Diagnostics.Debug.WriteLine("++++++++++++++++++++++++++prev value:" + currentBoard.mValues[i, j] + "new value: " + next.mValues[i, j] + "chaange i:" + i + "j:" + j); if (currentBoard.mValues[i, j] == nodeState.none && next.mValues[i, j] == currentPiece) { System.Diagnostics.Debug.WriteLine("++++++++++++++++++++++++++prev "); dropGoat.X = i; dropGoat.Y = j; System.Diagnostics.Debug.WriteLine("++++++++++++++++++++++++++prev value x:"+dropGoat.X+"y:"+dropGoat.Y); } } } } transitionSeconds = 0.0f; if (dropGoat.X != -1) { capGoatPosition = getGridPosition((int)dropGoat.X, (int)dropGoat.Y); capGoatscale.X = 1.5f; capGoatscale.Y = 1.5f; capGoatAlpha = 255; } //toPosition = pieceVelocity.Normalize(); pieceTransition = true; return; } //find diff of position for (int i = 0; i < 5; i++) { for (int j = 0; j < 6; j++) { if (currentBoard.mValues[i, j] == next.mValues[i, j]) { } else { System.Diagnostics.Debug.WriteLine("prev value:" + currentBoard.mValues[i, j] + "new value: " + next.mValues[i, j] + "chaange i:" + i + "j:" + j); if (currentBoard.mValues[i, j] == currentPiece || next.mValues[i, j] == currentPiece) { if (currentBoard.mValues[i, j] == currentPiece) { pieceFrom.X = i; pieceFrom.Y = j; } else { pieceTo.X = i; pieceTo.Y = j; } } else { if (currentBoard.mValues[i, j] == nodeState.goat || next.mValues[i, j] == nodeState.goat) { capGoat.X = i; capGoat.Y = j; } } } } } fromPosition = getGridPosition((int)pieceFrom.X, (int)pieceFrom.Y); toPosition = getGridPosition((int)pieceTo.X, (int)pieceTo.Y); prevPiecePosition = getGridPosition((int)pieceFrom.X, (int)pieceFrom.Y); pieceVelocity = (toPosition - fromPosition); transitionSeconds = 0.0f; pieceXDirection = fromPosition.X > toPosition.X ? -1 : 1; pieceYDirection = fromPosition.Y > toPosition.Y ? -1 : 1; if (capGoat.X != -1) { capGoatPosition = getGridPosition((int)capGoat.X, (int)capGoat.Y); capGoatscale.X = 1; capGoatscale.Y = 1; capGoatAlpha = 255; } //toPosition = pieceVelocity.Normalize(); pieceTransition = true; }
void mainScreenTouchHanlder() { TouchCollection touches = TouchPanel.GetState(); if (/*!touching &&*/ touches.Count > 0) { touching = true; TouchLocation touch = touches.First(); System.Diagnostics.Debug.WriteLine("X" + touch.Position.X + "Y" + touch.Position.Y); //make a move by computer // twoPlayerBtn.setPos(400,200); twoPlayerBtn.handeTouch(touch); onePlayerBtnGoat.handeTouch(touch); onePlayerBtnTiger.handeTouch(touch); settingsBtn.handeTouch(touch); helpBtn.handeTouch(touch); creditsBtn.handeTouch(touch); rateBtn.handeTouch(touch); } else { if (twoPlayerBtn.pressed) { twoPlayerBtn.pressed = false; currentMode = gameMode.twoPlayers; gameState = gameStateTwoPlayer; currentBoard = currentBoardTwoPlayer; goatsCaptured = currentBoard.mGoatsIntoBoard - getGoatCount(); newMoveDone = true;//to check who won switchToGamePlayScreen(); } if (onePlayerBtnGoat.pressed) { onePlayerBtnGoat.pressed = false; currentMode = gameMode.vsGoat; System.Diagnostics.Debug.WriteLine("current screen" + currentMode); gameState = gameStateVsGoat; currentBoard = currentBoardVsGoat; goatsCaptured = currentBoard.mGoatsIntoBoard - getGoatCount(); newMoveDone = true;//to check who won switchToGamePlayScreen(); } if (onePlayerBtnTiger.pressed) { onePlayerBtnTiger.pressed = false; currentMode = gameMode.vsTiger; gameState = gameStateVsTiger; currentBoard = currentBoardVsTiger; goatsCaptured = currentBoard.mGoatsIntoBoard - getGoatCount(); newMoveDone = true;//to check who won switchToGamePlayScreen(); } if (settingsBtn.pressed) { System.Diagnostics.Debug.WriteLine("settings press"); settingsBtn.pressed = false; showSettingsOverlay(); } if(helpBtn.pressed){ System.Diagnostics.Debug.WriteLine("help button press"); helpBtn.pressed = false; showHelpScreen(); } if (creditsBtn.pressed) { System.Diagnostics.Debug.WriteLine("credits button press"); creditsBtn.pressed = false; showCreditsScreen(); } if (rateBtn.pressed) { System.Diagnostics.Debug.WriteLine("credits button press"); rateBtn.pressed = false; showRateScreen(); } } }
void getInputAndUpdateGame(GameTime gameTime) { if (pieceTransition) { pieceTransitionHandler(gameTime); return; } //todo if (newMoveDone) { currentBoard.gameWon = CheckForWin(); if (sfxStateOn) { if (currentBoard.gameWon) { chimeEffect.Play(); } effect.Play(); } newMoveDone = false; //if (prevtime != null) //{ // System.Diagnostics.Debug.WriteLine(prevTotSeconds + "move by cpu prev time " + (gameTime.TotalGameTime.TotalSeconds - prevTotSeconds)); // double diff = gameTime.TotalGameTime.TotalSeconds - prevTotSeconds; // if (diff > 0.5) // { // currentBoard = nextBoard; // effect.Play(); // } //} //else //{ // newMoveDone = false; //} } if (currentBoard.gameWon) { currentScreen = gameScreens.winnersOverlay; resetWinnersOverlay(); DeleteCurrentSavedFile(); return; } if ((currentMode == gameMode.vsTiger && currentBoard.mTurnForPlayer) || (currentMode == gameMode.vsGoat && !currentBoard.mTurnForPlayer)) { System.Diagnostics.Debug.WriteLine("move by cpu: " + currentBoard.mTurnForPlayer); /* int movesDepth = 4; if (currentBoard.mGoatsIntoBoard > 14) { movesDepth = 4; } */ Board next = currentBoard.FindNextMove(level+1); gameState.positionslist.Add(currentBoard.mValues); gameState.mGoatsIntoBoardList.Add(currentBoard.mGoatsIntoBoard); pieceTransitionStarter(next); prevtime = gameTime; //prevTotSeconds = gameTime.TotalGameTime.TotalSeconds; System.Diagnostics.Debug.WriteLine("value time elapse"+gameTime.ElapsedGameTime.TotalSeconds); System.Diagnostics.Debug.WriteLine("value time elapse" + gameTime.TotalGameTime.TotalSeconds); //find diff of position currentBoard = next; //nextBoard = next; newMoveDone = true; goatsCaptured = currentBoard.mGoatsIntoBoard - getGoatCount(); } else { TouchCollection touches = TouchPanel.GetState(); if (!touching && touches.Count > 0) { touching = true; TouchLocation touch = touches.First(); System.Diagnostics.Debug.WriteLine("X" + touch.Position.X + "Y" + touch.Position.Y); //make a move by computer undoBtn.handeTouch(touch); if (currentBoard.mTurnForPlayer) System.Diagnostics.Debug.WriteLine("moving: tiger"); else { System.Diagnostics.Debug.WriteLine("moving: goat"); } if (currentBoard.mTurnForPlayer) { if (puckTouched) { foreach (var tobemovedpos in possiblePositions) { System.Diagnostics.Debug.WriteLine("actual moving:" + tobemovedpos.X + tobemovedpos.Y + "puck" + grid[tobemovedpos.X, tobemovedpos.Y]); if (getTouchArea(positions[tobemovedpos.X, tobemovedpos.Y]).Contains((int)touch.Position.X, (int)touch.Position.Y)) { System.Diagnostics.Debug.WriteLine("moving:" + tobemovedpos.X + tobemovedpos.Y + "puck" + grid[tobemovedpos.X, tobemovedpos.Y]); Board next = currentBoard.MakeMove(touchedPos, tobemovedpos); gameState.positionslist.Add(currentBoard.mValues); gameState.mGoatsIntoBoardList.Add(currentBoard.mGoatsIntoBoard); pieceTransitionStarter(next); currentBoard = next; newMoveDone = true; goatsCaptured = currentBoard.mGoatsIntoBoard - getGoatCount(); } } possiblePositions.Clear(); puckTouched = false; } else { for (int i = 0; i < 5; i++) { for (int j = 0; j < 6; j++) { if (getTouchArea(positions[i, j]).Contains((int)touch.Position.X, (int)touch.Position.Y)) { System.Diagnostics.Debug.WriteLine("touchedpos:" + i + j + "puck" + grid[i, j]); touchedPos = new Point(i, j); if (grid[i, j] == nodeState.tiger) { puckTouched = true; higPos = touchedPos; foreach (var move in currentBoard.GetShortMovesForTigerPuck(currentBoard.mValues, new Point(i, j))) { System.Diagnostics.Debug.WriteLine("possible pos:" + move.X + move.Y + "puck" + grid[i, j]); possiblePositions.Add(new Point(move.X, move.Y)); } foreach (var move in currentBoard.GetCaptureMovesForTigerPuck(currentBoard.mValues, new Point(i, j))) { System.Diagnostics.Debug.WriteLine("possible pos:" + move.X + move.Y + "puck" + grid[i, j]); possiblePositions.Add(new Point(move.X, move.Y)); } } //when touch for a piece is detected exit. prevent overlapping pieces touch i = 5; j = 6; } } } } } else { if (puckTouched) { foreach (var tobemovedpos in possiblePositions) { System.Diagnostics.Debug.WriteLine("actual moving:" + tobemovedpos.X + tobemovedpos.Y + "puck" + grid[tobemovedpos.X, tobemovedpos.Y]); if (getTouchArea(positions[tobemovedpos.X, tobemovedpos.Y]).Contains((int)touch.Position.X, (int)touch.Position.Y)) { System.Diagnostics.Debug.WriteLine("moving:" + tobemovedpos.X + tobemovedpos.Y + "puck" + grid[tobemovedpos.X, tobemovedpos.Y]); Board next = currentBoard.MakeMove(touchedPos, tobemovedpos); gameState.positionslist.Add(currentBoard.mValues); gameState.mGoatsIntoBoardList.Add(currentBoard.mGoatsIntoBoard); pieceTransitionStarter(next); currentBoard = next; newMoveDone = true; System.Diagnostics.Debug.WriteLine("value time elapse" + gameTime.TotalGameTime.TotalSeconds); prevtime = gameTime; prevTotSeconds = gameTime.TotalGameTime.TotalSeconds; goatsCaptured = currentBoard.mGoatsIntoBoard - getGoatCount(); } } possiblePositions.Clear(); puckTouched = false; } else { for (int i = 0; i < 5; i++) { for (int j = 0; j < 6; j++) { if (getTouchArea(positions[i, j]).Contains((int)touch.Position.X, (int)touch.Position.Y)) { System.Diagnostics.Debug.WriteLine("touchedpos:" + i + j + "puck" + grid[i, j]); touchedPos = new Point(i, j); if (currentBoard.mGoatsIntoBoard == 15 && grid[i, j] == nodeState.goat) { higPos = touchedPos; foreach (var move in currentBoard.GetMovesForGoatPuck(currentBoard.mValues, new Point(i, j))) { System.Diagnostics.Debug.WriteLine("possible pos:" + move.X + move.Y + "puck" + grid[i, j]); puckTouched = true; possiblePositions.Add(new Point(move.X, move.Y)); } } else if (currentBoard.mGoatsIntoBoard < 15 && grid[i, j] == nodeState.none) { //higPos = touchedPos; Board next = currentBoard.MakeMove(new Point(i, j), new Point(i, j)); gameState.positionslist.Add(currentBoard.mValues); gameState.mGoatsIntoBoardList.Add(currentBoard.mGoatsIntoBoard); pieceTransitionStarter(next); currentBoard = next; newMoveDone = true; prevTotSeconds = gameTime.TotalGameTime.TotalSeconds; System.Diagnostics.Debug.WriteLine("value time elapse" + gameTime.TotalGameTime.TotalSeconds); goatsCaptured = currentBoard.mGoatsIntoBoard - getGoatCount(); } } } } } } //System.Diagnostics.Debug.WriteLine("MOVE DONE FOR"); } else if (touches.Count == 0) { if (undoBtn.pressed) { if (gameState.positionslist.Count != 0 && !(gameState.positionslist.Count == 1 && (currentMode == gameMode.vsGoat || currentMode == gameMode.vsTiger))) { if (currentMode == gameMode.vsGoat || currentMode == gameMode.vsTiger) { gameState.positionslist.RemoveAt(gameState.positionslist.Count - 1); gameState.mGoatsIntoBoardList.RemoveAt(gameState.mGoatsIntoBoardList.Count - 1); } currentBoard.mValues = gameState.positionslist.Last(); gameState.positionslist.RemoveAt(gameState.positionslist.Count - 1); currentBoard.mGoatsIntoBoard = gameState.mGoatsIntoBoardList.Last(); gameState.mGoatsIntoBoardList.RemoveAt(gameState.mGoatsIntoBoardList.Count - 1); if (currentMode == gameMode.twoPlayers) { currentBoard.mTurnForPlayer = !currentBoard.mTurnForPlayer; } } } goatsCaptured = currentBoard.mGoatsIntoBoard - getGoatCount(); undoBtn.pressed = false; touching = false; } } }
public int MiniMax(int depth, bool needMax, out Board childWithMax) { childWithMax = this; String animal = "goat"; if (mTurnForPlayer) { animal = "tiger"; } //System.Diagnostics.Debug.Assert(mTurnForPlayer == needMax); if (depth == 0 || IsTerminalNode()) { // System.Diagnostics.Debug.WriteLine("TERMINAL NODE REACHED"); RecursiveScore = mScore; // System.Diagnostics.Debug.WriteLine("#" + animal+ "needmax" + needMax + "depth:" + depth + "recursive" + RecursiveScore); return mScore; } //System.Diagnostics.Debug.WriteLine("BEFORE GETTING CHILDREN"); int countofchild = 0; int bestValue = 0; if (needMax) { bestValue = int.MinValue; foreach (Board cur in GetChildren()) { countofchild++; //System.Diagnostics.Debug.WriteLine("CHILDREN" + ++countofchild); Board dummy; int score = cur.MiniMax(depth - 1, !needMax, out dummy); if (bestValue < score) { bestValue = score; childWithMax = cur; } //System.Diagnostics.Debug.WriteLine("score:" + score + "needmax" + needMax + "depth:" + depth + "bestvalue" + bestValue); } } else { bestValue = int.MaxValue; foreach (Board cur in GetChildren()) { countofchild++; //System.Diagnostics.Debug.WriteLine("CHILDREN" + ++countofchild); Board dummy; int score = cur.MiniMax(depth - 1, !needMax, out dummy); if (bestValue >= score) { bestValue = score; childWithMax = cur; } } } if (countofchild == 0) { if (needMax) { RecursiveScore = -mScore; } else { RecursiveScore = 20000 + depth; ////RecursiveScore = 1000; } //System.Diagnostics.Debug.WriteLine("NO CHILDREN FOR THIS BOARD"); childWithMax = this; //System.Diagnostics.Debug.WriteLine(animal + "needmax" + needMax + "depth:" + depth + "recursive" + RecursiveScore); return RecursiveScore; } RecursiveScore = bestValue; //System.Diagnostics.Debug.WriteLine("CHILDREN FOR THIS BOARD"); //System.Diagnostics.Debug.WriteLine(animal + "needmax" + needMax + "depth:" + depth + "recursive" + RecursiveScore); return RecursiveScore; }