井字棋数组字符错误
Tic Tac Toe array wrong characters
屏幕截图说明了这一切,我使用的字符被正确地放入数组中。然而,其他一些随机字符也被插入到数组中。我糊涂了!
main.cp:
#include "Players.h"
#include "GameLayout.h"
#include "Game.h"
#include <iostream>
#include <string>
using namespace std;
int main ()
{
char gameBoard [3][3];
cout << "**** Welcome to Leviathan's first TicTacToe Game! ****nn";
Players playersObject;
playersObject.getPlayersNames();
playersObject.printPlayersNames();
GameLayout gameObject;
gameObject.printLayout();
Game gamestartObject;
gamestartObject.gameStart(gameBoard);
}
game.cpp:
#include "Players.h"
#include "GameLayout.h"
#include "Game.h"
#include <iostream>
#include <string>
using namespace std;
void Game::gameStart(char board[3][3])
{
char player1char,player2char;
size_t i,j;
cout << "Enter player 1 character :";
cin >> player1char;
cout << "Enter player 2 character :";
cin >> player2char;
int row,column;
bool isDone = false;
while(isDone == false)
{
cout << "Player 1->choose row";
cin >> row;
cout << "Player 1->choose column";
cin >> column;
board[row-1][column-1] = player1char;
cout << "Player 2->choose row";
cin >> row;
cout << "Player 2->choose column";
cin >> column;
board[row-1][column-1] = player2char;
GameLayout layout;
cout << " |1||2||3|" <<endl;
for(i=0; i<3; i++)
{
cout << i+1 << "|";
for(j=0; j<3; j++)
{
cout <<" "<<board[i][j] << " " ;
}
cout << endl;
}
}
}
Players.cpp:
#include "Players.h"
#include "GameLayout.h"
#include "Game.h"
#include <iostream>
#include <string>
using namespace std;
void Players::getPlayersNames()
{
string p1,p2;
cout << "Enter player 1 name : ";
cin >> p1;
cout << "nEnter player 2 name : ";
cin >> p2;
_player1Name = p1;
_player2Name = p2;
}
void Players::printPlayersNames()
{
cout << "Alright " << _player1Name << " and " << _player2Name <<", the game has begun!nn";
}
GameLayout.cpp:
#include "Players.h"
#include "GameLayout.h"
#include "Game.h"
#include <iostream>
#include <string>
using namespace std;
void GameLayout::printLayout()
{
size_t i,j;
char board[3][3];
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
board[i][j] = '.';
}
}
cout << " |1||2||3|" <<endl;
for(i=0; i<3; i++)
{
cout << i+1 << "|";
for(j=0; j<3; j++)
{
cout <<" "<<board[i][j] << " " ;
}
cout << endl;
}
}
Game.h:
#ifndef GAME_H
#define GAME_H
class Game
{
public:
void gameStart(char board[3][3]);
private:
};
#endif // GAME_H
gamellayout:
#ifndef GAMELAYOUT_H
#define GAMELAYOUT_H
class GameLayout
{
public:
void printLayout();
private:
};
#endif // GAMELAYOUT_H
Players.h:
#ifndef PLAYERS_H
#define PLAYERS_H
#include <string>
class Players
{
public:
void getPlayersNames();
void printPlayersNames();
private:
std::string _player1Name;
std::string _player2Name;
};
#endif // PLAYERS_H
函数中的局部非静态变量(如gameBoard
)未初始化。你需要显式初始化它,否则它的内容将是不确定的,读取它的值将导致未定义行为。
你可以简单地像
那样做char gameBoard [3][3] = { ' ' };
相关文章:
- 我的字符计数代码计算错误.为什么
- 错误:字符数组的初始值设定项太多
- 需要帮助在 c++ 中将字符串转换为字符 ----错误 "const char *" 类型的值不能用于初始化 "char" 类型的实体
- C++ 字符错误
- CppLint铸造字符*错误
- R data.table 和 STRING_ELT() 错误:如何修复"字符向量"与"字符"错误?
- C URL用UTF8字符错误解码
- C++:如何在 do while 循环中解决字符错误
- 遍历字符* 错误没有从字符* 转换为 int
- Arduino SIM800L:将字符串转换为字符* 错误
- Qt 中的常量字符* 错误行为
- 返回c++中的字符错误数组
- C++:队列中的第一个字符错误
- c++ SFML 按键字符错误
- C++字符*错误,程序崩溃
- 输出字符串/字符错误(c++)
- 将输入字符串转换为字符* <错误 Ptr>
- 井字棋数组字符错误
- c++读取字符串字符错误
- CouchDB UTF-8字符错误