C++ 五子棋游戏代码转换为Java实现
C++ 五子棋游戏代码转换为Java实现
本文将详细介绍将C++五子棋游戏代码转换为Java版本的步骤,并提供完整的Java代码示例。
C++ 代码
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
// 储存棋盘状态的二维数组
int chessBoard[8][8] = {
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}
};
// 判断胜负
boolean isWin(int color) {
// 判断横向连续五个棋子
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 4; j++) {
if (chessBoard[i][j] == color && chessBoard[i][j+1] == color
&& chessBoard[i][j+2] == color && chessBoard[i][j+3] == color
&& chessBoard[i][j+4] == color) {
return true;
}
}
}
// 判断纵向连续五个棋子
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 8; j++) {
if (chessBoard[i][j] == color && chessBoard[i+1][j] == color
&& chessBoard[i+2][j] == color && chessBoard[i+3][j] == color
&& chessBoard[i+4][j] == color) {
return true;
}
}
}
// 判断正斜向连续五个棋子
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (chessBoard[i][j] == color && chessBoard[i+1][j+1] == color
&& chessBoard[i+2][j+2] == color && chessBoard[i+3][j+3] == color
&& chessBoard[i+4][j+4] == color) {
return true;
}
}
}
// 判断反斜向连续五个棋子
for (int i = 0; i < 4; i++) {
for (int j = 4; j < 8; j++) {
if (chessBoard[i][j] == color && chessBoard[i+1][j-1] == color
&& chessBoard[i+2][j-2] == color && chessBoard[i+3][j-3] == color
&& chessBoard[i+4][j-4] == color) {
return true;
}
}
}
return false;
}
// 计算估价函数
int evaluate(int color, int x, int y) {
int score = 0;
int count = 0;
// 横向
for (int i = x-1; i >= 0; i--) {
if (chessBoard[y][i] == color) {
count++;
} else {
break;
}
}
for (int i = x+1; i < 8; i++) {
if (chessBoard[y][i] == color) {
count++;
} else {
break;
}
}
switch (count) {
case 1: score += 1; break;
case 2: score += 10; break;
case 3: score += 100; break;
case 4: score += 1000; break;
}
count = 0;
// 纵向
for (int i = y-1; i >= 0; i--) {
if (chessBoard[i][x] == color) {
count++;
} else {
break;
}
}
for (int i = y+1; i < 8; i++) {
if (chessBoard[i][x] == color) {
count++;
} else {
break;
}
}
switch (count) {
case 1: score += 1; break;
case 2: score += 10; break;
case 3: score += 100; break;
case 4: score += 1000; break;
}
count = 0;
// 正斜向
for (int i = x-1, j = y-1; i >= 0 && j >= 0; i--, j--) {
if (chessBoard[j][i] == color) {
count++;
} else {
break;
}
}
for (int i = x+1, j = y+1; i < 8 && j < 8; i++, j++) {
if (chessBoard[j][i] == color) {
count++;
} else {
break;
}
}
switch (count) {
case 1: score += 1; break;
case 2: score += 10; break;
case 3: score += 100; break;
case 4: score += 1000; break;
}
count = 0;
// 反斜向
for (int i = x+1, j = y-1; i < 8 && j >= 0; i++, j--) {
if (chessBoard[j][i] == color) {
count++;
} else {
break;
}
}
for (int i = x-1, j = y+1; i >= 0 && j < 8; i--, j++) {
if (chessBoard[j][i] == color) {
count++;
} else {
break;
}
}
switch (count) {
case 1: score += 1; break;
case 2: score += 10; break;
case 3: score += 100; break;
case 4: score += 1000; break;
}
return score;
}
// 人类玩家落子
void humanPlayer(int& x, int& y) {
cout << "请落子(x, y):" << endl;
cin >> x >> y;
while (chessBoard[y][x] != 0) {
cout << "该位置已有棋子,请重新落子(x, y):" << endl;
cin >> x >> y;
}
chessBoard[y][x] = 1;
}
// 电脑玩家落子
void computerPlayer(int& x, int& y) {
srand(time(NULL));
int maxScore = 0;
// 遍历棋盘,找到最高分数的位置
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (chessBoard[i][j] == 0) {
int score = evaluate(2, j, i);
if (score > maxScore) {
maxScore = score;
x = j;
y = i;
}
}
}
}
chessBoard[y][x] = 2;
}
// 打印棋盘
void printChessBoard() {
cout << " ";
for (int i = 0; i < 8; i++) {
cout << i << " ";
}
cout << endl;
for (int i = 0; i < 8; i++) {
cout << i << " ";
for (int j = 0; j < 8; j++) {
if (chessBoard[i][j] == 0) {
cout << "+ ";
} else if (chessBoard[i][j] == 1) {
cout << "● ";
} else {
cout << "○ ";
}
}
cout << endl;
}
}
int main() {
int x, y;
int turn = 1; // 1表示人类玩家,2表示电脑玩家
while (!isWin(1) && !isWin(2)) {
printChessBoard();
if (turn == 1) {
humanPlayer(x, y);
} else {
computerPlayer(x, y);
}
turn = 3 - turn; // 切换玩家
}
printChessBoard();
if (isWin(1)) {
cout << "人类玩家获胜!" << endl;
} else {
cout << "电脑玩家获胜!" << endl;
}
return 0;
}
Java 代码
import java.util.Scanner;
import java.util.Random;
public class Gobang {
// 储存棋盘状态的二维数组
static int[][] chessBoard = new int[8][8];
// 判断胜负
public static boolean isWin(int color) {
// 判断横向连续五个棋子
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 4; j++) {
if (chessBoard[i][j] == color && chessBoard[i][j+1] == color
&& chessBoard[i][j+2] == color && chessBoard[i][j+3] == color
&& chessBoard[i][j+4] == color) {
return true;
}
}
}
// 判断纵向连续五个棋子
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 8; j++) {
if (chessBoard[i][j] == color && chessBoard[i+1][j] == color
&& chessBoard[i+2][j] == color && chessBoard[i+3][j] == color
&& chessBoard[i+4][j] == color) {
return true;
}
}
}
// 判断正斜向连续五个棋子
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (chessBoard[i][j] == color && chessBoard[i+1][j+1] == color
&& chessBoard[i+2][j+2] == color && chessBoard[i+3][j+3] == color
&& chessBoard[i+4][j+4] == color) {
return true;
}
}
}
// 判断反斜向连续五个棋子
for (int i = 0; i < 4; i++) {
for (int j = 4; j < 8; j++) {
if (chessBoard[i][j] == color && chessBoard[i+1][j-1] == color
&& chessBoard[i+2][j-2] == color && chessBoard[i+3][j-3] == color
&& chessBoard[i+4][j-4] == color) {
return true;
}
}
}
return false;
}
// 计算估价函数
public static int evaluate(int color, int x, int y) {
int score = 0;
int count = 0;
// 横向
for (int i = x-1; i >= 0; i--) {
if (chessBoard[y][i] == color) {
count++;
} else {
break;
}
}
for (int i = x+1; i < 8; i++) {
if (chessBoard[y][i] == color) {
count++;
} else {
break;
}
}
switch (count) {
case 1: score += 1; break;
case 2: score += 10; break;
case 3: score += 100; break;
case 4: score += 1000; break;
}
count = 0;
// 纵向
for (int i = y-1; i >= 0; i--) {
if (chessBoard[i][x] == color) {
count++;
} else {
break;
}
}
for (int i = y+1; i < 8; i++) {
if (chessBoard[i][x] == color) {
count++;
} else {
break;
}
}
switch (count) {
case 1: score += 1; break;
case 2: score += 10; break;
case 3: score += 100; break;
case 4: score += 1000; break;
}
count = 0;
// 正斜向
for (int i = x-1, j = y-1; i >= 0 && j >= 0; i--, j--) {
if (chessBoard[j][i] == color) {
count++;
} else {
break;
}
}
for (int i = x+1, j = y+1; i < 8 && j < 8; i++, j++) {
if (chessBoard[j][i] == color) {
count++;
} else {
break;
}
}
switch (count) {
case 1: score += 1; break;
case 2: score += 10; break;
case 3: score += 100; break;
case 4: score += 1000; break;
}
count = 0;
// 反斜向
for (int i = x+1, j = y-1; i < 8 && j >= 0; i++, j--) {
if (chessBoard[j][i] == color) {
count++;
} else {
break;
}
}
for (int i = x-1, j = y+1; i >= 0 && j < 8; i--, j++) {
if (chessBoard[j][i] == color) {
count++;
} else {
break;
}
}
switch (count) {
case 1: score += 1; break;
case 2: score += 10; break;
case 3: score += 100; break;
case 4: score += 1000; break;
}
return score;
}
// 人类玩家落子
public static void humanPlayer(int[] coord) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入落子位置(x, y):");
coord[0] = scanner.nextInt();
coord[1] = scanner.nextInt();
while (chessBoard[coord[1]][coord[0]] != 0) {
System.out.print("该位置已有棋子,请重新输入落子位置(x, y):");
coord[0] = scanner.nextInt();
coord[1] = scanner.nextInt();
}
chessBoard[coord[1]][coord[0]] = 1;
}
// 电脑玩家落子
public static void computerPlayer(int[] coord) {
Random random = new Random();
int maxScore = 0;
// 遍历棋盘,找到最高分数的位置
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (chessBoard[i][j] == 0) {
int score = evaluate(2, j, i);
if (score > maxScore) {
maxScore = score;
coord[0] = j;
coord[1] = i;
}
}
}
}
chessBoard[coord[1]][coord[0]] = 2;
}
// 打印棋盘
public static void printChessBoard() {
System.out.print(" ");
for (int i = 0; i < 8; i++) {
System.out.print(i + " ");
}
System.out.println();
for (int i = 0; i < 8; i++) {
System.out.print(i + " ");
for (int j = 0; j < 8; j++) {
if (chessBoard[i][j] == 0) {
System.out.print("+ ");
} else if (chessBoard[i][j] == 1) {
System.out.print("● ");
} else {
System.out.print("○ ");
}
}
System.out.println();
}
}
public static void main(String[] args) {
int[] coord = new int[2];
int turn = 1; // 1表示人类玩家,2表示电脑玩家
// 初始化棋盘
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
chessBoard[i][j] = 0;
}
}
while (!isWin(1) && !isWin(2)) {
printChessBoard();
if (turn == 1) {
humanPlayer(coord);
} else {
computerPlayer(coord);
}
turn = 3 - turn; // 切换玩家
}
printChessBoard();
if (isWin(1)) {
System.out.println("人类玩家获胜!");
} else {
System.out.println("电脑玩家获胜!");
}
}
}
代码转换说明
- 数据类型: C++ 中的
int对应 Java 中的int,bool对应boolean。 - 数组: C++ 中的二维数组
int chessBoard[8][8]对应 Java 中的二维数组int[][] chessBoard = new int[8][8]。 - 函数: C++ 中的函数
isWin(int color)对应 Java 中的静态方法public static boolean isWin(int color)。 - 引用传递: C++ 中的引用传递
int& x, int& y对应 Java 中的数组传递int[] coord。 - 输入输出: C++ 中的
cin对应 Java 中的Scanner,cout对应System.out.println。 - 随机数生成: C++ 中的
srand(time(NULL))对应 Java 中的Random random = new Random();。 - 循环语句: C++ 中的
for循环对应 Java 中的for循环。 - 条件语句: C++ 中的
if语句对应 Java 中的if语句。 - switch 语句: C++ 中的
switch语句对应 Java 中的switch语句。
总结
本文通过代码示例,详细介绍了将 C++ 五子棋游戏代码转换为 Java 版本的步骤。开发者可以通过学习本文提供的代码示例和转换说明,快速将 C++ 代码移植到 Java 环境中。
注意: 由于 C++ 和 Java 的语法差异,部分代码需要根据具体情况进行调整。
建议: 为了更好地理解代码转换过程,开发者可以逐行分析 C++ 代码和 Java 代码,并对比两者的差异。
原文地址: http://www.cveoy.top/t/topic/nUYI 著作权归作者所有。请勿转载和采集!