题目

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

简述

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

代码实现

整体设计

  类和属性、方法

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. jenkins支持git分支发布

    https://blog.csdn.net/wc1695040842/article/details/102228804 核心就是需要安装一个Git Parameter 的插件结合使用. 如果同时有多 ...

  2. SEH hook 的一种方法

    Windows内核分析索引目录:https://www.cnblogs.com/onetrainee/p/11675224.html 技术学习来源:火哥(QQ:471194425) 该方法的一些原理暂 ...

  3. java高并发系列 - 第4天:JMM相关的一些概念

    JMM(java内存模型),由于并发程序要比串行程序复杂很多,其中一个重要原因是并发程序中数据访问一致性和安全性将会受到严重挑战.如何保证一个线程可以看到正确的数据呢?这个问题看起来很白痴.对于串行程 ...

  4. Web前端基础(8):JavaScript(二)

    1. 数据类型转换 1.1 将数值类型转换成字符串类型 1.1.1 隐式转换 在js中,当运算符在运算时,如果两边数据不统一,CPU就无法计算,这时我们编译器会自动将运算符两边的数据做一个数据类型转换 ...

  5. UIImageView三种方式 和 位置分布

    typedef NS_ENUM(NSInteger, UIViewContentMode) { UIViewContentModeScaleToFill, //为将图片按照整个区域进行拉伸(会破坏图片 ...

  6. Swift 字典模型互转总结

    现在很多iOS项目的开发开始转向Swift语言. 相信 Swift语言很快会成为iOS工程师 必备技能. 字典转模型, 模型转转字典在开发过程中扮演非常重要的角色. 今天就和大家分享一下使用Swift ...

  7. 多线程CGD调度组原理

    我们常用的GCD调度组方式 //GCD常用调度组写法 -(void)demo1{ //创建调度组和队列 dispatch_group_t group = dispatch_group_create() ...

  8. 使用 gitlab 进行代码管理

    这里使用 gitlab 做服务器, 客户端主要使用 git extensions. ============================= gitlab 项目成员类型: ============= ...

  9. 爬虫---Beautiful Soup 初始

    我们在工作中,都会听说过爬虫,那么什么是爬虫呢? 什么是网络爬虫 爬虫基本原理 所谓网络爬虫就是一个自动化数据采集工具,你只要告诉它要采集哪些数据,丢给它一个 URL,就能自动地抓取数据了.其背后的基 ...

  10. echarts使用简介

    1. echarts是百度开源的一个前端图表库 2. 官网地址: https://www.echartsjs.com/zh/index.html 3. 使用介绍: 3.1. 下载echarts.js ...