NimSystem实现
Posted jyroy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NimSystem实现相关的知识,希望对你有一定的参考价值。
题目
题目比较长,我直接放截图吧
简述
一个比较经典的类与对象的题目,三个类实现了一个比较简单的系统,具体的每个类的要求可以从上面的题目描述中看出(只要你有耐心读完。。),不再赘述,代码如下
代码实现
整体设计
类和属性、方法
NimGame类
1 /** 2 * @author jyroy 3 * NimGame类 4 */ 5 public class NimGame { 6 public int stonecount; 7 public int upperremoval; 8 public String player1; 9 public String player2; 10 11 }
NimPlayer类
1 import java.text.DecimalFormat; 2 3 /** 4 * @author jyroy 5 * player类 6 */ 7 public class NimPlayer { 8 private String username; 9 private String familyname; 10 private String givenname; 11 private int gamesplayed; 12 private int gameswon; 13 public String getUsername() { 14 return username; 15 } 16 public void setUsername(String username) { 17 this.username = username; 18 } 19 public String getFamilyname() { 20 return familyname; 21 } 22 public void setFamilyname(String familyname) { 23 this.familyname = familyname; 24 } 25 public String getGivenname() { 26 return givenname; 27 } 28 public void setGivenname(String givenname) { 29 this.givenname = givenname; 30 } 31 public int getGamesplayed() { 32 return gamesplayed; 33 } 34 public void setGamesplayed(int gamesplayed) { 35 this.gamesplayed = gamesplayed; 36 } 37 public int getGameswon() { 38 return gameswon; 39 } 40 public void setGameswon(int gameswon) { 41 this.gameswon = gameswon; 42 } 43 @Override 44 public String toString() { 45 return username + "," + familyname + "," + givenname 46 + "," + gamesplayed + " games, " + gameswon + " wins"; 47 } 48 49 public String rate() { 50 return String.valueOf(gameswon/gamesplayed*100); 51 } 52 53 public void rank() { 54 String s = gameswon/gamesplayed*100+"%"; 55 DecimalFormat df=new DecimalFormat("00"); 56 String str2=df.format(gamesplayed); 57 System.out.printf("%-5s",s); 58 System.out.printf("| "); 59 System.out.printf(str2 + " games | "+givenname + " " + familyname+"\\n"); 60 } 61 62 63 }
NimSystem类
1 import java.util.ArrayList; 2 import java.util.Collections; 3 import java.util.Comparator; 4 import java.util.HashMap; 5 import java.util.Iterator; 6 import java.util.List; 7 import java.util.Map; 8 import java.util.Map.Entry; 9 import java.util.Scanner; 10 import java.util.TreeMap; 11 12 /** 13 * @author jyroy 14 * NimSystem类 15 */ 16 public class NimSys { 17 18 static ArrayList<NimPlayer> playerList = new ArrayList<NimPlayer>(); 19 20 /** 21 * add a new user 22 * @param username 23 * @param family_name 24 * @param given_name 25 */ 26 public static void addplayer(String username,String family_name, String given_name) { 27 NimPlayer nimplayer = new NimPlayer(); 28 nimplayer.setUsername(username); 29 nimplayer.setFamilyname(family_name); 30 nimplayer.setGivenname(given_name); 31 nimplayer.setGamesplayed(0); 32 nimplayer.setGameswon(0); 33 for(int i=0;i<playerList.size();i++) { 34 if(playerList.get(i).getUsername().equals(username)) { 35 System.out.println("The player already exists."); 36 return; 37 } 38 } 39 playerList.add(nimplayer); 40 return; 41 } 42 43 44 /** 45 * remove a user or all users 46 * @param username 47 */ 48 public static void removeplayer(String username) { 49 if(username!=null) { 50 for(int i=0;i<playerList.size();i++) { 51 if(playerList.get(i).getUsername().equals(username)) { 52 playerList.remove(i); 53 return; 54 } 55 } 56 System.out.println("The player does not exist."); 57 return; 58 }else if(username==null) { 59 System.out.println("Are you sure you want to remove all players? (y/n)"); 60 Scanner sc=new Scanner(System.in); 61 String s=sc.nextLine(); 62 if(s.equals("y")) { 63 playerList.clear(); 64 } 65 return; 66 } 67 } 68 69 70 /** 71 * edit a user 72 * @param username 73 * @param family_name 74 * @param given_name 75 */ 76 public static void editplayer(String username,String family_name, String given_name) { 77 for(int i=0;i<playerList.size();i++) { 78 if(playerList.get(i).getUsername().equals(username)) { 79 playerList.get(i).setUsername(username); 80 playerList.get(i).setFamilyname(family_name); 81 playerList.get(i).setGivenname(given_name); 82 return; 83 } 84 } 85 System.out.println("The player does not exist."); 86 return; 87 } 88 89 90 /** 91 * reset a user 92 * @param username 93 */ 94 public void resetstats(String username) { 95 if(username!=null) { 96 for(int i=0;i<playerList.size();i++) { 97 if(playerList.get(i).getUsername().equals(username)) { 98 playerList.get(i).setGamesplayed(0); 99 playerList.get(i).setGameswon(0); 100 return; 101 } 102 } 103 System.out.println("The player does not exist."); 104 }else if(username==null) { 105 System.out.println("Are you sure you want to reset all player statistics? (y/n)"); 106 Scanner sc=new Scanner(System.in); 107 String s=sc.nextLine(); 108 if(sc.equals("y")) { 109 for(int i=0;i<playerList.size();i++) { 110 playerList.get(i).setGamesplayed(0); 111 playerList.get(i).setGameswon(0); 112 } 113 } 114 } 115 } 116 117 118 /** 119 * display a user or all users 120 * @param username 121 */ 122 public static void displayplayer(String username) { 123 if(username!=null) { 124 for(int i=0;i<playerList.size();i++) { 125 if(playerList.get(i).getUsername().equals(username)) { 126 System.out.println(playerList.get(i).toString()); 127 return; 128 } 129 } 130 System.out.println("The player does not exist."); 131 }else if(username==null) { 132 for(int i=0;i<playerList.size();i++) { 133 System.out.println(playerList.get(i).toString()); 134 } 135 } 136 return; 137 } 138 139 140 /** 141 * rank all users in desc or asc 142 * @param order 143 */ 144 public static void rankings(String order) { 145 Map<String, String> map = new TreeMap<String, String>(); 146 for(int i=0;i<playerList.size();i++) { 147 map.put(playerList.get(i).getUsername(), playerList.get(i).rate()); 148 } 149 if(order==null||order.equals("desc")){ 150 List<Entry<String, String>> list = new ArrayList<Entry<String, String>>(map.entrySet()); 151 152 Collections.sort(list,new Comparator<Map.Entry<String,String>>() { 153 154 public int compare(Entry<String, String> o1, Entry<String, String> o2) { 155 return o2.getValue().compareTo(o1.getValue()); 156 } 157 }); 158 159 int c=0; 160 Iterator<Entry<String, String>> it2 = list.iterator(); 161 while(it2.hasNext()) { 162 Entry<String, String> entry = it2.next(); 163 for(int j=0;j<playerList.size();j++) { 164 if(entry.getKey().equals(playerList.get(j).getUsername())) { 165 playerList.get(j).rank(); 166 } 167 } 168 if(c==9) { 169 return; 170 } 171 c+=1; 172 } 173 }else if(order.equals("asc")) { 174 Iterator<Entry<String, String>> it2 = map.entrySet().iterator(); 175 int c=0; 176 while(it2.hasNext()) { 177 Entry<String, String> entry = it2.next(); 178 for(int j=0;j<playerList.size();j++) { 179 if(entry.getKey().equals(playerList.get(j).getUsername())) { 180 playerList.get(j).rank(); 181 } 182 } 183 if(c==9) { 184 return; 185 } 186 c+=1; 187 } 188 } 189 } 190 191 /** 192 * start a game 193 * @param initialstones 194 * @param upperbound 195 * @param username1 196 * @param username2 197 */ 198 public static void startgame(int initialstones, int upperbound, String username1, String username2) { 199 int f=0; 200 NimPlayer player1 = null; 201 NimPlayer player2 = null; 202 for(int i=0;i<playerList.size();i++) { 203 if(playerList.get(i).getUsername().equals(username1)) { 204 f+=1; 205 player1 = playerList.get(i); 206 }else if(playerList.get(i).getUsername().equals(username2)) { 207 f+=1; 208 player2 = playerList.get(i); 209 } 210 } 211 if(f==2) { 212 System.out.println("\\n"+"Initial stone count:" + initialstones); 213 System.out.println("Maximum stone removal: " + upperbound); 214 System.out.println("Player 1: " + player1.getGivenname() + " " + player1.getFamilyname()); 215 System.out.println("Player 2: " + player2.getGivenname() + " " + player2.getFamilyname()+"\\n"); 216 int currentstone = initialstones; 217 int round = 1; 218 while(round>0) { 219 System.out.printf("\\n"+currentstone + "stones left: "); 220 for(int i1=0;i1<currentstone;i1++) { 221 System.out.printf("* "); 222 } 223 if(round%2==1) { 224 System.out.println("\\n"+username1 + "’s turn - remove how many?"); 225 }else { 226 System.out.println("\\n"+username2 + "’s turn - remove how many?"); 227 } 228 Scanner sc=new Scanner(System.in); 229 int s=sc.nextInt(); 230 if(s>upperbound) { 231 System.out.println("Invalid move. You must remove between 1 and " + upperbound + "stones."); 232 continue; 233 }else if(s==0) { 234 System.out.printf("Invalid move. You must remove between 1 and "); 235 if(upperbound>currentstone) { 236 System.out.print(currentstone); 237 }else { 238 System.out.print(upperbound); 239 } 240 System.out.printf("stones."); 241 continue; 242 }else { 243 currentstone -= s; 244 } 245 if(currentstone==0) { 246 System.out.println("Game Over"); 247 if(round%2==1) { 248 System.out.println(username2 + " wins!"); 249 player2.setGameswon(player2.getGameswon()+1); 250 player2.setGamesplayed(player2.getGamesplayed()+1); 251 player1.setGamesplayed(player1.getGamesplayed()+1); 252 return; 253 }else { 254 System.out.println(username1 + " wins!"); 255 player1.setGameswon(player1.getGameswon()+1); 256 player1.setGamesplayed(player1.getGamesplayed()+1); 257 player2.setGamesplayed(player2.getGamesplayed()+1); 258 return; 259 } 260 } 261 round+=1; 262 } 263 }else { 264 System.out.println("One of the players does not exist."); 265 return; 266 } 267 } 268 /** 269 * Exits the Nimsys program 270 */ 271 public static void exit() { 272 System.out.println(""); 273 System.exit(0); 274 } 275 276 public static void main(String[] args) { 277 System.out.println("Welcome to Nim"); 278 while(true) { 279 System.out.printf("$"); 280 Scanner sc=new Scanner(System.in); 281 String s=sc.nextLine(); 282 String[] arr = s.split(" "); 283 if(arr[0].equals("addplayer")) { 284 addplayer(arr[1].split(",")[0], arr[1].split(",")[1], arr[1].split(",")[2]); 285 }else if(arr[0].equals("removeplayer")){ 286 if(arr.length!=1) removeplayer(arr[1]); 287 else removeplayer(null); 288 }else if(arr[0].equals("editplayer")) { 289 editplayer(arr[1].split(",")[0], arr[1].split(",")[1], arr[1].split(",")[2]); 290 }else if(arr[0].equals("displayplayer")) { 291 if(arr.length!=1) displayplayer(arr[1]); 292 else displayplayer(null); 293 }else if(arr[0].equals("rankings")) { 294 if(arr.length!=1) rankings(arr[1]); 295 else rankings(null); 296 }else if(arr[0].equals("startgame")) { 297 startgame(Integer.parseInt(arr[1].split(",")[0]), Integer.parseInt(arr[1].split(",")[1]), arr[1].split(",")[2], arr[1].split(",")[3]); 298 }else if(arr[0].equals("exit")) { 299 exit(); 300 } 301 } 302 } 303 304 305 }
以上是关于NimSystem实现的主要内容,如果未能解决你的问题,请参考以下文章