我正在学习 C,所以决定尝试制作一个井字游戏,使用 ASCII 艺术作为表格。
我还没有很多...
#include <stdio.h>
#define WIDTH 2;
#define HEIGHT 2;
int main (int argc, char *argv[]) {
printf("Welcome to Tic Tac Toe!\n\n");
int width = WIDTH;
int height = HEIGHT;
// Make grid
for (int y = 0; y <= height; y++) {
for (int x = 0; x <= width; x++) {
printf("%d%d", x, y);
if (x != width) {
printf("||");
}
}
if (y != height) {
printf("\n");
for (int i = 0; i < (width + (width * 4)); i++) {
printf("=");
}
printf("\n");
} else {
printf("\n");
}
}
// Ask for user input
printf("Please enter the cell where you would like to place an X, e.g. for example the first top left cell is '00'\n");
}
在命令行上运行时,我得到了这个输出
Welcome to Tic Tac Toe!
00||10||20
==========
01||11||21
==========
02||12||22
Please enter the cell where you would like to place an X, e.g. for example the first top left cell is '00'
现在,当我弄清楚如何获取多个字符的输入时(getchar()
到目前为止,我只知道如何用于获取单个字符,尽管对于本示例来说这可能工作正常),我想再次循环并放置一个X 对应的单元格。
我是否应该编写一个用于打印表格的函数,该函数采用诸如“int markerX,int markerY”之类的参数来放置X?
然后我将如何存储标记的位置,以便我可以检查游戏是否赢了?
我的选择哪个单元格放置标记是在命令行上要求用户输入游戏的最佳方式吗?
谢谢!