2012-01-13 76 views
1

我目前正在写一个简单的井字游戏,这被证明不是那么简单(至少对我来说,我是一个初学者)。我无法写这应打印像这样的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; 
    } 
} 

它有点乱,因为我说我太新本。如果您有任何建议要清理它,请随时与我联系。

+0

当这个问题来了个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

回答

2

代码中有很多错误。这里有一些:

使用strcmp功能在C

str[] == "upperLeft"

比较字符串不是有效的C表达式。

同样在此定义:

char move[]; 

不是有效的阵列的定义,它忽略了[]之间元件的数量。

此外

scanf("%s", &move); 

%s转换规范需要一个指针到char作为参数。 &move的值是一个指向数组的指针,而不是指向char的指针。调用函数这种方式来代替:

scanf("%s", move); 
0

一个新行,'\n'"\n",算作一个字符,即使最终端需要两个字符输出。 (在DOS/Windows中,这是通过CRT将\n转换为CRLF来实现的,而在Unix中,这是由TTY驱动程序完成的。)

1

尝试循环遍历行和列。 我不想写答案给你,但像...

function print_board() 
    iterate over rows 
     iterate over columns 
      print board location 
      if this is not the third spot, print a vertical bar character 
     print a newline followed be a line of dashes followed by a newline 
2

使用strcmp功能在C

str[] == "upperLeft" 

比较字符串不是有效的C表达式。

同样在此定义:

char move[]; 

不是有效的阵列的定义,它忽略了[]之间元件的数量。

此外,

scanf("%s", &move); 

%s转换规范需要一个指针到char作为参数。 &move的值是一个指向数组的指针,而不是指向char的指针。调用函数这种方式来代替:

scanf("%s", move); 
0

这是井字 无效板()printf函数 {

printf("\n\n\tTic Tac Toe\n\n"); 

printf("Player 1 (X) - Player 2 (O)\n\n\n"); 


printf("  |  |  \n"); 
printf(" %c | %c | %c \n", square[1], square[2], square[3]); 

printf("_____|_____|_____\n"); 
printf("  |  |  \n"); 

printf(" %c | %c | %c \n", square[4], square[5], square[6]); 

printf("_____|_____|_____\n"); 
printf("  |  |  \n"); 

printf(" %c | %c | %c \n", square[7], square[8], square[9]); 

printf("  |  |  \n\n"); 

}

+1

尝试提供一些解释,而不仅仅是代码。 StackOverflow答案应该旨在[教导一个人钓鱼](https://zh.wiktionary.org/wiki/give_a_man_a_fish_and_you_feed_him_for_a_day ;_teach_a_man_to_fish_and_you_feed_him_for_a_lifetime) – Adrian 2017-08-11 18:57:53