Beispiel #1
0
        private void RedrawField()
        {
            Board.Field[,] board = RotateBoard(Board1);
            if (false)
            {
                board = RotateBoard(Board1);
            }
            else
            {
                int counter = 0;
                board = new Board.Field[Board1.WidthHeight, Board1.WidthHeight];
                foreach (var field in Board1.Fields)
                {
                    board[counter % Board1.WidthHeight, counter / Board1.WidthHeight] = field;
                    counter++;
                }
            }

            for (int i = 0; i < BoardWH; i++)
            {
                for (int j = 0; j < BoardWH; j++)
                {
                    int t = i + j * BoardWH;
                    FieldRectangles[t].Fill = ReturnFieldBrush(board[t / BoardWH, t % BoardWH]);
                }
            }
        }
Beispiel #2
0
 public void PlayMove(int x, int y, Board.Field owner)
 {
     if (_game.Board.Fields[x, y] != Board.Field.Free)
     {
         MessageBox.Show("Field is not free!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
         return;
     }
     _game.Board.SetOwner(x, y, owner);
 }
Beispiel #3
0
        private Brush ReturnFieldBrush(Board.Field field)
        {
            switch (field)
            {
            case Board.Field.Empty:
                return(Brushes.White);

            case Board.Field.Head:
                return(Brushes.Black);

            case Board.Field.Tail:
                return(Brushes.Green);

            case Board.Field.Food:
                return(Brushes.LightGreen);
            }
            return(Brushes.White);
        }
Beispiel #4
0
        /// <summary>
        /// Rotates the board to a standard orientation so that the snake direction alwyas ends up facing the same way
        /// </summary>
        /// <param name="initBoard"></param>
        /// <returns></returns>
        private Board.Field[,] RotateBoard(Board initBoard)
        {
            int times = (int)initBoard.SnakeDirection;

            int counter = 0;

            Board.Field[,] board = new Board.Field[initBoard.WidthHeight, initBoard.WidthHeight];
            foreach (var field in initBoard.Fields)
            {
                board[counter % initBoard.WidthHeight, counter / initBoard.WidthHeight] = field;
                counter++;
            }

            board = Arrays.RotateHorizontally(board, initBoard.WidthHeight / 2 - initBoard.SnakeHeadX < 0 ? initBoard.WidthHeight / 2 - initBoard.SnakeHeadX + initBoard.WidthHeight : initBoard.WidthHeight / 2 - initBoard.SnakeHeadX);
            board = Arrays.RotateVertically(board, initBoard.WidthHeight / 2 - initBoard.SnakeHeadY < 0 ? initBoard.WidthHeight / 2 - initBoard.SnakeHeadY + initBoard.WidthHeight : initBoard.WidthHeight / 2 - initBoard.SnakeHeadY);

            for (int i = 0; i < 3 - times; i++)
            {
                board = Arrays.RotateMatrix(board, initBoard.WidthHeight);
            }

            return(board);
        }
Beispiel #5
0
        public void FieldClicked(int x, int y)
        {
            if (_over)
            {
                return;
            }
            Board.Field clickedField = _model.GetField(x, y);

            if (clickedField == (Board.Field)_model.GetPlayer())
            {
                _selected = true;
                _model.SetSelected(x, y);
            }
            else if (clickedField == Board.Field.Free)
            {
                if (!_selected)
                {
                    return;
                }
                var coords = _model.GetSelectedCoordinates();
                int x2     = coords.X;
                int y2     = coords.Y;

                if (Math.Abs(x2 - x) > 2 || Math.Abs(y2 - y) > 2)
                {
                    return;
                }

                _model.SetField(x, y);


                _model.SetSelected(-1, -1);
                _selected = false;

                if (Math.Abs(x2 - x) == 2 || Math.Abs(y2 - y) == 2)
                {
                    _model.ReleaseField(x2, y2);
                }

                Conversion(x, y);

                _model.NextPlayer();


                int res = CheckGameOver();
                if (_over)
                {
                    if (res == 1)
                    {
                        FormMessage fm = new FormMessage("Winner: Player 1!  Resault: " + _model.GetPointsPlayer1() + ":" + _model.GetPointsPlayer2());
                        fm.Opacity = 0;
                        fm.ShowDialog();
                    }
                    if (res == 2)
                    {
                        FormMessage fm = new FormMessage("Winner: Player 2!  Resault: " + _model.GetPointsPlayer1() + ":" + _model.GetPointsPlayer2());
                        fm.Opacity = 0;
                        fm.ShowDialog();
                    }
                    if (res == 0)
                    {
                        FormMessage fm = new FormMessage("Draw!");
                        fm.Opacity = 0;
                        fm.ShowDialog();
                    }
                }
            }

            _view.Redraw();
        }
Beispiel #6
0
        public void FieldClicked(int x, int y)
        {
            if (!_joined)
            {
                return;
            }
            if (_over)
            {
                return;
            }
            if (!_canPlay && !_recv)
            {
                return;
            }

            Board.Field clickedField = _model.GetField(x, y);

            if (clickedField == (Board.Field)_me)
            {
                _selected = true;
                _model.SetSelected(x, y);
            }
            else if (clickedField == Board.Field.Free)
            {
                if (!_selected)
                {
                    return;
                }
                _canPlay = !_canPlay;
                if (!_recv)
                {
                    try
                    {
                        if (_me == 1)
                        {
                            _client.Send(Encoding.ASCII.GetBytes(_model.GetSelectedCoordinates().X + "" + _model.GetSelectedCoordinates().Y + "" + x + "" + y));
                        }
                        else
                        {
                            _server.Client.Send(Encoding.ASCII.GetBytes(_model.GetSelectedCoordinates().X + "" + _model.GetSelectedCoordinates().Y + "" + x + "" + y));
                        }
                    }

                    catch (Exception ex)
                    {
                        EndGame();
                        return;
                    }
                }

                var coords = _model.GetSelectedCoordinates();
                int x2     = coords.X;
                int y2     = coords.Y;

                if (Math.Abs(x2 - x) > 2 || Math.Abs(y2 - y) > 2)
                {
                    return;
                }

                _model.SetField(x, y);

                _model.SetSelected(-1, -1);
                _selected = false;

                if (Math.Abs(x2 - x) == 2 || Math.Abs(y2 - y) == 2)
                {
                    _model.ReleaseField(x2, y2);
                }

                Conversion(x, y);

                _model.NextPlayer();

                int res = CheckGameOver();
                if (_over)
                {
                    if (res == 1)
                    {
                        FormMessage fm = new FormMessage("Winner: Player 1!  Result: " + _model.GetPointsPlayer1() + ":" + _model.GetPointsPlayer2());
                        fm.Opacity = 0;
                        fm.ShowDialog();
                    }
                    if (res == 2)
                    {
                        FormMessage fm = new FormMessage("Winner: Player 2!  Result: " + _model.GetPointsPlayer1() + ":" + _model.GetPointsPlayer2());
                        fm.Opacity = 0;
                        fm.ShowDialog();
                    }
                    if (res == 0)
                    {
                        FormMessage fm = new FormMessage("Draw!");
                        fm.Opacity = 0;
                        fm.ShowDialog();
                    }
                }
            }


            _view.Redraw();
        }
Beispiel #7
0
        public void FieldClicked(int x, int y)
        {
            if (!_joined)
            {
                return;
            }
            if (_over)
            {
                return;
            }
            if (!_canPlay && !_recv)
            {
                return;
            }

            Board.Field clickedField = _model.GetField(x, y);

            if (clickedField == (Board.Field)_me)
            {
                _selected = true;
                _model.SetSelected(x, y);
            }
            else if (clickedField == Board.Field.Free)
            {
                if (!_selected)
                {
                    return;
                }
                _canPlay = !_canPlay;
                if (!_recv)
                {
                    IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(ConfigurationManager.AppSettings["ServerIPAddress"]), int.Parse(ConfigurationManager.AppSettings["ServerListenerMovePort"]));
                    TcpClient  tcp  = new TcpClient();
                    try
                    {
                        tcp.Client.Connect(ipep);
                        tcp.Client.Send(Encoding.ASCII.GetBytes(_model.GetSelectedCoordinates().X + "" + _model.GetSelectedCoordinates().Y + "" + x + "" + y));
                        tcp.Close();
                    }
                    catch (Exception ex)
                    {
                        EndGame();
                    }
                }

                var coords = _model.GetSelectedCoordinates();
                int x2     = coords.X;
                int y2     = coords.Y;

                if (Math.Abs(x2 - x) > 2 || Math.Abs(y2 - y) > 2)
                {
                    return;
                }

                _model.SetField(x, y);

                _model.SetSelected(-1, -1);
                _selected = false;

                if (Math.Abs(x2 - x) == 2 || Math.Abs(y2 - y) == 2)
                {
                    _model.ReleaseField(x2, y2);
                }

                Conversion(x, y);

                _model.NextPlayer();

                int res = CheckGameOver();
                if (_over)
                {
                    if (res == 1)
                    {
                        FormMessage fm = new FormMessage("Winner: Player 1!  Resault: " + _model.GetPointsPlayer1() + ":" + _model.GetPointsPlayer2());
                        fm.Opacity = 0;
                        fm.ShowDialog();
                    }
                    if (res == 2)
                    {
                        FormMessage fm = new FormMessage("Winner: Player 2!  Resault: " + _model.GetPointsPlayer1() + ":" + _model.GetPointsPlayer2());
                        fm.Opacity = 0;
                        fm.ShowDialog();
                    }
                    if (res == 0)
                    {
                        FormMessage fm = new FormMessage("Draw!");
                        fm.Opacity = 0;
                        fm.ShowDialog();
                    }
                }
            }


            _view.Redraw();
        }