在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Java/ 五子棋java程序,輸入坐標即可下棋,但是出現(xiàn)了問題無法編譯。

五子棋java程序,輸入坐標即可下棋,但是出現(xiàn)了問題無法編譯。

import java.util.Scanner;

public class FIve {

public static void main(String[] args) {
    new FIve().run();
}
public void run() {
    Scanner scanner = new Scanner(System.in);
    System.out.println("請輸入黑棋昵稱");
    String nameBlack = scanner.nextLine();
    System.out.println("請輸入白棋昵稱");
    String nameWhite = scanner.nextLine();

    int size = 15;
    int[][] table = new int[size][size];

    printTable(table, size);

    int win = 0;
    int currentPlayer = 0;
    do {
        //  下棋
        String playerName = currentPlayer == 0 ? nameBlack : nameWhite;
        System.out.println("該" + playerName + "下棋");

        int x, y;
        boolean canPlace = true;
        do{
            if (!canPlace){
                System.out.println("這個位置不能放棋子,請重新輸入");
            }
            String code = scanner.nextLine();
            y = code.charAt(0) - 'A';
            x = Integer.valueOf(code.substring(1)) - 1;

            canPlace = true;
            canPlace = canPlace && x >= 0 && x < size;
            canPlace = canPlace && y >= 0 && y < size;
            canPlace = canPlace && table[y][x] == 0;
        }while (!canPlace);

        table[y][x] = currentPlayer == 0 ? 1 : 2;
        currentPlayer++;
        currentPlayer %= 2;
        //  打印棋盤
        printTable(table, size);
        //  檢測輸贏
        win = check(table, x, y );
    } while (win == 0) ;

    if (win == 1) {
        System.out.println(nameBlack + "勝利");
    } else if (win == 2) {
        System.out.println(nameWhite + "勝利");
    } else if (win == 3) {
        System.out.println("平局");
    }
}


public void printTable(int[][] table, int size) {
    System.out.print("   ");
    for (int i = 0; i < size; i++) {
        int value = i + 1;
        System.out.print(value < 10 ? value + "  " : value + " ");
    }
    System.out.println();

    for (int i = 0; i < size; i++) {
        char title = (char) ('A' + i);
        System.out.print(title + "   ");
        for (int j = 0; j < size; j++) {
            int value = table[i][j];
            char c = ' ';
            switch (value) {
                case 0: c = '.'; break;
                case 1: c = 'x'; break;
                case 2: c = 'o'; break;
            }
            System.out.print(c + "  ");
        }
        System.out.println();
    }
}
public int check(int[][] table, int x, int y){
boolean test = false;
test = test || moreThaFive(table, x, y, 0, -1);
test = test || moreThaFive(table, x, y, 1, -1);
test = test || moreThaFive(table, x, y, 1, 0);
test = test || moreThaFive(table, x, y, 1, 1);

if (test){
    return table[y][x];
}
return 0;
}

public boolean moreThaFive(int[][] table, int x, int y, int dx, int dy){
    int count = 0;
    count += count(table, x, y, dx, dy);
    count += count(table, x, y, -dx, -dy);
    count -= 1;
    return count >= 5;
}

public int count(int[][] table, int originX, int originY, int dx, int dy){
    int originValue = table[originY][originX];
    int count = 0;
    int x = originX;
    int y = originY;

    int value;
    do {
        count ++;
        originX += dx;
        originY += dy;
        value = table[originY][originX];
    } while (value == originValue);
    return count;
}

}
當黑棋輸入第一個棋子時,就會出現(xiàn)錯誤警告,然后程序就會莫名結束。
圖片描述
圖片描述

回答
編輯回答
初念

數(shù)組下標越位,去到提示的行數(shù)找找原因

2017年10月27日 13:41
編輯回答
夢一場

數(shù)組下標越界,在出錯的地方打個斷點調(diào)試

2017年12月20日 10:38
編輯回答
愿如初

數(shù)組越界,ArrayIndexOutOfBoundException,指向117行,原因為originY值為-1超出了數(shù)組下標0~array.length-1的范圍

