我正在做連接4程序,我遇到了檢查獲勝者的問題。我知道我需要做什麼。尋找垂直贏家的'for'循環,水平贏家的循環以及對角贏家的循環。我想不出如何讓它尋找一個勝利者。如何檢查連接4與循環中的贏家
這是我迄今爲止
import javax.swing.JOptionPane;
public class connect4
{
public static void main(String [] args)//[row][column]
{
String grid[][] = new String[7][8];
for(int x = 1; x <= 6; x++)
{
for(int y = 1; y <= 7; y++)
{
grid[x][y] = " ";
}
}
String p1 = JOptionPane.showInputDialog("Player 1 pick a single letter for your piece.");
String p2 = JOptionPane.showInputDialog("Player 2 pick a single letter for your piece.");
board(grid);
playerinterface(grid, p1, p2);
}
public static void board(String grid[][])
{
String board =
(" 1 2 3 4 5 6 7" + "\n" +
"| "+grid[1][1]+" | "+grid[1][2]+" | "+grid[1][3]+" | "+grid[1][4]+" | "+grid[1][5]+" | "+grid[1][6]+" | "+grid[1][7]+" | " +"\n" +
"| "+grid[2][1]+" | "+grid[2][2]+" | "+grid[2][3]+" | "+grid[2][4]+" | "+grid[2][5]+" | "+grid[2][6]+" | "+grid[2][7]+" | " +"\n" +
"| "+grid[3][1]+" | "+grid[3][2]+" | "+grid[3][3]+" | "+grid[3][4]+" | "+grid[3][5]+" | "+grid[3][6]+" | "+grid[3][7]+" | " +"\n" +
"| "+grid[4][1]+" | "+grid[4][2]+" | "+grid[4][3]+" | "+grid[4][4]+" | "+grid[4][5]+" | "+grid[4][6]+" | "+grid[4][7]+" | " +"\n" +
"| "+grid[5][1]+" | "+grid[5][2]+" | "+grid[5][3]+" | "+grid[5][4]+" | "+grid[5][5]+" | "+grid[5][6]+" | "+grid[5][7]+" | " +"\n" +
"| "+grid[6][1]+" | "+grid[6][2]+" | "+grid[6][3]+" | "+grid[6][4]+" | "+grid[6][5]+" | "+grid[6][6]+" | "+grid[6][7]+" | "
);
JOptionPane.showMessageDialog(null, board);
}
public static void playerinterface(String grid[][], String p1, String p2)
{
boolean done = false;
while(!done)
{
int player = 1;
if(player%2 != 0)//player1
{
player++;
String q = JOptionPane.showInputDialog(" PLAYER 1"+"\n"+"Put in the number of the column that"+"\n"+"you woulf like to drop your piece in.");
int choice1 = Integer.parseInt(q);
}
else//player2
{
player++;
String q2 = JOptionPane.showInputDialog(" PLAYER 2"+"\n"+"Put in the number of the column that"+"\n"+"you woulf like to drop your piece in.");
int choice2 = Integer.parseInt(q2);
}
}
}
public static void drop(int choice1, int choice2, int player)
{
while()
{
}
}
public static void check()
{
for(int x = 1; x <= 6; x++)//rows
{
}
}
}
你是不是指'心理'? –
哈哈大聲笑雅我剛剛取得了我正在聽的歌曲的名稱,我想我拼錯了它。哎呀哈哈 –