可以帮我优化一下java五子棋代码吗
程序要求棋盘是15x15的规格,通过输入坐标下棋,一方棋子为“x”,一方给棋子为“o”,下期结束后输出赢家的坐标,以下是我的代码(代码过长但不知道怎么改):
import java.util.*;
public class Gomuku{
static Scanner in=new Scanner(System.in);
static String Qipang[][]=new String[15][15];
static boolean first=true;
static int i,j,k;
public static void main(String[] args){
while(first){
String str=in.next();
int a[];
i=str.charAt(0)-65;
j = Integer.valueOf(play.substring(1))-1;
if(first){
if(k!=0||k%2!=0){
Qipang[i][j]="O";
}
else{
Qipang[i][j]="X";
}
Pangduan pd=new Pangduan(Qipang);
boolean end=pd.Chakan(i,j);
if(end){
first=false;
a=jd1(i,j);
if(a[0]!=-1){
for(int b=0;b<a.length;b++){
int m=a[b]/15+65;
StringBuffer sb=new StringBuffer();
sb.append((char)m);
System.out.print(sb.toString()+(a[b]%15+1)+" ");
}
}
a=jd2(i,j);
if(a[0]!=-1){
for(int b=0;b<a.length;b++){
int m=a[b]/15+65;
StringBuffer sb=new StringBuffer();
sb.append((char)m);
System.out.print(sb.toString()+(a[b]%15+1)+" ");
}
}
a=jd3(i,j);
if(a[0]!=-1){
for(int b=0;b<a.length;b++){
int m=a[b]/15+65;
StringBuffer sb=new StringBuffer();
sb.append((char)m);
System.out.print(sb.toString()+(a[b]%15+1)+" ");
}
}
a=jd4(i,j);
if(a[0]!=-1){
for(int b=0;b<a.length;b++){
int m=a[b]/15+65;
StringBuffer sb=new StringBuffer();
sb.append((char)m);
System.out.print(sb.toString()+(a[b]%15+1)+" ");
}
}
}
k++;
}
}
}
//判断行
public static int[] jd1(int x,int y){
int k=1;
int[] a={-1,-1,-1,-1};
for(int i=y+1;i<15;i++){
if(Qipang[x][i]<Qipang[x][y]){
a[k-1]=15*x+i;
k++;
}else{
break;
}
}
for(int i=y-1;i>=0;i--){
if(Qipang[x][i]==Qipang[x][y]){
a[k-1]=15*x+i;
k++;
}else{
break;
}
}
a[k-1]=x*15+y;
for(int i=0;i<4;i++){
for(int j=0;j<4-i;j++){
if(a[j]>a[j+1]){
int t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
return a;
}
//判断列
public static int[] jd2(int x,int y){
int k=1;
int[] a={-1,-1,-1,-1};
for(int i=x+1;i<15;i++){
if(Qipang[i][y]==Qipang[x][y]){
a[k-1]=15*i+y;
k++;
}else{
break;
}
}
for(int i=x-1;i>=0;i--){
if(Qipang[i][y]==Qipang[x][y]){
a[k-1]=15*i+y;
k++;
}else{
break;
}
}
a[k-1]=x*15+y;
for(int i=0;i<4;i++){
for(int j=0;j<4-i;j++){
if(a[j]>a[j+1]){
int t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
return a;
}
//判断左上、右下
public static int[] jd3(int x,int y){
int k=1;
int[] a={-1,-1,-1,-1};
for(int i=x+1,j=y+1;i<15&&j<15;i++,j++){
if(Qipang[i][j]=Qipang[x][y]){
a[k-1]=15*i+j;
k++;
}else{
break;
}
}
for(int i=x-1,j=y-1;i>=0&&j>=0;i--,j--){
if(Qipang[i][j]==Qipang[x][y]){
a[k-1]=15*i+j;
k++;
}else{
break;
}
}
a[k-1]=x*15+y;
for(int i=0;i<4;i++){
for(int j=0;j<4-i;j++){
if(a[j]>a[j+1]){
int t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
return a;
}
//判断右上、左下
public static int[] jd4(int x,int y){
int k=1;
int[] a={-1,-1,-1,-1};
for(int i=x-1,j=y+1;i>=0&&j<15;i--,j++){
if(Qipang[i][j]=Qipang[x][y]){
a[k-1]=15*i+j;
k++;
}else{
break;
}
}
for(int i=x+1,i=y-1;i<15&&j>=0;i++,j--){
if(Qipang[i][j]==Qipang[x][y]){
a[k-1]=15*i+j;
k++;
}else{
break;
}
}
a[k-1]=x*15+y;
for(int i=0;i<4;i++){
for(int j=0;j<4-i;j++){
if(a[j]>a[j+1]){
int t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
return a;
}
}
class Pangduan{
static private String[][] Qipang=new String[15][15];
public Pangduan(String[] QiPang){
Pangduan.Qipang=Qipang;
}
public boolean Chakan(int x,int y){
int k1=this.jd1(x, y);
int k2=this.jd2(x, y);
int k3=this.jd3(x, y);
int k4=this.jd4(x, y);
if(k1>=5||k2>=5||k3>=5||k4>=5){
return true;
}
else{
return false;}
}
//判断行
public int jd1(int x,int y){
int k=1;
for(int i=y+1;i<15;i++){
if(Qipang[x][i]==Qipang[x][y]){
k++;
}
else{
break;
}
}
for(int i=y-1;i>=0;i--){
if(Qipang[x][i]==Qipang[x][y]){
k++;
}
else{
break;
}
}
return k;
}
//判断列
public int jd2(int x,int y){
int k=1;
for(int i=x+1;i<15;i++){
if(Qipang[i][y]==Qipang[x][y]){
k++;
}
else{
break;
}
}
for(int i=x-1;i>=0;i--){
if(Qipang[i][y]==Qipang[x][y]){
k++;
}
else{
break;
}
}
return k;
}
//判断左上、右下
public int jd3(int x,int y){
int k=1;
for(int i=x+1,i=y+1;i<15&&j<15;i++,j++){
if(Qipang[i][j]==Qipang[x][y]){
k++;
}
else{
break;
}
}
for(int i=x-1,j=y-1;i>=0&&j>=0;i--,j--){
if(Qipang[i][j]==Qipang[x][y]){
k++;
}
else{
break;
}
}
return k;
}
//判断右上、左下
public int jd4(int x,int y){
int k=1;
for(int i=x-1,i=y+1;i>=0&&j<15;i--,j++){
if(Qipang[i][j]==Qipang[x][y]){
k++;
}
else{
break;
}
}
for(int i=x+1,j=y-1;i<15&&j>=0;i--,j--){
if(Qipang[i][j]==Qipang[x][y]){
k++;
}
else{
break;
}
}
return k;
}
}
当然可以帮你优化代码。以下是一些可以改进的部分:
1. 命名规范:遵循Java的命名规范,将类名首字母大写,方法和变量名使用驼峰命名法,可以提高代码的可读性。
2. 使用常量:将”15”这样的魔法数值提取为常量,增加代码的可维护性。
3. 使用数组或集合简化代码:可以使用数组或集合来存储和处理判断连子的函数的返回值,避免多个相似的函数。
4. 代码重构:将重复的代码提取为单独的方法,减少代码的冗余。
5. 简化条件判断:简化if-else语句的嵌套,优化条件判断的逻辑。请注意,我对代码进行了一些简化和重构,以提高可读性和可维护性。
import java.util.Scanner;
public class Gomuku {
private static final int BOARD_SIZE = 15;
private static final String EMPTY = "-"; // 空白位置的标记
private static final String PLAYER1 = "O"; // 玩家1的标记
private static final String PLAYER2 = "X"; // 玩家2的标记
private static Scanner in = new Scanner(System.in);
private static String[][] board = new String[BOARD_SIZE][BOARD_SIZE]; // 棋盘
private static boolean isFirstPlayer = true; // 标记当前是否是玩家1的回合
private static int moveCount = 0; // 已经进行的移动次数
public static void main(String[] args) {
initializeBoard(); // 初始化棋盘
while (true) {
String input = in.next();
int row = input.charAt(0) - 'A'; // 将输入的行号转换为对应的数组索引
int col = Integer.parseInt(input.substring(1)) - 1; // 将输入的列号转换为对应的数组索引
if (isValidMove(row, col)) { // 检查移动是否合法
makeMove(row, col); // 进行移动
printBoard(); // 打印当前棋盘
if (isGameEnd(row, col)) { // 检查游戏是否结束
break;
}
switchPlayer(); // 切换玩家
} else {
System.out.println("Invalid move. Please try again.");
}
}
}
private static void initializeBoard() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
board[i][j] = EMPTY; // 将棋盘初始化为全部空白位置
}
}
}
private static boolean isValidMove(int row, int col) {
return row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE && board[row][col].equals(EMPTY);
// 移动合法的条件:行列索引在合法范围内,并且目标位置为空白位置
}
private static void makeMove(int row, int col) {
String currentPlayer = isFirstPlayer ? PLAYER1 : PLAYER2; // 获取当前玩家的标记
board[row][col] = currentPlayer; // 在目标位置放置当前玩家的标记
moveCount++; // 增加移动次数
}
private static boolean isGameEnd(int row, int col) {
return checkRow(row, col) || checkColumn(row, col) || checkDiagonal(row, col);
// 判断游戏是否结束的条件:检查行、列、对角线是否存在连子
}
private static boolean checkRow(int row, int col) {
String currentPlayer = isFirstPlayer ? PLAYER1 : PLAYER2; // 获取当前玩家的标记
int count = 1; // 连子计数器,初始为1(当前移动位置)
// 向左检查
for (int i = col - 1; i >= 0; i--) {
if (board[row][i].equals(currentPlayer)) {
count++;
} else {
break;
}
}
// 向右检查
for (int i = col + 1; i < BOARD_SIZE; i++) {
if (board[row][i].equals(currentPlayer)) {
count++;
} else {
break;
}
}
return count >= 5; // 连子数达到5个及以上,游戏结束
}
private static boolean checkColumn(int row, int col) {
String currentPlayer = isFirstPlayer ? PLAYER1 : PLAYER2; // 获取当前玩家的标记
int count = 1; // 连子计数器,初始为1(当前移动位置)
// 向上检查
for (int i = row - 1; i >= 0; i--) {
if (board[i][col].equals(currentPlayer)) {
count++;
} else {
break;
}
}
// 向下检查
for (int i = row + 1; i < BOARD_SIZE; i++) {
if (board[i][col].equals(currentPlayer)) {
count++;
} else {
break;
}
}
return count >= 5; // 连子数达到5个及以上,游戏结束
}
private static boolean checkDiagonal(int row, int col) {
String currentPlayer = isFirstPlayer ? PLAYER1 : PLAYER2; // 获取当前玩家的标记
int count1 = 1; // 连子计数器,初始为1(当前移动位置)
int count2 = 1; // 连子计数器,初始为1(当前移动位置)
// 检查左上到右下对角线
for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {
if (board[i][j].equals(currentPlayer)) {
count1++;
} else {
break;
}
}
for (int i = row + 1, j = col + 1; i < BOARD_SIZE && j < BOARD_SIZE; i++, j++) {
if (board[i][j].equals(currentPlayer)) {
count1++;
} else {
break;
}
}
// 检查右上到左下对角线
for (int i = row - 1, j = col + 1; i >= 0 && j < BOARD_SIZE; i--, j++) {
if (board[i][j].equals(currentPlayer)) {
count2++;
} else {
break;
}
}
for (int i = row + 1, j = col - 1; i < BOARD_SIZE && j >= 0; i++, j--) {
if (board[i][j].equals(currentPlayer)) {
count2++;
} else {
break;
}
}
return count1 >= 5 || count2 >= 5; // 连子数达到5个及以上,游戏结束
}
private static void switchPlayer() {
isFirstPlayer = !isFirstPlayer; // 切换玩家
}
private static void printBoard() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
System.out.print(board[i][j] + " "); // 打印棋盘上每个位置的标记
}
System.out.println(); // 换行
}
}
}
好
希望这样的注释能够帮助你理解代码的优化部分和逻辑。如果你有任何疑问,请随时提问!
以下是一个简单的五子棋游戏的Java代码实现:
```java
import java.util.Scanner;
public class GobangGame {
private static final int BOARD_SIZE = 15; // 棋盘大小
private static final char PLAYER1 = 'x'; // 玩家1棋子
private static final char PLAYER2 = 'o'; // 玩家2棋子
private static char[][] board = new char[BOARD_SIZE][BOARD_SIZE]; // 棋盘
public static void main(String[] args) {
initBoard(); // 初始化棋盘
printBoard(); // 打印棋盘
char currentPlayer = PLAYER1; // 当前玩家
while (true) {
System.out.println("请玩家 " + currentPlayer + " 输入坐标(例如:3,4):");
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
String[] coordinates = input.split(",");
int row = Integer.parseInt(coordinates[0]) - 1;
int col = Integer.parseInt(coordinates[1]) - 1;
if (isValidMove(row, col)) {
board[row][col] = currentPlayer;
printBoard();
if (isWinningMove(row, col)) {
System.out.println("玩家 " + currentPlayer + " 获胜!");
break;
}
if (isBoardFull()) {
System.out.println("平局!");
break;
}
currentPlayer = (currentPlayer == PLAYER1) ? PLAYER2 : PLAYER1;
} else {
System.out.println("无效的坐标,请重新输入!");
}
}
}
// 初始化棋盘
private static void initBoard() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
board[i][j] = '-';
}
}
}
// 打印棋盘
private static void printBoard() {
System.out.print(" ");
for (int i = 1; i <= BOARD_SIZE; i++) {
System.out.print(i + " ");
}
System.out.println();
for (int i = 0; i < BOARD_SIZE; i++) {
System.out.print((i + 1) + " ");
for (int j = 0; j < BOARD_SIZE; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println();
}
}
// 判断是否为有效的落子位置
private static boolean isValidMove(int row, int col) {
if (row < 0 || row >= BOARD_SIZE || col < 0 || col >= BOARD_SIZE) {
return false;
}
return board[row][col] == '-';
}
// 判断是否为获胜的落子位置
private static boolean isWinningMove(int row, int col) {
char player = board[row][col];
// 判断横向是否连成五子
int count = 0;
for (int i = col - 4; i <= col + 4; i++) {
if (i < 0 || i >= BOARD_SIZE) {
continue;
}
if (board[row][i] == player) {
count++;
if (count == 5) {
return true;
}
} else {
count = 0;
}
}
// 判断纵向是否连成五子
count = 0;
for (int i = row - 4; i <= row + 4; i++) {
if (i < 0 || i >= BOARD_SIZE) {
continue;
}
if (board[i][col] == player) {
count++;
if (count == 5) {
return true;
}
} else {
count = 0;
}
}
// 判断左上到右下是否连成五子
count = 0;
for (int i = row - 4, j = col - 4; i <= row + 4 && j <= col + 4; i++, j++) {
if (i < 0 || i >= BOARD_SIZE || j < 0 || j >= BOARD_SIZE) {
continue;
}
if (board[i][j] == player) {
count++;
if (count == 5) {
return true;
}
} else {
count = 0;
}
}
// 判断左下到右上是否连成五子
count = 0;
for (int i = row + 4, j = col - 4; i >= row - 4 && j <= col + 4; i--, j++) {
if (i < 0 || i >= BOARD_SIZE || j < 0 || j >= BOARD_SIZE) {
continue;
}
if (board[i][j] == player) {
count++;
if (count == 5) {
return true;
}
} else {
count = 0;
}
}
return false;
}
// 判断棋盘是否已满
private static boolean isBoardFull() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == '-') {
return false;
}
}
}
return true;
}
}
```
嗯,你看下这个是否满足你的需求,设计思路:使用了一个二维数组来表示棋盘,其中每个元素可以是0、X或O。在主循环中,程序会不断询问玩家要下在哪,直到有一方获胜或棋盘被填满。在每次下棋之前,程序会检查该位置是否已经被占据或者是否超出了棋盘范围。如果下棋成功,程序会交换玩家并检查是否有一方获胜。
import java.util.Scanner;
public class TicTacToe {
public static void main(String[] args) {
int[][] board = new int[15][15];
Scanner scanner = new Scanner(System.in);
String player = "X";
String opponent = "O";
int row = 0;
int col = 0;
while (true) {
System.out.println("It's " + player + "'s turn.");
System.out.print("Enter row and column (e.g. 1 2): ");
String input = scanner.nextLine();
String[] parts = input.split(" ");
row = Integer.parseInt(parts[0]);
col = Integer.parseInt(parts[1]);
if (row < 1 || row > 15 || col < 1 || col > 15) {
System.out.println("Invalid input.");
continue;
}
if (board[row - 1][col - 1] != 0) {
System.out.println("That spot is taken.");
continue;
}
board[row - 1][col - 1] = player.charAt(0);
if (checkWin(board, player)) {
System.out.println(player + " wins!");
break;
}
player = (player == "X" ? "O" : "X");
}
}
private static boolean checkWin(int[][] board, String player) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == board[i+1][j+1] == board[i+2][j+2] != 0) {
return true;
}
}
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == board[i+1][j-1] == board[i+2][j-2] != 0) {
return true;
}
}
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == board[i-1][j+1] == board[i-2][j+2] != 0) {
return true;
}
}
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == board[i-1][j-1] == board[i-2][j-2] != 0) {
return true;
}
}
}
return false;
}
}
不知道你这个问题是否已经解决, 如果还没有解决的话:1.
int a = 10;
int b = 20;
int n = 100000;
for (int i =0;i<n;i++){
a ++;
b ++;
}
2.
int a = 10;
int b = 20;
int n = 100000;
for (int i =0;i<n;i++){
for(int j = 0;j<n;j++)
{
a ++;
b ++;
{
}
同理,大O表达式为 O(n2)
3.更多