我做过纸牌游戏,类似于纸牌游戏。它差不多完成了。我遇到的最后一件事就是打印前5名。每当玩家结束游戏时,它会将score.txt文件写入他在桌子上剩下多少堆,让我们说4.所以在.txt文件中我有以下数字:1,2,3,4,5,6,7,8,9
。如何从文件的前5位打印,所以它是1,2,3,4,5
。
DeckOfCards class:
import java.io.*;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class DeckOfCards {
private ArrayList<Cards> deck;
private int currentCard=-1; //index of the next card to be dealt
private Scanner in;
private int i;
//ArrayList<Integer> indexesToRemove = new ArrayList();
/**
*
* Constructor to build a deck of cards
*/
public DeckOfCards() {
this.deck = new ArrayList<Cards>();
}
public void removeCard(int i) {
this.deck.remove(i);
}
public Cards getCard(int i){
return this.deck.get(i);
}
public void addCard(Cards addCards){
this.deck.add(addCards);
}
//Draws from the deck1
public void draw(DeckOfCards comingFrom){
currentCard++;
this.deck.add(comingFrom.getCard(0));
comingFrom.removeCard(0);
}
public void saveScore(){
try (PrintStream out = new PrintStream(new FileOutputStream("scores.txt"))) {
out.print(deck.size());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
卡类:
public class Cards {
private String suit;
private String value;
public Cards(String suit, String value)
{
this.suit = suit;
this.value = value;
}
public String getSuit() {
return suit;
}
public void setSuit(String suit) {
this.suit = suit;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public String toString() {
return suit + value;
}
}
希望它有所帮助。我摆脱了一些方法,我认为它们并不有用。还有菜单,就像我说的每个案例都有效,除了显示前10个结果。 1 - 打印包装(这样你可以检查它是否正常播放) 2 - 随机播放 3 - 交易卡 4 - 移动,将最后一堆移到上一个堆上 5 - 移动,将最后一堆移回两堆 6 - Amalgamate在中间堆积(通过给出他们的数字) 7 - 在命令行上打印显示的卡 8 - 为我打一次(如果有两个可能的动作,则进行'最远'动作) 9 - 多次为我打球 10 - 显示前10名结果
答案 0 :(得分:0)
当游戏结束时,读取内存中的输入文件(如列表中所示),将新结果添加到排行榜中应该进入的位置,将新结果写入文件中,然后取出第一个列表(元素0是最佳分数,元素n是最差分数)。
public void endGame(int finalScore) throws IOException {
List<String> scores = retrieveScores();
addNewScore(finalScore, scores);
writeScores(scores);
showLeaderBoard(scores);
}
private List<String> retrieveScores() throws IOException {
BufferedReader reader = new BufferedReader(new FileReader("score.txt"));
String scoreLine = reader.readLine(); // read line that contains scores
List<String> scores = new ArrayList<>();
if (scoreLine != null) { // in case of first game
String[] tempScore = scoreLine.split(", ");
scores = new ArrayList<>(Arrays.asList(tempScore));
}
reader.close();
return scores;
}
private void addNewScore(int finalScore, List<String> scores) {
boolean foundSpotForNewScore = false;
int i = 0;
while (!foundSpotForNewScore && i < scores.size()) {
if (finalScore <= Integer.parseInt(scores.get(i))) {
foundSpotForNewScore = true;
}
i++;
}
scores.add(i, String.valueOf(finalScore));
}
private void writeScores(List<String> scores) throws IOException {
FileWriter writer = new FileWriter("score.txt");
String outputScores = scores.toString();
outputScores = outputScores.replace("[", "");
outputScores = outputScores.replace("]", "");
writer.write(outputScores);
writer.close();
}
private void showLeaderBoard(List<String> scores) {
System.out.println("*** TOP 5 LEADERBOARD ***");
int i = 0;
while (i < 5 && i < scores.size()) {
System.out.println(scores.get(i));
i++;
}
System.out.println("*** TOP 5 LEADERBOARD ***");
}