题目

题目比较长,我直接放截图吧

简述

  一个比较经典的类与对象的题目,三个类实现了一个比较简单的系统,具体的每个类的要求可以从上面的题目描述中看出(只要你有耐心读完。。),不再赘述,代码如下

代码实现

整体设计

  类和属性、方法

NimGame类

 /**
* @author jyroy
* NimGame类
*/
public class NimGame {
public int stonecount;
public int upperremoval;
public String player1;
public String player2; }

NimPlayer类

 import java.text.DecimalFormat;

 /**
* @author jyroy
* player类
*/
public class NimPlayer {
private String username;
private String familyname;
private String givenname;
private int gamesplayed;
private int gameswon;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getFamilyname() {
return familyname;
}
public void setFamilyname(String familyname) {
this.familyname = familyname;
}
public String getGivenname() {
return givenname;
}
public void setGivenname(String givenname) {
this.givenname = givenname;
}
public int getGamesplayed() {
return gamesplayed;
}
public void setGamesplayed(int gamesplayed) {
this.gamesplayed = gamesplayed;
}
public int getGameswon() {
return gameswon;
}
public void setGameswon(int gameswon) {
this.gameswon = gameswon;
}
@Override
public String toString() {
return username + "," + familyname + "," + givenname
+ "," + gamesplayed + " games, " + gameswon + " wins";
} public String rate() {
return String.valueOf(gameswon/gamesplayed*100);
} public void rank() {
String s = gameswon/gamesplayed*100+"%";
DecimalFormat df=new DecimalFormat("00");
String str2=df.format(gamesplayed);
System.out.printf("%-5s",s);
System.out.printf("| ");
System.out.printf(str2 + " games | "+givenname + " " + familyname+"\n");
} }

