0
我是C++的新手,我正在爲課程製作程序。該計劃是兩個人之間的井字遊戲。我已經完成了一個不使用函數的程序版本,我試圖使用它們。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()
我該怎麼做?
使用std :: vector或std :: array。 – 2017-10-05 15:09:03
感謝您的回覆,將研究如何使用它們。 –
您可能還想了解通過引用傳遞和傳遞函數中的值之間的區別。特別是在你的函數addPlayToBoard中 – hellyale