我正在制作一个井字棋游戏,到目前为止一切正常,但它没有占用我的坐标,我需要将给我的图标放在我在黑板上写的地方。这是我的代码:
主要课程:
package com.mycompany.tictactoe;
import java.util.Scanner;
public class Tictactoe {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Player player1 = new Player();
Player player2 = new Player();
player1.icon = 'X';
player1.PlayerInfo();
player2.icon = 'O';
player2.PlayerInfo();
Board board = new Board();
boolean GameOver = false;
while (!GameOver) {
board.PrintBoard(board.board);
Player currentPlayer = (board.Row + board.Col) % 2 == 0 ? player1 : player2;
System.out.print("Player " + currentPlayer.name + " enter row value as " + currentPlayer.icon + ": ");
int row = scanner.nextInt();
System.out.print("Player " + currentPlayer.name + " enter col value as " + currentPlayer.icon + ": ");
int col = scanner.nextInt();
if (row >= 0 && row < 3 && col >= 0 && col < 3) {
if (board.board[row][col] == ' ') {
board.board[row][col] = currentPlayer.icon;
GameOver = Board.HaveWon(board.board, currentPlayer);
if (GameOver) {
System.out.println("Player " + currentPlayer.name + " has won");
}
} else {
System.out.println("Invalid move.");
}
} else {
System.out.println("Invalid input, row and column values must be between 0 and 2.");
}
}
}
}
玩家等级:
package com.mycompany.tictactoe;
import java.util.Scanner;
public class Player {
Scanner scanner = new Scanner(System.in);
char icon;
String name;
public void PlayerInfo() {
System.out.print("Enter player's name: ");
name = scanner.nextLine();
}
}
板类:
package com.mycompany.tictactoe;
public class Board {
int Row = 0;
int Col = 0;
char[][] board = new char[3][3];
public void PrintBoard(char[][] board) {
for (int row = 0; row < board.length; row++) {
for (int col = 0; col < board[row].length; col++) {
System.out.print(board[row][col] + " | ");
}
System.out.println();
if (row < board.length - 1) {
System.out.println("---------");
}
}
}
public static boolean HaveWon(char[][] board, Player player) {
//Check the rows
for (int Row = 0; Row < board.length; Row++){
if(board[Row][0] == player.icon && board[Row][1] == player.icon && board[Row][2] == player.icon){
return true;
}
}
//Check for Colums
for (int Col = 0; Col < board[0].length; Col++){
if(board[0][Col] == player.icon && board[1][Col] == player.icon && board[2][Col] == player.icon){
return true;
}
}
//Check diagonaly
if(board[0][0] == player.icon && board[1][1] == player.icon && board[2][2] == player.icon){
return true;
}
if(board[0][2] == player.icon && board[1][1] == player.icon && board[2][0] == player.icon){
return true;
}
return false;
}
}
如果您对如何使我的代码更好有任何其他建议,我将非常感激:)
“...我正在制作井字游戏,到目前为止一切正常,但它没有占用我的坐标...”
原因是因为board数组需要用空格字符初始化。
您可以在 main 循环中检查它。
if (board.board[row][col] == ' ')
我在这里使用了 Arrays#fill 方法,以避免编写另一个循环。
我将它添加到默认构造函数中。从技术上讲,您也可以使用 初始化块。
Board() {
for (char[] row : board) Arrays.fill(row, ' ');
}
“...如果您对如何使我的代码更好有任何其他建议,我将非常感激:)”
由于 board 只是一个 3 x 3,所以我不会使用任何循环。
@Override
public String toString() {
return
" %s | %s | %s %n".formatted(board[0][0], board[0][1], board[0][2])
+ "%s%n".formatted("-".repeat(11))
+ " %s | %s | %s %n".formatted(board[1][0], board[1][1], board[1][2])
+ "%s%n".formatted("-".repeat(11))
+ " %s | %s | %s".formatted(board[2][0], board[2][1], board[2][2]);
}