英文:
Tic-Tac-Toe game results in draw every time
问题
以下是你要翻译的代码部分:
public class TicTacToe {
public static boolean elementsAreAllEqual(char[] arr) {
for(int i = 0; i < arr.length; i++) {
if(arr[i] != arr[i + 1]) {
return false;
}
}
return true;
}
public static boolean some1HasWon(char[][] g) {
char[] treE = new char[3];
// 横向直线(3个相同)
for(int r = 0; r < g.length; r++) {
for(int c = 0; c < g.length; c++) {
treE[c] = g[r][c];
}
if(elementsAreAllEqual(treE)) {
return true;
}
treE = new char[3];
}
// 纵向直线(3个相同)
for(int c = 0; c < g.length; c++) {
for(int r = 0; r < g.length; r++) {
treE[r] = g[c][r];
}
if(elementsAreAllEqual(treE)) {
return true;
}
treE = new char[3];
}
// 对角线(向上)
for(int i = 0; i < g.length; i++) {
treE[i] = g[g.length-i-1][i];
}
if(elementsAreAllEqual(treE)) {
return true;
}
treE = new char[3];
// 对角线(向下)
for(int i = 0; i < g.length; i++) {
treE[i] = g[i][i];
}
if(elementsAreAllEqual(treE)) {
return true;
}
return false;
}
public static void main(String args[]) {
char[][] grid = new char[3][3];
Scanner s = new Scanner(System.in);
System.out.println("Tic-Tac-Toe\n");
int goCount = 0;
char player;
boolean draw = true;
boolean end = false;
do {
if(goCount % 2 == 0) {
player = 'X';
System.out.print("'" + player + "', choose your location (row, column): ");
}
else {
player = 'O';
System.out.print("'" + player + "', choose your location (row, column): ");
}
String position = s.nextLine();
String[] p = position.split(" ");
int[] pos = {Integer.parseInt(p[0]), Integer.parseInt(p[1])};
grid[pos[0]][pos[1]] = player;
System.out.println();
for(int y = 0; y < 3; y++) {
System.out.print("\t");
for(int x = 0; x < 3; x++) {
if(grid[y][x] != ' ') {
System.out.print(grid[y][x] + " ");
}
else {
System.out.print(" ");
}
}
System.out.println();
}
System.out.println();
if(goCount > 4) {
if(some1HasWon(grid)) {
end = true;
}
else if(goCount >= 8) {
draw = true;
end = true;
}
}
goCount++;
}
while(end != true);
if(draw) {
System.out.println("The game was a draw.");
}
else if(goCount % 2 == 0) {
System.out.println("Player 'X' wins the game.");
}
else {
System.out.println("Player 'O' wins the game.");
}
}
}
注意:我已经将代码中的 HTML 实体编码(如 <
和 >
)替换为正常的符号。
英文:
I'd like to think the code is pretty clear. I am a beginner programmer and every thing looks fine to me. What should you do in situations like these when everything looks perfect but the program doesn't run as planned?
In short, I'm asking to advice for outlook on coding projects & a solution as to why my method I spent over 45 minutes on does nothing.
import java.util.Scanner;
public class TicTacToe {
public static boolean elementsAreAllEqual(char[] arr) {
for(int i = 0; i < arr.length; i++) {
if(arr[i] != arr[i + 1]) {
return false;
}
}
return true;
}
public static boolean some1HasWon(char[][] g) {
char[] treE = new char[3];
// horizontal straight lines (rows of 3 alike)
for(int r = 0; r < g.length; r++) {
for(int c = 0; c < g.length; c++) {
treE[c] = g[r][c];
}
if(elementsAreAllEqual(treE)) {
return true;
}
treE = new char[3];
}
// vertical straight lines (columns of 3 alike)
for(int c = 0; c < g.length; c++) {
for(int r = 0; r < g.length; r++) {
treE[r] = g[c][r];
}
if(elementsAreAllEqual(treE)) {
return true;
}
treE = new char[3];
}
// upward diagonal
for(int i = 0; i < g.length; i++) {
treE[i] = g[g.length-i-1][i];
}
if(elementsAreAllEqual(treE)) {
return true;
}
treE = new char[3];
// downward diagonal
for(int i = 0; i < g.length; i++) {
treE[i] = g[i][i];
}
if(elementsAreAllEqual(treE)) {
return true;
}
return false;
}
public static void main(String args[]) {
char[][] grid = new char[3][3];
Scanner s = new Scanner(System.in);
System.out.println("Tic-Tac-Toe\n");
int goCount = 0;
char player;
boolean draw = true;
boolean end = false;
do {
if(goCount % 2 == 0) {
player = 'X';
System.out.print("'" + player + "', choose your location (row, column): ");
}
else {
player = 'O';
System.out.print("'" + player + "', choose your location (row, column): ");
}
String position = s.nextLine();
String[] p = position.split(" ");
int[] pos = {Integer.parseInt(p[0]), Integer.parseInt(p[1])};
grid[pos[0]][pos[1]] = player;
System.out.println();
for(int y = 0; y < 3; y++) {
System.out.print("\t");
for(int x = 0; x < 3; x++) {
if(grid[y][x] != ' ') {
System.out.print(grid[y][x] + " ");
}
else {
System.out.print(" ");
}
}
System.out.println();
}
System.out.println();
if(goCount > 4) {
if(some1HasWon(grid)) {
end = true;
}
else if(goCount >= 8) {
draw = true;
end = true;
}
}
goCount++;
}
while(end != true);
if(draw) {
System.out.println("The game was a draw.");
}
else if(goCount % 2 == 0) {
System.out.println("Player 'X' wins the game.");
}
else {
System.out.println("Player 'O' wins the game.");
}
}
}
答案1
得分: 2
你的方法 `elementaAreAllEqual` 在 `i = arr.length - 1` 时访问了数组之外的元素,你应该仅迭代到 `arr.length - 1`。
```java
public static boolean elementsAreAllEqual(char[] arr) {
for(int i = 0; i < arr.length - 1; i++) {
if(arr[i] != arr[i + 1]) {
return false;
}
}
return true;
}
你对 draw = true
的初始化是每次都获得平局的原因。无论 some1HasWon
的结果如何,变量 draw
都是始终为 true
。
if(goCount > 4) {
if(some1HasWon(grid)) {
end = true;
draw = false;
} else if(goCount >= 8) {
draw = true;
end = true;
}
}
英文:
Your method elementaAreAllEqual
accesses elements outside the array when i = arr.length - 1
, you should iterate only upto arr.length - 1
.
public static boolean elementsAreAllEqual(char[] arr) {
for(int i = 0; i < arr.length - 1; i++) {
if(arr[i] != arr[i + 1]) {
return false;
}
}
return true;
}
Your initialization of draw = true
is the reason why you're getting a draw every time. Irrespective of the outcome of some1HasWon
the variable draw
is always true
.
if(goCount > 4) {
if(some1HasWon(grid)) {
end = true;
draw = false;
} else if(goCount >= 8) {
draw = true;
end = true;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论