Advertisement
cd62131

Board Game (incomplete)

Dec 5th, 2013
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.63 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.   private final String[] players = {"Player1", "Player2"};
  5.   private final String[] state = {"○", "×"};
  6.   private String board[][] = { {" ", " ", " "}, {" ", " ", " "},
  7.     {" ", " ", " "}};
  8.  
  9.   public String[] players() {
  10.     return players;
  11.   }
  12.  
  13.   public String[] state() {
  14.     return state;
  15.   }
  16.  
  17.   public String[][] board() {
  18.     return board;
  19.   }
  20.  
  21.   @Override
  22.   public String toString() {
  23.     StringBuilder sb = new StringBuilder();
  24.     sb.append("┌─┬─┬─┐\n");
  25.     for (int i = 0; i < 3; i++) {
  26.       sb.append("│");
  27.       for (int j = 0; j < 3; j++) {
  28.         sb.append(board[i][j]);
  29.         sb.append("│");
  30.       }
  31.       sb.append("\n");
  32.     }
  33.     sb.append("└─┴─┴─┘\n");
  34.     return sb.toString();
  35.   }
  36.  
  37.   private static int read(Scanner in) {
  38.     int val = 0;
  39.     while (true) {
  40.       try {
  41.         val = new Integer(in.next());
  42.       }
  43.       catch (Exception e) {
  44.       }
  45.       if (val >= 0 && val < 3) break;
  46.     }
  47.     return val;
  48.   }
  49.  
  50.   public static void main(String[] args) {
  51.     Main game = new Main();
  52.     Scanner in = new Scanner(System.in);
  53.     for (int i = 0; i < 3 * 3; i++) {
  54.       System.out.println(game);
  55.       StringBuilder sb = new StringBuilder();
  56.       sb.append("[" + game.players()[i % 2] + "の番]\n");
  57.       sb.append("縦と横のインデックスを半角スペース区切りで指定してください。");
  58.       System.out.println(sb);
  59.       int row = read(in);
  60.       int col = read(in);
  61.       game.board()[row][col] = game.state()[i % 2];
  62.     }
  63.     in.close();
  64.   }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement