본문 바로가기

ASP.NET_ 낱말퍼즐/프로그램

ASP.NET으로 '낱말잇기 퍼즐'만들기_ 프로그램하기_PuzzleView

Page_Load에서 PageSetting() 함수를 호출한다.

 

 (Page_Load에서 함수를 호출하지 않고 PageSetting내의 내용을 바로 실행해도 별다른 이상은 없지만, 

 

알아보기 좋기위한 일종의 규칙이라고 볼 수 있다.)

 

PageSetting에서는 DB를 연결하고, DB로부터 데이터를 받아 테이블을 출력하기 위해 _Puzzle 클래스의 TableSetting 함수를 호출한다.

_Puzzle.TableSetting(BoxNum, _StrQuestion, false)
BoxNum- 몇행 몇열의 Box를 만들지를 말한다(5 × 5 = 25 에서는 BoxNum = 5))
StrQuestion- 퍼즐의 정답이 나열 된 답을 말한다(망,둥,이,,오,치,,발,전,기,,열,,자,,,기,,제,,전,구,,품,절)
false-  Box안에 답을 같이 출력한다면 "true" 출력하지 않는다면 "false"  


 protected void Page_Load(object sender, EventArgs e)
    {
           PageSetting();
    }

 protected void PageSetting()
    {
        DataRow row = _Puzzle.ViewPuzzle("SP_WordPuzzle_View", _idx);
        if (row != null)
        {
            string idx = row["idx"].ToString();
            string widthQuestion = row["widthQuestion"].ToString();
            string lenghtQuestion = row["lenghtQuestion"].ToString();
            string regDate = row["regDate"].ToString();
            string answer = row["answer"].ToString();
            int BoxNum = _Puzzle.multiplyNum((answer.Length - answer.Replace(",", "").Length) + 1);
            _StrQuestion = answer;
            tablePrint.InnerHtml = _Puzzle.TableSetting(BoxNum, _StrQuestion, false);
           
            widthContent.Text = widthQuestion;
            lenghtContent.Text= lenghtQuestion;
        }

    }

InnerHtml - 지정된 html 서버 컨트롤의 여는 태그 및 닫는 태그 사이에 있는 내용을 가져옵니다.


PuzzleClass.cs_ViewPuzzle

     public DataRow ViewPuzzle(string procName, int idx)
    {
        SqlConnection conn = CDBConn.GetConn();
        SqlCommand cmd = new SqlCommand();
        DataSet ds = new DataSet();
        DataRow row;

        cmd.CommandText = procName;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Connection = conn;
        cmd.Parameters.AddWithValue("@idx", idx);

        SqlDataAdapter adap = new SqlDataAdapter(cmd);
        adap.Fill(ds);

        if ((ds != null) && (ds.Tables[0].Rows.Count > 0))
        {
            row = ds.Tables[0].Rows[0];
            return row;
        }
        else
        {
            return null;
        }
    }



이럴때는 클래스를 이용하자~
문제를 출제하기위해 출력되는 테이블이나, 문제를 풀기위해 출력되는 테이블은 정답이 같이 출력되는지만 다를뿐 큰차이는 없다.
이럴때는 더더욱 함수로 따로 정리하여 정리하는 것이 좋다. 비슷한 코딩이 여러군데에서 사용된다면, 공통으로 사용하도록 클래스에 저장하고 클래스내의 함수를 출력하기만 하면 더욱 효율적이다.

PuzzleClass.cs_TableSetting
TableSetting의 경우 문제출제페이지(PuzzleWrite.aspx.cs)와 문제출력페이지(PuzzleView.aspx.cs) 두 군데에서 호출된다.
다만, PuzzleWrite는 _Puzzle.TableSetting(_BoxNum, "", false)_Puzzle.TableSetting(_BoxNum, _StrQuestion, true)형태로
PuzzleView는 _Puzzle.TableSetting(BoxNum, _StrQuestion, false) 형태이다.
때문에 (StrQuestion == "") 조건으로 WriteTableSetting 또는 ViewTableSetting 함수를 return한다. 
public string TableSetting(int BoxNum, string StrQuestion, bool flag)
    {
        string returnValue = "";
        if (StrQuestion == "")
        {
            returnValue = WriteTableSetting(BoxNum);
        }
        else
        {
            returnValue = ViewTableSetting(BoxNum,StrQuestion, flag);
        }
        return returnValue;
    }



PuzzleClass.cs_ViewTableSetting

     public string ViewTableSetting(int BoxNum, string StrQuestion, bool flag)
    {
        string returnValue = "";
        _question = ArraySetting(StrQuestion);
        _questionNum = ArrayNum(_question);

        int num = 0;
        string numText = "";
        string color = "";
        string inputBox = "";
        string answerText = "";


        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.AppendLine("<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\">");
        for (int i = 0; i < BoxNum; i++)
        {
            sb.AppendLine("<tr>");
            for (int j = 0; j < BoxNum; j++)
            {
                numText = "";
                color = "";
                inputBox = "";
                answerText = "";
                
                if (_questionNum[num] > 0)
                {
                    numText = _questionNum[num].ToString();
                }
                if (_question[num] == "")
                {
                    color = "\"#cccccc\"";
                    inputBox = "<input type=\"hidden\" id=\"test" + num + "\" class=\"inputtest\" />";
                }
                else
                {
                    color = "\"#FFFFFF\"";
                    if (flag == true)
                    {
                        answerText = _question[num];
                    }
                    inputBox = "<input type=\"text\" id=\"test" + num + "\" MaxLength=\"1\" name=\"test1\" class=\"inputtest\" style=\"width:50px; padding:10px;text-align:center; font-size:30px; font-weight:bold; border:0px solid;\" value=\"" + answerText + "\" />";
                }

                sb.AppendLine("<td width=\"50\" height=\"50\" bgcolor = " + color + " style=\"vertical-align:bottom;\">");
                sb.AppendLine(numText + inputBox + "</td>");
                num++;
            }
            sb.AppendLine("</tr>");
        }
        sb.AppendLine("</table>");
       
        returnValue = sb.ToString();
        return returnValue;
    }


ViewPuzzle.aspx.cs_Button1_Click
사용자가 풀어 본 답이 DB에 저장된 정답과 일치하는지 비교한다. 사용자가 입력해 puzzleAnswer에 입력된 값은 "망둥이오치..."의
형태지만, DB에서 가져온 정답의 형태는 "망,둥,이,,," 이기 때문에 .Replace로 ','를 ''로 대체한다.
Replace한 답과 puzzleAnswer.Value이 일치한다면 정답! 그렇지 않으면 오답!

     protected void Button1_Click(object sender, EventArgs e)
    {
        _StrQuestion = _StrQuestion.Replace(",", "");

       
        if (puzzleAnswer.Value == _StrQuestion)
        {
            result.Text = "정답입니다";
        }
        else
        {
            result.Text = "오답입니다";
        }
    }