Пример #1
0
        public void Initialise(Game game)
        {
            this.board = game.Board;

            for(var i=0; i < board.Length -1; i++)
            {
                var c = board[i];
                if (c == machineChar || c == userChar)
                    MakeMove(i, (c == machineChar));
            }

        }
Пример #2
0
        public IHttpActionResult GetMoveFromComputer(string userId, Game game)
        {
            if (!ModelState.IsValid)
                return BadRequest(ModelState);

            TicTacToeEngine engine = new TicTacToeEngine();
            // game.Board = "  OX X   ";
            engine.Initialise(game);

            var isMachineTurn = game.UserIdCurrent.ToLower().Trim() != jarvisName.ToLower().Trim();

            var isEnded = engine.MakeBestMove(isMachineTurn);
            game.UserIdCurrent = isMachineTurn ? jarvisName : userId;
            game.IsTerminated = isEnded;
            game.Board = engine.Board;
            if (game.IsTerminated)
            {
                var result = engine.GetResultState(game.Board);
                if (result == TicTacToeEngine.TicTacToeResult.MachineWin)
                    game.UserIdWinner = jarvisName;
                else
                    game.UserIdWinner = userId;
            }

            db.Entry(game).State = EntityState.Modified;

            try
            {
                db.SaveChanges();

            }
            catch
            {

            }
            return Ok(game);

        }
Пример #3
0
        public IHttpActionResult PostGame(Game game)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            TicTacToeEngine engine = new TicTacToeEngine();
            engine.Initialise(game);
            game.IsTerminated = engine.IsGameFullCompleted(game.Board);
            if (game.IsTerminated)
            {
                var result = engine.GetResultState(game.Board);
                if (result == TicTacToeEngine.TicTacToeResult.MachineWin)
                    game.UserIdWinner = game.UserIdOpponent;
                else
                    game.UserIdWinner = game.UserIdCreator;
            }



            db.Games.Add(game);
            db.SaveChanges();

            return CreatedAtRoute("DefaultApi", new { id = game.Id }, game);
        }
Пример #4
0
        public async Task<IHttpActionResult> PutGame(int id, Game game)
        {

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != game.Id)
            {
                return BadRequest();
            }

            TicTacToeEngine engine = new TicTacToeEngine();
            engine.Initialise(game);

 
            if (game.UserIdOpponent == jarvisName)
            {
                var isMachineTurn = game.UserIdCurrent.ToLower().Trim() != jarvisName.ToLower().Trim();
                var isEnded = engine.MakeBestMove(isMachineTurn);
                game.Board = engine.Board;
            }

            else
            {
                game.UserIdCurrent = game.UserIdCurrent == game.UserIdCreator ? game.UserIdOpponent : game.UserIdCreator;

            }
            game.IsTerminated = engine.IsGameFullCompleted(game.Board);


            if (game.IsTerminated)
            {
                var result = engine.GetResultState(game.Board);
                if (result == TicTacToeEngine.TicTacToeResult.MachineWin)
                    game.UserIdWinner = game.UserIdOpponent;
                else
                    game.UserIdWinner = game.UserIdCreator;
            }

            db.Entry(game).State = EntityState.Modified;

            try
            {
                db.SaveChanges();

                NotificationHubClient hub = NotificationHubClient
                                .CreateClientFromConnectionString("Endpoint=sb://tictactoenotifications.servicebus.windows.net/;SharedAccessKeyName=DefaultFullSharedAccessSignature;SharedAccessKey=+Au+w96izwXkztajDDeRB4r+6hsCsN0Gt1lN0Yg7lxM=", "OfficeTicTacToeNotificationHub");

                //var toast = @"<toast><visual><binding template=""ToastText01""><text id=""1"">Hello from a .NET App!</text></binding></visual></toast>";


                //await hub.SendWindowsNativeNotificationAsync(toast);

                var payload = @"<toast>
                                   <visual>
                                       <binding template=""ToastText01"">
                                           <text id=""1"">" + game.Id + @"</text>
                                       </binding>
                                   </visual>
                                </toast>";

                var tag = (game.UserIdCurrent == game.UserIdCreator) ? game.UserIdOpponent : game.UserIdCreator;

                Notification notification = new WindowsNotification(payload);
                notification.Headers.Add("X-WNS-Cache-Policy", "cache");
                notification.Headers.Add("X-WNS-Type", "wns/raw");
                notification.ContentType = "application/octet-stream";

                await hub.SendNotificationAsync(notification);
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!GameExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }

            return Ok(game);
        }