Ejemplo n.º 1
0
        public string GetBoardSquareStateSymbol(BoardSquareState state)
        {
            switch (state)
            {
            case BoardSquareState.Water: return(" ");

            case BoardSquareState.Carrier: return("■");

            case BoardSquareState.BattleShip: return("■");

            case BoardSquareState.Submarine: return("■");

            case BoardSquareState.Cruiser: return("■");

            case BoardSquareState.Patrol: return("■");

            case BoardSquareState.Hit: return("x");

            case BoardSquareState.Miss: return("-");

            case BoardSquareState.Neighbour: return(" ");

            case BoardSquareState.Dead: return("F");

            default:
                throw new ArgumentOutOfRangeException(nameof(state), state, null);
            }
        }
Ejemplo n.º 2
0
        private bool yourTurn; //True if it is currently your turn

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Constructs a new Connect4ClientGame for a player with the given name.
        /// </summary>
        /// <param name="_name">Name of this player</param>
        public Connect4ClientGame(string _name)
        {
            if (_name.Contains("\n"))
            {
                throw new InvalidOperationException();
            }

            gameBoard = new BoardSquareState[6][];
            for (int i = 0; i < gameBoard.Length; i++)
            {
                gameBoard[i] = new BoardSquareState[7];
                for (int j = 0; j < gameBoard[i].Length; j++)
                {
                    gameBoard[i][j] = BoardSquareState.empty;
                }
            }

            name = _name;
            moveCol = -1;
            waitingForMoveReply = false;

            client = new Connect4Client(this);
            client.Disconnected += DisconnectedHandler;
            client.GameStarted += GameStartedHandler;
            client.IllegalMove += IllegalMoveHandler;
            client.LegalMove += LegalMoveHandler;
            client.MoveMade += MoveMadeHandler;
            client.Tick += TickHandler;
        }
Ejemplo n.º 3
0
        public string GetSymbolsForWebBoard(BoardSquareState boardSquareState)
        {
            switch (boardSquareState)
            {
            case BoardSquareState.Water: return(" ");

            case BoardSquareState.Carrier: return("🚢");

            case BoardSquareState.BattleShip: return("🚢");

            case BoardSquareState.Submarine: return("🚢");

            case BoardSquareState.Cruiser: return("🚢");

            case BoardSquareState.Patrol: return("🚢");

            case BoardSquareState.Hit: return("🎯");

            case BoardSquareState.Miss: return("�");

            case BoardSquareState.Neighbour: return(" ");

            case BoardSquareState.Dead: return("☠�");

            default:
                throw new ArgumentOutOfRangeException(nameof(boardSquareState), boardSquareState, null);
            }
        }
Ejemplo n.º 4
0
 public GameBoardViewModel(GameBoardViewModel copy)
 {
     _squares = copy._squares.Select(s => new GameBoardSquareViewModel(s.Row, s.Column, this)
     {
         State = s.State
     }).ToList();
     _nextMove = copy._nextMove;
 }
 public GameBoardViewModel(GameBoardViewModel copy)
 {
     _squares = copy._squares.Select(s => new GameBoardSquareViewModel(s.Row, s.Column, this)
       {
     State = s.State
       }).ToList();
       _nextMove = copy._nextMove;
 }
        public ComputerOpponent(GameBoardViewModel viewModel, BoardSquareState computerColor, int maxDepth)
        {
            _maxDepth = maxDepth;
              _computerColor = computerColor;
              _viewModel = viewModel;
              _viewModel.PropertyChanged += GameBoardViewModel_PropertyChanged;

              MakeMoveIfCorrectTurn();
        }
