gpt4 book ai didi

c++ - 源.cpp :LINE:COL: error: function definition is not allowed here

转载 作者:太空宇宙 更新时间:2023-11-04 03:27:02 25 4
gpt4 key购买 nike

<分区>

我是 C++ 的初学者,我正在尝试制作一个尚未完成的国际象棋游戏。当我尝试编译下面的代码时,它给了我错误:此处不允许定义函数。对于 isValidMove、movePiece 和 Main任何人都可以告诉我为什么它给我这个。

代码是:

#include <iostream>
#include <string>
#include <stdlib.h>
#include <ctype.h>
#include <cmath>
#include <algorithm>

using namespace std;

char board[8][8] = {
'R','N','B','Q','K','B','N','R',
'P','P','P','P','P','P','P','P',
'-','-','-','-','-','-','-','-',
'-','-','-','-','-','-','-','-',
'-','-','-','-','-','-','-','-',
'-','-','-','-','-','-','-','-',
'p','p','p','p','p','p','p','p',
'r','n','b','q','k','b','n','r'};

void swap(char board[8][8], int inRow, int inCol, int outRow, int outCol) {
char tmp = board[inRow][inCol];
board[inRow][inCol] = board[outRow][outCol];
board[outRow][outCol] = tmp;
}

void printBoard(char board[8][8]) {
cout << "# a b c d e f g h " << endl;
for(int i = 0; i <= 7; i++) {
cout << 8 - i << " ";
for(int j = 0; j <= 7; j++) {
cout << board[i][j] << " ";
}
cout << endl;
}
}

bool isValidMoveRook(int inCol, int inRow, int outCol, int outRow, char board[8][8]) {
int smallCol = min(inCol, outCol);
int smallRow = min(inRow, outRow);
int maxCol = max(inCol, outCol);
int maxRow = max(inRow, outRow);
if(inRow == outRow) {
for(int i = smallCol + 1; i < maxCol; i++) {
if(board[inRow][i] != '-') {
return false;
}
}
return true;
}
else if(outCol == inCol) {
for(int i = smallRow + 1; i < maxRow; i++) {
if(board[i][inCol] != '-') {
return false;
}
}
return true;
}
else {
return false;
}
}
bool isValidMoveBishop(int inCol, int inRow, int outCol, int outRow, char board[8][8]) {
int rowDiff = outRow - inRow;
int colDiff = outCol - inCol;
if(abs(outRow - inRow) == abs(outCol - inCol)) {
if(rowDiff < 0 && colDiff < 0) {
for(int i = 1; i > rowDiff; i--) {
if(board[inRow + rowDiff][inCol + rowDiff] != '-') {
return false;
}
}
}
}

else {
return true;
}
}

bool isValidMove(int inCol, int inRow, int outCol, int outRow, char board[8][8]) {
if(board[inRow][inCol] == '-') {
return false;
}
else {
if(board[inRow][inCol] == 'R' || board[inRow][inCol] == 'r' && isValidMoveRook(inCol, inRow, outCol, outRow, board)) {
return true;
}
}
}


void movePiece(char board[8][8]) {
string input;
string output;
cout << "Please enter your piece position : ";
cin >> input;
cout << "Please enter where you want to place your piece : ";
cin >> output;

int inCol = input[0] - 'a';
int inRow = 7 - (input[1] - '1');
int outCol = output[0] - 'a';
int outRow = 7 - (output[1] - '1');
while(!isValidMove(inCol, inRow, outCol, outRow, board)) {
cout << "Invalid input try again" << endl;
cout << "Please enter your piece position : ";
cin >> input;
cout << "Please enter where you want to place your piece : ";
cin >> output;
inCol = input[0] - 'a';
inRow = 7 - (input[1] - '1');
outCol = output[0] - 'a';
outRow = 7 - (output[1] - '1');
}
if(board[outRow][outCol] == '-') {
swap(board, inRow, inCol, outRow, outCol);
}
else {
if(isupper(board[inRow][inCol]) != isupper(board[outRow][outCol])) {
board[outRow][outCol] = board[inRow][inCol];
board[inRow][inCol] = '-';
}
}
}

int main() {
printBoard(board);
while (true) {
movePiece(board);
printBoard(board);
}
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com