//Command Pattern: Recevier public override int[] makeMove(Player player) { 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); }
public bool BoardIsFull() { IRule rule = new Connect4Rule(); RuleCommand gameOn = new ConcreteRuleCommand(rule); CommandInvoker ruleC = new CommandInvoker(gameOn); return(pieceCount >= ruleC.getMaxPiece(RowsCount, ColumnsCount)); }
//Command Pattern: Recevier public override int[] makeMove(Player player) { IRule rule = new Connect4Rule(); RuleCommand gameOn = new ConcreteRuleCommand(rule); CommandInvoker ruleC = new CommandInvoker(gameOn); Boolean moveMade = false; int col_move; int[] move = new int[2]; Console.Write("player {0} turn's ", Name); while (!moveMade) { Console.Write("Make a Move from 1 to {0} or read menu by 999: ", Board.Instance.ColumnsCount); if (Int32.TryParse(Console.ReadLine(), out col_move)) { if (1 <= col_move && col_move <= Board.Instance.ColumnsCount) { move = ruleC.getValidMove(col_move); if (move != null) { Console.Clear(); Board.Instance.DisplayBoard(); moveMade = true; } else { Console.WriteLine("\nThat column is full."); continue; } } else if (col_move == 999) { Menu.Instance.DisplayMenu(); } } else { Console.Clear(); Board.Instance.DisplayBoard(); Console.WriteLine("\nPlease enter an valid integer."); continue; } } return(move); }
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; }