更改网格内的输出

Change output inside grid

本文关键字:输出 网格      更新时间:2023-10-16

我正在尝试使用以下控制台显示类来显示井字游戏 - 这是用于作业- 我得到了以下代码来使用-控制台显示一个网格,其中列标记为 0-9,并显示标记为 0-9 的行 - 控制台还在网格内显示随机字符 - 我需要将随机字符更改为我选择的字符 - 但我在代码中找不到在中间网格中设置空间的部分。

如果有人能帮助我确定代码中决定网格中显示的内容的部分,我将不胜感激。

注意 - 必须将字符集更改为(使用多字节字符集)才能看到程序的正确输出。 转换器字符集位于 Visual Studio 中的"项目属性"-"常规"-"项目默认值"下

 #include "stdafx.h"
    #include <windows.h>
    #include <iostream>
    #include <tchar.h>

    using std::cin;
    bool setxychar( int x, int y, const char* pBuf, int len )
    {
        HANDLE console_handle;
        COORD cursor_coord;
        DWORD actual=0;
        console_handle= GetStdHandle(STD_OUTPUT_HANDLE);
        cursor_coord.X=x;       // (40-(strlen(buffer)/2)); 
        cursor_coord.Y=y;
        //This would be the equivalent to gotoxy(x,y).
        if (SetConsoleCursorPosition(console_handle,cursor_coord)) 
        {
            // Look up this function in your VC++ help/index
            WriteConsole(console_handle,pBuf,len,&actual,NULL);
            return true;
        }
        return false;
    }
    // setxychar()  - Overloaded
    // this method allows you to print a single character at a location
    bool setxychar( int x, int y, char Buf )
    {
        return( setxychar( x, y, &Buf, 1 ) );
    }
    int _tmain(int argc, _TCHAR* argv[])
    {
        const char graybar = 'xB2';
        char *buffer ="Tic Tac Toe";
        int x=0, y=0;
        for(x=0; x<80; x++) // vertical borders
        {
            y=0;
            setxychar(x,y,&graybar,1);
            y=23;
            setxychar(x,y,&graybar,1);
        }
        for( y=0; y<24; y++ )   // horizontal borders
        {
            x=0;
            setxychar(x,y,&graybar,1);
            x=79;
            setxychar(x,y,&graybar,1);
        }
        setxychar(3,3,' ');  // position pointer
        printf( "  0 1 2 3 4 5 6 7 8 9 A  B C D E Fn" );
        char testchar = 0;      // A warning is put up by the compiler 
        for(y=4; y<20; y++ )    // "warning C4309: 'initializing' : truncation of constant value"
        {                       // 80 hex is the same as -128,  Hex is just easier
            setxychar(1,y,' ');  // position pointer
            printf( "%X", 16*(y-4) );
            for(x=6; x<38; x+=2)
            {
                setxychar(x,y,&testchar,1);
                testchar++;
            }
        }
        // Putting text to screen just for fun
        setxychar(52,6,buffer,(int)strlen(buffer));
        setxychar(1,22,' ');  // position pointer
        cin.get();
        return 0;
    }

看起来相关部分是:

char testchar = 0; 

setxychar(x,y,&testchar,1); 

testchar++;