列で勝つことができるように、コネクト4ゲームにこの方法があります。ただし、インデックス配列が範囲外の例外 -1 を取得します。通過する配列のサイズは 8x8 です (private int [][] values = new int [8][8];)。
どこで間違ったのですか?
public int winInAColumn(){
int sum; //set sum as an integer
for(int j=0;j<8;j++){ //loop through the rows
for(int i=4;i>-1;i--){ //loop through columns. I must equal 4 as there must be three disks next to
sum = 0; //set sum to 0
for(int k=i;k>i-4;k--){ //loop backwards through k while k is bigger than i minus 4
sum+=values[k][j]; //sum + sum = the value in i and j
}
if(Math.abs(sum)==4){
if(sum/4 == 1){
if(JOptionPane.showConfirmDialog(null, "Blue Has Won", "Game over", JOptionPane.OK_CANCEL_OPTION) == 0){
//
}
else
{
System.exit(0);
}
}
else if (sum/4 == -1){
if(JOptionPane.showConfirmDialog(null, "Blue Has Won", "Game over", JOptionPane.OK_CANCEL_OPTION) == 0)
{
//
}
else
{
System.exit(0);
}
}
}
}
}
return 0;
//JOptionPane.showMessageDialog(null, "No one won this time.");
}