Ejemplo n.º 7
0
        public ComputerOpponent(GameBoardViewModel viewModel, BoardSquareState computerColor, int maxDepth)
        {
            _maxDepth                   = maxDepth;
            _computerColor              = computerColor;
            _viewModel                  = viewModel;
            _viewModel.PropertyChanged += GameBoardViewModel_PropertyChanged;

            MakeMoveIfCorrectTurn();
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Determines whether the given move is valid
        /// </summary>
        public bool IsValidMove(int row, int col, BoardSquareState state)
        {
            // check the cell is empty
            if (GetSquare(row, col).State != BoardSquareState.EMPTY)
            {
                return(false);
            }

            // if counters are surrounded in any direction, the move is valid
            return(_navigationFunctions.Any(navFunction => MoveSurroundsCounters(row, col, navFunction, state)));
        }
Ejemplo n.º 9
0
        public void GameRender(int x, int y)
        {
            BoardSquareState state = Shown.Board[x][y];

            Shown.Board[x][y] = BoardSquareState.Target;

            Console.Clear();
            Console.WriteLine(GameBoard.DisplayTables(Shown));
            Console.WriteLine("Press Spacebar to quit.                            Press S to save");

            Shown.Board[x][y] = state;
        }
Ejemplo n.º 10
0
 /// <summary>
 /// Determines whether there are any valid moves that the given player can make.
 /// </summary>
 private bool CanPlayerMakeAMove(BoardSquareState state)
 {
     // test all the board locations to see if a move can be made
     for (int row = 0; row < 8; row++)
     {
         for (int col = 0; col < 8; col++)
         {
             if (IsValidMove(row, col, state))
             {
                 return(true);
             }
         }
     }
     return(false);
 }
Ejemplo n.º 11
0
        public static string GetBoardSquareStateSymbol(BoardSquareState state)
        {
            switch (state)
            {
            case BoardSquareState.Empty: return(" ");

            case BoardSquareState.Ship: return("□");

            case BoardSquareState.Miss: return("·");

            case BoardSquareState.Hit: return("■");

            case BoardSquareState.Dead: return("F");

            default:
                throw new ArgumentOutOfRangeException(nameof(state), state, null);
            }
        }
Ejemplo n.º 12
0
        public string GetBoardSquareStateSymbol(BoardSquareState state)
        {
            switch (state)
            {
            case BoardSquareState.NotUsed: return(" ");

            case BoardSquareState.Hit: return("X");

            case BoardSquareState.Dead: return("D");

            case BoardSquareState.Miss: return("-");

            case BoardSquareState.Border: return("^");

            case BoardSquareState.Ship: return("■");

            case BoardSquareState.Target: return("O");

            default:
                throw new ArgumentOutOfRangeException(nameof(state), state, null);
            }
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Determines whether the given move 'surrounds' any of the opponents pieces.
        /// </summary>
        private bool MoveSurroundsCounters(int row, int column,
                                           NavigationFunction navigationFunction, BoardSquareState state)
        {
            int index = 1;

            var squares = NavigateBoard(navigationFunction, row, column);

            foreach (var square in squares)
            {
                BoardSquareState currentCellState = square.State;

                // the cell that is the immediate neighbour must be of the other colour
                if (index == 1)
                {
                    if (currentCellState != InvertState(state))
                    {
                        return(false);
                    }
                }
                else
                {
                    // if we have reached a cell of the same colour, this is a valid move
                    if (currentCellState == state)
                    {
                        return(true);
                    }

                    // if we have reached an empty cell - fail
                    if (currentCellState == BoardSquareState.EMPTY)
                    {
                        return(false);
                    }
                }

                index++;
            }

            return(false);
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Flips all the opponents pieces that are surrounded by the given move.
        /// </summary>
        private void FlipOpponentsCounters(int row, int column, BoardSquareState state)
        {
            foreach (var navigationFunction in _navigationFunctions)
            {
                // are any pieces surrounded in this direction?
                if (!MoveSurroundsCounters(row, column, navigationFunction, state))
                {
                    continue;
                }

                BoardSquareState opponentsState = InvertState(state);

                var squares = NavigateBoard(navigationFunction, row, column);
                foreach (var square in squares)
                {
                    if (square.State == state)
                    {
                        break;
                    }

                    square.State = state;
                }
            }
        }
Ejemplo n.º 15
0
 public GamingBoard(int size)
 {
     BoardName = "GamingBoard";
     Board     = new BoardSquareState[size, size];
 }
Ejemplo n.º 16
0
        private long whiteTimeLeft; //Time remaining for White's turns

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Constructs a new game between two players. Randomly selects positions (black or white), and sets
        /// up a timer to manage time left for a turn.
        /// </summary>
        /// <param name="client1">Player 1</param>
        /// <param name="client2">Player 2</param>
        /// <param name="timeLimit">Time limit for this game</param>
        public Connect4Game(Connect4ClientConnection client1, Connect4ClientConnection client2, int _timeLimit, 
            Connect4Service.WhoGoesFirst choosingMethod)
        {
            switch (choosingMethod)
            {
                case Connect4Service.WhoGoesFirst.first:
                    black = client1;
                    white = client2;
                    break;
                case Connect4Service.WhoGoesFirst.second:
                    black = client2;
                    white = client1;
                    break;
                case Connect4Service.WhoGoesFirst.random:
                    int tmp;
                    lock (r)
                    {
                        tmp = r.Next() % 2;
                    }
                    if (tmp == 0)
                    {
                        black = client1;
                        white = client2;
                    }
                    else
                    {
                        black = client2;
                        white = client1;
                    }
                    break;
                default:
                    break;
            }

            black.MoveRequest += MoveRequest;
            white.MoveRequest += MoveRequest;
            black.Resign += Resigned;
            white.Resign += Resigned;
            black.Disconnected += Disconnected;
            white.Disconnected += Disconnected;

            timeLimit = _timeLimit;

            whiteTimeLeft = _timeLimit * 1000;
            blackTimeLeft = _timeLimit * 1000;
            blacksMove = true;
            gameBoard = new BoardSquareState[6][];
            for (int i = 0; i < 6; i++)
            {
                gameBoard[i] = new BoardSquareState[7];
                for (int j = 0; j < 7; j++)
                {
                    gameBoard[i][j] = BoardSquareState.empty;
                }
            }

            itsOver = false;

            black.SendPlay(white.Name, _timeLimit, "black");
            white.SendPlay(black.Name, _timeLimit, "white");

            gameTimer = new Timer(1000); // tick every second
            Tick(null, null);
            gameTimer.Elapsed += Tick;
            gameTimer.Start();
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Returns the number of pieces traveled before encountering a space with a different
        /// state than the initial color. Requests parameters indicating direction to travel:
        /// ie: dx = 1, dy = 1 indicates up-left diagonal, dx = 0, dy = -1 indicates down.
        /// It is not recommended to send calls for |dx|, |dy| > 1, though function will still
        /// behave properly under those conditions.
        /// </summary>
        /// <param name="row">Starting position's row</param>
        /// <param name="col">Starting position's column</param>
        /// <param name="dx">Direction to travel in the rows</param>
        /// <param name="dy">Direction to travel in the columns</param>
        /// <param name="color">Initial color</param>
        /// <returns>Distance traveled before encountering a space in a different state than color</returns>
        private int PiecesInARow(int row, int col, int dx, int dy, BoardSquareState color)
        {
            int count = 0;

            while (true)
            {
                row += dx;
                col += dy;

                if (row > 5 || row < 0 || col > 6 || col < 0 || gameBoard[row][col] != color)
                {
                    break;
                }

                count++;
            }

            return count;
        }
Ejemplo n.º 18
0
 /// <summary>
 /// Returns true if the specified move is a winning move for the specified player.
 /// </summary>
 /// <param name="col">the column to test</param>
 /// <param name="row">the row to test</param>
 /// <param name="check">the color to check</param>
 /// <returns>whether or not it is a winning move</returns>
 private bool IsWinningMove(int col, int row, BoardSquareState check)
 {
     return (PiecesInARow(row, col, 0, -1, check) + PiecesInARow(row, col, 0, 1, check) + 1) >= 4
         || (PiecesInARow(row, col, -1, 0, check) + 1) >= 4
         || (PiecesInARow(row, col, -1, -1, check) + PiecesInARow(row, col, 1, 1, check) + 1) >= 4
         || (PiecesInARow(row, col, 1, -1, check) + PiecesInARow(row, col, -1, 1, check) + 1) >= 4;
 }
Ejemplo n.º 19
0
 public TrackingBoard()
 {
     BoardName = "TrackingBoard";
     Board     = new BoardSquareState[BoardSize, BoardSize];
     FillBoard();
 }
Ejemplo n.º 20
0
 public TrackingBoard(int size)
 {
     BoardName = "TrackingBoard";
     Board     = new BoardSquareState[size, size];
 }
        /// <summary>
        /// Determines whether the given move 'surrounds' any of the opponents pieces.
        /// </summary>
        private bool MoveSurroundsCounters(int row, int column,
      NavigationFunction navigationFunction, BoardSquareState state)
        {
            int index = 1;

              var squares = NavigateBoard(navigationFunction, row, column);
              foreach(var square in squares)
              {
            BoardSquareState currentCellState = square.State;

            // the cell that is the immediate neighbour must be of the other colour
            if (index == 1)
            {
              if (currentCellState != InvertState(state))
              {
            return false;
              }
            }
            else
            {
              // if we have reached a cell of the same colour, this is a valid move
              if (currentCellState == state)
              {
            return true;
              }

              // if we have reached an empty cell - fail
              if (currentCellState == BoardSquareState.EMPTY)
              {
            return false;
              }
            }

            index++;
              }

              return false;
        }
 private BoardSquareState InvertState(BoardSquareState state)
 {
     return state == BoardSquareState.BLACK ? BoardSquareState.WHITE : BoardSquareState.BLACK;
 }
        /// <summary>
        /// Flips all the opponents pieces that are surrounded by the given move.
        /// </summary>
        private void FlipOpponentsCounters(int row, int column, BoardSquareState state)
        {
            foreach (var navigationFunction in _navigationFunctions)
              {
            // are any pieces surrounded in this direction?
            if (!MoveSurroundsCounters(row, column, navigationFunction, state))
              continue;

            BoardSquareState opponentsState = InvertState(state);

            var squares = NavigateBoard(navigationFunction, row, column);
            foreach (var square in squares)
            {
              if (square.State == state)
            break;

              square.State = state;
            }
              }
        }
 /// <summary>
 /// Determines whether there are any valid moves that the given player can make.
 /// </summary>
 private bool CanPlayerMakeAMove(BoardSquareState state)
 {
     // test all the board locations to see if a move can be made
     for (int row = 0; row < 8; row++)
     {
     for (int col = 0; col < 8; col++)
     {
         if (IsValidMove(row, col, state))
         {
             return true;
         }
     }
     }
     return false;
 }
Ejemplo n.º 25
0
 private BoardSquareState InvertState(BoardSquareState state)
 {
     return(state == BoardSquareState.BLACK ? BoardSquareState.WHITE : BoardSquareState.BLACK);
 }
Ejemplo n.º 26
0
 public GamingBoard()
 {
     BoardName = "GamingBoard";
     Board     = new BoardSquareState[BoardSize, BoardSize];
     FillBoard();
 }
Ejemplo n.º 27
0
 private int CountShips(int optionsAmount, BoardSquareState boardSquareState)
 {
     return(optionsAmount - Player.Ships.Count(s => s.BoardSquareState == boardSquareState && s.ShipCoordinates.Count != 0));
 }
        /// <summary>
        /// Determines whether the given move is valid
        /// </summary>
        public bool IsValidMove(int row, int col, BoardSquareState state)
        {
            // check the cell is empty
              if (GetSquare(row, col).State != BoardSquareState.EMPTY)
            return false;

              // if counters are surrounded in any direction, the move is valid
              return _navigationFunctions.Any(navFunction => MoveSurroundsCounters(row, col, navFunction, state));
        }
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            BoardSquareState state = (BoardSquareState)Enum.Parse(typeof(BoardSquareState), (string)parameter);

            return(value.Equals(state) ? Visibility.Visible : Visibility.Collapsed);
        }