Ejemplo n.º 1
0
 public TicTacToeMoveEventArgs(int row, int column, TicTacToePlayer move, bool gameOver)
 {
     Row      = row;
     Column   = column;
     Move     = move;
     GameOver = gameOver;
 }
Ejemplo n.º 2
0
 public static TicTacToePlayer TogglePlayer(this TicTacToePlayer player)
 {
     if (player == TicTacToePlayer.None)
     {
         return(TicTacToePlayer.None);
     }
     return(player == TicTacToePlayer.O ? TicTacToePlayer.X : TicTacToePlayer.O);
 }
Ejemplo n.º 3
0
 public void AddToQueue(AppUser user)
 {
     var player = new TicTacToePlayer
     {
         ConnectionId = Context.ConnectionId,
         User         = user,
     };
 }
Ejemplo n.º 4
0
        public bool JoinGame(Player playerTwo)
        {
            if (IsGameFull)
            {
                return(false);
            }

            Players[1] = TicTacToePlayer.NewTicTacToePlayer(playerTwo, LocationMarker.X, 1);
            IsGameFull = true;
            return(true);
        }
Ejemplo n.º 5
0
 public static TicTacToeGame NewTicTacToeGame(string gameId, Player playerOne)
 {
     return(new TicTacToeGame
     {
         Id = gameId,
         Players = new TicTacToePlayer[2] {
             TicTacToePlayer.NewTicTacToePlayer(playerOne, LocationMarker.O, 0), null
         },
         GameBoard = GameBoard.NewGameBoard()
     });
 }
Ejemplo n.º 6
0
        protected virtual int MapToInt(TicTacToePlayer player)
        {
            if (player == AiTicTacToePlayer)
            {
                return(1);
            }
            if (player == RealTicTacToePlayer)
            {
                return(-1);
            }

            return(0);
        }
Ejemplo n.º 7
0
        static void Main(string[] args)
        {
            TicTacToePlayer playerFirst  = SetPlayer("First");
            TicTacToePlayer playerSecond = SetPlayer("Second");
            bool            random       = SetRandom();
            TicTacToe       game         = new TicTacToe();

            game.UserInterface = new TicTacToeConsoleUserInterface();
            game.Start(new TicTacToeSettings {
                RandomStart = random
            }, playerFirst, playerSecond);
            Console.ReadKey();
        }
Ejemplo n.º 8
0
 public void Setup()
 {
     _playerA = new TicTacToePlayer()
     {
         Mark = TicTacToeMarkEnum.O
     };
     _playerB = new TicTacToePlayer()
     {
         Mark = TicTacToeMarkEnum.X
     };
     _game = new TicTacToeGame
     {
         PlayerA = _playerA,
         PlayerB = _playerB,
     };
 }
Ejemplo n.º 9
0
        public MainWindow()
        {
            InitializeComponent();

            var userInput = new TicTacToeUserWpfInput {
                CellType = TicTacToeCellType.Cross
            };
            var random  = new Random((int)DateTime.Now.Ticks);
            var p2      = new TicTacToePlayer(userInput);
            var buttons = new List <Button>();

            buttons.Add(Button1);
            buttons.Add(Button2);
            buttons.Add(Button3);
            buttons.Add(Button4);
            buttons.Add(Button5);
            buttons.Add(Button6);
            buttons.Add(Button7);
            buttons.Add(Button8);
            buttons.Add(Button9);

            foreach (var button in buttons)
            {
                button.Click += (sender, args) =>
                {
                    var i = buttons.IndexOf(button);
                    userInput.SendInput(i / 3, i % 3);
                };
            }

            _buttons = buttons;
            var ttt = new TicTacToe(new TicTacToeWpfOutput(_buttons));

            ttt.GameEnded += OnGameEnded;

            var p1Input = new TicTacToeCpuInput(TicTacToeCpuDifficulty.Impossible, (r, c) => ttt[r, c])
            {
                CellType = TicTacToeCellType.Circle
            };
            var p1 = new TicTacToePlayer(p1Input);

            p1.PlayerWaiting += sender => p1Input.GetInput();

            ttt.Start(p1, p2);
        }
Ejemplo n.º 10
0
        public void SetIndex(int row, int column, TicTacToePlayer move)
        {
            if (move == NextTicTacToePlayer)
            {
                SetIndexFast(row, column, move);

                TicTacToePlayer winner;
                if (GameOver(row, column, out winner))
                {
                    //TODO: fix cast
                    SigmaWindow window = (SigmaWindow)_monitor.Window;

                    if (winner == TicTacToePlayer.None)
                    {
                        Task.Factory.StartNew(() => window.SnackbarMessageQueue.Enqueue("It's a tie!", "Got it", null));
                    }
                    else
                    {
                        Task.Factory.StartNew(() => window.SnackbarMessageQueue.Enqueue($"Player: {winner} has won the game!", "Got it", null));
                    }

                    _gameOver = true;

                    foreach (TicTacToeButton ticTacToeButton in _buttons)
                    {
                        ticTacToeButton.IsEnabled = false;
                    }
                }

                NextTicTacToePlayer = NextTicTacToePlayer.TogglePlayer();

                Field.SetValue(MapToInt(move), 0, row * _field.GetLength(0) + column);
                OnFieldChange(row, column, move, _gameOver);

                if (Autoplay)
                {
                    if (NextTicTacToePlayer == AiTicTacToePlayer)
                    {
                        OnAiMove();
                    }
                }
            }
        }
