/* given a string of the format COLrow>Colrow, make a move in the table */ /* returns 0 if couldnt move, returns 1 if moved */ internal int move(string i_MoveMessage, Player i_PlayerTurn) { int success = 0; bool punish = false; CheckersLogic cLogic = new CheckersLogic(this, i_PlayerTurn); if (cLogic.isLegalMove(i_MoveMessage, ref i_PlayerTurn)) { // before performing a move, check if player could perform eat and didnt do it. string check = cLogic.checkIfCanEat(i_MoveMessage); if (!check.Equals(string.Empty) && !check.Equals(i_MoveMessage) && !cLogic.isEatMove(i_MoveMessage)) { punish = true; } if (!punish) { move(i_MoveMessage, i_PlayerTurn, punish); } success = punish ? 0 : 1; } return(success); }
// play a game until someone wins / draw private void playGame() { bool hasNoLegalMoves = false; bool surrender = false; // we start with X player int playerIdTurn = 0; string moveMessage = string.Empty; CheckerSquare[] cSquare; string[] moveMessages; // play the game till its over while (true) { CheckersLogic logic = new CheckersLogic(m_Table, getPlayerById(playerIdTurn)); //// if first player can eat again, we give him another turn if (m_CanEatAgain && playerIdTurn == 0) { m_CanEatAgain = false; } else { Console.WriteLine(m_Turn + " turn " + getMoveType(playerIdTurn) + ":"); // if we are playing against the computer if (m_Turn.Equals("Computer\'s")) { cSquare = m_Table.GetCheckerSquares(1); moveMessages = logic.getPossibleMovesForPlayer(ref cSquare); hasNoLegalMoves = logic.hasNoLegalMoves(moveMessages); playerIdTurn = 1; if (hasNoLegalMoves) { break; } Random random = new Random(); moveMessage = moveMessages[random.Next(moveMessages.Length)]; if (m_CanEatAgain) { moveMessage = m_EatMove; m_CanEatAgain = false; } else { string eat = string.Empty; // filter the non-legal moves while (moveMessage.Equals("Aa>Aa")) { moveMessage = moveMessages[random.Next(moveMessages.Length)]; } eat = logic.checkIfCanEat(moveMessage); // prefer the eating move moveMessage = eat.Equals(string.Empty) ? moveMessage : eat; } } else { // player's (non-computer) turn cSquare = m_Table.GetCheckerSquares(playerIdTurn); moveMessages = logic.getPossibleMovesForPlayer(ref cSquare); hasNoLegalMoves = logic.hasNoLegalMoves(moveMessages); if (hasNoLegalMoves) { break; } moveMessage = getLegalMoveMessage(playerIdTurn); // if players wants and can quit if (moveMessage.Equals("Q")) { surrender = true; break; } } int moveIndicator = m_Table.move(moveMessage, getPlayerById(playerIdTurn)); if (moveIndicator == 0) { Console.WriteLine("Please enter legal move"); } else { // made a move, clear the screen Ex02.ConsoleUtils.Screen.Clear(); // print state after the move m_Table.printTable(); Console.WriteLine(m_Turn + " move was: " + getMoveType(playerIdTurn) + ": " + moveMessage); // checks if move was eatmove if (logic.isEatMove(moveMessage)) { logic.m_Player.m_JustAte = true; // checks if the player can eat again, if he can, camEat will hold the new move string canEat = logic.canEatAgain(moveMessage); if (canEat.Equals(string.Empty)) { // if he cant eat anymore, just switch turns logic.m_Player.m_JustAte = false; switchTurn(ref playerIdTurn); m_CanEatAgain = false; m_EatMove = string.Empty; } else { m_CanEatAgain = true; m_EatMove = canEat; } } else { // if it wasnt eatmove, checks if he could eat someone and didnt do it, if so he lose man switchTurn(ref playerIdTurn); } } } if (m_Table.m_NumO == 0 || m_Table.m_NumX == 0 || surrender || hasNoLegalMoves) { Console.WriteLine("GAME OVER !"); break; } } // something got us out of the game, check what happend calculatePointsAfterGame(); printResults(); checkIfPlayAgain(); }