コード public class Board { static boolean game; public static String board[][] = new String[9][9]; public static String white = "◯"; public static String black = "●"; public static String EMPTY = " "; public static String stone; public static String rev_stone; //ボードの情報を保存する public static void boardInitValue() { for(int i=0; i<9; i++) { for(int j=0; j<9; j++) { board[i][j] = EMPTY; } } } //ボードの初期表示項目を設定する public static void boardSetup() { for(int i=1; i<9; i++) { //x軸に数字を並べる board[0][i] = String.valueOf(i); //y軸に数字を並べる board[i][0] = String.valueOf(i); } //ゲーム開始時のオセロの配置 board[4][4] = white; board[5][4] =black; board[5][5] =white; board[4][5] = black; stone = black; rev_stone = white; game = true; } public static void showBoard() { //まだ空いている座標があるか boolean existempty = false; //黒い駒の数集計用 int cnt_black = 0; //白い駒の数集計用 int cnt_white = 0; for(int i=0; i<9; i++) { for(int j=0; j<9; j++) { System.out.print("|" + board[j][i]); if (board[j][i].equals(EMPTY)) { existempty = true; } else if (board[j][i].equals(black)) { cnt_black++; } else if (board[j][i].equals(white)) { cnt_white++; } //7列目記載の際に、最後に「|」を入れて改行する if(j==8) { System.out.println("|"); } } } System.out.println(black + ":" + cnt_black); System.out.println(white + ":" + cnt_white); System.out.println("――――――――――――――"); if (existempty) { System.out.println(stone + "のターンです"); } else { System.out.println(stone + "ゲーム終了!"); game = false; } } static public void setStone(int x, int y) { // 駒を配置できる場合+ if (board[y][x].equals(EMPTY)) { board[y][x] = stone; // ひっくり返す処理 turnStone(x, y); // 次うつ駒の設定 String next_rev_storn = stone; stone = rev_stone; rev_stone = next_rev_storn; // オセロ版の描写 showBoard(); } else { // 既に駒がおいてある位置を指定した場合 System.out.println("その位置に駒はおけません"); } } static public void turnStone(int x, int y) { // 8方向の駒の配置を確認し、ひっくり返すメソッド 関係ないと思われるので省略 } } 実行するクラス import java.util.Scanner; public class Othello { public static void main(String[] args){ Scanner s = new Scanner(System.in); Board.boardInitValue(); Board.boardSetup(); Board.showBoard(); System.out.print("駒をおくx座標を入力してください:"); int x = s.nextInt(); System.out.print("駒をおくy座標を入力してください:"); int y = s.nextInt(); Board.setStone(x, y); } } ```### 前提・実現したいこと javaでオセロを作りたいです。 ここに質問の内容を詳しく書いてください。 javaで二次元配列でコンソール上にオセロを作りたいのですが、駒が入力した座標と逆になり、上手くいきません!クラスは2つ作りました。具体的には 標準入力でXに6,yに5を代入し、その二次元配列の芭蕉に駒を置くと、yが6,xが5の場所に置かれてしまいます。ちなみに、解決策として、privateをx,yに記述し、宣言したところ治ったのですが、どうしてなのかわかりません。何が起こっているのか、ご教授お願いいたします。 ### 該当のソースコード static public void setStone(int x, int y) { // 版外の座標を指定した場合 if (x > 7 || y > 7) { System.out.println("その位置に駒はおけません"); } // 駒を配置できる場合+ if (board[y][x].equals(EMPTY)) { board[y][x] = stone; // ひっくり返す処理 turnStone(x, y); // 次うつ駒の設定 String next_rev_storn = stone; stone = rev_stone; rev_stone = next_rev_storn; // オセロ版の描写 showBoard(); } else { // 既に駒がおいてある位置を指定した場合 System.out.println("その位置に駒はおけません"); } } } } ここにより詳細な情報を記載してください。
このままではコードが読みづらいので、質問を編集し、<code>ボタンを押し、出てくる’’’の枠の中にコードを貼り付けてください
ご指摘ありがとうございます。全体のコードを記載しようと思いましたが、文字数制限で記載しきれませんでした。念のため、省略した箇所のコードも記載します。
static public void turnStone(int x, int y) {
// 8方向の駒の配置を確認し、ひっくり返す
turnLeftUp(x, y);
turnUp(x, y);
turnRightUp(x, y);
turnLeft(x, y);
turnRight(x, y);
turnLeftDown(x, y);
turnDown(x, y);
turnRightDown(x, y);
}
static public void turnLeftUp(int x, int y) {
if (y > 1 && x > 1) {
// となりの駒
String next = board[y - 1][x - 1];
// となりの駒が裏駒の場合
if (next.equals(rev_stone)) {
// さらにその一つとなりから順に確認
for (int i = 2; true; i++) {
if (x - i < 0 || y - i < 0 || board[y - i][x - i].equals(EMPTY)) {
// 駒がない場合終了
break;
} else if (board[y - i][x - i].equals(stone)) {
// 自駒の場合
// あいだの駒をすべて自駒にひっくりかえす
for (int t = 1; t < i; t++) {
// 配列の要素を上書き
board[y - t][x - t] = stone;
}
break;
}
}
}
}
}
static public void turnUp(int x, int y) {
if (y > 1) {
// となりの駒
String next = board[y - 1][x];
// となりの駒が裏駒の場合
if (next.equals(rev_stone)) {
// さらにその一つとなりから順に確認
for (int i = 2; true; i++) {
if (y - i < 0 || board[y - i][x].equals(EMPTY)) {
// 駒がない場合終了
break;
} else if (board[y - i][x].equals(stone)) {
// 自駒の場合
// あいだの駒をすべて自駒にひっくりかえす
for (int t = 1; t < i; t++) {
// 配列の要素を上書き
board[y - t][x] = stone;
}
break;
}
}
}
}
}
static public void turnRightUp(int x, int y) {
if (y > 1 && x < 7) {
// となりの駒
String next = board[y - 1][x + 1];
// となりの駒が裏駒の場合
if (next.equals(rev_stone)) {
// さらにその一つとなりから順に確認
for (int i = 2; true; i++) {
if (x + i > 8 || y - i < 0 || board[y - i][x + i].equals(EMPTY)) {
// 駒がない場合終了
break;
} else if (board[y - i][x + i].equals(stone)) {
// 自駒の場合
// あいだの駒をすべて自駒にひっくりかえす
for (int t = 1; t < i; t++) {
// 配列の要素を上書き
board[y - t][x + t] = stone;
}
break;
}
}
}
}
}
static public void turnDown(int x, int y) {
if (y < 7) {
// となりの駒
String next = board[y + 1][x];
// となりの駒が裏駒の場合
if (next.equals(rev_stone)) {
// さらにその一つとなりから順に確認
for (int i = 2; true; i++) {
if (y + i > 8 || board[y + i][x].equals(EMPTY)) {
// 駒がない場合終了
break;
} else if (board[y + i][x].equals(stone)) {
// 自駒の場合
// あいだの駒をすべて自駒にひっくりかえす
for (int t = 1; t < i; t++) {
// 配列の要素を上書き
board[y + t][x] = stone;
}
break;
}
}
}
}
}
static public void turnRight(int x, int y) {
if (x < 7) {
// となりの駒
String next = board[y][x + 1];
// となりの駒が裏駒の場合
if (next.equals(rev_stone)) {
// さらにその一つとなりから順に確認
for (int i = 2; true; i++) {
if (x + i > 8|| board[y][x + i].equals(EMPTY)) {
// 駒がない場合終了
break;
} else if (board[y][x + i].equals(stone)) {
// 自駒の場合
// あいだの駒をすべて自駒にひっくりかえす
for (int t = 1; t < i; t++) {
// 配列の要素を上書き
board[y][x + t] = stone;
}
break;
}
}
}
}
}
static public void turnLeftDown(int x, int y) {
if (y < 7 && x > 1) {
// となりの駒
String next = board[y + 1][x - 1];
// となりの駒が裏駒の場合
if (next.equals(rev_stone)) {
// さらにその一つとなりから順に確認
for (int i = 2; true; i++) {
if (x - i < 0 || y + i > 8 || board[y + i][x - i].equals(EMPTY)) {
// 駒がない場合終了
break;
} else if (board[y + i][x - i].equals(stone)) {
// 自駒の場合
// あいだの駒をすべて自駒にひっくりかえす
for (int t = 1; t < i; t++) {
// 配列の要素を上書き
board[y + t][x - t] = stone;
}
break;
}
}
}
}
}
static public void turnLeft(int x, int y) {
if (x > 1) {
// となりの駒
String next = board[y][x - 1];
// となりの駒が裏駒の場合
if (next.equals(rev_stone)) {
// さらにその一つとなりから順に確認
for (int i = 2; true; i++) {
if (x - i < 0 || board[y][x - i].equals(EMPTY)) {
// 駒がない場合終了
break;
} else if (board[y][x - i].equals(stone)) {
// 自駒の場合
// あいだの駒をすべて自駒にひっくりかえす
for (int t = 1; t < i; t++) {
// 配列の要素を上書き
board[y][x - t] = stone;
}
break;
}
}
}
}
}
static public void turnRightDown(int x, int y) {
if (y < 7 && x < 7) {
// となりの駒
String next = board[y + 1][x + 1];
// となりの駒が裏駒の場合
if (next.equals(rev_stone)) {
// さらにその一つとなりから順に確認
for (int i = 2; true; i++) {
if (x + i > 8 || y + i > 8|| board[y + i][x + i].equals(EMPTY)) {
// 駒がない場合終了
break;
} else if (board[y + i][x + i].equals(stone)) {
// 自駒の場合
// あいだの駒をすべて自駒にひっくりかえす
for (int t = 1; t < i; t++) {
// 配列の要素を上書き
board[y + t][x + t] = stone;
}
break;
}
}
}
}
}
インデントをスペース2文字にしたり、無駄な空行を無くしたりすると結構文字数が減らせるのでは無いでしょうか。
また、はっきり申し上げてコメントを付け過ぎです。
「// 配列の要素を上書き」というコメントなど意味がありません。
やたら public static があるのも、クラス設計が出来ていないためと思われます。
ソースが張られていないので分かりませんが、
showBoard() で x と y を逆にして出力している、とかではないですか。
あるいは、setStone(int x, int y) に渡す x と y が逆になっているとか。
> ソースが張られていない
失礼しました。当方で、張られていたのを見落としていただけでした。
やはり、showBoard() の実装に違和感を覚えます。よく見直されてみてはいかがでしょうか。
こういう場合は全てのコードが無くても問題は再現すると思います。最低限のコードにして、閲覧者に配慮するようにしましょう。
了解いたしました。もう一度、コードを見直し質問し直します。ご迷惑おかけして申し訳ありません。
回答1件
あなたの回答
tips
プレビュー