このページのコンテンツ一覧(5個位までにしてください。上が最新で下が最古です)
import java.io.*; public class Adventure { String[][] map; String[][] first; private int money; // 所持金 private int position; // 現在位置 int goal; // 目標金額 int gold; /** * マップと目標金額を読み込みインスタンス化する * @param gold 宝の数 * @param goal 目標金額 */ public Adventure(int gold, int goal){ money = 0; position = 40; // 現在位置を中心にする this.gold = gold; this.goal = goal; initMap(); } /** * マップの初期化 * */ private void initMap(){ map = new String[9][9]; first = new String[9][9]; for(int x_ = 0; x_ < 9; x_++){ for(int y_ = 0; y_ < 9; y_++){ map[x_][y_] = "・"; first[x_][y_] = "・"; } } map[4][4] = "*"; int x, y; for(int i = 0; i < gold; i++){ while(true){ x = (int) (Math.random() * 9); y = (int) (Math.random() * 9); if(map[x][y].equals("・")) break; } map[x][y] = "G"; first[x][y] = "G"; } while(true){ x = (int) (Math.random() * 9); y = (int) (Math.random() * 9); if((x != 4 || y != 4) && map[x][y].equals("・")) break; } map[x][y] = "T"; first[x][y] = "T"; } /** * 冒険を開始する * * @throws IOException */ public void start() throws IOException{ BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)); System.out.println("冒険を開始します・"); moveMap(); while(money < goal && money >= 0){ System.out.println("所持金:" + money + "G"); System.out.println("宝の残数:" + gold); System.out.println("移動方向を入力してください tenkeyに対応しています"); System.out.println("上:8 下:2 左:4 右:6"); String str = buf.readLine(); if(!str.equals("2") && !str.equals("4") && !str.equals("6") && !str.equals("8")) System.out.println("正しい数字を入力してください"); else move(Integer.parseInt(str)); } if(money < 0){ System.out.println("罠にかかりました"); System.out.println("===== Game Over ====="); }else { System.out.println("所持金が目標値を越えました"); System.out.println("===== Congratulation!!! ====="); } System.out.println("初期Map"); printMap(); } /** * 移動を行う * * @param num 入力された移動方向 */ private void move(int num){ boolean isOK = true; switch(num){ case 2: if((position/9) == 8) isOK = false; else position += 9; break; case 4: if((position%9) == 0) isOK = false; else position--; break; case 6: if((position%9) == 8) isOK = false; else position++; break; case 8: if((position/9) == 0) isOK = false; else position -= 9; break; } if(!isOK) System.out.println("移動できませんでした"); int getGold = check(); if(getGold > 0) { System.out.println("----- 宝発見! -----"); System.out.println(); } money += getGold; moveMap(); } /** * 移動位置が宝か罠か判定する * * @return 宝なら 1、罠なら -goal */ public int check(){ int x = position % 9; int y = position / 9; if(map[x][y].equals("T")) return -goal; if(map[x][y].equals("G")) { gold--; return 1; } return 0; } /** * 移動したマップを表示 * 宝、罠の位置は表示されない */ public void moveMap(){ System.out.println("┌─────────┐"); for(int y = 0; y < 9; y++){ System.out.print("│"); for(int x = 0; x < 9; x++){ if(x == position%9 && y == position/9){ System.out.print("P"); map[x][y] = "*"; } else if(map[x][y].equals("*")) System.out.print("*"); else System.out.print("・"); } System.out.println("│"); } System.out.println("└─────────┘"); } /** * 初期マップを表示 * 宝と罠の位置も表示される * */ public void printMap(){ System.out.println("┌─────────┐"); for(int y = 0; y < 9; y++){ System.out.print("│"); for(int x = 0; x < 9; x++){ System.out.print(first[x][y]); } System.out.println("│"); } System.out.println("└─────────┘"); } public static void main(String[] args) throws IOException{ try{ BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)); System.out.println("宝の数を設定してください(1〜20"); int gold = Integer.parseInt(buf.readLine()); if(gold > 20 || gold <= 0) throw new NumberFormatException(); System.out.println("目標金額を設定してください(1〜" + gold + ")"); int goal = Integer.parseInt(buf.readLine()); if(goal <= 0 || goal > gold) throw new NumberFormatException(); Adventure p = new Adventure(gold, goal); p.start(); } catch(NumberFormatException e){ System.out.println("正しい値を入力してください"); } } }
// T53_866.java import java.io.*; public class T53_866 { private Map map; // マップ private int money; // 所持金 private int position; // 現在位置 int goal; // 目標金額 /** * マップと目標金額を読み込みインスタンス化する * * @param map * * @param goal */ public T53_866(Map map, int goal){ this.map = map; money = 0; position = 40; // 現在位置を中心にする this.goal = goal; } /** * 冒険を開始する * * @throws IOException */ public void start() throws IOException{ BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)); System.out.println("冒険を開始します・"); map.moveMap(position); while(money < goal && money >= 0){ System.out.println("所持金:" + money + "G"); System.out.println("宝の残数:" + map.getGold()); System.out.println("移動方向を入力してください tenkeyに対応しています"); System.out.println("上:8 下:2 左:4 右:6"); String str = buf.readLine(); if(!str.equals("2") && !str.equals("4") && !str.equals("6") && !str.equals("8")) System.out.println("正しい数字を入力してください"); else move(Integer.parseInt(str)); } if(money < 0){ System.out.println("罠にかかりました"); System.out.println("===== Game Over ====="); }else { System.out.println("所持金が目標値を越えました"); System.out.println("===== Congratulation!!! ====="); } System.out.println("初期Map"); map.printMap(); } /** * 移動を行う * * @param num 入力された移動方向 */ private void move(int num){ boolean isOK = true; switch(num){ case 2: if((position/9) == 8) isOK = false; else position += 9; break; case 4: if((position%9) == 0) isOK = false; else position--; break; case 6: if((position%9) == 8) isOK = false; else position++; break; case 8: if((position/9) == 0) isOK = false; else position -= 9; break; } if(!isOK) System.out.println("移動できませんでした"); int gold = map.check(position); if(gold > 0) { System.out.println("----- 宝発見! -----"); System.out.println(); } money += gold; map.moveMap(position); } public static void main(String[] args) throws IOException{ try{ BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)); System.out.println("宝の数を設定してください(1〜20"); int gold = Integer.parseInt(buf.readLine()); if(gold > 20 || gold <= 0) throw new NumberFormatException(); System.out.println("目標金額を設定してください(1〜" + gold + ")"); int goal = Integer.parseInt(buf.readLine()); if(goal <= 0 || goal > gold) throw new NumberFormatException(); T53_866 p = new T53_866(new Map(gold, goal), goal); p.start(); } catch(NumberFormatException e){ System.out.println("正しい値を入力してください"); } } }
// Map.java public class Map { String[][] map; String[][] first; int gold; int goal; public Map(int gold, int goal){ this.gold = gold; this.goal = goal; map = new String[9][9]; first = new String[9][9]; for(int x_ = 0; x_ < 9; x_++){ for(int y_ = 0; y_ < 9; y_++){ map[x_][y_] = "・"; first[x_][y_] = "・"; } } map[4][4] = "*"; int x, y; for(int i = 0; i < gold; i++){ while(true){ x = (int) (Math.random() * 9); y = (int) (Math.random() * 9); if(map[x][y].equals("・")) break; } map[x][y] = "G"; first[x][y] = "G"; } while(true){ x = (int) (Math.random() * 9); y = (int) (Math.random() * 9); if((x != 4 || y != 4) && map[x][y].equals("・")) break; } map[x][y] = "T"; first[x][y] = "T"; } /** * 移動位置が宝か罠か判定する * * @param position 移動位置 * @return 宝なら 1、罠なら -goal */ public int check(int position){ int x = position % 9; int y = position / 9; if(map[x][y].equals("T")) return -goal; if(map[x][y].equals("G")) { gold--; return 1; } return 0; } /** * 移動したマップを表示 * 宝、罠の位置は表示されない * @param position */ public void moveMap(int position){ System.out.println("┌─────────┐"); for(int y = 0; y < 9; y++){ System.out.print("│"); for(int x = 0; x < 9; x++){ if(x == position%9 && y == position/9){ System.out.print("P"); map[x][y] = "*"; } else if(map[x][y].equals("*")) System.out.print("*"); else System.out.print("・"); } System.out.println("│"); } System.out.println("└─────────┘"); } /** * 初期マップを表示 * 宝と罠の位置も表示される * */ public void printMap(){ System.out.println("┌─────────┐"); for(int y = 0; y < 9; y++){ System.out.print("│"); for(int x = 0; x < 9; x++){ System.out.print(first[x][y]); } System.out.println("│"); } System.out.println("└─────────┘"); } public int getGold(){return gold; } }
設計力のなさを痛感。
// MinesweeperApplet.java import java.applet.Applet; import javax.swing.JFrame; /* * <applet code="MinesweeperApplet.class" width="300" height="300"></applet> */ public class MinesweeperApplet extends Applet { public void init() { MinesweeperFrame frame = MinesweeperFrame.getInstance(); frame.setTitle( "マインスイーパ" ); frame.setSize( 300, 300 ); frame.setVisible(true); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } }
// MinesweeperFrame.java import java.awt.BorderLayout; import java.awt.GridLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; public class MinesweeperFrame extends JFrame { private static final int CELLS = 5; private BlockButton[][] buttons = new BlockButton[CELLS][CELLS]; private JLabel message; private int bombCount; private int left; private static MinesweeperFrame self = new MinesweeperFrame(); public static MinesweeperFrame getInstance() { return self; } private MinesweeperFrame() { JPanel panel = new JPanel( new BorderLayout() ); JPanel panel1 = new JPanel(); JPanel panel2 = new JPanel( new GridLayout(CELLS, CELLS) ); for( int i=0; i<buttons.length; i++ ) { for( int j=0; j<buttons[i].length; j++ ) { if( Math.random() > 0.2 ) { buttons[i][j] = new SafeBlockButton( i, j ); } else { buttons[i][j] = new BombBlockButton( i, j ); bombCount++; } panel2.add( buttons[i][j] ); } } int all = buttons.length * buttons[0].length; left = all - bombCount; message = new JLabel( "爆弾数:" + bombCount + " あと" + left + "回でクリア" ); panel1.add( message ); panel.add( panel1, BorderLayout.NORTH ); panel.add( panel2, BorderLayout.CENTER ); add( panel ); } /** * 爆弾のある位置を表示。 * */ public void showAll() { for( int i=0; i<buttons.length; i++ ) { for( int j=0; j<buttons[i].length; j++ ) { buttons[i][j].setEnabled(false); if( buttons[i][j] instanceof BombBlockButton ) buttons[i][j].setText( "B" ); } } } /** * 指定された場所のとなりにある爆弾の数を返却 * @param i 指定された場所 x 座標 * @param j 指定された場所 y 座標 * @return 爆弾の数 */ public int getNeighborBlock( int i, int j ) { message.setText( "爆弾数:" + bombCount + " あと" + --left + "回でクリア" ); if( left == 0 ) { JOptionPane.showMessageDialog( this, "(∩´∀`)∩", null, JOptionPane.INFORMATION_MESSAGE, null ); showAll(); } int count = 0; for( int i_=i-1; i_<=i+1; i_++ ) { for( int j_=j-1; j_<=j+1; j_++ ) { try { if( buttons[i_][j_] instanceof BombBlockButton ) count++; } catch( ArrayIndexOutOfBoundsException aioobe ) {} } } return count; } }
// BlockButton.java import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; public abstract class BlockButton extends JButton implements ActionListener { private int i; private int j; public BlockButton( int i, int j ) { this.i = i; this.j = j; addActionListener( this ); } public void actionPerformed(ActionEvent ae) { setEnabled(false); process(); } public abstract void process(); public int getI() { return i; } public int getJ() { return j; } }
// SafeBlockButton.java /** * 安全なブロックを表すボタン * */ public class SafeBlockButton extends BlockButton { public SafeBlockButton( int i, int j ) { super(i, j); } public void process() { MinesweeperFrame frame = MinesweeperFrame.getInstance(); int count = frame.getNeighborBlock( getI(), getJ() ); setText( "" + count ); } }
// BombBlockButton.java import javax.swing.JOptionPane; /** * 爆弾のあるブロックを表すボタン * */ public class BombBlockButton extends BlockButton { public BombBlockButton( int i, int j ) { super(i, j); //// デバック用 //setText( "b" ); } public void process() { MinesweeperFrame frame = MinesweeperFrame.getInstance(); frame.showAll(); JOptionPane.showMessageDialog( frame, " _, ._\n( ゚ Д゚)", null, JOptionPane.ERROR_MESSAGE, null ); } }
import java.util.StringTokenizer; /** * 入力された日付から曜日を表示する。 適当にいじって * * @author nanasi */ public class Youbi { public static void main(String[] args) { if (args == null || args.length < 1) { System.out.println("yyyy.MM.dd 形式で引数を与えてください"); System.exit(0); } Youbi me = new Youbi(); // 年月日に分解 int[] dateToken = me.tokenizeDate(args[0]); if (dateToken == null || dateToken.length != 3) { System.err.println("入力日付エラー"); System.exit(1); } // 曜日を表示 String day = me.getDay(dateToken[0], dateToken[1], dateToken[2]); if (day == null) { System.err.println("1582/10/15以前の曜日は計算できない"); System.exit(2); } System.out.println(day); System.exit(0); } /** * 文字列の日付を数値に分解する * * @param stringdate * "yyyy.MM.dd" 形式の日付 * @return int[]{yyyy, MM, dd} エラーの場合はnull */ private int[] tokenizeDate(String stringdate) { final String DELIM = "."; StringTokenizer tokenizer = new StringTokenizer(stringdate, DELIM); if (tokenizer.countTokens() != 3) return null; int[] tokens = new int[3]; for (int i = 0; i < 3; i++) { String token = tokenizer.nextToken(); // 文字列が数値かチェック for (int j = 0; j < token.length(); j++) { if (!Character.isDigit(token.charAt(j))) return null; } tokens[i] = Integer.valueOf(token).intValue(); } return tokens; } /** * 与えられた年月日から曜日を返す * * @param y * @param m * @param d * @return 文字列の曜日 エラーの場合はnull */ private String getDay(int y, int m, int d) { if (y == 1582 && m == 10 && d < 15) return null; int day = (y + (y / 4) - (y / 100) + (y / 400) + (13 * m + 8) / 5 + d) % 7; String[] dayName = new String[] { "日", "月", "火", "水", "木", "金", "土" }; if (dayName.length <= day) return null; return dayName[day]; } }
// MultiplyMatrixDemo.java public class MultiplyMatrixDemo { public static void main(String[] args) { double[][] matrix1 = new double[][] { { 3.3, 4.4, 5.5, 6.6, 7.7, }, { 0, 1.1, 2.2, 8.8, 9.9, }, { 10, 11.1, 12.2, 13.3, 1.44, }, { 9.99, 9.99, 8.99, 8.99, 7.99, }, }; double[][] matrix2 = new double[][] { { 0, 1.11, 2.22, 3.33, 4.44, 5.55, }, { 1.22, 2.33, 3.44, 4.55, 5.66, 6.77, }, { 1.26, 2.37, 3.48, 4.59, 5.6, 6.71, }, { 1.36, 2.47, 3.58, 4.69, 5.76, 6.81, }, { 8.36, 9.47, 0.58, 1.69, 2.76, 3.81, }, }; System.out.println( "行列A(" + matrix1.length + "x" + matrix1[0].length + ") =" ); MultiplyMatrix.printMatrix( matrix1 ); System.out.println( "行列B(" + matrix2.length + "x" + matrix2[0].length + ") =" ); MultiplyMatrix.printMatrix( matrix2 ); MultiplyMatrix.multiply( matrix1, matrix2 ); System.out.println( "---計算結果---" ); System.out.println( "行列C(" + MultiplyMatrix.answer.length + "x" + MultiplyMatrix.answer[0].length + ") =" ); MultiplyMatrix.printMatrix( MultiplyMatrix.answer ); } }
// MultiplyMatrix.java public class MultiplyMatrix { // 答えの配列 static double[][] answer; /** * 行列の出力 * @param matrix 行列 */ public static void printMatrix( double[][] matrix ) { // 出力 int ROWS = matrix.length; int COLS = matrix[0].length; for( int i=0; i<ROWS; i++ ) { System.out.print("[ "); for( int j=0; j<COLS; j++ ) { System.out.printf( "%8.3f\t", matrix[i][j] ); } System.out.println("]"); } } /** * 行列のかけ算 * @param matrix1 かけられる行列 * @param matrix2 かける行列 */ public static void multiply( double[][] matrix1, double[][] matrix2 ) { // 答えの行列の確保 int ROWS = matrix1.length; int COLS = matrix2[0].length; answer = new double[ROWS][COLS]; // 計算 for( int i=0; i<ROWS; i++ ) { for( int j=0; j<COLS; j++ ) { double sum = 0; for( int k=0; k<matrix1[0].length; k++ ) { sum += matrix1[i][k] * matrix2[k][j]; } answer[i][j] = sum; } } } }