javaにて、コマンドプロンプト上で作動する9×9マスの簡単な〇×ゲームを作成したのですが、
枠線の変え方がわかりません。
[ ][ ][ ]
[ ][ ][ ]
[ ][ ][ ]
↓
┌─┬─┬─┐
│○│○│×│
├─┼─┼─┤
│○│○│×│
├─┼─┼─┤
│×│×│○│
└─┴─┴─┘
現在が上の状態なので、下のように9マスのボードにしたいと考えています。
javaを始めて日が浅く、ネットで調べても出てこなかったため、質問させていただきました。
ご助力お願い致します。
コードが長文になってしまい、申し訳ありません。
import java.util.Arrays;
import java.util.Scanner;
public class MaruBatsu2 {
public static void main(String[] args) {
boolean isNotAllTrue = true;
int count = 0; int col; int row; String input; String[][] arrays = new String[3][3]; for (String[] array : arrays) { Arrays.fill(array, " "); } Scanner sc = new Scanner(System.in); int colIndex = 0; int rowIndex = 0; while(isNotAllTrue) { System.out.print("[○の番] 縦と横のインデックスを半角スペース区切りで指定してください。>"); String str = sc.nextLine(); if(!(str.matches("[0-9]+ [0-9]+"))) { System.out.println("エラー:整数を入力してください。"); }else if (!(str.matches("[0-2]{1} [0-2]{1}"))) { System.out.println("エラー:整数は 0 ~ 2 の間で指定してください。"); }else if ( str.length() != 3 ){ System.out.println("エラー: 整数は2つ入力してください。"); }else { row = Character.getNumericValue(str.charAt(0)); col = Character.getNumericValue(str.charAt(2)); if(arrays[row ][col ].equals(" ")) { arrays[row ][col ] = "〇"; count++; } else { System.out.println("エラー:指定された場所は既に入力されています。"); continue; } showGrid(arrays); System.out.println(""); if(count >= 9) { isNotAllTrue = false; System.out.println("ゲームを終了します。"); } while(isNotAllTrue) { System.out.print("[×の番] 縦と横のインデックスを半角スペース区切りで指定してください。>"); String nums = sc.nextLine(); if(!(nums.matches("[0-9]+ [0-9]+"))) { System.out.println("エラー:整数を入力してください。"); }else if(!(nums.matches("[0-2]{1} [0-2]{1}"))) { System.out.println("エラー:整数は 0 ~ 2 の間で指定してください。"); }else if (nums.length() != 3 ){ System.out.println("エラー: 整数は2つ入力してください。"); continue; }else { row = Character.getNumericValue(nums.charAt(0)); col = Character.getNumericValue(nums.charAt(2)); if(arrays[row ][col ].equals(" ")) { arrays[row ][col ] = "×"; count++; } else { System.out.println("エラー:指定された場所は既に入力されています。"); continue; } showGrid(arrays); System.out.println(""); if(count >= 9) { isNotAllTrue = false; System.out.println("ゲームを終了します。"); } break; } } } } } public static void showGrid(String [][] arrays){ for(int i = 0; i < 3; i++) { System.out.println(""); for(int j = 0; j < 3; j++) { System.out.print("[" + arrays[i][j] + "]"); } } }
}
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/21 11:13