Exemplo n.º 1
0
        private void STATE_SOLVING(Request theRequest)
        {
            myState = solverState.SOLVING;

            SudokuLibApi.EnableGuessing();

            myMainWindow.DisableClearButton();
            myMainWindow.DisableExitButton();

            // Convert the user input to a byte array to send to the Sudoku API

            byte[] myBytes = new byte[81];

            int byteIdx = 0;

            for (int i = 1; i <= 9; ++i)
            {
                for (int j = 1; j <= 9; ++j)
                {
                    if (myMainWindow.myInputBoxes[i, j].Text != string.Empty)
                    {
                        if (myMainWindow.myInputBoxes[i, j].Text == "?")
                        {
                            myBytes[byteIdx] = Convert.ToByte(0);
                        }
                        else
                        {
                            myBytes[byteIdx] = Convert.ToByte(Int32.Parse(myMainWindow.myInputBoxes[i, j].Text));
                        }
                        ++byteIdx;
                    }
                    else
                    {
                        myBytes[byteIdx] = Convert.ToByte(0);
                        ++byteIdx;
                    }
                }
            }

            // Call the Sudoko API to initialize the board
            SudokuLibApi.Reset();
            SudokuLibApi.Initialize(myBytes);

            EventOccured(SolverFSM.transitionEvent.WAIT_FOR_RESPONSE, null);
        }
Exemplo n.º 2
0
        private void ProcessRequests()
        {
            if (myExecutingRequest == null)
            {
                if (myRequests.Count > 0)
                {
                    Request req = myRequests.First();
                    myExecutingRequest = req;

                    if (myRequests.TryDequeue(out req))
                    {
                        if (req.myRequestType == RequestType.Initialize)
                        {
                            try
                            {
                                SudokuLibApi.Initialize(req.GetByteData());
                            }
                            catch (Exception e)
                            {
                                ErrorMessage em = new ErrorMessage(e.Message);
                                em.ShowDialog();
                            }
                        }
                        if (req.myRequestType == RequestType.Solve)
                        {
                            SudokuLibApi.Solve();
                        }
                        else if (req.myRequestType == RequestType.Reset)
                        {
                            SudokuLibApi.Reset();
                        }
                        else if (req.myRequestType == RequestType.IsSolved)
                        {
                            bool theResult = SudokuLibApi.GetIsSolved();
                            req.SetData(theResult);
                            req.SetQueueSize(myRequests.Count);


                            SolverFSM m = (SolverFSM)req.myRequester;
                            if (m != null)
                            {
                                m.RequestResponse(req);
                            }
                        }
                        else if (req.myRequestType == RequestType.GetSolved)
                        {
                            IntPtr theCurrentBoard = SudokuLibApi.GetCurrentBoard();
                            byte[] bytes           = new byte[SudokuLibApi.RESPONSE_SIZE];
                            Marshal.Copy(theCurrentBoard, bytes, 0, SudokuLibApi.RESPONSE_SIZE);
                            req.SetData(bytes);
                            req.SetQueueSize(myRequests.Count);

                            SolverFSM m = (SolverFSM)req.myRequester;
                            if (m != null)
                            {
                                m.RequestResponse(req);
                            }
                        }
                        else if (req.myRequestType == RequestType.Shutdown)
                        {
                            bool theResult = SudokuLibApi.Shutdown();
                            req.SetData(theResult);
                            req.SetQueueSize(myRequests.Count);


                            SolverFSM m = (SolverFSM)req.myRequester;
                            if (m != null)
                            {
                                m.RequestResponse(req);
                            }
                        }
                        else if (req.myRequestType == RequestType.GetBoardData)
                        {
                            IntPtr theBoardData = SudokuLibApi.GetBoardData();
                            int[]  bytes        = new int[SudokuLibApi.RESPONSE_SIZE];
                            Marshal.Copy(theBoardData, bytes, 0, SudokuLibApi.RESPONSE_SIZE);
                            req.SetData(bytes);
                            req.SetQueueSize(myRequests.Count);

                            SolverFSM m = (SolverFSM)req.myRequester;
                            if (m != null)
                            {
                                m.RequestResponse(req);
                            }
                        }
                    }

                    myExecutingRequest = null;
                }
            }
        }