我目前正在写一个简单的井字游戏,这被证明不是那么简单(至少对我来说,我是一个初学者)。我无法写这应打印像这样的display_board功能:显示板在井字游戏
_|_|_
_|_|_
| |
而且当玩家板标志着一个特定的位置添加一个X或O。我打算做的是把这一切都放到一个字符串中,但这有点令人困惑,特别是我想添加的新行,所以电路板正常出来。一个新的直线运算符在一个字符串中算作一个还是两个字符? 如果你想看看我的代码进行比赛,那就是:
#include <stdio.h>
#include <stdbool.h>
int board[3][3] = {
{0, 0, 0},
{0, 0, 0},
{0, 0, 0}
};
int main (void)
{
int const user1 = 1;
int const user2 = 2;
char move[];
while (! all_locations_filled()) {
printf("User-1, please enter your move:");
scanf("%s", &move);
if(valid_location(move)) {
mark_location(user1, move);
display_board();
else if(won_the_game(user1) {
printf("Congratulations User-1, You Won the Game!");
break;
}
else {
printf("Invalid Move");
}
}
printf("User-2, please enter your move:");
scanf("%s", &move);
if(valid_location(move)) {
mark_location(user2, move);
display_board();
else if(won_the_game(user2) {
printf("Congratulations User-2, You Won the Game!");
break;
}
else {
printf("Invalid Move");
}
}
}
bool valid_location(char str[]) {
if (str[] == "upperLeft" || str[] == "up" || str[] == "upperRight" || str[] == "left" || str[] == "center" || str[] == "right" || str[] == "lowerLeft" || str[] == "down" || str[] == "lowerRight") {
return true;
}
}
void mark_location(int userU, char str[]) {
if (str[] == "upperLeft") {
board[0][0] = userU;
else if (str[] == "up") {
board[0][1] = userU;
else if (str[] == "upperRight") {
board[0][2] = userU;
else if (str[] == "left") {
board[1][0] = userU;
else if (str[] == "center") {
board[1][1] = userU;
else if (str[] == "right") {
board[1][2] = userU;
else if (str[] == "lowerLeft") {
board[2][0] = userU;
else if (str[] == "down") {
board[2][1] = userU;
else if (str[] == "lowerRight") {
board [2][2] = userU;
}
}
它有点乱,因为我说我太新本。如果您有任何建议要清理它,请随时与我联系。
当这个问题来了个CodeGolf.SE为[Noughts and Crosses(aka Tic-Tac-Toe)](http://codegolf.stackexchange.com/q/1054/78),我用curses来控制显示。这可能是你想要的更多支持,但它仍然是一个相当低的水平。 (看无版本的版本,BTW。) – dmckee 2012-01-13 21:37:54