[java]代码库
package gps.start;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
import gps.car.Car;
/**
*
* @author
* 锋客
* 思路:
* 1.通过random控制车辆生成信息,包括车牌
* 2.通过list存储车辆:类型,颜色;
* 3.实现逻辑:
* 按照车位,随机抽出车辆放入,判断用户抢占车位是否是自己车辆
*
*/
public class StartGameComplex {
// 车辆信息管理
static ArrayList<Car> list_car = new ArrayList<Car>();
// 车位管理
static ArrayList<Car> list_park = new ArrayList<Car>();
// 车数
static int num_car = 0;
// 车位
static int num_park = 0;
// 系统提供车辆类型
static List<String> car_type = new ArrayList<String>();
// 系统提供车辆颜色
static List<String> car_colour = new ArrayList<String>();
static {
car_type.add("Porsche");
car_type.add("QQ");
car_type.add("Mazda");
car_type.add("Mercedes_Benz");
car_colour.add("blue");
car_colour.add("red");
car_colour.add("whitee");
car_colour.add("yellow");
}
// 主方法:设置主方法
public static void main(String[] args) {
StartGameComplex startGameComplex = new StartGameComplex();
// 设置游戏难度
startGameComplex.setStartScreen();
// 准备游戏(输入个人车辆信息)
startGameComplex.ready();
// 开始游戏
startGameComplex.startGame();
//结果生成
startGameComplex.resultGame();
}
private void resultGame() {
System.out.println("请输入要抢占的车位(1-"+list_park.size()+")");
Scanner scanner=new Scanner(System.in);
int num=scanner.nextInt();
while(true){
if(list_park.get(num-1).getId().equals(list_car.get(0).getId())){
System.out.println("抢占车位成功!!!");
break;
}else{
System.out.println("该车位已被抢占");
System.out.println("请重新选择车位:");
num=scanner.nextInt();
}
}
}
/**
* 游戏规则: 1.先生成车辆信息,然后存入list_car中 2.将车位部署;
*/
private void startGame() {
// 生成车辆信息
Random random = new Random();
for (int i = 1; i < num_car; i++) {
Car car_temp = new Car(Integer.toString(random.nextInt(100000)), car_type.get(random.nextInt(4)),
car_colour.get(random.nextInt(4)));
list_car.add(car_temp);
}
// selectAll(list_car,"car");
// 部署车位
for (int i = 0; i < num_park; i++) {
Car car_temp = list_car.get(random.nextInt(list_car.size()));
list_park.add(car_temp);
}
//显示车位信息
//selectAll(list_park,"park");
}
/**
* 生成自己车辆信息
*
* @return
*/
public Car ready() {
System.out.println("请输入车辆信息:");
Scanner cursor = new Scanner(System.in);
System.out.println("车牌:");
String id = cursor.next();
System.out.println("类型:");
String type = cursor.next();
System.out.println("颜色:");
String colour = cursor.next();
Car userCar = new Car(id, type, colour);
list_car.add(userCar);
return userCar;
}
/**
* 设置游戏难度
*/
public void setStartScreen() {
System.out.println("*******游戏开始********");
System.out.println("*******请设置游戏难度********");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入总共参与的车数:");
num_car = scanner.nextInt();
do {
System.out.println("请输入车位:");
num_park = scanner.nextInt();
if (num_car > num_park) {
System.out.println("车位数过小,请重新输入:");
}
} while (num_car > num_park);
}
/**
* 打印list集合信息
*
* @param list
*/
public void selectAll(List<Car> list, String type) {
switch (type) {
case "car":
for (Car car : list) {
System.out.println("车牌号:" + car.getId() + "|车型:" + car.getType() + "|颜色:" + car.getColour());
}
break;
case "park":
for (Car car : list) {
System.out.println("车位占有者:" + car.getId() + "|车型:" + car.getType() + "|颜色:" + car.getColour());
}
break;
default:
break;
}
}
}
[源代码打包下载]