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

public class HighScore {
	private int score;
	private String date;
	public HighScore(int score,String date) {
		this.score=score;
		this.date=date;
	}
	@Override
	public String toString() {
		return this.score+" "+this.date;
	}
	public String toCSV() {
		return this.score+","+this.date;
	}
	public int getScore() {
		return this.score;
	}

}

アプリケーションクラス

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.Scanner;

public class HighScoreGame {

	public static void main(String[] args) {
		final String FILE="score.csv";
		final int MAX=100000;
		Scanner scan =new Scanner(System.in);
		Random rand=new Random();
		boolean isHighScore=false;
		System.out.println("****乱数ゲーム****");
		HighScore hs=readFile(FILE);
		int highScore;
		if(hs==null) {
			System.out.println("HighScore 0");
			highScore=0;
		}else {
			System.out.println("HighScore "+hs);
			highScore=hs.getScore();
		}
		while(true) {
			System.out.print("やりますかyes...y no...n>");
			String select=scan.next();
			if(select.equals("n")) {
				System.out.println("アプリを終了します");
				if(isHighScore) {
					Date today=new Date();
					SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd");
					String date=sdf.format(today);
					hs=new HighScore(highScore,date);
					writeRecord(FILE,hs);
				}
				scan.close();
				break;
			}
			int no=rand.nextInt(MAX)+1;
			System.out.println(no);
			if(no > highScore) {
				highScore=no;
				isHighScore=true;
				System.out.println("new Record");
			}

		}
	}
	public static HighScore readFile(String file) {
		HighScore hs=null;
		BufferedReader br=null;
		try {
			FileInputStream fis=new FileInputStream(file);
			InputStreamReader isr=new InputStreamReader(fis,"UTF-8");
			br=new BufferedReader(isr);
			String line=br.readLine();
			if(line!=null && !line.equals("")) {
				String[] data=line.split(",");
				hs=new HighScore(Integer.parseInt(data[0]),data[1]);
			}
		} catch (FileNotFoundException e) {
					e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return hs;

	}
	public static void writeRecord(String fileName,HighScore hs) {
		BufferedWriter bw=null;
		try {
			FileOutputStream fos=new FileOutputStream(fileName);
			OutputStreamWriter osw=new OutputStreamWriter(fos,"UTF-8");
			bw=new BufferedWriter(osw);
			bw.append(hs.toCSV());
			bw.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();

		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(bw !=null) {
				try {
					bw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

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

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.Scanner;

public class HighScoreGame {

	public static void main(String[] args) {
		final String FILE="score2.csv";
		final int MAX=100000;
		Scanner scan =new Scanner(System.in);
		Random rand=new Random();
		int highScore=0;
		boolean isHighScore=false;
		HighScore hs=null;
		System.out.println("****乱数ゲーム****");
		File file=new File(FILE);
		if(file.exists()) {
			hs=readFile(file);
			System.out.println("HighScore "+hs);
			highScore=hs.getScore();
		}else {
			System.out.println("HighScore 0");
		}
		while(true) {
			System.out.print("やりますかyes...y no...n>");
			String select=scan.next();
			if(select.equals("n")) {
				System.out.println("アプリを終了します");
				if(isHighScore) {
					Date today=new Date();
					SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd");
					String date=sdf.format(today);
					hs=new HighScore(highScore,date);
					writeRecord(FILE,hs);
				}
				scan.close();
				break;
			}
			int no=rand.nextInt(MAX)+1;
			System.out.println(no);
			if(no > highScore) {
				highScore=no;
				isHighScore=true;
				System.out.println("new Record");
			}
		}
	}
	public static HighScore readFile(File file) {
		HighScore hs=null;
		BufferedReader br=null;
		try {
			FileInputStream fis=new FileInputStream(file);
			InputStreamReader isr=new InputStreamReader(fis,"UTF-8");
			br=new BufferedReader(isr);
			String line=br.readLine();
			String[] data=line.split(",");
			hs=new HighScore(Integer.parseInt(data[0]),data[1]);
		} catch (FileNotFoundException e) {
					e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return hs;
	}
	public static void writeRecord(String fileName,HighScore hs) {
		BufferedWriter bw=null;
		try {
			FileOutputStream fos=new FileOutputStream(fileName);
			OutputStreamWriter osw=new OutputStreamWriter(fos,"UTF-8");
			bw=new BufferedWriter(osw);
			bw.append(hs.toCSV());
			bw.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();

		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(bw !=null) {
				try {
					bw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

コメント

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