OK では、スクープを紹介します。C++ クラス用の掃海艇スタイルのプログラムを作成しています。2D 配列を使用してボードを作成し、2 番目の 2D 配列を使用して推測を保存しています。ボード配列の各値に対して、乱数 gen を実行し、0 から 99 までの値を割り当てます。配列内の各値は、その値が 85 より大きい場合、爆弾と見なされます。プログラムはユーザー入力を受け入れ、爆弾です。対応する bool 配列 (isGuessed) でない場合、position は true に変更されます。次に、この bool 配列が reDraw 関数に送られ、再描画されます。すべての false bool は「?」として表示されます。true はすべて「X」として表示されます。ただし、再描画すると、配列内の複数の場所が 1 つの推測だけで X に変更されます。私の考えでは、ボードは配列の位置に関連して描かれていません。誰かが助けてくれますか。明確にするために、関数 reDraw が正しく機能しない理由を理解しようとしています。これがすべての私のコードです。
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
//Function Prototypes
void reDraw (bool guessed [] [10], int rows, int columns);
void newBoard (int rows, int columns);
int main(int argc, const char * argv[])
{
//Declare Variables
const int ROWS = 10;
const int COLUMNS = 10;
int board [ROWS][COLUMNS];
bool wasGuessed [10] [10];
bool playAgain = true;
char newGame;
int rowGuess, columnGuess, numOfBombs = 0, score = 0;
bool bomb = false;
srand((unsigned) time(0));
// Welcome User, Give Direction
cout<<"\n\n\t\tWelcome To Matt's Minesweeper\n\n";
while (playAgain) {
//function to randomly populate array elements
for (int row = 0; row < ROWS; row++) {
for (int column = 0; column < COLUMNS; column++) {
board [row] [column] = rand() % 100;
if (board [row] [column] >= 85) { //counter for bombs
numOfBombs++;
}
}
}
// Create a new Display Board
newBoard(10, 10);
//Begin Game
cout<<"\nTotal Bombs In This Game: "<<numOfBombs;
// Process Guesses
do {
cout<<"\nPlease Input your ROW & COLUMN Guess coordinate (i.e. 3 2): ";
cin>>rowGuess>>columnGuess;
if (board [rowGuess] [columnGuess] >= 85) {
bomb = true;
cout<<"\n\n\t\tXXXXXX BOMB HIT XXXXXXX\n\t\t\tGame Over.\n\n";
cout<<"Your Score Score: "<<score;
cout<<"\n\n Play Again (y/n):";
cin>>newGame;
switch (newGame) {
case 'y':
cout<<"\n\n";
playAgain = true;
break;
default:
playAgain = false;
break;
}
} else {
wasGuessed [rowGuess] [columnGuess] = true;
score++;
reDraw(wasGuessed, 10, 10);
}
} while (!bomb);
}
return 0;
}
void reDraw (bool guessed [] [10], int rows, int columns)
{
// Format row and column headers
cout<<" 0 1 2 3 4 5 6 7 8 9\n";
cout<<"0";
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if ((j+1) % 10 == 0)
if ((i+1) == 10) {
if (guessed [i] [j])
cout<<"X "<<" \n";
else
cout<<"? "<<" \n";
}else{
if (guessed [i] [j])
cout<<"X "<<" \n"<<i+1;
else
cout<<"? "<<" \n"<<i+1;
}
if ((j+1) % 10 != 0)
if (guessed [j] [i])
cout<<" X"<<" ";
else
cout<<" ?"<<" ";
}
}
}
void newBoard (int rows, int columns)
{
cout<<" 0 1 2 3 4 5 6 7 8 9\n";
cout<<"0";
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if ((j+1) % 10 == 0)
if ((i+1) == 10) {
cout<<"? "<<" \n";
}else
cout<<"? "<<" \n"<<i+1;
if ((j+1) % 10 != 0)
cout<<" ?"<<" ";
}
}
}
助けてくれてありがとう!