配列の値を別のクラスのクラスから変更したい場合、実際には配列の値は変更されません。 array [0] [0] = 'X'のように動作しません。だからここのコードです:C++別のクラスのクラスから配列を変更する
#pragma once
#include <iostream>
#include "players.h"
using namespace std;
class checkerBoard
{
public:
checkerBoard();
void initBoard();
void printBoard();
char Board[5][5];
private:
int boardheight = 5;
int boardwidth = 5;
char normalSymbol = '.';
players player;
};
チェッカーボードクラス:
#include "checkerBoard.h"
checkerBoard::checkerBoard()
{
}
void checkerBoard::initBoard() {
for (int y = 0; y < boardheight; y++) {
for (int x = 0; x < boardwidth; x++) {
Board[y][x] = normalSymbol;
}
}
}
void checkerBoard::printBoard() {
for (int y = 0; y < boardheight; y++) {
for (int x = 0; x < boardwidth; x++) {
cout << Board[y][x];
cout << " ";
}
cout << endl;
}
}
はい、私は名前空間の使用は良いアイデアをイマイチ知っています。ここ は、配列を変更する必要があるコードですが、そのはやっていない:
ヘッダー:
#pragma once
#include <iostream>
#include <string>
#include "players.h"
#include "checkerBoard.h"
using namespace std;
class tictactoeEngine
{
public:
tictactoeEngine();
void turnPlayer1();
void turnPlayer2();
int turn = 1;
players players;
checkerBoard board;
bool finished = false;
private:
int yCoordinates;
int xCoordinates;
};
のcppファイル:
#include "tictactoeEngine.h"
tictactoeEngine::tictactoeEngine()
{
}
void tictactoeEngine::turnPlayer1() {
while (turn == 1) {
cout << "Wich Y-Coordinates are you using " << flush << players._namePlayer1 << " ?:";
cin >> yCoordinates;
cout << "And wich X-Coordinates are you using?: " << flush;
cin >> xCoordinates;
board.Board[yCoordinates + 1][xCoordinates + 1] = players._symbolPlayer1;
yCoordinates = 0;
xCoordinates = 0;
turn = 2;
}
}
void tictactoeEngine::turnPlayer2() {
while (turn == 2) {
cout << "Wich Y-Coordinates are you using " << flush << players._namePlayer2 << " ?:";
cin >> yCoordinates;
cout << "And wich X-Coordinates are you using?: " << flush;
cin >> xCoordinates;
board.Board[yCoordinates - 1][xCoordinates - 1] = players._symbolPlayer1;
yCoordinates = 0;
xCoordinates = 0,
turn = 1;
}
}
私は誰も私を助けることを願って:)
私はあなたが何を求めているのか明確ではありません。 – Ian
「配列を変更しなければならないコードはありますが、実行していない」とは何を意味するのか不明です。私たちがあなたを手助けするためには、短い自己完結型の例と具体的な問題の説明が必要です。これを見てください:https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ –
*はい私は名前空間の使用が良いアイデアではないことを知っています*、本当ですか? *ディレクティブを使うことは悪い考えですか? – PcAF