コード例 #1
0
ファイル: Computer.cs プロジェクト: FCSadoyama/tic-tac-toe
 public Computer(string name, MarkEnum mark, DifficultyEnum difficulty)
 {
     this.Name       = name;
     this.Mark       = mark;
     this.Difficulty = difficulty;
     this.BestMove   = 5;
 }
コード例 #2
0
ファイル: Line.cs プロジェクト: FCSadoyama/tic-tac-toe
 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);
 }
コード例 #3
0
        public bool ChangeMark(MarkEnum newMark)
        {
            if (!IsMarkEmpty())
            {
                return(false);
            }

            this.Type = newMark;
            return(true);
        }
コード例 #4
0
ファイル: Line.cs プロジェクト: FCSadoyama/tic-tac-toe
 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);
 }
コード例 #5
0
        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);
        }
コード例 #6
0
ファイル: Line.cs プロジェクト: FCSadoyama/tic-tac-toe
 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);
 }
コード例 #7
0
ファイル: Match.cs プロジェクト: FCSadoyama/tic-tac-toe
        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);
        }
コード例 #8
0
        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);
            }
        }
コード例 #9
0
 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);
 }
コード例 #10
0
        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());
            }
        }
コード例 #11
0
ファイル: Line.cs プロジェクト: FCSadoyama/tic-tac-toe
 public bool Contains(MarkEnum mark)
 {
     return(Spot0.Type == mark || Spot1.Type == mark || Spot2.Type == mark);
 }
コード例 #12
0
ファイル: AI.cs プロジェクト: FCSadoyama/tic-tac-toe
 /// <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);
コード例 #13
0
ファイル: Human.cs プロジェクト: FCSadoyama/tic-tac-toe
 public Human(string name, MarkEnum mark)
 {
     this.Name = name;
     this.Mark = mark;
 }
コード例 #14
0
ファイル: Player.cs プロジェクト: FCSadoyama/tic-tac-toe
 protected MarkEnum SwitchMark(MarkEnum mark)
 {
     return(mark == this.Mark ? this.GetEnemyMark():this.Mark);
 }
コード例 #15
0
ファイル: AI.cs プロジェクト: FCSadoyama/tic-tac-toe
 /// <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);
コード例 #16
0
 public bool MakeMove(int position, MarkEnum newMark)
 {
     ISpot[] lines = Grid.GetAllSpots();
     return(this.Grid.GetAllSpots()[position - 1].ChangeMark(newMark));
 }
コード例 #17
0
ファイル: Grid.cs プロジェクト: FCSadoyama/tic-tac-toe
 public bool Contains(MarkEnum mark)
 {
     return(_horizontal0.Contains(mark) || _horizontal1.Contains(mark) || _horizontal2.Contains(mark));
 }