试图在C中制作TIC-TAC-TOE游戏
因此,我试图在C中制作一款TIC-TAC-TOE游戏,并且我已经写出了大部分代码,没有错误消息,但是我想知道要添加什么代码以使计算机也可以玩游戏。我只是不知道在这里还要添加什么,以确保游戏可以进行交互 - 不仅仅是用户输入一个字母并完成的用户。如果有人可以提供帮助,那将不胜感激!代码如下:
#include <time.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
system("clear");
const char ROWS = 3;
const char COLS = 3;
int rows, cols, tile = 1, choice;
for (rows = 0; rows < ROWS; rows++)
{
for (cols = 0; cols < COLS; cols++)
{
printf("[%d]", tile++);
}
printf("\n");
}
printf("\nWhere would you like to put your X: ");
scanf("%d", &choice);
tile = 1;
for (rows = 0; rows < ROWS; rows++)
{
for (cols = 0; cols < COLS; cols++)
{
if (tile == 1 || tile == 2 || tile == 3 || tile == 4 || tile == 5 || tile == 6 || tile== 7 || tile == 8 || tile == 9)
{
printf("[%d]", tile++);
}
if (choice == tile)
{
tile -= tile;
printf("[X]");
choice += 1000;
}
}
printf("\n");
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您不必担心实现计算机播放器,而是您的第一步是为第二个人提供支持。
首先,您将需要一些数组来表示游戏板的状态。类型
char
的多维数组似乎合适,其中每个char
可以具有以下三个值之一:'e''
for empty'x'
对于第一个播放器'o'
对于第二播放器,您可以定义这样的数组:
另外,您还需要一个额外的变量来跟踪当前的转弯。类型
bool
的变量first_players_turn
可能是合适的。当它是第一个播放器时,该变量应将其设置为true
,否则为false
。我还建议您创建自己的功能
print_board
将游戏板打印到屏幕上。您还需要一个函数
estuatue_game_state
才能确定游戏的状态,这可能是以下内容之一:要表示这4个可能的游戏状态,我建议您使用
代码>
,像这样:
您现在可以声明函数
evaluate_game_state
像这样:只要函数
evarueatue_game_state
returnsgamestate_undecided
,您将必须提示用户在循环中输入。这是我对上述所有内容的实现,我已经对其进行了一些测试并似乎有效,但是我尚未测试所有角色案例。我从 get_int_int_from_user >修改,以便它接受 variadic参数,以便我可以通过包含<<的字符串代码>%d 到该功能,并将其视为
printf
格式指定符。尽管您使用printf
和scanf
也可以使用,但该解决方案的缺点是scanf
不执行适当的输入验证,因此我使用了我的功能get_int_from_user
而不是。该程序具有以下行为:
如果您想制作一个好的计算机对手,那么我建议您看看 Wikipedia关于minimax的文章。
Instead of worrying about implementing a computer player, I think your first step shoud be to offer support for a second human player.
First, you will need some array to represent the state of the game board. A multi-dimensional array of type
char
seems appropriate, in which eachchar
can have one of three values:'E'
for empty'X'
for first player'O'
for second playerYou could define the array like this:
Also, you will need an additional variable to keep track of whose turn it currently is. A variable of type
bool
with the namefirst_players_turn
would probably be appropriate. When it is the first player's turn, this variable should be set totrue
, otherwise tofalse
.I also suggest that you create your own function
print_board
for printing the game board to the screen.You will also need a function
evaluate_game_state
in order to determine the state of the game, which can be one of the following:To represent these 4 possible game states, I recommend that you use an
enum
, like this:You can now declare the function
evaluate_game_state
like this:As long as the function
evaluate_game_state
returnsGAMESTATE_UNDECIDED
, you will have to prompt the user for input in a loop.Here is my implementation of everything stated above, which I have tested a bit and seems to work, but I have not tested all corner cases. I took over the function
get_int_from_user
from this answer of mine from another question, and made a slight modification, so that it accepts variadic arguments, so that I can pass a string containing%d
to that function and it treats it as aprintf
format specifier. Although your implementation of usingprintf
andscanf
would also work, that solution has the disadvantage thatscanf
does not perform proper input validation, so I used my functionget_int_from_user
instead.This program has the following behavior:
If you want to make a good computer opponent, then I suggest that you take a look at the Wikipedia article on Minimax.