NimSystem类

 import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.TreeMap; /**
* @author jyroy
* NimSystem类
*/
public class NimSys { static ArrayList<NimPlayer> playerList = new ArrayList<NimPlayer>(); /**
* add a new user
* @param username
* @param family_name
* @param given_name
*/
public static void addplayer(String username,String family_name, String given_name) {
NimPlayer nimplayer = new NimPlayer();
nimplayer.setUsername(username);
nimplayer.setFamilyname(family_name);
nimplayer.setGivenname(given_name);
nimplayer.setGamesplayed(0);
nimplayer.setGameswon(0);
for(int i=0;i<playerList.size();i++) {
if(playerList.get(i).getUsername().equals(username)) {
System.out.println("The player already exists.");
return;
}
}
playerList.add(nimplayer);
return;
} /**
* remove a user or all users
* @param username
*/
public static void removeplayer(String username) {
if(username!=null) {
for(int i=0;i<playerList.size();i++) {
if(playerList.get(i).getUsername().equals(username)) {
playerList.remove(i);
return;
}
}
System.out.println("The player does not exist.");
return;
}else if(username==null) {
System.out.println("Are you sure you want to remove all players? (y/n)");
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
if(s.equals("y")) {
playerList.clear();
}
return;
}
} /**
* edit a user
* @param username
* @param family_name
* @param given_name
*/
public static void editplayer(String username,String family_name, String given_name) {
for(int i=0;i<playerList.size();i++) {
if(playerList.get(i).getUsername().equals(username)) {
playerList.get(i).setUsername(username);
playerList.get(i).setFamilyname(family_name);
playerList.get(i).setGivenname(given_name);
return;
}
}
System.out.println("The player does not exist.");
return;
} /**
* reset a user
* @param username
*/
public void resetstats(String username) {
if(username!=null) {
for(int i=0;i<playerList.size();i++) {
if(playerList.get(i).getUsername().equals(username)) {
playerList.get(i).setGamesplayed(0);
playerList.get(i).setGameswon(0);
return;
}
}
System.out.println("The player does not exist.");
}else if(username==null) {
System.out.println("Are you sure you want to reset all player statistics? (y/n)");
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
if(sc.equals("y")) {
for(int i=0;i<playerList.size();i++) {
playerList.get(i).setGamesplayed(0);
playerList.get(i).setGameswon(0);
}
}
}
} /**
* display a user or all users
* @param username
*/
public static void displayplayer(String username) {
if(username!=null) {
for(int i=0;i<playerList.size();i++) {
if(playerList.get(i).getUsername().equals(username)) {
System.out.println(playerList.get(i).toString());
return;
}
}
System.out.println("The player does not exist.");
}else if(username==null) {
for(int i=0;i<playerList.size();i++) {
System.out.println(playerList.get(i).toString());
}
}
return;
} /**
* rank all users in desc or asc
* @param order
*/
public static void rankings(String order) {
Map<String, String> map = new TreeMap<String, String>();
for(int i=0;i<playerList.size();i++) {
map.put(playerList.get(i).getUsername(), playerList.get(i).rate());
}
if(order==null||order.equals("desc")){
List<Entry<String, String>> list = new ArrayList<Entry<String, String>>(map.entrySet()); Collections.sort(list,new Comparator<Map.Entry<String,String>>() { public int compare(Entry<String, String> o1, Entry<String, String> o2) {
return o2.getValue().compareTo(o1.getValue());
}
}); int c=0;
Iterator<Entry<String, String>> it2 = list.iterator();
while(it2.hasNext()) {
Entry<String, String> entry = it2.next();
for(int j=0;j<playerList.size();j++) {
if(entry.getKey().equals(playerList.get(j).getUsername())) {
playerList.get(j).rank();
}
}
if(c==9) {
return;
}
c+=1;
}
}else if(order.equals("asc")) {
Iterator<Entry<String, String>> it2 = map.entrySet().iterator();
int c=0;
while(it2.hasNext()) {
Entry<String, String> entry = it2.next();
for(int j=0;j<playerList.size();j++) {
if(entry.getKey().equals(playerList.get(j).getUsername())) {
playerList.get(j).rank();
}
}
if(c==9) {
return;
}
c+=1;
}
}
} /**
* start a game
* @param initialstones
* @param upperbound
* @param username1
* @param username2
*/
public static void startgame(int initialstones, int upperbound, String username1, String username2) {
int f=0;
NimPlayer player1 = null;
NimPlayer player2 = null;
for(int i=0;i<playerList.size();i++) {
if(playerList.get(i).getUsername().equals(username1)) {
f+=1;
player1 = playerList.get(i);
}else if(playerList.get(i).getUsername().equals(username2)) {
f+=1;
player2 = playerList.get(i);
}
}
if(f==2) {
System.out.println("\n"+"Initial stone count:" + initialstones);
System.out.println("Maximum stone removal: " + upperbound);
System.out.println("Player 1: " + player1.getGivenname() + " " + player1.getFamilyname());
System.out.println("Player 2: " + player2.getGivenname() + " " + player2.getFamilyname()+"\n");
int currentstone = initialstones;
int round = 1;
while(round>0) {
System.out.printf("\n"+currentstone + "stones left: ");
for(int i1=0;i1<currentstone;i1++) {
System.out.printf("* ");
}
if(round%2==1) {
System.out.println("\n"+username1 + "’s turn - remove how many?");
}else {
System.out.println("\n"+username2 + "’s turn - remove how many?");
}
Scanner sc=new Scanner(System.in);
int s=sc.nextInt();
if(s>upperbound) {
System.out.println("Invalid move. You must remove between 1 and " + upperbound + "stones.");
continue;
}else if(s==0) {
System.out.printf("Invalid move. You must remove between 1 and ");
if(upperbound>currentstone) {
System.out.print(currentstone);
}else {
System.out.print(upperbound);
}
System.out.printf("stones.");
continue;
}else {
currentstone -= s;
}
if(currentstone==0) {
System.out.println("Game Over");
if(round%2==1) {
System.out.println(username2 + " wins!");
player2.setGameswon(player2.getGameswon()+1);
player2.setGamesplayed(player2.getGamesplayed()+1);
player1.setGamesplayed(player1.getGamesplayed()+1);
return;
}else {
System.out.println(username1 + " wins!");
player1.setGameswon(player1.getGameswon()+1);
player1.setGamesplayed(player1.getGamesplayed()+1);
player2.setGamesplayed(player2.getGamesplayed()+1);
return;
}
}
round+=1;
}
}else {
System.out.println("One of the players does not exist.");
return;
}
}
/**
* Exits the Nimsys program
*/
public static void exit() {
System.out.println("");
System.exit(0);
} public static void main(String[] args) {
System.out.println("Welcome to Nim");
while(true) {
System.out.printf("$");
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
String[] arr = s.split(" ");
if(arr[0].equals("addplayer")) {
addplayer(arr[1].split(",")[0], arr[1].split(",")[1], arr[1].split(",")[2]);
}else if(arr[0].equals("removeplayer")){
if(arr.length!=1) removeplayer(arr[1]);
else removeplayer(null);
}else if(arr[0].equals("editplayer")) {
editplayer(arr[1].split(",")[0], arr[1].split(",")[1], arr[1].split(",")[2]);
}else if(arr[0].equals("displayplayer")) {
if(arr.length!=1) displayplayer(arr[1]);
else displayplayer(null);
}else if(arr[0].equals("rankings")) {
if(arr.length!=1) rankings(arr[1]);
else rankings(null);
}else if(arr[0].equals("startgame")) {
startgame(Integer.parseInt(arr[1].split(",")[0]), Integer.parseInt(arr[1].split(",")[1]), arr[1].split(",")[2], arr[1].split(",")[3]);
}else if(arr[0].equals("exit")) {
exit();
}
}
} }

