gpt4 book ai didi

java - 有人可以帮助我解决我的 Gomoku 程序中的获胜场景吗?

转载 作者:行者123 更新时间:2023-11-30 10:06:13 32 4
gpt4 key购买 nike

我为一项作业编写了一个程序,我们必须编写一个简单的五子棋程序。我以为我拥有了一切,但是当我编译并运行时,即使我只有 4 个,即使它们彼此不相邻,它也会引发胜利场景。 (只有当连续出现五个相同的……X 或 O 时,它才应该引发一场胜利)。我觉得我应该在每次转弯前将我的计数器重置为 0,但我不确定我应该在哪里这样做。任何提示将不胜感激!

import java.util.Scanner;

public class Gomoku1
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);

char[][] map = new char [19][19];

int row = 0;
int column = 0;

//fill game with dots
for (int i = 0; i < map.length; i++)
{
for (int j = 0; j < map[i].length; j++)
{
map[i][j] = '.';
}
}

printMap(map);

char player1Choice = 'X';
char player2Choice = 'O';

int [] place;
while (true)
{

System.out.println("Player 1's turn!");
place = userTurn(map, player1Choice);

if (isValidMove(map, place[0], place[1]) == false)
{
System.out.println("Invalid move! Try again!");
place = userTurn(map, player1Choice);
}
if (isValidMove(map, place[0], place[1])) {
map[place[0]][place[1]] = player1Choice;
printMap(map);
}
if (isBoardFull(map) == true)
{
System.out.println("Board is full. Tied game.");
break;
}

if (hasPlayerWon(map, player1Choice) == true)
{
System.out.println("Player 1 Wins!");
break;
}

else
{

System.out.println("Player 2's turn!: ");
place = userTurn(map, player2Choice);

//System.out.println(isValidMove(map, row, column));

if (isValidMove(map, place[0], place[1]) == false)
{
System.out.println("Invalid move! Try again!");
place = userTurn(map, player2Choice);
}
if (isValidMove(map, place[0], place[1])) {
map[place[0]][place[1]] = player2Choice;
printMap(map);
}
if (isBoardFull(map) == true)
{
System.out.println("Board is full. Tied game.");
break;
}
if (hasPlayerWon(map, player2Choice) == true)
{
System.out.println("Player 2 Wins!");
break;
}
}


}
}

public static void printMap (char[][] map)
{
for (int i = 0; i < map.length; i++)
{
for (int j = 0; j < map[i].length; j++)
{
System.out.printf("%2c", map[i][j]);
}
System.out.println();
}
}

public static int [] userTurn (char[][] map, char playerChoice)
{
Scanner input = new Scanner(System.in);

System.out.print("Enter row: ");
int row = input.nextInt();

System.out.print("Enter column: ");
int column = input.nextInt();

int place [] = {row, column};
return place;
}

public static boolean isValidMove (char[][] map, int row, int column)
{
//System.out.println ("n is valid move");
if (row < 0 || row > 18 || column < 0 || column > 18 || map[row][column]=='O' || map[row][column]=='X')
{
return false;
}
else
{
return true;
}
}

public static boolean isBoardFull (char[][] map)
{
int openSpots = 0;
for (int i = 0; i < map.length; i++)
{
for (int j = 0; j < map.length; j++)
{
if (!(map[i][j]=='.'))
openSpots++;
}
}
if (openSpots == 361)
{
return true;
}
return false;
}

public static boolean hasPlayerWon(char[][] map, int player)
{
if (isHorizontalWin(map, player) == true || isVerticalWin(map, player) == true || isDiagonalWin(map, player) == true)
{
return true;
}
return false;
}

public static boolean isHorizontalWin(char[][] map, int player)
{
int count = 0;

int r;
int c;



for (int i = 0; i < map.length; i++)
{
for (int j = 0; j < map.length; j++)
{
if (map[i][j]==(player))
{
r = i;
c = j;
while (r >= 0 && r <= 18 && c >= 0 && c <= 18 && map[r][c] == player)
{
count ++;
r += 0;
c += 1;
}
}
}
}
if (count == 5)
{
return true;

}
return false;


}

public static boolean isVerticalWin(char[][] map, int player)
{
int count = 0;

int r;
int c;

for (int i = 0; i < map.length; i++)
{
for (int j = 0; j < map.length; j++)
{
if (map[i][j]==(player))
{
r = i;
c = j;
while (r >= 0 && r <= 18 && c >= 0 && c <= 18 && map[r][c] == player)
{
count ++;
r += 1;
c += 0;
}
}
}
}
if (count == 5)
{
return true;

}
return false;

}
public static boolean isDiagonalWin(char[][] map, int player)
{
int count = 0;
int r;
int c;

for (int i = 0; i < map.length; i++)
{
for (int j = 0; j < map.length; j++)
{
if (map[i][j]==(player))
{
r = i;
c = j;
while (r >= 0 && r <= 18 && c >= 0 && c <= 18 && map[r][c] == player)
{
count++;
r += 1;
c += 1;
}
}
}
}
if (count == 5)
{
return true;

}
return false;


}


}

最佳答案

检查获胜条件的所有三个函数都有问题:isHorizo​​ntalWinisVerticalWinisDiagonalWin。这三个变量都增加了变量 count,但是这个变量永远不会设置回零。此外,检查 count == 5 是否应该在循环内进行。下面是一个关于如何修复 isHorizo​​ntalWin 的示例:

    public static boolean isHorizontalWin(char[][] map, int player)
{
int count = 0;

int r;
int c;

for (int i = 0; i < map.length; i++)
{
for (int j = 0; j < map.length; j++)
{
if (map[i][j]==(player))
{
r = i;
c = j;
while (r >= 0 && r <= 18 && c >= 0 && c <= 18 && map[r][c] == player)
{
count ++;
r += 0;
c += 1;
}
if (count == 5)
{
return true;
} else {
count = 0;
}
}
}
}
return false;
}

关于java - 有人可以帮助我解决我的 Gomoku 程序中的获胜场景吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54793153/

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