我正在使用ASP.NET和C#创建一个Sudoku游戏。我需要使用类和继承来完全在代码隐藏页面中构建结构(即aspx页面上没有asp:TextBox控件)。
我正在努力让我的输入在我开始新游戏时明确。我生成了一个新的拼图解决方案,但我之前的方框并不清楚,我已经尝试了所有我能想到的东西。
以下是与问题相关的几个代码块。
构建拼图并将其存储在Puzzle对象中的代码。
private Puzzle newPuzzle(int[,] solution, int numbersVisible, int maxNumbersPerBox, int maxOccurancesPerNumber)
{
Puzzle newPuzzle = new Puzzle();
SudokuTextBox newTextbox;
Number newNumber;
Random randomRC = new Random();
//variable to hold the correct answer at the given location
int answer;
//variables to hold the location within the answers array
int rowLoc;
int colLoc;
//counter to count the number of times we while loop
int counter = 0;
//variables to hold the randomly-chosen rows & col values
int row;
int col;
//array to hold the positions of the numbers we are going to show
string[] show;
show = new string[numbersVisible];
while(counter < numbersVisible)
{
//generate random numbers that gives us the location of the numbers in the solution array that we are going to show
row = randomRC.Next(0, 9);
col = randomRC.Next(0, 9);
//if the random numbers are not already in the array
if (!show.Contains(row.ToString() + ":" + col.ToString()))
{
//add them to the array
show[counter] = row.ToString() + ":" + col.ToString();
//increase the counter
counter++;
} //end if...contains
} //end while
// BUILDING THE PUZZLE
//start looping through the puzzle rows
for (int pr = 0; pr < 3; pr++)
{
//another loop for puzzle columns
for (int pc = 0; pc < 3; pc++)
{
box = new Box(); //create a new Box object
//another loop for box rows
for (int br = 0; br < 3; br++)
{
//another loop for box columns
for (int bc = 0; bc < 3; bc++)
{
newTextbox = new SudokuTextBox();
newNumber = new Number();
//grab the answer to this particular SudokuTextBox from the solutions array
rowLoc = (pr + br + (2 * pr));
colLoc = (pc + bc + (2 * pc));
answer = solution[rowLoc, colLoc];
newNumber.setNumber(answer); //set the Number to the found answer
newTextbox.setTextBoxValue(newNumber); //fill in the textbox with Number
//if this SudokuTextBox is chosen to be given at the start of the puzzle
if (show.Contains((rowLoc + ":" + colLoc).ToString()))
{
//make this SudokuTextBox visible
newTextbox.setVisibility(true);
}
else {
newTextbox.setVisibility(false);
} //end if
box.setItem(newTextbox, br, bc); //add the SudokuTextBox to the correct position inside Box
} //end box column loop
} //end box row loop
newPuzzle.setItem(box, pr, pc); //add the Box to the correct position inside Puzzle
} //end puzzle column loop
} //end puzzle row loop
return newPuzzle;
} //end easy()
在会话中存储新的谜题:
//when the Easy button is pressed
protected void btnEasy_Click(object sender, EventArgs e)
{
//generate a new random number
Random newRandomSoln = new Random();
//keep picking new solutions until we get one that's different than the last one
do
{
solution = chooseSolution(newRandomSoln.Next(1, 11));
}
while (solution == Session["solution"]);
//store the new solution
Session["solution"] = solution;
//generate a new puzzle
Session["puzzle"] = newPuzzle( (int[,])Session["solution"], 32, 4, 4 );
}
构建表结构的代码,用Puzzle中存储的答案填充它,并将其添加到aspx页面:
////////////////////////////////
// CREATING THE PUZZLE
///////////////////////////////
Table structure = new Table(); //table to be the outer structure of the puzzle
TableRow row; //row variable to make new rows
TableCell cell; //cell variable to make new cells
Table boxTable; //table that will hold individual Boxes
TableRow boxRow; //row that will hold 3 SudokuTextBoxes
TableCell boxCell; //cell that will hold a single SudokuTextBoxes
TextBox input; //textbox that will hold the textbox in SudokuTextBox
int answer; //int to hold the answer to a particular textbox
//start looping through the puzzle rows
for (int pr = 0; pr < 3; pr++)
{
row = new TableRow(); //create a new outer row
//another loop for puzzle columns
for (int pc = 0; pc < 3; pc++)
{
cell = new TableCell(); //create a new outer cell
boxTable = new Table(); //create a new inner table
box = new Box(); //create a new Box object
box = ((Puzzle)Session["puzzle"]).getItem(pr, pc); //find the box at the current location in the puzzle
//another loop for box rows
for (int br = 0; br < 3; br++)
{
boxRow = new TableRow(); //create a new inner row
//another loop for box columns
for(int bc = 0; bc < 3; bc++)
{
boxCell = new TableCell(); //create a new inner cell
textbox = new SudokuTextBox(); //create a new SudokuTextBox object
textbox = box.getItem(br, bc); //find the SudokuTextBox at the current location in the box
//grab the answer to this particular SudokuTextBox from the solutions array
answer = ((int[,])Session["solution"])[ (pr + br + (2 * pr)), (pc + bc + (2 * pc)) ];
input = textbox.getTextBox(); //grab the textbox inside SudokuTextBox and store it
input.MaxLength = 1; //only allow 1 character to be typed into the textbox
//give the textbox an ID so we can find it later
input.ID = ("tb" + (pr + br + (2 * pr)) + "_" + (pc + bc + (2 * pc))).ToString();
boxCell.Controls.Add(input); //add the textbox to the inner cell
boxRow.Controls.Add(boxCell); //add the inner cell to the inner row
} //end box column loop
boxTable.Controls.Add(boxRow); //add the inner row to the inner table
} //end box row loop
cell.Controls.Add(boxTable); //add the inner table to the outer cell
row.Controls.Add(cell); //add the outer cell to the outer row
} //end puzzle column loop
structure.Controls.Add(row); //add the outer row to the outer table
} //end puzzle row loop
pnlPuzzle.Controls.Add(structure);
////////////////////////////////
// end puzzle
///////////////////////////////
和SudokuTextBox类代码:
public class SudokuTextBox
{
private System.Web.UI.WebControls.TextBox textbox;
private bool visible;
private Number number;
public SudokuTextBox()
{
textbox = new System.Web.UI.WebControls.TextBox();
visible = false;
number = new Number();
} //end constructor
//function to make a new textbox
public System.Web.UI.WebControls.TextBox getTextBox()
{
return textbox;
}
//function to get the value of a textbox
public Number getTextBoxValue()
{
return number;
}
//????????????
public void setTextBoxValue(Number newNumber)
{
this.number.setNumber(newNumber.getNumber());
}
//function to get the visibility of a textbox
public bool getVisibility()
{
return visible;
}
//function to change the visibility of a textbox
public void setVisibility(bool newVisible)
{
if (newVisible)
{
//if the textbox is visible
//get the number
//and make it disabled
textbox.Text = number.getNumber().ToString();
textbox.ReadOnly = true;
textbox.BackColor = System.Drawing.Color.FromArgb(150, 148, 115);
} else
{
//if it is not visible
//hide the number
//and make it enabled
textbox.ReadOnly = false;
textbox.Text = "";
textbox.BackColor = System.Drawing.Color.White;
}
}
//function to change the color of the textbox if it is wrong
public void setWrongNumber()
{
textbox.BackColor = System.Drawing.Color.FromArgb(5, 156, 202, 252);
}
//function to change the color of the textbox if it is correct
public void setCorrectNumber()
{
//but don't change disable text boxes
if(textbox.ReadOnly != true)
{
textbox.BackColor = System.Drawing.Color.White;
}
}
//function to change the color of the textbox if it is blank
public void setBlankNumber()
{
//but don't change disable text boxes
if (textbox.ReadOnly != true)
{
textbox.BackColor = System.Drawing.Color.White;
}
}
//function to show the value of a textbox when clicking the "Hint" button
//also changes the color of the textbox so we know it was shown with a hint
public void setHint()
{
setVisibility(true);
}
} //end class
答案 0 :(得分:0)
感谢所有给你两分钱的人。我只是想让每个人都知道我已经解决了这个问题:
我最终分离出构建表结构的代码,将其从Page_Load中取出并将其放入自己的函数中。然后我从Page_Load调用了这个函数。当我点击&#34;新拼图时,我也调用了这个功能。按钮。我还添加了类似于上面评论过的逻辑,以便在构建新表之前清除上一个表结构:
foreach(Control c in pnlPuzzle.Controls){
pnlPuzzle.Controls.Remove(c);
}
我不确定为什么这会解决我的问题,但它有效!