📄 grid.java
字号:
import java.lang.Integer;
import java.io.PrintStream;
import java.lang.StringBuffer;
/**
* The Grid class represents the grid of cards arranged by row and column.
*
* The default size of the grid is 4 x 6, but this is configurable.
* Maximum supported size for grid is 26 x 9.
*
* @author Terri Paik
* @author Zhu Jing
* StudentID:0631118
* Project: 3
* Date:2009/04/02
*/
public class Grid {
private Card[][] cards;
/* Default values */
private int numRows = 4;
private int numCols = 6;
private String possibleValues = "OPQRSTUVWXYZ";
/**
* Creates a new default grid.
*/
public Grid() {
createRandomGrid();
}
/**
* Creates a new grid with specific size and possible values.
*
* @param config The configuration properties.
* Three properties from <code>config</code> will be processed IF
* they exist: <br>
* <code>grid.rows</code> is the number of rows.<br>
* <code>grid.cols</code> is the number of columns. <br>
* <code>grid.possibleValues</code> is a String containing possible
* character values for the grid. The length of possibleValues will be
* exactly half of (rows * cols).
*/
public Grid(Config config) {
/* TO DO!
* Implement this constructor.
* Read the properties from config to set numRows, numCols, and
* possibleValues fields. If the property does not exist, just keep
* the default value (do nothing).
*
* Then call createRandomGrid().
*
* See Game(String) constructor for how to use the Config object.
*/
String s1=config.getProperty("grid.rows");
String s2=config.getProperty("grid.cols");
String s3=config.getProperty("grid.possibleValues");
if(s1!=null)
numRows=Integer.parseInt(s1);
if(s2!=null)
numCols=Integer.parseInt(s2);
if(s2!=null)
possibleValues=s3;
createRandomGrid();
}
/**
* Populates the cards[][] array in a random fashion.
*
* There must be two cards created from each character in possibleValues.
* They must be arranged in random order on the grid.
*/
private void createRandomGrid() {
/* TO DO!
* Implement this method
*/
StringBuffer strBuffer=new StringBuffer();
for(int i=0;i<2;++i)
strBuffer.append(possibleValues);
cards=new Card[numRows][numCols];
if(strBuffer.length()==numRows*numCols)
{
for(int i=0;i<numRows;++i)
for(int j=0;j<numCols;++j)
{
int k=(int)Math.floor(Math.random()*strBuffer.length());
cards[i][j]=new Card(strBuffer.charAt(k));
strBuffer.deleteCharAt(k);
}
}
else
throw new AssertionError();
}
/**
* Gets a card at the specified grid location.
* For example, if the location is "b2", then the Card at cards[1][1] is
* returned.
* If the location string is not valid, return null.
*
* @param location a String such as "b2" where the
* first character is the row (<code>a, b, c</code>, etc) and the
* second character is the column (<code>1, 2, 3</code>, etc).
* @return the Card at the location specified, or null if the location
* is invalid.
*/
public Card getCard(String location) {
/* TO DO!
* Implement this method.
*/
char c1=(char)(numRows+97-1);
if(location.matches("[a-" + c1 +"][1-" + numCols+"]"))
{
int i =location.charAt(0);
char c2=location.charAt(1);
int j=i-97;
int k =Integer.parseInt(c2 + "")-1;
return cards[j][k];
}
return null;
}
/**
* Tests whether this Grid has been completely solved.
*
* @return true if all cards are facing up, false otherwise.
*/
public boolean isSolved() {
/* TO DO!
* Implement this method.
*/
int num=0;
for(int i=0;i<numRows;i++)
for(int j=0;j<numCols;j++)
if (cards[i][j].isFacingUp())
++num;
return (num==numRows*numCols);
}
/**
* Prints the grid to stdout, including the row and column headers.
*/
public void print() {
System.out.println();
// Print header
System.out.print(" ");
for (int i = 0; i < numCols; i++) {
System.out.print(" " + (i + 1));
}
System.out.println();
// Print each row
int i = 0;
char col = 'a';
for (; i < numRows; i++, col++) {
System.out.print(col + " ");
for (int j = 0; j < numCols; j++) {
cards[i][j].print();
System.out.print(" ");
}
System.out.println();
}
System.out.println();
}
}
/*private int FacingUpNum(){
int num=0;
for(int i=0;i<numRows;i++)
for(int j=0;j<numCols;j++)
if (cards[i][j].isFacingUp())
++num;
return num;
}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -