본문 바로가기

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

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

클래스 선언:  PuzzleClass _Puzzle = new PuzzleClass();
PuzzleWrite.aspx.cs
//'칸생성'버튼 클릭
//TableNum- DropDownList의 id를 말한다.

protected void BtnCreateTable_Click(object sender, EventArgs e)
    {
        _BoxNum = Convert.ToInt32(TableNum.SelectedValue);
        tablePrint.InnerHtml = _Puzzle.TableSetting(_BoxNum, "", false); //DropDownList에서 선택된 값으로 Box의 칸생성
       
BtnCreateNum.Visible = true;//'칸생성'을 클릭했을 때 '번호생성' 버튼이 보여지도록 설정
     }

 //'번호생성' 버튼클릭 
protected void BtnCreateNum_Click(object sender, EventArgs e)
    {
        _StrQuestion = puzzleAnswer.Value;//PuzzleWrite.aspx에서 만들어뒀던 서버컨트롤

        _BoxNum = Convert.ToInt32(TableNum.SelectedValue);
        _question = _Puzzle.ArraySetting(_StrQuestion);//_StrQuestion 을 배열로 Setting
        _questionNum = _Puzzle.ArrayNum(_question);//배열 _question 을 넘겨 번호생성
        tablePrint.InnerHtml = _Puzzle.TableSetting(_BoxNum, _StrQuestion, true);//TableSetting 함수를 호출하여 Box 생성
        questionEnd.Visible = true;
        widthQuestion.Visible = true;
        lenghtQuestion.Visible = true;
    }

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

//'출제완료'버튼클릭
//.Value.Replace("\n", "<br />")은 문제를 입력받은 형태 그대로 유지 할 수 있도록 Enter 키를 "<br />"로 바꿔주는 작업이다. 
protected void questionEnd_Click1(object sender, EventArgs e)
    {
        _StrQuestion = puzzleAnswer.Value;

        string widthContent = widthQuestion.Value.Replace("\n", "<br />");
        string lenghtContent = lenghtQuestion.Value.Replace("\n", "<br />");
        _Puzzle.InsertPuzzle(widthContent, lenghtContent, _StrQuestion);//DB에 출제된 문제를 insert하기위해 InsertPuzzle호출
        Response.Redirect("Default.aspx");
    }


PuzzleWrite.aspx.cs_PuzzleClass_InsertPuzzle


     public void InsertPuzzle(string widthQuestion,string lenghtQuestion,string answer)
    {

        SqlConnection conn = CDBConn.GetConn();
        SqlCommand cmd = new SqlCommand();

        cmd.CommandText = "SP_WordPuzzle_Insert";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Connection = conn;

        cmd.Parameters.AddWithValue("@widthQuestion", widthQuestion);
        cmd.Parameters.AddWithValue("@lenghtQuestion", lenghtQuestion);
        cmd.Parameters.AddWithValue("@answer", answer);
       
        conn.Open();
        int.Parse(cmd.ExecuteNonQuery().ToString());
        conn.Close();
    }




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

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_WriteTableSetting

     public string WriteTableSetting(int BoxNum)
    {
        string returnValue = "";

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

        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 = "";

                if (BoxNum > 0)
                {
                    numText = BoxNum.ToString();
                }
                if (BoxNum.ToString() == "")
                {
                    color = "\"#cccccc\"";
                    inputBox = "<input type=\"hidden\" id=\"test" + num + "\" class=\"inputtest\" />";
                }
                else
                {
                    color = "\"#FFFFFF\"";
                    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;\" />";
                }

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