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

            Players[1] = TicTacToePlayer.NewTicTacToePlayer(playerTwo, LocationMarker.X, 1);
            IsGameFull = true;
            return(true);
        }
Example #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()
     });
 }
Example #6
0
        protected virtual int MapToInt(TicTacToePlayer player)
        {
            if (player == AiTicTacToePlayer)
            {
                return(1);
            }
            if (player == RealTicTacToePlayer)
            {
                return(-1);
            }

            return(0);
        }
Example #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();
        }
Example #8
0
 public void Setup()
 {
     _playerA = new TicTacToePlayer()
     {
         Mark = TicTacToeMarkEnum.O
     };
     _playerB = new TicTacToePlayer()
     {
         Mark = TicTacToeMarkEnum.X
     };
     _game = new TicTacToeGame
     {
         PlayerA = _playerA,
         PlayerB = _playerB,
     };
 }
Example #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);
        }
Example #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();
                    }
                }
            }
        }
Example #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());
        }
Example #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.");
     }
 }
Example #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();
        }
Example #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;
Example #15
0
 protected void OnFieldChange(int row, int column, TicTacToePlayer move, bool gameOver)
 {
     FieldChange?.Invoke(this, new TicTacToeMoveEventArgs(row, column, move, gameOver));
 }
Example #16
0
 public static string GetPlayerText(this TicTacToePlayer player)
 {
     return(player == TicTacToePlayer.None ? "" : player.ToString());
 }
Example #17
0
 private void SetIndexFast(int row, int column, TicTacToePlayer move)
 {
     _field[row, column] = move;
     _buttons[row, column].MainContent = move.GetPlayerText();
 }
Example #18
0
 private bool IsWinningMoveByPlayer(TicTacToePlayer player, TicTacToeCell cell) =>
 IsThreeInTheRow(player, cell) ||
 IsThreeInTheColumn(player, cell) ||
 IsThreeInTheDiagonal(player, cell) ||
 IsThreeInTheOtherDiagonal(player, cell);
Example #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;
Example #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;
Example #21
0
 private bool IsValidMove(TicTacToePlayer player, TicTacToeCell cell) =>
 IsValidPlayer(player) && !IsGameFinished() && !IsOutOfBounds(cell) && !IsCellOccopied(cell);
Example #22
0
 private bool IsValidPlayer(TicTacToePlayer player) =>
 player == State.Turn;
Example #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;
Example #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;
 }