50,530
社区成员
发帖
与我相关
我的任务
分享
import java.util.Arrays;
public class NimPlayer {
public String username;
public String family_name;
public String given_name;
public int game;
public int win;
public int percentage;
public final static int MAX_PLAYER_NUM = 100;
static NimPlayer [] players = new NimPlayer[MAX_PLAYER_NUM];
static int numofplayer = 0;
public void addplayer(String username, String family_name, String given_name){
for (int index = 0; index < numofplayer; index++) {
if (players[index].username == username) {
System.out.println("The player already exists.");
System.out.println();
System.out.printf(">");
return;
}
}
players[numofplayer].username = username;
players[numofplayer].family_name = family_name;
players[numofplayer].given_name = given_name;
numofplayer++;
System.out.println();
System.out.printf(">");
}
public void removeplayer(String username){
for (int index = 0; index < numofplayer; index++) {
if (players[index].username == username) {
players[index] = players[numofplayer-1];
players[numofplayer-1] = null;
numofplayer--;
System.out.println();
System.out.printf(">");
return;
}
}
System.out.println("The player does not exist.");
System.out.println();
System.out.printf(">");
}
public void removeallplayer(){
System.out.println("Are you sure you want to remove all players? (y/n)");
String flag = Nimsys.keyboard.next();
if (flag == ("y")){
for (int index = 0; index < numofplayer; index++){
players[index] = null;
}
}
System.out.println();
System.out.printf(">");
}
public void editplayer(String username, String new_family_name, String new_given_name){
for (int index = 0; index < numofplayer; index++) {
if (players[index].username == username) {
players[index].family_name = new_family_name;
players[index].given_name = new_given_name;
System.out.println();
System.out.printf(">");
return;
}
}
System.out.println("The player does not exist.");
System.out.println();
System.out.printf(">");
}
public void resetstats(String username){
for (int index = 0; index < numofplayer; index++) {
if (players[index].username == username) {
players[index].game = 0;
players[index].win = 0;
players[index].percentage = 0;
System.out.println();
System.out.printf(">");
return;
}
}
System.out.println("The player does not exist.");
System.out.println();
System.out.printf(">");
}
public void resetstatsall(){
System.out.println("Are you sure you want to reset all player statistics? (y/n)");
String flag = Nimsys.keyboard.next();
if (flag == ("y")){
for (int index = 0; index < numofplayer; index++){
players[index].game = 0;
players[index].win = 0;
players[index].percentage = 0;
}
}
System.out.println();
System.out.printf(">");
}
public void displayplayer(String username){
for (int index = 0; index < numofplayer; index++) {
if (players[index].username == username) {
System.out.println(getdescription(index));
System.out.println();
System.out.printf(">");
return;
}
}
System.out.println("The player does not exist.");
System.out.println();
System.out.printf(">");
}
public void displayallplayer(){
Arrays.sort(players);
for (int index = 0; index < numofplayer; index++){
System.out.println(getdescription(index));
}
}
public static String getdescription(int index){
String description = (players[index].username + ", " + players[index].given_name + ", " + players[index].family_name + ", " + players[index].game + "games, " + players[index].win + "wins");
return description;
}
import java.util.Scanner;
public class Nimsys {
public static Scanner keyboard;
public static void main(String[] args){
keyboard = new Scanner(System.in);
System.out.println("Welcome to Nim");
System.out.printf("\n>");
NimPlayer player = new NimPlayer();
NimGame newgame = new NimGame();
while(true){
String[] command = keyboard.next().split(",| ");
if (command[0] == "addplayer")
player.addplayer(command[1], command[2], command[3]);
else if ((command[0] == "removeplayer") && (command.length != 1))
player.removeplayer(command[1]);
else if ((command[0] == "removeplayer") && (command.length == 1))
player.removeallplayer();
else if (command[0] == "editplayer")
player.editplayer(command[1], command[2], command[3]);
else if ((command[0] == "resetstats") && (command.length != 1))
player.resetstats(command[1]);
else if ((command[0] == "resetstats") && (command.length == 1))
player.resetstatsall();
else if ((command[0] == "displayplayer") && (command.length != 1))
player.displayplayer(command[1]);
else if ((command[0] == "displayplayer") && (command.length == 1))
player.displayallplayer();
else if (command[0] == "startgame")
newgame.startgame(Integer.parseInt(command[1]), Integer.parseInt(command[2]), command[3], command[4]);
}
}
public class NimGame {
public void startgame(int initialstones, int upperbound, String username1, String username2){
int currentplayer;
int nextplayer;
int removal;
if (checkplayer(username1) && checkplayer(username2)){
currentplayer = getplyerindex(username1);
nextplayer = getplyerindex(username2);
System.out.println();
System.out.println("Initial stone count: " + initialstones);
System.out.println("Maximum stone removal: " + upperbound);
System.out.println("Player1: " + NimPlayer.players[currentplayer].given_name + NimPlayer.players[currentplayer].family_name);
System.out.println("Player2: " + NimPlayer.players[nextplayer].given_name + NimPlayer.players[nextplayer].family_name);
}
else{
System.out.println("One of the players does not exist.");
System.out.println();
System.out.printf(">");
return;
}
while (initialstones != 0)
{
System.out.println();
System.out.printf(initialstones + " stones left:");
int stone;
for(stone = initialstones;stone >0;stone--)
System.out.print(" *");
System.out.println("\n" + NimPlayer.players[currentplayer].given_name + "'s turn - remove how many?");
removal = Nimsys.keyboard.nextInt();
if (removal > 0 && removal < min(initialstones, upperbound)){
initialstones = initialstones - removal;
int temp = currentplayer;
nextplayer = currentplayer;
currentplayer = temp;
}
else{
System.out.println("/nInvalid move. You must remove between 1 and " + min(initialstones, upperbound) + "stones.");
continue;
}
}
System.out.println();
System.out.println("Game Over");
System.out.println(NimPlayer.players[currentplayer].given_name + " " + NimPlayer.players[currentplayer].family_name + " wins!");
NimPlayer.players[currentplayer].game++;
NimPlayer.players[currentplayer].win++;
NimPlayer.players[nextplayer].game++;
}
public static boolean checkplayer(String username){
for (int index = 0; index < NimPlayer.numofplayer; index++){
if (NimPlayer.players[index].username == username)
return true;
}
return false;
}
public static int getplyerindex(String username){
int playerindex = 0;
for (int index = 0; index < NimPlayer.numofplayer; index++){
if (NimPlayer.players[index].username == username)
playerindex = index;
}
return playerindex;
}
private static int min(int n1, int n2) {
int temp = 0;
if (n1 < n2)
temp = n1;
else
temp = n2;
return temp;
}
}