Ejemplo n.º 11
0
        public bool GameOver(int row, int column, out TicTacToePlayer winner)
        {
            if ((winner = CheckColumn(column)) == TicTacToePlayer.None)
            {
                if ((winner = CheckRow(row)) == TicTacToePlayer.None)
                {
                    if ((winner = CheckDiagonal(row, column)) == TicTacToePlayer.None)
                    {
                        winner = CheckAntiDiagonal(row, column);
                    }
                }
            }


            if (winner != TicTacToePlayer.None)
            {
                return(true);
            }

            return(!FieldContainsEmptyCell());
        }
Ejemplo n.º 12
0
 public void RegisterPlayer(string name, BoardCell type)
 {
     if (Player1 == null)
     {
         Player1 = TicTacToePlayer.Create(name, type);
     }
     else if (Player2 == null)
     {
         if (Player1.Name == name)
         {
             throw new InvalidOperationException("Name already registered.");
         }
         if (Player1.Type == type)
         {
             throw new InvalidOperationException("Type already registered.");
         }
         Player2 = TicTacToePlayer.Create(name, type);
     }
     else
     {
         throw new InvalidOperationException("Players already registered.");
     }
 }
Ejemplo n.º 13
0
        static void Main(string[] args)
        {
            var ttt = new TicTacToe(new TicTacToeConsoleOutput());

            ttt.GameEnded += OnGameEnded;

            var random  = new Random((int)DateTime.Now.Ticks);
            var p1Input = new TicTacToeCpuInput(TicTacToeCpuDifficulty.Impossible, (r, c) => ttt[r, c])
            {
                CellType = TicTacToeCellType.Circle
            };
            var p2Input = new TicTacToeCpuInput(TicTacToeCpuDifficulty.Easy, (r, c) => ttt[r, c])
            {
                CellType = TicTacToeCellType.Cross
            };                                                                                                                           //new TicTacToeUserConsoleInput { CellType = TicTacToeCellType.Cross };
            var p1 = new TicTacToePlayer(p1Input);
            var p2 = new TicTacToePlayer(p2Input);

            p2.PlayerWaiting += sender => p2Input.GetInput();
            p1.PlayerWaiting += sender => p1Input.GetInput();

            ttt.Start(p1, p2);
            waitHandle.WaitOne();
        }
Ejemplo n.º 14
0
 private bool IsThreeInTheRow(TicTacToePlayer player, TicTacToeCell cell) =>
 State.Cells[cell.Row, 0] == player &&
 State.Cells[cell.Row, 1] == player &&
 State.Cells[cell.Row, 2] == player;
Ejemplo n.º 15
0
 protected void OnFieldChange(int row, int column, TicTacToePlayer move, bool gameOver)
 {
     FieldChange?.Invoke(this, new TicTacToeMoveEventArgs(row, column, move, gameOver));
 }
Ejemplo n.º 16
0
 public static string GetPlayerText(this TicTacToePlayer player)
 {
     return(player == TicTacToePlayer.None ? "" : player.ToString());
 }
Ejemplo n.º 17
0
 private void SetIndexFast(int row, int column, TicTacToePlayer move)
 {
     _field[row, column] = move;
     _buttons[row, column].MainContent = move.GetPlayerText();
 }
Ejemplo n.º 18
0
 private bool IsWinningMoveByPlayer(TicTacToePlayer player, TicTacToeCell cell) =>
 IsThreeInTheRow(player, cell) ||
 IsThreeInTheColumn(player, cell) ||
 IsThreeInTheDiagonal(player, cell) ||
 IsThreeInTheOtherDiagonal(player, cell);
Ejemplo n.º 19
0
 private bool IsThreeInTheOtherDiagonal(TicTacToePlayer player, TicTacToeCell cell) =>
 cell.Row + cell.Col == 2 &&
 State.Cells[0, 2] == player &&
 State.Cells[1, 1] == player &&
 State.Cells[2, 0] == player;
Ejemplo n.º 20
0
 private bool IsThreeInTheDiagonal(TicTacToePlayer player, TicTacToeCell cell) =>
 cell.Row == cell.Col &&
 State.Cells[0, 0] == player &&
 State.Cells[1, 1] == player &&
 State.Cells[2, 2] == player;
Ejemplo n.º 21
0
 private bool IsValidMove(TicTacToePlayer player, TicTacToeCell cell) =>
 IsValidPlayer(player) && !IsGameFinished() && !IsOutOfBounds(cell) && !IsCellOccopied(cell);
Ejemplo n.º 22
0
 private bool IsValidPlayer(TicTacToePlayer player) =>
 player == State.Turn;
Ejemplo n.º 23
0
 private bool IsThreeInTheColumn(TicTacToePlayer player, TicTacToeCell cell) =>
 State.Cells[0, cell.Col] == player &&
 State.Cells[1, cell.Col] == player &&
 State.Cells[2, cell.Col] == player;
Ejemplo n.º 24
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TicTacToeMove"/> class.
 /// </summary>
 /// <param name="player">The TicTacToe player.</param>
 /// <param name="cell">The TicTacToe cell being marked.</param>
 public TicTacToeMove(TicTacToePlayer player, TicTacToeCell cell)
 {
     Player = player;
     Cell   = cell;
 }