用java编写人机交互的井字游戏(从3*3到10*10的棋盘都能实现)
时间: 2024-03-10 15:49:21 浏览: 243
好的,我可以为您提供一个简单的井字游戏的 Java 代码。这个游戏是基于控制台的,可以实现不同大小的棋盘。
```java
import java.util.Scanner;
public class TicTacToe {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int size = 3; // 棋盘大小(默认为3)
System.out.print("输入棋盘大小(3-10之间的整数):");
size = scanner.nextInt();
char[][] board = new char[size][size]; // 棋盘
initializeBoard(board); // 初始化棋盘
char currentPlayer = 'X'; // 当前玩家
boolean isGameFinished = false; // 游戏是否结束
while (!isGameFinished) {
displayBoard(board); // 显示棋盘
System.out.print("玩家 " + currentPlayer + " 请输入行号和列号(例如:1 2):");
int row = scanner.nextInt();
int col = scanner.nextInt();
if (isValidMove(board, row, col)) { // 判断是否为有效移动
board[row][col] = currentPlayer; // 保存玩家的棋子到棋盘中
if (isGameWon(board, currentPlayer)) { // 判断游戏是否胜利
isGameFinished = true;
System.out.println("玩家 " + currentPlayer + " 获胜!");
} else if (isBoardFull(board)) { // 判断棋盘是否已满
isGameFinished = true;
System.out.println("平局!");
} else {
currentPlayer = (currentPlayer == 'X') ? 'O' : 'X'; // 切换玩家
}
} else {
System.out.println("无效移动,请重新输入!");
}
}
scanner.close();
}
// 初始化棋盘
public static void initializeBoard(char[][] board) {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
board[i][j] = '-';
}
}
}
// 显示棋盘
public static void displayBoard(char[][] board) {
System.out.print(" ");
for (int i = 0; i < board.length; i++) {
System.out.print(" " + (i + 1));
}
System.out.println();
for (int i = 0; i < board.length; i++) {
System.out.print(i + 1);
for (int j = 0; j < board[i].length; j++) {
System.out.print(" " + board[i][j]);
}
System.out.println();
}
}
// 判断是否为有效移动
public static boolean isValidMove(char[][] board, int row, int col) {
if (row < 1 || row > board.length || col < 1 || col > board.length) { // 行号和列号必须在棋盘范围内
return false;
}
if (board[row - 1][col - 1] != '-') { // 该位置已经有棋子
return false;
}
return true;
}
// 判断游戏是否胜利
public static boolean isGameWon(char[][] board, char player) {
// 判断每一行是否胜利
for (int i = 0; i < board.length; i++) {
boolean isWin = true;
for (int j = 0; j < board[i].length; j++) {
if (board[i][j] != player) {
isWin = false;
break;
}
}
if (isWin) {
return true;
}
}
// 判断每一列是否胜利
for (int i = 0; i < board.length; i++) {
boolean isWin = true;
for (int j = 0; j < board[i].length; j++) {
if (board[j][i] != player) {
isWin = false;
break;
}
}
if (isWin) {
return true;
}
}
// 判断对角线是否胜利
boolean isWin = true;
for (int i = 0; i < board.length; i++) {
if (board[i][i] != player) {
isWin = false;
break;
}
}
if (isWin) {
return true;
}
// 判断反对角线是否胜利
isWin = true;
for (int i = 0; i < board.length; i++) {
if (board[i][board.length - i - 1] != player) {
isWin = false;
break;
}
}
if (isWin) {
return true;
}
return false;
}
// 判断棋盘是否已满
public static boolean isBoardFull(char[][] board) {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (board[i][j] == '-') {
return false;
}
}
}
return true;
}
}
```
希望这个代码可以帮助您实现人机交互的井字游戏。
阅读全文