public class SimpleDotComTestDrive {
private static String guess;
int numofGuesses = 0;
GameHelper helper = new GameHelper();
ArrayList<SimpleDotCom> dotComsList = new ArrayList<SimpleDotCom>();
private void setupGame()
{
SimpleDotCom dot1 =new SimpleDotCom();
SimpleDotCom dot2 =new SimpleDotCom();
SimpleDotCom dot3 =new SimpleDotCom();
dot1.setName( "Go2.com");
dot2.setName("Pets.com");
dot3.setName("AskMe.com");
System.out.println("your goal id to sink three dot coms");
System.out.println("Pets.com,AskMe.com,Go2.com");
System.out.println("Try to sink them all in the fewest number of guessses");
for(SimpleDotCom dotComToSet:dotComsList)
{
ArrayList<String> newLocation = helper.placeDotcom(3);
dotComToSet.setLocationCells(newLocation);
}
}
public void startPlaying()
{
while(!dotComsList.isEmpty())
{
String userGuess = helper.getUserInput("enter a guess:");
checkUserGuess(userGuess);
}
finishGame();
}
private void checkUserGuess(String userGuess)
{
numofGuesses++;
String result ="miss";
for(SimpleDotCom dotComToTest : dotComsList)
{
result = dotComToTest.checkYourself(userGuess);
if(result.equals("hit"))
{
break;
}
if(result.equals("kill"))
{
dotComsList.remove(dotComToTest);
break;
}
}
System.out.println(result);
}
void finishGame()
{
System.out.println("game over !!");
if(numofGuesses<10)
{
System.out.println("a congratulations message");
}
else
{
System.out.println("be perfect next time!!");
}
}
public static void main(String[] args)
{
SimpleDotComTestDrive game =new SimpleDotComTestDrive();
game.setupGame();
game.startPlaying();
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/package SimpleDotComTestDrive;import java.util.ArrayList;/**
*
* @author 诸葛一帆
*/
public class SimpleDotCom {
private ArrayList<String> locationCells;
private String name;
public void setLocationCells(ArrayList<String> locs)
{
locationCells = locs;
}
public void setName(String n) {
name = n;
}
public String checkYourself(String userInput) {
String result="miss";
int index = locationCells.indexOf(userInput);
if(index>=0)
{
locationCells.remove(index); if(locationCells.isEmpty())
{
result = "kill";
System.out.println("Ouch! you sunk"+name+" :( ");
}
else
result ="hit";
}
return result;
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/package SimpleDotComTestDrive;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;/**
*
* @author 诸葛一帆
*/
public class GameHelper {
private static final String alphabet ="abcdefg";
private int gridLength =7;
private int gridSize = 49;
private int [] grid = new int[gridSize];
private int comCount = 0; public String getUserInput(String prompt) {
String inputLine = null;
System.out.print(prompt+" ");
try
{
BufferedReader is =new BufferedReader(
new InputStreamReader(System.in));
inputLine = is.readLine();
if(inputLine.length()==0)
return null;
}
catch(IOException e)
{
System.out.println("IOException: "+e);
}
return inputLine.toLowerCase();
} public ArrayList<String> placeDotcom(int comSize) {
ArrayList<String> alphaCells = new ArrayList<String>();
String [] alphacoords = new String[comSize];
String temp = null;
int [] coords = new int[comSize];
int attempts=0;
boolean success = false;
int location =0;
comCount++;
int incr = 1;
if((comCount % 2)==1)
{
incr = gridLength;
}
while( !success & attempts++ < 200)
{
location =(int)(Math.random()*gridSize);
int x = 0;
success = true;
while(success && x < comSize)
{
if(grid[location]==0)
{
coords[x++]=location;
location += incr;
if(location >= gridSize)
{
success = false;
}
if(x>0 && (location % gridLength==0))
{
success = false;
}
else
{
success = false;
}
}
} int y = 0;
int row =0;
int column = 0;
while(y<comSize)
{
grid[coords[y]] = 1;
row = (int)(coords[y] / gridLength);
column = coords[y] % gridLength;
temp = String.valueOf(alphabet.charAt(column));
alphaCells.add(temp.concat(Integer.toString(row)));
y++;
} }
return alphaCells;
}
}
private static String guess;
int numofGuesses = 0;
GameHelper helper = new GameHelper();
ArrayList<SimpleDotCom> dotComsList = new ArrayList<SimpleDotCom>();
private void setupGame()
{
SimpleDotCom dot1 =new SimpleDotCom();
SimpleDotCom dot2 =new SimpleDotCom();
SimpleDotCom dot3 =new SimpleDotCom();
dot1.setName( "Go2.com");
dot2.setName("Pets.com");
dot3.setName("AskMe.com");
System.out.println("your goal id to sink three dot coms");
System.out.println("Pets.com,AskMe.com,Go2.com");
System.out.println("Try to sink them all in the fewest number of guessses");
for(SimpleDotCom dotComToSet:dotComsList)
{
ArrayList<String> newLocation = helper.placeDotcom(3);
dotComToSet.setLocationCells(newLocation);
}
}
public void startPlaying()
{
while(!dotComsList.isEmpty())
{
String userGuess = helper.getUserInput("enter a guess:");
checkUserGuess(userGuess);
}
finishGame();
}
private void checkUserGuess(String userGuess)
{
numofGuesses++;
String result ="miss";
for(SimpleDotCom dotComToTest : dotComsList)
{
result = dotComToTest.checkYourself(userGuess);
if(result.equals("hit"))
{
break;
}
if(result.equals("kill"))
{
dotComsList.remove(dotComToTest);
break;
}
}
System.out.println(result);
}
void finishGame()
{
System.out.println("game over !!");
if(numofGuesses<10)
{
System.out.println("a congratulations message");
}
else
{
System.out.println("be perfect next time!!");
}
}
public static void main(String[] args)
{
SimpleDotComTestDrive game =new SimpleDotComTestDrive();
game.setupGame();
game.startPlaying();
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/package SimpleDotComTestDrive;import java.util.ArrayList;/**
*
* @author 诸葛一帆
*/
public class SimpleDotCom {
private ArrayList<String> locationCells;
private String name;
public void setLocationCells(ArrayList<String> locs)
{
locationCells = locs;
}
public void setName(String n) {
name = n;
}
public String checkYourself(String userInput) {
String result="miss";
int index = locationCells.indexOf(userInput);
if(index>=0)
{
locationCells.remove(index); if(locationCells.isEmpty())
{
result = "kill";
System.out.println("Ouch! you sunk"+name+" :( ");
}
else
result ="hit";
}
return result;
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/package SimpleDotComTestDrive;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;/**
*
* @author 诸葛一帆
*/
public class GameHelper {
private static final String alphabet ="abcdefg";
private int gridLength =7;
private int gridSize = 49;
private int [] grid = new int[gridSize];
private int comCount = 0; public String getUserInput(String prompt) {
String inputLine = null;
System.out.print(prompt+" ");
try
{
BufferedReader is =new BufferedReader(
new InputStreamReader(System.in));
inputLine = is.readLine();
if(inputLine.length()==0)
return null;
}
catch(IOException e)
{
System.out.println("IOException: "+e);
}
return inputLine.toLowerCase();
} public ArrayList<String> placeDotcom(int comSize) {
ArrayList<String> alphaCells = new ArrayList<String>();
String [] alphacoords = new String[comSize];
String temp = null;
int [] coords = new int[comSize];
int attempts=0;
boolean success = false;
int location =0;
comCount++;
int incr = 1;
if((comCount % 2)==1)
{
incr = gridLength;
}
while( !success & attempts++ < 200)
{
location =(int)(Math.random()*gridSize);
int x = 0;
success = true;
while(success && x < comSize)
{
if(grid[location]==0)
{
coords[x++]=location;
location += incr;
if(location >= gridSize)
{
success = false;
}
if(x>0 && (location % gridLength==0))
{
success = false;
}
else
{
success = false;
}
}
} int y = 0;
int row =0;
int column = 0;
while(y<comSize)
{
grid[coords[y]] = 1;
row = (int)(coords[y] / gridLength);
column = coords[y] % gridLength;
temp = String.valueOf(alphabet.charAt(column));
alphaCells.add(temp.concat(Integer.toString(row)));
y++;
} }
return alphaCells;
}
}
dot2.setName("Pets.com");
dot3.setName("AskMe.com");后加上
dotComsList.add(dot1);
dotComsList.add(dot2);
dotComsList.add(dot3);
这回保证通过,我也纠结了半天,