gpt4 book ai didi

c++如何输出我在函数中操作的数组?

转载 作者:行者123 更新时间:2023-11-28 01:42:33 25 4
gpt4 key购买 nike

<分区>

我是 C++ 新手,正在为类编写程序。该程序是两人之间的井字游戏。我已经完成了一个不使用函数的程序版本,我正在尝试使用它们。

我想在一个函数中编辑一个数组并输出该函数以供稍后在程序中使用。

这是代码;

// This is a assessment project which plays ticTacToe between two players.

#include <iostream>

using namespace std;

int main() {

void displayBoard(char ticTacToeGame[][3]); // sets up use of displayBoard()
char userPlay(); // sets up use of userPlay()
char addplayToBoard(char play, char ticTacToeGame[][3] ); // sets up use of addPlayToBoard()


char ticTacToeGame[3][3] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'}; // game board array


// declaration of variables
char play;

displayBoard(ticTacToeGame); // displays the board to user
play = userPlay(); // gets users play and stores it as a char

return 0;
} // end of main()

// function used to display the board
void displayBoard(char ticTacToeGame[][3]) {

// display board
for (int row = 0; row < 3; row++) {

cout << endl;

for (int column = 0; column < 3; column++) {
cout << "| " << ticTacToeGame[row][column] << " ";
}

cout << "|\n";

if (row < 2) {
for (int i = 0; i < 19; i++) {
cout << "-";
}
}
}


} // end of displayBoard()

// function used to get users play
char userPlay() {

// declaration of variables
char play;

cout << "Play: ";
cin >> play;
cout << endl;

return play;

} // end of userPlay()

// function used to add users play to board
char addPlayToBoard(char play, char ticTacToeGame[][3]) {

for (int row = 0; row < 3; row++) {
for (int column = 0; column < 3; column++) {
if (ticTacToeGame[row][column] == play){
ticTacToeGame[row][column] = 'O';
}
}
}
return ticTacToeGame;

} // end of addPlayToBoard()

我该怎么做?

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