public void getMove() { bool gameLoop = true; bool inputLoop; Player currentPlayer; int[] move = new int[2]; while (gameLoop) { if (GameData.Instance.currentPlayer == 1) { currentPlayer = player1; } else { currentPlayer = player2; } Console.Clear(); Board.Instance.DisplayPlayer(); Board.Instance.DisplayBoard(); do { inputLoop = true; move = currentPlayer.makeMove(currentPlayer); if (move != null) { DropPiece(move, currentPlayer.Colour); saveHistory(GameData.Instance.inputNumber, move, currentPlayer.ID, currentPlayer.Name, currentPlayer.Colour); inputLoop = false; GameData.Instance.inputNumber++; } } while (inputLoop); IRule rule = new Connect4Rule(); RuleCommand gameOn = new ConcreteRuleCommand(rule); CommandInvoker ruleC = new CommandInvoker(gameOn); if (ruleC.hasWinner(currentPlayer)) { Console.Clear(); Board.Instance.DisplayBoard(); Console.Write("Player {0} has won!!", currentPlayer.Name); Console.WriteLine("\nPress enter to quit."); GameData.Instance.winner = currentPlayer.ID; gameLoop = false; } if (Board.Instance.BoardIsFull()) { System.Console.Clear(); Board.Instance.DisplayBoard(); Console.WriteLine("\nIt is a draw."); Console.WriteLine("\nPress enter to quit."); gameLoop = false; } else { if (GameData.Instance.currentPlayer == 1) { GameData.Instance.currentPlayer = 2; } else { GameData.Instance.currentPlayer = 1; } } } Console.ReadKey(); }
//Command Pattern: Recevier public override int[] makeMove(Player player) { //easy rule; IRule rule = new Connect4Rule(); RuleCommand gameOn = new ConcreteRuleCommand(rule); CommandInvoker ruleC = new CommandInvoker(gameOn); int[] move = new int[2]; Random random = new Random(); bool moveMade = false; while (!moveMade) { int AI_Move = random.Next(1, 7); if (ruleC.getValidMove(AI_Move) != null) { move = ruleC.getValidMove(AI_Move); moveMade = true; } else { continue; } } return(move); //Hard Rule haven't been implement yet //IRule rule = new Connect4Rule(); //RuleCommand gameOn = new ConcreteRuleCommand(rule); //CommandInvoker ruleC = new CommandInvoker(gameOn); //int[] move = new int[2]; //bool moveMade = false; //while (!moveMade) //{ // for(int i = 0; i<Board.Instance.ColumnsCount; i++) // { // if (ruleC.getValidMove(i) != null) // { // move = ruleC.getValidMove(i); // if (ruleC.getComputerHardMove(i,player) != null) // { // move = ruleC.getComputerHardMove(i, player); // moveMade = true; // } // else // { // return move; // } // } // else // { // continue; // } // } //} //return move; }