我目前正在寫一個簡單的井字遊戲,這被證明不是那麼簡單(至少對我來說,我是一個初學者)。我無法寫這應打印像這樣的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