[英]Replacing an underscore with a user entered character (In C)
This is for Homework 这是为了功课
I have to program a game of TicTacToe and the only issue I have left is formatting. 我必须编写一个TicTacToe游戏,而我剩下的唯一问题就是格式化。 Here is my function for the matrix where the user and computer have to enter their O's and X's in.
这是我的矩阵函数,用户和计算机必须在其中输入O和X。
void the_matrix() { // Like the movie
//Get rid of the underscores
int m;
printf("The current state of the board:\n");
for (m = 0; m < 3; m++) {
printf("%c_ %c_ %c_\n", table[m][0], table[m][1], table[m][2]);
}
printf("\n");
}
So the project calls for the compiler to display the game like this(I'm only showing what the first row should look like on this post for simplicity.): 因此,该项目要求编译器以这种方式显示游戏(为简单起见,我仅显示本文第一行的外观):
"_ _ _" [Ignore the quotation marks] “ _ _ _” [忽略引号]
then the user enter 'O' for the (1,1) position 然后用户在(1,1)位置输入“ O”
O _ _. O _ _。
When I run my program it looks like this 当我运行程序时,它看起来像这样
O_ _ _ O_ _ _
I know why the program's doing that, it's because my printf statement set it up to be that way. 我知道程序为什么要这样做,这是因为我的printf语句将其设置为这种方式。 But my question is how would I swap the underscore with an O or and X so the underscore wouldn't be displayed.
但是我的问题是我该如何将下划线替换为O或X,以便不显示下划线。
Any help would be appreicated! 任何帮助将不胜感激!
printf ("%c_")
will print a character followed by an underscore. printf ("%c_")
将打印一个字符,后跟一个下划线。
What you need is to printf ("%c")
and the argument should decide if you print an O, and X, or an underscore. 您需要的是
printf ("%c")
,该参数应决定您是否打印O,X和下划线。
On second thought, the easiest thing to do is probably to initialize your array to have _
in every space, and your logic should recognize that as "empty". 再次考虑,最简单的操作可能是将数组初始化为在每个空间中都包含
_
,并且您的逻辑应将其识别为“空”。 Then you can just print the character that appears in your array. 然后,您可以只打印出现在数组中的字符。
I'd distinguish the logic/state of the board from the way you display it. 我从显示方式上区分了电路板的逻辑/状态。 So the values of the board could have a clear meaning, like
0
for blank, 1
for player 1, and 2
for player 2, whereas the way you print it may be different then depending on whether you have a GUI or a terminal. 因此,电路板的值可能具有明确的含义,例如
0
表示空白, 1
表示播放器2
表示播放器2,但是打印方式可能会有所不同,具体取决于您是否具有GUI或终端。 And especially your algorithms will not depend on display issues, eg you will not have to change your algorithms just because you decide to display a blank field by a .
特别是您的算法将不依赖于显示问题,例如,您不必仅因为决定用来显示空白字段而更改算法
.
instead of a -
: 而不是
-
:
See the following code illustrating this approach: 请参见下面的代码说明此方法:
typedef enum {
BLANK = 0,
PLAYER1 = 1,
PLAYER2 = 2
} BoardValue;
BoardValue table[3][3] = { BLANK };
char displayValue(int state) {
return (state == PLAYER1 ? 'O' : (state == PLAYER2 ? 'X' : '-'));
}
void the_matrix() { // Like the movie
//Get rid of the underscores
int m;
printf("The current state of the board:\n");
for (m = 0; m < 3; m++) {
printf("%c %c %c\n", displayValue(table[m][0]), displayValue(table[m][1]), displayValue(table[m][2]));
}
printf("\n");
}
int main() {
table[0][0] = PLAYER1;
table[1][1] = PLAYER2;
the_matrix();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.