using System; using System.Web.UI; using System.Web.UI.WebControls; using System.Threading; using System.Globalization; using SbsSW.SwiPlCs; namespace ChessBoard { public partial class ChessBoard : System.Web.UI.Page { // It is nessesary to store thise information in the session state // to give a error message if two clients work on the same session // happens if a second client browser was created by + n // or the back botton was used const string cs_cb_size = "cb_size"; // amount of queens (ChessBoardSize) const string cs_sol_count = "sol_count"; // solution count protected int m_solution_count = 0; protected string m_cb_size = ""; protected void Page_Load(object sender, EventArgs e) { if (Session[cs_cb_size] != null) { m_cb_size = (string)Session[cs_cb_size]; } if (Session[cs_sol_count] != null) { m_solution_count = (int)Session[cs_sol_count]; } if (Page.IsPostBack == false) { // To show the actual solution after the language was changes ( Response.Redirect ) ShowSolution(); } } #region localize methods // tool to translate resource files see // ResEx // /// /// Gets the lamnguage abraviation ( de, en or fr ) /// private string Language { get { // string lang = string.Empty;//default to the invariant culture string lang = "de"; //default to the German culture if (null != Session["PreferredCulture"] && !string.IsNullOrEmpty((string)Session["PreferredCulture"])) lang = (string)Session["PreferredCulture"]; // Wenn in cookie speichern //string lang = string.Empty;//default to the invariant culture //HttpCookie cookie = Request.Cookies["PreferredCulture"]; //if (cookie != null && cookie.Value != null) // lang = cookie.Value; return lang; } } protected override void InitializeCulture() { // override virtual method InitializeCulture() to check if profile contains a user language setting string lang = Language; Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(lang); Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang); } /// /// /// /// DE oder EN oder FR private void StoreCulture(string culture) { if (null == Session["PreferredCulture"] || (string)Session["PreferredCulture"] != culture) { //Save selected user language in profile Session["PreferredCulture"] = culture; //Force re-initialization of the page to fire InitializeCulture() // redirect to take the new culture effects Response.Redirect(Request.Url.LocalPath); } } #endregion protected override void OnPreRender(EventArgs e) { Session[cs_cb_size] = tb_amount.Text; Session[cs_sol_count] = m_solution_count; tb_solution_count.Text = m_solution_count.ToString(); } #region lib public static void buildImagePathWitTheme(string theme, Image img) { if (!img.ImageUrl.StartsWith("~/")) img.ImageUrl = "~/" + buildImagePathWitTheme(theme, img.ImageUrl); } public static string buildImagePathWitTheme(string theme, string baseUrl) { string sRet = baseUrl; string prefix = "App_Themes/"; if (!baseUrl.Contains(prefix)) { sRet = prefix + theme; if (!baseUrl.StartsWith("/")) sRet += "/"; sRet += baseUrl; } return "/" + sRet; } #endregion #region private helpers private void drawBoard() { int rowCnt; // Current row count int rowCtr; // Total number of cells per row (columns) int cellCtr;// Current cell counter int cellCnt; rowCnt = int.Parse(tb_amount.Text); cellCnt = int.Parse(tb_amount.Text); for (rowCtr = 1; rowCtr <= rowCnt; rowCtr++) { // Create new row and add it to the table. TableRow tRow = new TableRow(); tbl_board.Rows.Add(tRow); for (cellCtr = 1; cellCtr <= cellCnt; cellCtr++) { // Create a new cell and add it to the row. TableCell tCell = new TableCell(); tCell.Text = "Row " + rowCtr + ", Cell " + cellCtr; System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image(); double test_x = Math.IEEERemainder((rowCtr - 1) * rowCnt + cellCtr, 2); int gerade = (int)Math.IEEERemainder(rowCnt + 1, 2); // 1 | 0 if ((int)Math.IEEERemainder((rowCtr + 1) * (rowCnt + gerade) + cellCtr, 2) != 0) { img.ImageUrl = "Images/White_Square.gif"; } else { img.ImageUrl = "Images/Black_Square.gif"; } buildImagePathWitTheme(Page.Theme, img); tCell.Controls.Add(img); tRow.Cells.Add(tCell); } } } private void SetQueen(int row, int col) { TableRow TR = tbl_board.Rows[row - 1]; TableCell TC = TR.Cells[col - 1]; System.Web.UI.WebControls.Image img = (System.Web.UI.WebControls.Image)TC.Controls[0]; if (img.ImageUrl.EndsWith("Images/White_Square.gif")) img.ImageUrl = "Images/White_Queen_White.gif"; else img.ImageUrl = "Images/White_Queen_Black.gif"; buildImagePathWitTheme(Page.Theme, img); } private void GetFirstQueenSolution(int AmountOfQueens) { if (Session["SolutionQuery"] != null) { ((PlQuery)Session["SolutionQuery"]).Free(true); } PlTermV av = new PlTermV(2); av[0] = new PlTerm(AmountOfQueens); PlQuery q = new PlQuery("queens", av); // Global.log(" FirstQueenSolution with qid = " + q.dump_qid); bool x = q.NextSolution(); Session["solution"] = q.Args[1].ToString(); Session["SolutionQuery"] = q; } private void GetNextQueenSolution() { if (Session["SolutionQuery"] == null) { err_msg.Text = Resources.Text.err_text_1; return; } PlQuery q = (PlQuery)Session["SolutionQuery"]; // Global.log(" NextQueenSolution with qid = " + q.dump_qid); if (q.NextSolution()) { Session["solution"] = q.Args[1].ToString(); } else { err_msg.Text = Resources.Text.err_text_2; } } private void ShowSolution() { if (Session["solution"] != null) { drawBoard(); string strsol = (string)Session["solution"]; strsol = strsol.TrimStart('['); strsol = strsol.TrimEnd(']'); string[] positions = strsol.Split(','); foreach (string SinglePos in positions) { string[] pos = SinglePos.ToString().Split('-'); SetQueen(Int32.Parse(pos[0]), Int32.Parse(pos[1])); } } } #endregion ///////////// // events protected void OnClickNextSolution(object sender, System.EventArgs e) { if (m_solution_count != Int32.Parse(tb_solution_count.Text)) { err_msg.Text = Resources.Text.err_text_same_session + "
"; if (m_cb_size != tb_amount.Text) { err_msg.Text += Resources.Text.err_text_crtl_n; } else { err_msg.Text += Resources.Text.err_text_back_btn; } } else { GetNextQueenSolution(); ShowSolution(); m_solution_count++; } } protected void OnClickStart(object sender, System.EventArgs e) { if (Page.IsValid) { GetFirstQueenSolution(Int32.Parse(tb_amount.Text)); ShowSolution(); m_solution_count = 1; } } protected void OnSelectedLanguageChanged(object sender, EventArgs e) { if (MyUICulture.SelectedIndex > 0) { // If another culture was selected, use that instead. StoreCulture(MyUICulture.SelectedItem.Value); } } } }