【发布时间】:2020-12-03 17:13:53
【问题描述】:
所以,我开始尝试学习c(我来自python),而我曾经学习python的一件事就是尝试在python中制作井字游戏。出于这个原因,我想我不妨尝试在 c 中制作井字游戏。所以,我想用我的代码首先完成的是将井字棋板上的每个位置存储为一个字符(将是 0 或 X),然后确保我找到了一种方法将它们打印到终端。您可以在我到目前为止制作的代码中看到这一点。 a1、a2、a3为前3,b1、b2、b3为中3,c1、c2、c3为后3:
a1 a2 a3
b1 b2 b3
c1 c2 c3
现在,我想确保我知道如何将一行井字游戏打印到命令行,您可以在底部看到puts(a1); puts(a2); puts(a3);。但是,当我运行这个程序时,我在终端中得到以下输出作为错误:
ticktacktoe.c:17:1: warning: parameter names (without types) in function declaration
char puts(a1); char puts(a2); char puts(a3);
^~~~
ticktacktoe.c:17:6: error: conflicting types for 'puts'
char puts(a1); char puts(a2); char puts(a3);
^~~~
In file included from ticktacktoe.c:1:0:
c:\mingw\include\stdio.h:677:41: note: previous declaration of 'puts' was here
_CRTIMP __cdecl __MINGW_NOTHROW int puts (const char *);
^~~~
ticktacktoe.c:17:1: warning: parameter names (without types) in function declaration
char puts(a1); char puts(a2); char puts(a3);
^~~~
ticktacktoe.c:17:21: error: conflicting types for 'puts'
char puts(a1); char puts(a2); char puts(a3);
^~~~
In file included from ticktacktoe.c:1:0:
c:\mingw\include\stdio.h:677:41: note: previous declaration of 'puts' was here
_CRTIMP __cdecl __MINGW_NOTHROW int puts (const char *);
^~~~
ticktacktoe.c:17:1: warning: parameter names (without types) in function declaration
char puts(a1); char puts(a2); char puts(a3);
^~~~
ticktacktoe.c:17:36: error: conflicting types for 'puts'
char puts(a1); char puts(a2); char puts(a3);
^~~~
In file included from ticktacktoe.c:1:0:
c:\mingw\include\stdio.h:677:41: note: previous declaration of 'puts' was here
_CRTIMP __cdecl __MINGW_NOTHROW int puts (const char *);
^~~~
我也尝试过使用 printf 函数,就像 printf('%c', a1); 一样,但这似乎也不起作用。这是我当前的代码,任何帮助将不胜感激:
#include<stdio.h>
#include<string.h>
//these are the variables for what is displayed on the tic tac toe board
char a1 = '0';
char a2 = '0';
char a3 = '0';
char b1 = '0';
char b2 = '0';
char b3 = '0';
char c1 = '0';
char c2 = '0';
char c3 = '0';
puts(a1); puts(a2); puts(a3);
【问题讨论】:
-
puts的签名是:int puts(const char *str)。你传给它一个字符串,而不是char。更改为以下内容(例如):char *a1 = "0";. -
尝试
putchar输出一个字符。 -
如果要打印单个字符,请使用
putchar。 -
我已经学习了基础知识,使用这本书toves.org/books/cpy。另外,你能解释一下我如何详细打印一个 char 变量吗?我不太明白你想说什么。
-
我建议您查看此类事物的数组,而不是每个单元格的命名变量。然后,您可以通过算法检查行、列和对角线。