📄 game.java
字号:
import java.util.Scanner;
import java.lang.String;
/**
* The Game class represents a single game of Memory.
*
* @author Terri Paik
* @author Zhu Jing
* StudentID:0631118
* Project: 3
* Date:2009/04/01
*/
public class Game {
private Grid grid;
private int numGuesses = 0;
private int delaySeconds = 3;
private static Scanner sc = new Scanner(System.in);
/**
* Creates a new game instance.
*/
public Game() {
grid = new Grid();
}
/**
* Creates a new game instance with configuration parameters
* read from a file.
*
* @param configFile File containing the configuration parameters
*/
public Game(String configFile) {
Config cfg = new Config(configFile);
String delay = cfg.getProperty("game.delay");
if (delay != null ) {
try {
delaySeconds = Integer.parseInt(delay);
} catch (NumberFormatException e) {
System.err.println("Invalid value for game.delay. Using default.");
}
}
grid = new Grid(cfg);
}
private void printWelcome() {
System.out.println("Welcome to Memory!");
}
private void printCongrats() {
System.out.println("CONGRATULATIONS! YOU WON!!!");
System.out.println("You finished the game in " + numGuesses
+ " guesses.");
}
/**
* Asks the user if they want to keep playing and returns true if they do.
*
* @return true if the user answered "y" or "Y" to the question,
* false if they answered "n" or "N".
*/
public boolean keepPlaying() {
/* TO DO!
* After the user answered the question yes or no,pick the answer "Y/y"or"N/n",
* if it is "Y/y",keep playing,otherwise,stop playing;
*
* You could use this statement for the prompt:
*
* System.out.print("Play another game? (y/n) ");
*
* Don't create a new Scanner. Use the Scanner sc object that
* has already been initialized as a field in this class.
*/
String s;
do{
System.out.print("Play another game?(y/n) ");
s=sc.nextLine();
}while(!s.equalsIgnoreCase("y") && !s.equalsIgnoreCase("n"));
//char k=s.charAt(0);
/*if(!s.equalsIngnoreCase('y') && !s.equalsIngnoreCase('n'))
{
System.out.print("Play another game?(y/n) ");
s=sc.nextLine();
}*/
return s.equalsIgnoreCase("y");
}
/**
* Prompts the user to enter a grid location for the card they wish
* to select, then returns the corresponding Card object.
* If the grid location is invalid or the card is already facing up,
* reports an error and prompts again (loops until valid card is chosen).
*
* @param cardNum Number of this card (for display purposes only)
* @return the Card selected by the user.
*/
private Card chooseNextCard(int cardNum) {
/* TO DO!
* Give an interger i to be a label to decide whether the user has to choose another card again;
* When i is 0,get the cardNum and decide whether the card is valid based on its current state;
* You could use this statement for the prompt:
*
* System.out.print("Card #" + cardNum + ": ");
*
* Read in the user's input and get the card using grid.getCard(String)
* If the card is null, print an error.
* If the card is facing up, print an error.
* Repeat until the user chooses a valid card.
*
* Don't create a new Scanner. Use the Scanner sc object that
* has already been initialized as a field in this class.
*/
Card card;
int i=0;
do{
System.out.print("Card #" + cardNum + ": ");
String str=sc.nextLine();
card=grid.getCard(str);
if(card==null)
{
System.out.print("Card"+cardNum+"is invalide!Please choose another one!");
}
else if(card.isFacingUp())
{
System.out.print("Card"+cardNum+"is facing up!Please choose another one!");
}
else
{
i=1;
}
}while(i==0);
return card;
}
/**
* Plays one complete game of Memory.
*/
public void play() {
printWelcome();
grid.print();
/* TO DO!
*
* Fill in the main game loop here. Repeat until grid.isSolved()...
* 1. Print the grid.
* 2. Ask the player to choose card #1 using chooseNextCard().
* 3. Set card #1 face up and print the grid.
* 4. Ask the player to choose card #2.
* 5. Set card #2 face up and print the grid.
* 6. If the cards do not match, pause for <delaySeconds> seconds,
* then set the cards face down and print the grid again.
*/
do{
Card card1=chooseNextCard(1);
card1.setFaceUp();
grid.print();
Card card2=chooseNextCard(2);
card2.setFaceUp();
grid.print();
numGuesses++;
if(!(card1.matches(card2))){
try{
for(int i=0;i<delaySeconds;i++)
{
System.out.print(".");
Thread.sleep(3000);
}
}
catch(Exception localException)
{
}
card1.setFaceDown();
card2.setFaceDown();
for(int i=0;i<30;++i)
{
System.out.println();
}
grid.print();
}
}while(!(grid.isSolved()));
printCongrats();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -