Java(ファイル読み書きを利用したゲーム作成)

Java

Q
1~100000の乱数を生成するゲームを作成し、そのハイスコアをファイルに書き込むことで管理せよ。
はじめてゲームをするときのハイスコアは0
記録が出た時の日付も記録すること。
[実行例]
(初回時)
****乱数ゲーム****
HighScore 0
やりますかyes…y no…n>y
33081
new Record
やりますかyes…y no…n>y
79048
new Record
やりますかyes…y no…n>y
63973
やりますかyes…y no…n>n
アプリを終了します

(二回目以降)
****乱数ゲーム****
HighScore 79048 2018/07/31
やりますかyes…y no…n>y
31186
やりますかyes…y no…n>y
29340
やりますかyes…y no…n>y
22127
やりますかyes…y no…n>y
5352
やりますかyes…y no…n>y
16096
やりますかyes…y no…n>y
80957
new Record
やりますかyes…y no…n>n
アプリを終了します

[解答例]
Score.java

01public class HighScore {
02  private int score;
03  private String date;
04  public HighScore(int score,String date) {
05    this.score=score;
06    this.date=date;
07  }
08  @Override
09  public String toString() {
10    return this.score+" "+this.date;
11  }
12  public String toCSV() {
13    return this.score+","+this.date;
14  }
15  public int getScore() {
16    return this.score;
17  }
18 
19}

アプリケーションクラス

001import java.io.BufferedReader;
002import java.io.BufferedWriter;
003import java.io.FileInputStream;
004import java.io.FileNotFoundException;
005import java.io.FileOutputStream;
006import java.io.IOException;
007import java.io.InputStreamReader;
008import java.io.OutputStreamWriter;
009import java.io.UnsupportedEncodingException;
010import java.text.SimpleDateFormat;
011import java.util.Date;
012import java.util.Random;
013import java.util.Scanner;
014 
015public class HighScoreGame {
016 
017  public static void main(String[] args) {
018    final String FILE="score.csv";
019    final int MAX=100000;
020    Scanner scan =new Scanner(System.in);
021    Random rand=new Random();
022    boolean isHighScore=false;
023    System.out.println("****乱数ゲーム****");
024    HighScore hs=readFile(FILE);
025    int highScore;
026    if(hs==null) {
027      System.out.println("HighScore 0");
028      highScore=0;
029    }else {
030      System.out.println("HighScore "+hs);
031      highScore=hs.getScore();
032    }
033    while(true) {
034      System.out.print("やりますかyes...y no...n>");
035      String select=scan.next();
036      if(select.equals("n")) {
037        System.out.println("アプリを終了します");
038        if(isHighScore) {
039          Date today=new Date();
040          SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd");
041          String date=sdf.format(today);
042          hs=new HighScore(highScore,date);
043          writeRecord(FILE,hs);
044        }
045        scan.close();
046        break;
047      }
048      int no=rand.nextInt(MAX)+1;
049      System.out.println(no);
050      if(no > highScore) {
051        highScore=no;
052        isHighScore=true;
053        System.out.println("new Record");
054      }
055 
056    }
057  }
058  public static HighScore readFile(String file) {
059    HighScore hs=null;
060    BufferedReader br=null;
061    try {
062      FileInputStream fis=new FileInputStream(file);
063      InputStreamReader isr=new InputStreamReader(fis,"UTF-8");
064      br=new BufferedReader(isr);
065      String line=br.readLine();
066      if(line!=null && !line.equals("")) {
067        String[] data=line.split(",");
068        hs=new HighScore(Integer.parseInt(data[0]),data[1]);
069      }
070    } catch (FileNotFoundException e) {
071          e.printStackTrace();
072    } catch (UnsupportedEncodingException e) {
073      e.printStackTrace();
074    } catch (IOException e) {
075      e.printStackTrace();
076    }
077    return hs;
078 
079  }
080  public static void writeRecord(String fileName,HighScore hs) {
081    BufferedWriter bw=null;
082    try {
083      FileOutputStream fos=new FileOutputStream(fileName);
084      OutputStreamWriter osw=new OutputStreamWriter(fos,"UTF-8");
085      bw=new BufferedWriter(osw);
086      bw.append(hs.toCSV());
087      bw.flush();
088    } catch (FileNotFoundException e) {
089      e.printStackTrace();
090    } catch (UnsupportedEncodingException e) {
091      e.printStackTrace();
092 
093    } catch (IOException e) {
094      e.printStackTrace();
095    }finally {
096      if(bw !=null) {
097        try {
098          bw.close();
099        } catch (IOException e) {
100          e.printStackTrace();
101        }
102      }
103    }
104  }
105}

(参考)
ファイルオブジェクトを使った例

001import java.io.BufferedReader;
002import java.io.BufferedWriter;
003import java.io.File;
004import java.io.FileInputStream;
005import java.io.FileNotFoundException;
006import java.io.FileOutputStream;
007import java.io.IOException;
008import java.io.InputStreamReader;
009import java.io.OutputStreamWriter;
010import java.io.UnsupportedEncodingException;
011import java.text.SimpleDateFormat;
012import java.util.Date;
013import java.util.Random;
014import java.util.Scanner;
015 
016public class HighScoreGame {
017 
018  public static void main(String[] args) {
019    final String FILE="score2.csv";
020    final int MAX=100000;
021    Scanner scan =new Scanner(System.in);
022    Random rand=new Random();
023    int highScore=0;
024    boolean isHighScore=false;
025    HighScore hs=null;
026    System.out.println("****乱数ゲーム****");
027    File file=new File(FILE);
028    if(file.exists()) {
029      hs=readFile(file);
030      System.out.println("HighScore "+hs);
031      highScore=hs.getScore();
032    }else {
033      System.out.println("HighScore 0");
034    }
035    while(true) {
036      System.out.print("やりますかyes...y no...n>");
037      String select=scan.next();
038      if(select.equals("n")) {
039        System.out.println("アプリを終了します");
040        if(isHighScore) {
041          Date today=new Date();
042          SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd");
043          String date=sdf.format(today);
044          hs=new HighScore(highScore,date);
045          writeRecord(FILE,hs);
046        }
047        scan.close();
048        break;
049      }
050      int no=rand.nextInt(MAX)+1;
051      System.out.println(no);
052      if(no > highScore) {
053        highScore=no;
054        isHighScore=true;
055        System.out.println("new Record");
056      }
057    }
058  }
059  public static HighScore readFile(File file) {
060    HighScore hs=null;
061    BufferedReader br=null;
062    try {
063      FileInputStream fis=new FileInputStream(file);
064      InputStreamReader isr=new InputStreamReader(fis,"UTF-8");
065      br=new BufferedReader(isr);
066      String line=br.readLine();
067      String[] data=line.split(",");
068      hs=new HighScore(Integer.parseInt(data[0]),data[1]);
069    } catch (FileNotFoundException e) {
070          e.printStackTrace();
071    } catch (UnsupportedEncodingException e) {
072      e.printStackTrace();
073    } catch (IOException e) {
074      e.printStackTrace();
075    }
076    return hs;
077  }
078  public static void writeRecord(String fileName,HighScore hs) {
079    BufferedWriter bw=null;
080    try {
081      FileOutputStream fos=new FileOutputStream(fileName);
082      OutputStreamWriter osw=new OutputStreamWriter(fos,"UTF-8");
083      bw=new BufferedWriter(osw);
084      bw.append(hs.toCSV());
085      bw.flush();
086    } catch (FileNotFoundException e) {
087      e.printStackTrace();
088    } catch (UnsupportedEncodingException e) {
089      e.printStackTrace();
090 
091    } catch (IOException e) {
092      e.printStackTrace();
093    }finally {
094      if(bw !=null) {
095        try {
096          bw.close();
097        } catch (IOException e) {
098          e.printStackTrace();
099        }
100      }
101    }
102  }
103}

コメント

タイトルとURLをコピーしました