public Computer(string name, MarkEnum mark, DifficultyEnum difficulty) { this.Name = name; this.Mark = mark; this.Difficulty = difficulty; this.BestMove = 5; }
public bool IsVictoryLine(out MarkEnum mark) { mark = MarkEnum.Null; if ((Spot0.Type == Spot1.Type && Spot1.Type == Spot2.Type) && Spot0.Type != MarkEnum.Null) { mark = Spot0.Type; return(true); } return(false); }
public bool ChangeMark(MarkEnum newMark) { if (!IsMarkEmpty()) { return(false); } this.Type = newMark; return(true); }
public int GetPossibleWinPosition(MarkEnum mark) { if (Spot0.Type == mark && Spot1.Type == mark && Spot2.Type == MarkEnum.Null) { return(Spot2.Position); } if (Spot0.Type == mark && Spot1.Type == MarkEnum.Null && Spot2.Type == mark) { return(Spot1.Position); } return(Spot0.Position); }
protected override bool FindWinOrBlockLine(IBoard board, MarkEnum mark) { ILine[] lines = board.GetAllGridLines(); foreach (ILine line in lines) { if (line.IsPossibleVictoryLine(mark)) { this.BestMove = line.GetPossibleWinPosition(mark); return(true); } } return(false); }
public bool IsPossibleVictoryLine(MarkEnum mark) { if (Spot0.Type == mark && Spot1.Type == mark && Spot2.Type == MarkEnum.Null) { return(true); } if (Spot0.Type == mark && Spot1.Type == MarkEnum.Null && Spot2.Type == mark) { return(true); } if (Spot0.Type == MarkEnum.Null && Spot1.Type == mark && Spot2.Type == mark) { return(true); } return(false); }
public GameStateEnum CheckMatchOver() { if (_board.IsGameOver()) { MarkEnum mark = this._board.GetWinner(); System.Console.Clear(); _board.Draw(); string winner = _players[0].Mark == mark ? _players[0].Name:_players[1].Name; string message = mark == MarkEnum.Null ? "And the game ends in a tie!" : $"And the winner is... {winner}!"; System.Console.WriteLine(message); return(GameStateEnum.Over); } return(GameStateEnum.Running); }
protected override int GetScore(IBoard board, int depth = 0) { board.IsGameOver(); MarkEnum mark = board.GetWinner(); if (mark == this.Mark) //Wins { return(this.MaxPlay - depth); } else if (mark == this.GetEnemyMark()) //Loses { return(this.MinPlay + depth); } else //Tie { return(this.TiePlay); } }
public static int GetSpot(bool isAvailable, MarkEnum mark) { if (isAvailable) { System.Console.WriteLine("Choose an available space to mark a {0}", mark.ToString()); } else { System.Console.WriteLine("Space is already filled, please, choose an available space to mark a {0}", mark.ToString()); } Int32.TryParse(System.Console.ReadLine(), out var selectedSpace); while (selectedSpace < 1 || selectedSpace > 9) { System.Console.WriteLine("Space is out of boundary, please, choose an available space to mark a {0}", mark.ToString()); Int32.TryParse(System.Console.ReadLine(), out selectedSpace); } return(selectedSpace); }
protected override int Minimax(IBoard board, MarkEnum player, DifficultyEnum difficulty, int depth = 0) { if (board.IsGameOver()) { return(this.GetScore(board, depth)); } IList <int> scores = new List <int>(); IList <int> moves = new List <int>(); var availableSpots = board.GetAvailableGridSpots(); foreach (ISpot move in availableSpots) { IBoard newBoard = new Board(board); newBoard.MakeMove(move.Position, player == this.Mark ? this.Mark:this.GetEnemyMark()); int result = this.Minimax(newBoard, this.SwitchMark(player), difficulty, depth++); scores.Add(result); moves.Add(move.Position); } if (player == this.Mark) { int maxScoreIndex = 0; if (difficulty == DifficultyEnum.Hard) { maxScoreIndex = scores.IndexOf(scores.Max()); this.BestMove = moves[maxScoreIndex]; } else { maxScoreIndex = scores.IndexOf(scores.Max()); this.BestMove = this.GetGoodMove(scores, moves, difficulty); } return(scores.Max()); } else { return(scores.Min()); } }
public bool Contains(MarkEnum mark) { return(Spot0.Type == mark || Spot1.Type == mark || Spot2.Type == mark); }
/// <summary> /// Searches for a possible win or lose /// </summary> /// <param name="board">IBoard</param> /// <param name="mark">MarkEnum</param> /// <returns>True or false</returns> abstract protected bool FindWinOrBlockLine(IBoard board, MarkEnum mark);
public Human(string name, MarkEnum mark) { this.Name = name; this.Mark = mark; }
protected MarkEnum SwitchMark(MarkEnum mark) { return(mark == this.Mark ? this.GetEnemyMark():this.Mark); }
/// <summary> /// Minimax Algorithm /// </summary> /// <param name="board">IBoard</param> /// <param name="player">MarkEnum</param> /// <param name="difficulty">DifficultyEnum</param> /// <param name="depth">int</param> /// <returns>Returns it's score (int)</returns> abstract protected int Minimax(IBoard board, MarkEnum player, DifficultyEnum difficulty, int depth = 0);
public bool MakeMove(int position, MarkEnum newMark) { ISpot[] lines = Grid.GetAllSpots(); return(this.Grid.GetAllSpots()[position - 1].ChangeMark(newMark)); }
public bool Contains(MarkEnum mark) { return(_horizontal0.Contains(mark) || _horizontal1.Contains(mark) || _horizontal2.Contains(mark)); }