文字数のため一部省略しますがオセロの基本的な動作は出来たのですが、置けないときにパスをしたいのですが一度パスをするとturnPass()が永遠にループされてしまいます。
paintComponentに一部処理を書いてマウスクリック時に動作を起こすような書き方をしているのですが、『ターンが入れ替わった時』とかにturnPassを実行したい場合はどのように書けばいいのでしょうか。
public class Reversi extends JPanel { final int WIDTH = 500; // ウィンドウの横幅 final int HEIGHT = 560; // ウィンドウの縦幅 int wm = 50; // 左右の余白 int hm = 80; // 上下の余白 int square = 50; // 1マスの大きさ static String str = "黒の番です。"; static String sAssist = "ON/OFF"; static int turn = 1; // 手番(1:黒,2:白) static JLabel lturn = new JLabel(str); static JButton bAssist = new JButton(sAssist); static boolean pass = false; static boolean assist = false; static int ban[][] = { // 盤面の状態 【0:配置なし,1:黒,2:白】 { 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 2, 1, 0, 0, 0 }, { 0, 0, 0, 1, 2, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0 } }; static boolean putStone[][] = { // 盤面に石が置けるか(初期値はすべてfalse)【true:可,false:不可】 { true, true, true, true, true, true, true, true }, { true, true, true, true, true, true, true, true }, { true, true, true, true, true, true, true, true }, { true, true, true, true, true, true, true, true }, { true, true, true, true, true, true, true, true }, { true, true, true, true, true, true, true, true }, { true, true, true, true, true, true, true, true }, { true, true, true, true, true, true, true, true } }; // コンストラクタ(初期化処理) public Reversi() { setPreferredSize(new Dimension(WIDTH, HEIGHT)); addMouseListener(new mouse()); lturn.setForeground(Color.white); this.add(lturn); bAssist.addActionListener(new ButtonClickAction()); this.add(bAssist); } // 画面描画 public void paintComponent(Graphics g) { // 背景 g.setColor(Color.gray); g.fillRect(0, 0, WIDTH, HEIGHT); // 盤面 g.setColor(new Color(0, 170, 0)); g.fillRect(wm, hm, square * 8, square * 8); // 横線 g.setColor(Color.black); for (int y = hm; y <= hm + square * 8; y = y + square) { g.drawLine(wm, y, wm + square * 8, y); } // 縦線 for (int x = wm; x <= wm + square * 8; x = x + square) { g.drawLine(x, hm, x, hm + square * 8); } // 小さい● g.fillOval(wm - 3 + square * 2, hm - 3 + square * 2, 6, 6); g.fillOval(wm - 3 + square * 6, hm - 3 + square * 2, 6, 6); g.fillOval(wm - 3 + square * 2, hm - 3 + square * 6, 6, 6); g.fillOval(wm - 3 + square * 6, hm - 3 + square * 6, 6, 6); // 盤面の石 for (int i = 0; i < ban.length; i++) { for (int j = 0; j < ban.length; j++) { // ban[][]の中身が0以外の場合 if (ban[i][j] != 0) { if (ban[i][j] == 1) { // 1だった場合 g.setColor(Color.black); } else if (ban[i][j] == 2) { // 2だった場合 g.setColor(Color.WHITE); } g.fillOval(wm + 10 + square * i, hm + 10 + square * j, 30, 30); // マス目に石を描画する } } } // 盤面の置ける場所を判定 putStoneJudge(); turnPass(); // 置ける場所に丸を表示 if (assist) { for (int i = 0; i < putStone.length; i++) { for (int j = 0; j < putStone.length; j++) { // putStone[][]の中身がtrueだった場合 if (putStone[i][j]) { if (turn == 1) { // 黒の番だった場合 g.setColor(Color.black); } else if (turn == 2) { // 白の番だった場合 g.setColor(Color.WHITE); } g.drawOval(wm + 10 + square * i, hm + 10 + square * j, 30, 30); // マス目に石を描画する } } } } // cpu(); } class mouse extends MouseAdapter { public void mouseClicked(MouseEvent e) { // クリックされた場所を判定 loop: for (int i = 0; i < ban.length; i++) { // 横列の判定 if ((e.getX() - wm) / square == i) { for (int j = 0; j < ban.length; j++) { // 縦列の判定 if ((e.getY() - hm) / square == j) { // マスに何も置かれてない状態ならば putStone(i, j); break loop; } } } } } } // 石が置けなければ相手にパスをする public static void turnPass() { loop:for(int i = 0; i < putStone.length; i++){ for(int j = 0; j < putStone.length; j++){ if(putStone[i][j]){ break loop; }else if(i == putStone.length - 1 && j == putStone.length - 1){ System.out.println("パス"); pass = true; changeTurn(); System.out.println(str); } } } } //ターンを交代させる public static void changeTurn(){ if (turn == 1) { turn = 2; str = "白の番です。"; } else { turn = 1; str = "黒の番です。"; } lturn.setText(str); }
changeTurnの内容を質問文に追記していただけませんか。あと質問文のコードはcodeタグで編集してくださいな。
回答1件
あなたの回答
tips
プレビュー