gpt4 book ai didi

C++ - 基本井字棋程序打印空白屏幕

转载 作者:太空宇宙 更新时间:2023-11-04 04:59:10 24 4
gpt4 key购买 nike

我正在用 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;

}

最佳答案

这似乎有效:

#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]),并且您在对齐框时包含了太多空格,因此值有点倾斜。希望这会有所帮助。

关于C++ - 基本井字棋程序打印空白屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38594626/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com