井字游戏每次都以平局结束。

huangapple go评论76阅读模式
英文:

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 实体编码(如 &lt;&gt;)替换为正常的符号。

英文:

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 &lt; 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 &lt; g.length; r++) {
for(int c = 0; c &lt; 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 &lt; g.length; c++) {
for(int r = 0; r &lt; g.length; r++) {
treE[r] = g[c][r];
}
if(elementsAreAllEqual(treE)) {
return true;
}
treE = new char[3];
}
// upward diagonal
for(int i = 0; i &lt; 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 &lt; 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(&quot;Tic-Tac-Toe\n&quot;);
int goCount = 0;
char player;
boolean draw = true;
boolean end = false;
do {
if(goCount % 2 == 0) {
player = &#39;X&#39;;
System.out.print(&quot;&#39;&quot; + player + &quot;&#39;, choose your location (row, column): &quot;);
}
else {
player = &#39;O&#39;;
System.out.print(&quot;&#39;&quot; + player + &quot;&#39;, choose your location (row, column): &quot;);
}
String position = s.nextLine();
String[] p = position.split(&quot; &quot;);
int[] pos = {Integer.parseInt(p[0]), Integer.parseInt(p[1])}; 
grid[pos[0]][pos[1]] = player;
System.out.println();
for(int y = 0; y &lt; 3; y++) {
System.out.print(&quot;\t&quot;);
for(int x = 0; x &lt; 3; x++) {
if(grid[y][x] != &#39; &#39;) {
System.out.print(grid[y][x] + &quot; &quot;);
}
else {
System.out.print(&quot;  &quot;);
}
}
System.out.println();
}
System.out.println();
if(goCount &gt; 4) {
if(some1HasWon(grid)) {
end = true;
}
else if(goCount &gt;= 8) {
draw = true;
end = true;
}
}
goCount++;
}
while(end != true);
if(draw) {
System.out.println(&quot;The game was a draw.&quot;);
}
else if(goCount % 2 == 0) {
System.out.println(&quot;Player &#39;X&#39; wins the game.&quot;);
}
else {
System.out.println(&quot;Player &#39;O&#39; wins the game.&quot;);
}
}
}

答案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 &lt; 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 &gt; 4) {
if(some1HasWon(grid)) {
end = true;
draw = false;
} else if(goCount &gt;= 8) {
draw = true;
end = true;
}
}

huangapple
  • 本文由 发表于 2020年10月3日 01:06:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/64175587.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定