void Operation(int o, int x, int y, int win, int a,int b,int p,char board[16][30], char mark[16][30], int mine) { switch (o) { case 1: Number(x, y,a,b, board, mark); while (mark[x-1][y-1] == '0') { int i, j; for (i = x - 2; i <= x ; i++) { for (j = y - 2; j <= y ; j++) { if ( i < 0 || i >= a || j < 0 || j >=b ) { continue; } else if(mark[i][j]=='.') { win++; Operation(1, i,j,win, a,b,p,board, mark,mine);} } } } break;怎么改
时间: 2023-06-27 19:03:05 浏览: 77
这段代码的问题不是很清楚,但是有一些潜在的问题需要注意。首先,这个函数似乎是用来处理操作的,但是它的参数比较多,建议尝试将其中一些参数封装成结构体或者类的成员变量,以简化函数调用。另外,这个函数似乎是递归调用的,但是没有很好地处理递归终止的条件,建议添加递归终止条件以避免死循环。最后,这段代码的命名规范也需要改进,变量名和函数名应该采用驼峰命名法,并且要有意义。
相关问题
#include<stdio.h> #define Row a #define Col b #define Mine c void Board(int board[Row][Col],int mark[Row][Col]) { int i,j,k=0; do{ for(i=0;i<Row;i++) { for(j=0;j<Col;j++) { scanf("%c",&board[i][j]); mark[i][j]="."; if(board[i][j]=="") k++; } printf("\n"); } }while(k!=Mine); } void Number(int x,int y,int board[Row][Col],int mark[Row][Col]) { int i,j,n=0; for(i=x-1;i<=x+1;i++) { for(j=y-1;j<=y+1;i++) { if(board[i][j]=="") n++; } } mark[x][y]=n; } void Print(int mark[Row][Col]) { int i,j; for(i=0;i<Row;i++) { for(j=0;j<Col;j++) { printf("%c",mark[i][j]); } } } void Operation(int o,int x,int y,int win,int board[Row][Col],int mark[Row][Col],int mine) { switch(o) { case 1:Number(x,y,board[Row][Col],mark[Row][Col]); while(mark[x][y]==0){int i,j; for(i=x-1;i<=x+1;i++) { for(j=y-1;j<=y+1;j++) { Number(i,j,board[Row][Col],mark[Row][Col]); win=win+1; } } } break; case 2:if(mark[x][y]=="."||mark[x][y]=="?"){mark[x][y]="!";mine=mine-1;} break; case 3:if(mark[x][y]=="."){mark[x][y]="?";}if(mark[x][y]=="!"){mark[x][y]="?";mine=mine+1;} break; case 4:if(mark[x][y]=="?"){mark[x][y]=".";}if(mark[x][y]=="!"){mark[x][y]=".";mine=mine+1;} break; case 9: if(mark[x][y]=="."||mark[x][y]=="?"||mark[x][y]=="!") break; int i,j,m=0,v=0; for(i=x-1;i<=x+1;i++) { for(j=y-1;j<=y+1;i++) { if(mark[i][j]=="!") m++; if(mark[i][j]=="."&&board[i][j]=="") v++; } } if(m==mark[x][y]&&v!=0) { printf("Hit mine, you lose"); break; } if(m==mark[x][y]&&v==0) { for(i=x-1;i<=x+1;i++) { for(j=y-1;j<=y+1;i++) { if(mark[i][j]==".") { Number(i,j,board[Row][Col],mark[Row][Col]); win=win+1; } } } } } } int main() { int win=0; int step=0; int t,a,b,c; scanf("%d",&t); switch(t) { case 1:a=9,b=9,c=10; break; case 2:a=16,b=16,c=40; break; case 3:a=16,b=30,c=99; break; case 4:scanf("%d %d %d",&a,&b,&c); break; } printf("\n"); int mine=c; int board[Row][Col],mark[Row][Col]; Board(board[Row][Col]); int o,x,y; while(win<ab-c) { scanf("%d %d %d",&o,&x,&y); step=step+1; if(o==1&&board[x][y]=="") { printf("Hit mine, you lose"); break; } if(o!=1&&o!=2&&o!=3&&o!=4&&o!=9) { win=win+1; } if(x<1||x>Row||y<1||y>Col) { win=win+1; } else { Operation(o,x,y); } printf("Game in progress"); printf("操作次数:%d 剩余雷数:%d",step,mine); Print(mark[Row][Col]); } while(win==ab-c) { printf("Game over, you win"); } int i,j; for(i=0;i<Row,i++) { for(j=0;j<Col;j++) { if(board[i][j]=="*") mark[i][j]=board[i][j]; } } Print(mark[Row][Col]); return 0; } 修改代码
#include<stdio.h>
#define Row 9
#define Col 9
#define Mine 10
void Board(char board[Row][Col], char mark[Row][Col]) {
int i, j, k=0;
do {
for(i=0; i<Row; i++) {
for(j=0; j<Col; j++) {
scanf(" %c", &board[i][j]);
mark[i][j]='.';
if(board[i][j]=='*') k++;
}
}
} while(k!=Mine);
}
void Number(int x, int y, char board[Row][Col], char mark[Row][Col]) {
int i, j, n=0;
for(i=x-1; i<=x+1; i++) {
for(j=y-1; j<=y+1; j++) {
if(i>=0 && i<Row && j>=0 && j<Col && board[i][j]=='*') n++;
}
}
mark[x][y]=n+'0';
}
void Print(char mark[Row][Col]) {
int i, j;
for(i=0; i<Row; i++) {
for(j=0; j<Col; j++) {
printf("%c ", mark[i][j]);
}
printf("\n");
}
}
void Operation(int o, int x, int y, int &win, char board[Row][Col], char mark[Row][Col], int &mine) {
switch(o) {
case 1:
if(board[x][y]=='*') {
printf("Hit mine, you lose\n");
break;
}
if(mark[x][y]=='.') {
Number(x, y, board, mark);
win++;
if(board[x][y]=='0') {
int i, j;
for(i=x-1; i<=x+1; i++) {
for(j=y-1; j<=y+1; j++) {
if(i>=0 && i<Row && j>=0 && j<Col && mark[i][j]=='.') {
Operation(1, i, j, win, board, mark, mine);
}
}
}
}
}
break;
case 2:
if(mark[x][y]=='.') {
mark[x][y]='!';
mine--;
}
break;
case 3:
if(mark[x][y]=='.') {
mark[x][y]='?';
} else if(mark[x][y]=='!') {
mark[x][y]='?';
mine++;
}
break;
case 4:
if(mark[x][y]=='?') {
mark[x][y]='.';
} else if(mark[x][y]=='!') {
mark[x][y]='.';
mine++;
}
break;
case 9:
if(mark[x][y]!='.') break;
int i, j, m=0, v=0;
for(i=x-1; i<=x+1; i++) {
for(j=y-1; j<=y+1; j++) {
if(i>=0 && i<Row && j>=0 && j<Col) {
if(mark[i][j]=='!') m++;
if(mark[i][j]=='.' && board[i][j]=='*') v++;
}
}
}
if(m==mark[x][y]-'0' && v!=0) {
for(i=x-1; i<=x+1; i++) {
for(j=y-1; j<=y+1; j++) {
if(i>=0 && i<Row && j>=0 && j<Col && mark[i][j]=='.') {
Operation(1, i, j, win, board, mark, mine);
}
}
}
}
break;
}
}
int main() {
int win=0, step=0;
int t, mine=Mine;
char board[Row][Col], mark[Row][Col];
printf("请选择难度:\n1.初级(9x9,10个雷)\n2.中级(16x16,40个雷)\n3.高级(16x30,99个雷)\n4.自定义\n");
scanf("%d", &t);
switch(t) {
case 1: break;
case 2: Row=Col=16; mine=40; break;
case 3: Col=30; mine=99; break;
case 4: scanf("%d %d %d", &Row, &Col, &mine); break;
}
printf("\n");
Board(board, mark);
int o, x, y;
printf("Game in progress\n");
while(win<Row*Col-mine) {
scanf("%d %d %d", &o, &x, &y);
step++;
if(x<0 || x>=Row || y<0 || y>=Col) {
printf("Invalid input\n");
continue;
}
Operation(o, x, y, win, board, mark, mine);
printf("操作次数:%d 剩余雷数:%d\n", step, mine);
Print(mark);
}
printf("Game over, you win\n");
Print(board);
return 0;
}
用java编写井字游戏“Tictactoe.java”,该页面带有简单的菜单栏,且只有一个“操作”菜单,下含“开始游戏”,“保存游戏”,“退出游戏”三个菜单条,游戏界面包括下拉框——九宫格选项(33,44;...10*10),页面可随用户选择变换大小,有“开始”,“再来一局”按钮:要求程序启动后,程序界面为灰色,需要启动菜单栏的开始游戏才能允许九宫格选项,点击开始/再来一局按钮游戏开始对战,双方各执“X”,"O"不同符号表示对战
好的,下面是一个基本的实现示例:
```
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Tictactoe extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel gamePanel;
private JComboBox<String> sizeComboBox;
private JButton startButton;
private JButton restartButton;
private JMenuItem startMenuItem;
private JMenuItem saveMenuItem;
private JMenuItem exitMenuItem;
private JFileChooser fileChooser;
private char[][] board;
private char currentPlayer;
private boolean gameEnded;
public Tictactoe() {
// 设置窗口标题和大小
super("Tictactoe");
setSize(400, 400);
// 创建菜单栏和菜单条
JMenuBar menuBar = new JMenuBar();
JMenu operationMenu = new JMenu("操作");
menuBar.add(operationMenu);
setJMenuBar(menuBar);
// 创建菜单项并添加到菜单条中
startMenuItem = new JMenuItem("开始游戏");
operationMenu.add(startMenuItem);
saveMenuItem = new JMenuItem("保存游戏");
operationMenu.add(saveMenuItem);
exitMenuItem = new JMenuItem("退出游戏");
operationMenu.add(exitMenuItem);
// 为菜单项添加事件处理程序
startMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
startGame();
}
});
saveMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
saveGame();
}
});
exitMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
// 创建下拉框并添加到内容面板中
String[] sizes = {"3x3", "4x4", "5x5", "6x6", "7x7", "8x8", "9x9", "10x10"};
sizeComboBox = new JComboBox<>(sizes);
sizeComboBox.setEnabled(false);
JPanel controlPanel = new JPanel(new FlowLayout());
controlPanel.add(new JLabel("九宫格大小:"));
controlPanel.add(sizeComboBox);
startButton = new JButton("开始");
startButton.setEnabled(false);
startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
startButton.setEnabled(false);
sizeComboBox.setEnabled(false);
gamePanel.removeAll();
gamePanel.setLayout(new GridLayout(getBoardSize(), getBoardSize()));
board = new char[getBoardSize()][getBoardSize()];
currentPlayer = 'X';
gameEnded = false;
for (int i = 0; i < getBoardSize(); i++) {
for (int j = 0; j < getBoardSize(); j++) {
JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!gameEnded) {
JButton clickedButton = (JButton) e.getSource();
int row = getButtonRow(clickedButton);
int col = getButtonCol(clickedButton);
if (board[row][col] == '\0') {
board[row][col] = currentPlayer;
clickedButton.setText("" + currentPlayer);
if (checkWin(row, col)) {
JOptionPane.showMessageDialog(Tictactoe.this, "恭喜" + currentPlayer + "获胜!");
gameEnded = true;
startButton.setEnabled(true);
} else if (checkTie()) {
JOptionPane.showMessageDialog(Tictactoe.this, "平局!");
gameEnded = true;
startButton.setEnabled(true);
} else {
currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
}
}
}
}
});
gamePanel.add(button);
}
}
gamePanel.revalidate();
gamePanel.repaint();
}
});
controlPanel.add(startButton);
restartButton = new JButton("再来一局");
restartButton.setEnabled(false);
restartButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < getBoardSize(); i++) {
for (int j = 0; j < getBoardSize(); j++) {
board[i][j] = '\0';
}
}
for (Component c : gamePanel.getComponents()) {
JButton button = (JButton) c;
button.setText("");
}
currentPlayer = 'X';
gameEnded = false;
restartButton.setEnabled(false);
}
});
controlPanel.add(restartButton);
add(controlPanel, BorderLayout.NORTH);
// 创建游戏面板并添加到内容面板中
gamePanel = new JPanel();
gamePanel.setBackground(Color.GRAY);
add(gamePanel, BorderLayout.CENTER);
// 创建文件选择器
fileChooser = new JFileChooser();
}
private int getBoardSize() {
String size = (String) sizeComboBox.getSelectedItem();
return Integer.parseInt(size.substring(0, size.indexOf('x')));
}
private int getButtonRow(JButton button) {
return gamePanel.getComponentZOrder(button) / getBoardSize();
}
private int getButtonCol(JButton button) {
return gamePanel.getComponentZOrder(button) % getBoardSize();
}
private boolean checkWin(int row, int col) {
// 检查行
for (int i = 0; i < getBoardSize(); i++) {
if (board[row][i] != currentPlayer) {
break;
}
if (i == getBoardSize() - 1) {
return true;
}
}
// 检查列
for (int i = 0; i < getBoardSize(); i++) {
if (board[i][col] != currentPlayer) {
break;
}
if (i == getBoardSize() - 1) {
return true;
}
}
// 检查对角线
if (row == col) {
for (int i = 0; i < getBoardSize(); i++) {
if (board[i][i] != currentPlayer) {
break;
}
if (i == getBoardSize() - 1) {
return true;
}
}
}
if (row + col == getBoardSize() - 1) {
for (int i = 0; i < getBoardSize(); i++) {
if (board[i][getBoardSize() - i - 1] != currentPlayer) {
break;
}
if (i == getBoardSize() - 1) {
return true;
}
}
}
return false;
}
private boolean checkTie() {
for (int i = 0; i < getBoardSize(); i++) {
for (int j = 0; j < getBoardSize(); j++) {
if (board[i][j] == '\0') {
return false;
}
}
}
return true;
}
private void startGame() {
sizeComboBox.setEnabled(true);
}
private void saveGame() {
int result = fileChooser.showSaveDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
// TODO: 保存游戏状态到文件
}
}
public static void main(String[] args) {
Tictactoe game = new Tictactoe();
game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.setVisible(true);
}
}
```
在这个示例中,我们创建了一个Tictactoe类作为游戏的主类,继承自JFrame类。在构造函数中,我们创建了菜单栏、下拉框、按钮和游戏面板,并且为菜单项和按钮添加了事件处理程序。我们使用二维数组来表示游戏面板,并且使用GridLayout布局管理器来动态改变游戏面板的大小。当用户点击一个按钮时,我们检查游戏是否结束,如果没有结束,我们更新游戏状态并检查是否有获胜者或平局。最后,我们使用文件选择器来实现保存游戏状态的功能。
请注意,这只是一个基本的实现示例,您可能需要根据您的具体需求进行修改和扩展。
阅读全文