Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main {
- private final String[] players = {"Player1", "Player2"};
- private final String[] state = {"○", "×"};
- private String board[][] = { {" ", " ", " "}, {" ", " ", " "},
- {" ", " ", " "}};
- public String[] players() {
- return players;
- }
- public String[] state() {
- return state;
- }
- public String[][] board() {
- return board;
- }
- @Override
- public String toString() {
- StringBuilder sb = new StringBuilder();
- sb.append("┌─┬─┬─┐\n");
- for (int i = 0; i < 3; i++) {
- sb.append("│");
- for (int j = 0; j < 3; j++) {
- sb.append(board[i][j]);
- sb.append("│");
- }
- sb.append("\n");
- }
- sb.append("└─┴─┴─┘\n");
- return sb.toString();
- }
- private static int read(Scanner in) {
- int val = 0;
- while (true) {
- try {
- val = new Integer(in.next());
- }
- catch (Exception e) {
- }
- if (val >= 0 && val < 3) break;
- }
- return val;
- }
- public static void main(String[] args) {
- Main game = new Main();
- Scanner in = new Scanner(System.in);
- for (int i = 0; i < 3 * 3; i++) {
- System.out.println(game);
- StringBuilder sb = new StringBuilder();
- sb.append("[" + game.players()[i % 2] + "の番]\n");
- sb.append("縦と横のインデックスを半角スペース区切りで指定してください。");
- System.out.println(sb);
- int row = read(in);
- int col = read(in);
- game.board()[row][col] = game.state()[i % 2];
- }
- in.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement