gpt4 book ai didi

java - 带有二维数组对角线检查的 Connect4 游戏

转载 作者:行者123 更新时间:2023-11-30 07:13:56 25 4
gpt4 key购买 nike

所以我对 Java 还很陌生,我有一项任务是创建一个 connect 4 游戏。我创建了一个 6 行 7 列的棋盘,里面充满了像这样的字符 -> '-',当用户输入期望列时,它会用 B 或 R(红色或黑色方格)替换 '-',不管这是什么一个背景。一切正常,除了我检查对角线的代码非常长的部分,我无法想出一种方法来遍历所有可能的 4 组对角线,玩家可以在其中获胜,除非在不同的 for 循环中执行每组对角线。 .. 我知道你能帮助我缩短它的可怕希望:(

这是对角线检查的代码:(哈哈,光看就心酸)

    public class Connect4 {
public static void main(String[] args) {

//Create board

Scanner input = new Scanner(System.in);
char[][] grid = new char[6][7];

for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
grid[i][j] = '-';
}
}

public static void checkWinner(char[][] grid) {
try{

//A LOT OF FOR LOOPS FOR DIAGONAL CHECKS
for (int i = 5; i > 1; i-- ) {
for(int j = 0; j < 4; j++) {
if ( grid[i][j] == 'R' &&
grid[i-1][j+1] == 'R' &&
grid[i-2][j+2] == 'R' &&
grid[i-3][j+3] == 'R') {
System.out.println("Player 1 Wins!");
System.exit(0);
}
else if ( grid[i][j] == 'B' &&
grid[i-1][j+1] == 'B' &&
grid[i-2][j+2] == 'B' &&
grid[i-3][j+3] == 'B') {
System.out.println("Player 2 Wins!");
System.exit(0);
}
}
}


for (int i = 5; i > 1; i--) {
for (int j = 1; j < 4; j++) {
if (grid[i][j] == 'R' &&
grid[i-1][j+1] == 'R' &&
grid[i-2][j+2] == 'R' &&
grid[i-3][j+3] == 'R') {
System.out.println("Player 1 Wins!");
System.exit(0);
}
else if (grid[i][j] == 'B' &&
grid[i-1][j+1] == 'B' &&
grid[i-2][j+2] == 'B' &&
grid[i-3][j+3] == 'B') {
System.out.println("Player 2 Wins!");
System.exit(0);
}
}
}

for (int i = 0; i < 4; i++) {
for (int j = 4; j < 7; j++) {
if ( grid[i][j] == 'R' &&
grid[i+1][j-1] == 'R' &&
grid[i+2][j-2] == 'R' &&
grid[i+3][j-3] == 'R') {
System.out.println("Player 1 Wins!");
System.exit(0);
}
else if (grid[i][j] == 'B' &&
grid[i+1][j-1] == 'B' &&
grid[i+2][j-2] == 'B' &&
grid[i+3][j-3] == 'B') {
System.out.println("Player 2 Wins!");
System.exit(0);
}
}
}

for (int i = 1; i < 2; i++) {
for (int j = 6; j > 5; j--) {
if ( grid[i][j] == 'R' &&
grid[i+1][j-1] == 'R' &&
grid[i+2][j-2] == 'R' &&
grid[i+3][j-3] == 'R') {
System.out.println("Player 1 Wins!");
System.exit(0);
}
else if (grid[i][j] == 'B' &&
grid[i+1][j-1] == 'B' &&
grid[i+2][j-2] == 'B' &&
grid[i+3][j-3] == 'B') {
System.out.println("Player 2 Wins!");
System.exit(0);
}
}
}

for (int i = 4; i < 5; i++){
for (int j = 2; j < 3; j++){
if (grid[i][j] == 'R' &&
grid[i-1][j+1] == 'R' &&
grid[i-2][j+2] == 'R' &&
grid[i-3][j+3] == 'R') {
System.out.println("Player 1 Wins!");
System.exit(0);
}
else if (grid[i][j] == 'B' &&
grid[i-1][j+1] == 'B' &&
grid[i-2][j+2] == 'B' &&
grid[i-3][j+3] == 'B') {
System.out.println("Player 2 Wins!");
System.exit(0);
}
}
}

for (int i = 0; i < 4; i++) {
for (int j = 3; j > 0; j--) {
if (grid[i][j] == 'R' &&
grid[i+1][j+1] == 'R' &&
grid[i+2][j+2] == 'R' &&
grid[i+3][j+3] == 'R') {
System.out.println("Player 1 Wins!");
System.exit(0);
}
else if (grid[i][j] == 'B' &&
grid[i+1][j+1] == 'B' &&
grid[i+2][j+2] == 'B' &&
grid[i+3][j+3] == 'B') {
System.out.println("Player 2 Wins!");
System.exit(0);
}
}
}

for(int i =0; i < 1; i++) {
for (int j = 0; j <1; j++) {
if (grid[i][j] == 'R' &&
grid[i+1][j+1] == 'R' &&
grid[i+2][j+2] == 'R' &&
grid[i+3][j+3] == 'R') {
System.out.println("Player 1 Wins!");
System.exit(0);
}
else if (grid[i][j] == 'B' &&
grid[i+1][j+1] == 'B' &&
grid[i+2][j+2] == 'B' &&
grid[i+3][j+3] == 'B') {
System.out.println("Player 2 Wins!");
System.exit(0);
}
}
}

for (int j = 0; j < 1; j++) {
for(int i =1 ; i < 3; i++) {
if (grid[i][j] == 'R' &&
grid[i+1][j+1] == 'R' &&
grid[i+2][j+2] == 'R' &&
grid[i+3][j+3] == 'R') {
System.out.println("Player 1 Wins!");
System.exit(0);
}
else if (grid[i][j] == 'B' &&
grid[i+1][j+1] == 'B' &&
grid[i+2][j+2] == 'B' &&
grid[i+3][j+3] == 'B') {
System.out.println("Player 2 Wins!");
System.exit(0);
}
}
}

for (int j = 3; j < 4; j++) {
for (int i = 0; i < 3; i++) {
if (grid[i][j] == 'R' &&
grid[i+1][j+1] == 'R' &&
grid[i+2][j+2] == 'R' &&
grid[i+3][j+3] == 'R') {
System.out.println("Player 1 Wins!");
System.exit(0);
}
else if (grid[i][j] == 'B' &&
grid[i+1][j+1] == 'B' &&
grid[i+2][j+2] == 'B' &&
grid[i+3][j+3] == 'B') {
System.out.println("Player 2 Wins!");
System.exit(0);
}
}
}

for (int i = 2; i < 3; i++) {
for (int j = 2; j > 0; j--) {
if (grid[i][j] == 'R' &&
grid[i+1][j+1] == 'R' &&
grid[i+2][j+2] == 'R' &&
grid[i+3][j+3] == 'R') {
System.out.println("Player 1 Wins!");
System.exit(0);
}
else if (grid[i][j] == 'B' &&
grid[i+1][j+1] == 'B' &&
grid[i+2][j+2] == 'B' &&
grid[i+3][j+3] == 'B') {
System.out.println("Player 2 Wins!");
System.exit(0);
}
}
}

for (int i = 1; i < 2; i++) {
for (int j = 2; j < 3; j++){
if (grid[i][j] == 'R' &&
grid[i+1][j+1] == 'R' &&
grid[i+2][j+2] == 'R' &&
grid[i+3][j+3] == 'R') {
System.out.println("Player 1 Wins!");
System.exit(0);
}
else if (grid[i][j] == 'B' &&
grid[i+1][j+1] == 'B' &&
grid[i+2][j+2] == 'B' &&
grid[i+3][j+3] == 'B') {
System.out.println("Player 2 Wins!");
System.exit(0);
}
}
}

for (int i = 1; i < 2; i++) {
for (int j = 1; j < 2; j++){
if (grid[i][j] == 'R' &&
grid[i+1][j+1] == 'R' &&
grid[i+2][j+2] == 'R' &&
grid[i+3][j+3] == 'R') {
System.out.println("Player 1 Wins!");
System.exit(0);
}
else if (grid[i][j] == 'B' &&
grid[i+1][j+1] == 'B' &&
grid[i+2][j+2] == 'B' &&
grid[i+3][j+3] == 'B') {
System.out.println("Player 2 Wins!");
System.exit(0);
}
}
}
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("Exception thrown :" + e);
}
}

最佳答案

与其检查棋盘上的每一个可能位,不如根据人类的游戏方式来玩游戏 - 只有当有人放下一 block 然后形成四 block 瓷砖的一行时,它才会连接 4。所以:不要检查所有可能的图 block ,使用刚刚放入的图 block ,并且只检查涉及该图 block 的行:

  • 这个新的瓷砖是否形成水平的?这意味着检查 column-3column+3 以覆盖所有可能的水平。
  • 这个新的瓷砖是否形成垂直?这意味着只检查它下面的三个图 block 。
  • 这个新瓷砖是否形成对角线?这意味着检查 {column-3, row-2} 到 {column+3, row+3}覆盖所有可能的对角线。我们甚至可以在检查水平线的同时检查它,因为它遍历相同的 column-3column+3 范围。

您已经知道图 block 的“颜色”是什么,因此您的支票(在假代码中)将简单地采用以下形式:

boolean matched = false;
int stretch = 0;
do {
checkTile = ...;
matched = checkedTile.color.equals(droppedTile.color);
if (matched) {
if (stretch == 3) playerWins()
stretch++;
}
} while(matched);

如果您发现具有相同颜色的最大一 block 瓷砖是 4,则完成。如果不是,则没有 connect-4。

也就是说,这是一项家庭作业:S.O.当您在编程时遇到问题时,我们会在这里为您服务,但我们不会在这里为您做功课。如果你遇到困难,问问你的同学甚至你的老师。互联网并不是寻求帮助的唯一场所,尤其是在您参加类(class)时。

关于java - 带有二维数组对角线检查的 Connect4 游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18899576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com