五子棋代碼如下 :
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class FiveInARow extends JFrame implements MouseListener {

Vector v = new Vector();    //所有的每步走棋信息
Vector white = new Vector(); //白方走棋信息
Vector black = new Vector(); //黑方走棋信息
boolean b; //用來判斷白旗還是黑棋
int whiteCount, blackCount; //計算悔棋步數(shù)
int w = 25; //間距大小
int px = 100, py = 100; //棋盤的大小
int pxw = px + w, pyw = py + w;
int width = w * 16, height = w * 16;
int vline = width + px; //垂直線的長度
int hline = height + py; //水平線的長度

/**
 * 構造方法
 */
public FiveInARow() {
    super("五子棋");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//關閉按鈕
    Container con = this.getContentPane();
    con.setLayout(new BorderLayout());
    this.addMouseListener(this);//添加監(jiān)聽
    this.setSize(600, 600);//設置窗體大小
    this.setBackground(Color.orange);
    this.setVisible(true);
}

/**
 * 畫棋盤和棋子
 * @param e
 */
public void paint(Graphics g) {
    g.clearRect(0, 0, this.getWidth(), this.getHeight());//清除畫板
    g.setColor(Color.BLACK);//繪制網(wǎng)格顏色
    g.drawRect(px, py, width, height);//網(wǎng)格大小
    g.drawString("單機版五子棋小游戲,右擊可以悔棋,歡迎使用", 180, 70);

    for (int i=0; i<15; i++) {
        g.drawLine(pxw+i*w, py, pxw+i*w, hline);//每條橫線和豎線
        g.drawLine(px, pyw+i*w, vline, pyw+i*w);
    }

    for (int x=0; x<v.size(); x++) {
        String str = (String)v.get(x);
        String tmp[] = str.split("-");
        int a = Integer.parseInt(tmp[0]);
        int b = Integer.parseInt(tmp[1]);
        a = a * w + px;
        b = b * w + py;
        if (x%2 == 0) {
            g.setColor(Color.WHITE);
        } else {
            g.setColor(Color.BLACK);
        }
        g.fillArc(a-w/2, b-w/2, w, w, 0, 360);
    }
}

@Override
public void mouseClicked(MouseEvent e) {
    if (e.getButton() == e.BUTTON1) {
        int x = e.getX();
        int y = e.getY();
        x = (x - x % w) + (x % w > w / 2 ? w : 0);
        y = (y - y % w) + (y % w > w / 2 ? w : 0);
        x = (x - px) / w;
        y = (y - py) / w;

        if (x >= 0 && y >= 0 && x <= 16 && y <= 16) {
            if (v.contains(x+"-"+y)) {
                System.out.println("已經(jīng)有棋了!");
            } else {
                v.add(x+"-"+y);
                this.repaint();
                if (v.size() % 2 == 0) {
                    black.add(x+"-"+y);
                    this.victory(x, y, black);

// System.out.println("黑棋");

                } else {
                    white.add(x+"-"+y);
                    this.victory(x, y, white);

// System.out.println("白棋");

                }

// System.out.println(e.getX()+"-"+e.getY());

            }
        } else {

// System.out.println(e.getX()+"-"+e.getY()+"|"+x+"-"+y+"t超出邊界了");

        }
    }

    if (e.getButton() == e.BUTTON3) {   //右擊悔棋的方法

// System.out.println("鼠標右擊--悔棋");

        if (v.isEmpty()) {
            JOptionPane.showMessageDialog(this, "沒有棋可悔");
        } else {
            if (v.size() % 2 == 0) {    //判斷是白棋悔棋,還是黑棋悔棋
                blackCount++;
                if (blackCount > 3) {
                    JOptionPane.showMessageDialog(this, "黑棋已經(jīng)悔了3步");
                } else {
                    v.remove(v.lastElement());
                    this.repaint();
                }
            } else {
                whiteCount++;
                if (whiteCount > 3) {
                    JOptionPane.showMessageDialog(this, "白棋已經(jīng)悔了3步");
                } else {
                    v.remove(v.lastElement());
                    this.repaint();
                }
            }
        }
    }
}

/**
 * 判斷勝利的方法
 * @param x
 * @param y
 * @param contain
 */
private void victory(int x, int y, Vector contain) {
    int cv = 0; //垂直方向棋子數(shù)量
    int ch = 0; //水平方向棋子數(shù)量
    int ci1 = 0; //斜面方向棋子數(shù)量1
    int ci2 = 0; //斜面方向棋子數(shù)量2

    //計算水平方向棋子數(shù)量
    for (int i=1; i<5; i++) {
        if (contain.contains((x+i)+"-"+y)) {
            ch++;
        } else {
            break;
        }
    }
    for (int i=1; i<5; i++) {
        if (contain.contains((x-i)+"-"+y)) {
            ch++;
        } else {
            break;
        }
    }

    //計算垂直方向棋子數(shù)量
    for (int i=1; i<5; i++) {
        if (contain.contains(x+"-"+(y+i))) {
            cv++;
        } else {
            break;
        }
    }
    for (int i=1; i<5; i++) {
        if (contain.contains(x+"-"+(y-i))) {
            cv++;
        } else {
            break;
        }
    }

    //計算45°斜面方向棋子數(shù)量
    for (int i=1; i<5; i++) {
        if (contain.contains((x+i)+"-"+(y+i))) {
            ci1++;
        } else {
            break;
        }
    }
    for (int i=1; i<5; i++) {
        if (contain.contains((x-i)+"-"+(y-i))) {
            ci1++;
        } else {
            break;
        }
    }

    //計算135°斜面方向棋子數(shù)量
    for (int i=1; i<5; i++) {
        if (contain.contains((x+i)+"-"+(y-i))) {
            ci2++;
        } else {
            break;
        }
    }
    for (int i=1; i<5; i++) {
        if (contain.contains((x-i)+"-"+(y+i))) {
            ci2++;
        } else {
            break;
        }
    }

    if (ch>=4 || cv>=4 ||ci1>=4 ||ci2>=4) {
        System.out.println(v.size()+"步棋");
        if (v.size() % 2 == 0) {
            //判斷是黑棋贏,還是白棋贏
            JOptionPane.showMessageDialog(null, "黑棋贏了");
        } else {
            JOptionPane.showMessageDialog(null, "白棋贏了");
        }
        this.v.clear();
        this.black.clear();
        this.white.clear();
        this.repaint();
    }
}

@Override
public void mouseEntered(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent e) {
    // TODO Auto-generated method stub

}

}

2017年8月27日 20:48
編輯回答
安若晴

首先報錯信息為ArrayIndexOutOfBoundException,即數(shù)組下標越界。
錯誤首次出現(xiàn)指向117行,原因為originY值為-1超出了數(shù)組下標0~array.length-1的范圍,導致報錯。

希望對你有幫助^_^

2017年1月16日 13:48