/// <summary>
        /// Initializes a new instance of the <see cref="SinglePlayerView"/> class.
        /// </summary>
        /// <param name="vm">The vm.</param>
        public SinglePlayerView(SinglePlayerVM vm)
        {
            InitializeComponent();

            this.vm           = vm;
            this.DataContext  = this.vm;
            this.vm.GameOver += GameOver;
        }
Beispiel #2
0
        /// <summary>
        /// Starts this instance.
        /// </summary>
        public void Start()
        {
            string cmd = GetCommandFromMenu();

            if (cmd == null)
            {
                return;
            }
            //MessageBox.Show(string.Format("Command to send: '{0}'", cmd));

            try
            {
                Communicator com = new Communicator(ip, port);
                com.SendMessage(cmd);
                cmd = com.ReadMessage();
                com.Dispose();

                Message m = Message.FromJSON(cmd);
                if (m.MessageType != MessageType.CommandResult)
                {
                    // received notification when not expecting it.
                    return;
                }

                CommandResult res = CommandResult.FromJSON(m.Data);
                // filter unwanted commands
                if (res.Command != Command.Generate)
                {
                    return;
                }

                if (!res.Success)
                {
                    MessageBox.Show(res.Data);
                    return;
                }

                Maze maze = Maze.FromJSON(res.Data);

                // successfully created a game. pass the handle to the GameModel and start.
                SinglePlayerModel model = new SinglePlayerModel(maze);
                SinglePlayerVM    vm    = new SinglePlayerVM(model);
                SinglePlayerView  view  = new SinglePlayerView(vm);

                view.ShowDialog();
            }catch (Exception e)
            {
                MessageBox.Show("Error connecting to the server!", "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        public SinglePlayerWindow(string name, int row, int col)
        {
            InitializeComponent();
            //this.KeyDown += Window_KeyDown;

            this.name = name;
            this.row  = row;
            this.col  = col;
            int    port = Properties.Settings.Default.ServerPort;
            string ip   = Properties.Settings.Default.ServerIP;

            vm = new SinglePlayerVM(this.name, this.row, this.col, port, ip);
            vm.GenerateGame(this.name, this.row, this.col);
            DataContext = vm;
        }