C语言实现简单三子棋程序的完整攻略包括以下步骤:
- 定义游戏规则和棋盘
首先需要定义游戏规则,规定每个玩家的棋子形状和放置的位置等信息。同时需要定义一个3x3的棋盘数组,用于记录每个位置上的棋子情况。
#include <stdio.h>
#define ROW 3
#define COL 3
// 定义棋盘
char board[ROW][COL] = {
{' ', ' ', ' '},
{' ', ' ', ' '},
{' ', ' ', ' '}
};
void print_board()
{
printf(" 1 2 3\n");
printf("1 %c | %c | %c \n", board[0][0], board[0][1], board[0][2]);
printf(" ---|---|---\n");
printf("2 %c | %c | %c \n", board[1][0], board[1][1], board[1][2]);
printf(" ---|---|---\n");
printf("3 %c | %c | %c \n", board[2][0], board[2][1], board[2][2]);
}
- 实现玩家交替落子
按照游戏规则和棋盘的定义,玩家交替进行游戏。需要实现一个函数,根据玩家输入的行列数目,判断该位置是否可以下子,如果可以,就在对应位置上放置该玩家的棋子,并更新棋盘。同时需要判断游戏是否结束,如果棋盘已经填满或者某个玩家已经连成三个棋子,就宣告游戏结束。
int is_full()
{
int i, j;
for (i = 0; i < ROW; i++) {
for (j = 0; j < COL; j++) {
if (board[i][j] == ' ')
return 0;
}
}
return 1;
}
int check_win(char c)
{
int i;
for (i = 0; i < ROW; i++) {
if (board[i][0] == c && board[i][1] == c && board[i][2] == c)
return 1;
if (board[0][i] == c && board[1][i] == c && board[2][i] == c)
return 1;
}
if (board[0][0] == c && board[1][1] == c && board[2][2] == c)
return 1;
if (board[0][2] == c && board[1][1] == c && board[2][0] == c)
return 1;
return 0;
}
void play_game()
{
int row, col;
char c = 'X';
while (1) {
printf("Player %c's turn:\n", c);
printf("Please input row and col (e.g. 1 2):\n");
scanf("%d %d", &row, &col);
if (row < 1 || row > 3 || col < 1 || col > 3) {
printf("Invalid input, please input valid row and col (1~3):\n");
continue;
}
if (board[row-1][col-1] != ' ') {
printf("The place has already been taken, please input again:\n");
continue;
}
board[row-1][col-1] = c;
print_board();
if (check_win(c)) {
printf("Player %c wins!\n", c);
break;
}
if (is_full()) {
printf("Game ends in a draw!\n");
break;
}
if (c == 'X')
c = 'O';
else
c = 'X';
}
}
- 进行游戏并输出结果
在主函数中,调用上述三个子函数,依次执行游戏规则、玩家交替落子和胜负判断等操作。
int main(void)
{
printf("Welcome to the 3x3 Tic-tac-toe game!\n");
print_board();
play_game();
return 0;
}
- 示例1
假设玩家1执黑子,玩家2执白子,依次放置棋子如下:
Welcome to the 3x3 Tic-tac-toe game!
1 2 3
1 | |
---|---|---
2 | |
---|---|---
3 | |
Player X's turn:
Please input row and col (e.g. 1 2):
1 1
1 2 3
1 X | |
---|---|---
2 | |
---|---|---
3 | |
Player O's turn:
Please input row and col (e.g. 1 2):
2 2
1 2 3
1 X | |
---|---|---
2 | O |
---|---|---
3 | |
Player X's turn:
Please input row and col (e.g. 1 2):
1 2
1 2 3
1 X | X |
---|---|---
2 | O |
---|---|---
3 | |
Player O's turn:
Please input row and col (e.g. 1 2):
3 3
1 2 3
1 X | X |
---|---|---
2 | O |
---|---|---
3 | | O
Player X's turn:
Please input row and col (e.g. 1 2):
2 3
1 2 3
1 X | X |
---|---|---
2 | O | X
---|---|---
3 | | O
Player O's turn:
Please input row and col (e.g. 1 2):
3 1
1 2 3
1 X | X |
---|---|---
2 | O | X
---|---|---
3 O | | O
Player X's turn:
Please input row and col (e.g. 1 2):
2 1
1 2 3
1 X | X |
---|---|---
2 X | O | X
---|---|---
3 O | | O
Player X wins!
输出结果为“Player X wins!”,表示玩家1胜利。
- 示例2
假设玩家1执白子,玩家2执黑子,依次放置棋子如下:
Welcome to the 3x3 Tic-tac-toe game!
1 2 3
1 | |
---|---|---
2 | |
---|---|---
3 | |
Player O's turn:
Please input row and col (e.g. 1 2):
1 1
1 2 3
1 O | |
---|---|---
2 | |
---|---|---
3 | |
Player X's turn:
Please input row and col (e.g. 1 2):
2 2
1 2 3
1 O | |
---|---|---
2 | X |
---|---|---
3 | |
Player O's turn:
Please input row and col (e.g. 1 2):
1 2
1 2 3
1 O | O |
---|---|---
2 | X |
---|---|---
3 | |
Player X's turn:
Please input row and col (e.g. 1 2):
2 1
1 2 3
1 O | O |
---|---|---
2 X | X |
---|---|---
3 | |
Player O's turn:
Please input row and col (e.g. 1 2):
1 3
1 2 3
1 O | O | O
---|---|---
2 X | X |
---|---|---
3 | |
Player O wins!
输出结果为“Player O wins!”,表示玩家1失败。
以上就是用C语言实现简单三子棋程序的完整攻略。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C语言实现简单三子棋程序 - Python技术站