NimSystem实现的更多相关文章

随机推荐

  1. 简析 Golang IO 包

    简析 Golang IO 包 io 包提供了 I/O 原语(primitives)的基本接口.io 包中定义了四个最基本接口 Reader.Writer.Closer.Seeker 用于表示二进制流的 ...

  2. 在.net 程序中使用Mustache模板字符串

    今天弄了一个配置随着使用环境动态切换的功能,一个基本的思路是: 将配置配置为模板的形式, 根据不同的环境定义环境变量 根据环境变量渲染模板,生成具体的配置 这里面就涉及到了一个字符串模板的功能,关于模 ...

  3. java基础(29):JDBC、DBUtils

    1. JDBC 1.1 JDBC概述 JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问 ...

  4. MySQL,必须掌握的6个知识点

    本人免费整理了Java高级资料,涵盖了Java.Redis.MongoDB.MySQL.Zookeeper.Spring Cloud.Dubbo高并发分布式等教程,一共30G,需要自己领取.传送门:h ...

  5. VMWare虚拟机应用介绍

    目录 一:VMWare简介 二:VMWare启动第一个虚拟机 一:VMWare简介   VMWare虚拟机软件是一个"虚拟PC"软件,它使你可以在一台机器上同时运行二个或更多Win ...

  6. Java性能之优化RPC网络通信

    服务框架的核心 大型服务框架的核心:RPC通信 微服务的核心是远程通信和服务治理 远程通信提供了服务之间通信的桥梁,服务治理提供了服务的后勤保障 服务的拆分增加了通信的成本,因此远程通信很容易成为系统 ...

  7. Javase之集合体系(4)之Map集合

    集合体系之Map集合 ##Map<K,V>( 接口 ) 特点:将键映射到值对象,一个映射不能包含重复的键:每个键只能映射一个值 Map集合与Collection集合的区别 ​ Map集合存 ...

  8. RSA 非对称加密算法的Java实现

    关于RSA的介绍Google一下很多,这里不做说明.项目开发中一般会把公钥放在本地进行加密,服务端通过私钥进行解密.Android项目开发中要用到这个加密算法,总结后实现如下: import andr ...

  9. Hystrix工作流

    Hystrix工作流程图: 流程图详解 1. 构造HystrixCommand对象或者HystrixObservableCommand对象 构造HystrixCommand 或HystrixObser ...

  10. [Go] vscode配置Go环境

    首先要先把环境变量配置好,配置好环境变量打开go文件的时候,会自动提示需要安装的扩展在/root/.profile和/home/当前用户/.profile都加上 export GOPATH=/var/ ...