我正在使用C ++编写一个基本的Tic Tac Toe游戏,因为我是初学者,但是这个函数打印出一个混乱的东西,通常是空白屏幕或带有奇数字符的混乱版本。 代码:
#include <iostream>
#include <string>
void printboard()
{
std::string array[9] = {"O","O","O","O","O","O","O","O","O"};
std::cout << " | | " << std::endl;
std::cout << " "; std::cout << array[2]; std::cout << " | "; std::cout << array[4]; std::cout << " | "; std::cout << array[3]; std::cout << " " << std::endl;
std::cout << " | | " << std::endl;
std::cout << "----|-----|----" << std::endl;
std::cout << " "; std::cout << array[5]; std::cout << " | "; std::cout << array[1]; std::cout << " | "; std::cout << array[3]; std::cout << " " << std::endl;
std::cout << " | | " << std::endl;
std::cout << "----|-----|----" << std::endl;
std::cout << " "; std::cout << array[7]; std::cout << " | "; std::cout << array[9]; std::cout << " | "; std::cout << array[8]; std::cout << " " << std::endl;
std::cout << " | | " << std::endl;
std::cout << " | | " << std::endl;
}
答案 0 :(得分:1)
这似乎有效:
#include <iostream>
#include <string>
void printboard()
{
std::string array[9] = { "O","O","O","O","O","O","O","O","O" };
std::cout << " | | " << std::endl;
std::cout << " "; std::cout << array[2]; std::cout << " | "; std::cout << array[4]; std::cout << " | "; std::cout << array[3]; std::cout << " " << std::endl;
std::cout << " | | " << std::endl;
std::cout << "----|-----|----" << std::endl;
std::cout << " | | " << std::endl;
std::cout << " "; std::cout << array[5]; std::cout << " | "; std::cout << array[1]; std::cout << " | "; std::cout << array[3]; std::cout << " " << std::endl;
std::cout << " | | " << std::endl;
std::cout << "----|-----|----" << std::endl;
std::cout << " | | " << std::endl;
std::cout << " "; std::cout << array[7]; std::cout << " | "; std::cout << array[6]; std::cout << " | "; std::cout << array[8]; std::cout << " " << std::endl;
std::cout << " | | " << std::endl;
}
int main()
{
printboard();
std::system("Pause");
return 0;
}
我注意到你的原始代码引用了大小为9的数组的[9],它超出了界限(该数组中的最后一个条目是[8]),并且在对齐框时包含了太多空格,所以价值有点偏差。希望这会有所帮助。