C++的井字遊戲如何 coding?
class TicTacToe
{
private:
//Name: horizontal
//Description: checks for a winner horizontally
//Preconditions: NONE
//Postconditions: returns a bool value (True = winner found)
bool horizontal();
//Name: vertical
//Description: checks for a winner vertically
//Preconditions: NONE
//Postconditions: returns a bool value (True = winner found)
bool vertical();
//Name: diagonal
//Description: checks for a winner diagonally
//Preconditions: NONE
//Postconditions: returns a bool value (True = winner found)
bool diagonal();
public: //The Game Board (array form)
int board[9];
int winningMoves[3];
bool winPresent;
int winner;
//Name: Default Constructor
//Description: initalizes the game board (board[9]) spaces to -1
TicTacToe();
//Name: checkWin
//Description: checks if an endgame has been reached. An checkWin is either
// a player 1 win, player 2 win, or a tie
//Precondition: None (board[9]) is examined.
//Postcondition: a bool value is returned
bool checkWin();
//Name: availableSpace
//Description: Checks for available space
//Precondition: NONE
//Postcondition: returns bool value
bool availableSpace();
//Name: validateMove
//Description: Checks if user entered a valid move
//Precondition: passes the move by value
//Postcondition: returns a bool value (True is valid)
bool validateMove(int playerMove);
//Name: updateBoard
//Description: updates board with new, validated move
//Precondition: new move is passed by value
//Postcondition: NONE
bool updateBoard(int newMove, int player, int &winCode);
};
#endif
2007-02-11 04:07:54 · 1 個解答 · 發問者 K 3 in 電腦與網際網路 ➔ 程式設計
如何 coding不用照上面的範例大綱
2007-02-11 04:09:47 · update #1
好棒的程式!!
但我還是有點問題, 打0是出去,但打1A並沒開始還有如果我想加入好
1) New Game
2) Help
3) Quit
等選項該如何做呢是該寫新的class嗎?
2007-02-14 04:17:01 · update #2
這是 Borland C 的版本
有些部分你要改成 C++ 的語法
此版本電腦會隨機放子
#include
#include
int a[3][3];
void show_position(void);
int check(void);
int main()
{
int i, j, player, pp, done;
char buf[3];
for (i=0; i< 3; i++)
for (j=0; j<3; j++)
a[i][j] = 0;
pp = 0;
done = 0;
player = 0;
do
{
show_position();
printf("Player %d select a position (like 1A; 0 to exit.): ", player);
scanf("%s", &buf);
if (buf[0] == '0') break;
i = buf[0] - '1';
j = buf[1] - 'A';
if ((i < 0) || (2 < i) || (j < 0) || (2 < j)) continue;
if (a[i][j] > 0) continue;
a[i][j] = player+1;
player = 1 - player;
done = check();
pp++;
} while ((done == 0) && (pp < 9));
printf("\n");
show_position();
player = 1 - player;
if (done > 0)
printf("Player %d WON!\n", player+1);
else
printf("It is TIE!\n");
return;
}
void show_position()
{
int i, j;
printf(" A B C\n");
for (i=0; i< 3; i++)
{
printf("%d ", i+1);
for (j=0; j<3; j++)
if (a[i][j] == 1)
printf("O ");
else if (a[i][j] == 2)
printf("X ");
else
printf("_ ");
printf("\n");
}
}
int check()
{
int i, j;
for (i=0; i< 3; i++)
if ((a[i][0] == a[i][1]) && (a[i][0] == a[i][2]))
if (a[i][0] > 0) return a[i][0];
for (j=0; j< 3; j++)
if ((a[0][j] == a[1][j]) && (a[0][j] == a[2][j]))
if (a[0][j] > 0) return a[0][j];
if ((a[0][0] == a[1][1]) && (a[0][0] == a[2][2]))
if (a[0][0] > 0) return a[0][0];
if ((a[0][2] == a[1][1]) && (a[0][2] == a[2][0]))
if (a[0][2] > 0) return a[0][2];
return(0);
}
如果有問題, 請來函討論. 不然, 我可能會錯失你再補充的疑點.
2007-02-11 07:43:01 · answer #1 · answered by JJ 7 · 0⤊ 0⤋