你能用C语言写个三子棋游戏吗
时间: 2023-08-15 10:08:01 浏览: 62
当然可以,以下是C语言三子棋游戏的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define BOARD_SIZE 3
#define PLAYER1 'X'
#define PLAYER2 'O'
char board[BOARD_SIZE][BOARD_SIZE];
int player = 1;
void init_board() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
board[i][j] = ' ';
}
}
}
void draw_board() {
printf("\n");
printf(" 1 2 3\n");
for (int i = 0; i < BOARD_SIZE; i++) {
printf("%d ", i + 1);
for (int j = 0; j < BOARD_SIZE; j++) {
printf(" %c ", board[i][j]);
if (j != BOARD_SIZE - 1) {
printf("|");
}
}
printf("\n");
if (i != BOARD_SIZE - 1) {
printf(" ---|---|---\n");
}
}
printf("\n");
}
int check_win() {
// 检查行
for (int i = 0; i < BOARD_SIZE; i++) {
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ') {
return 1;
}
}
// 检查列
for (int i = 0; i < BOARD_SIZE; i++) {
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ') {
return 1;
}
}
// 检查对角线
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ') {
return 1;
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ') {
return 1;
}
return 0;
}
int check_tie() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == ' ') {
return 0;
}
}
}
return 1;
}
void switch_player() {
if (player == 1) {
player = 2;
} else {
player = 1;
}
}
int make_move(int row, int col) {
if (board[row][col] == ' ') {
if (player == 1) {
board[row][col] = PLAYER1;
} else {
board[row][col] = PLAYER2;
}
return 1;
} else {
return 0;
}
}
int main() {
int row, col, move;
char play_again = 'y';
printf("Welcome to Tic Tac Toe!\n");
while (play_again == 'y') {
init_board();
player = 1;
while (1) {
draw_board();
printf("Player %d's turn.\n", player);
printf("Enter row and column (e.g. 1 2): ");
move = scanf("%d %d", &row, &col);
if (move != 2 || row < 1 || row > 3 || col < 1 || col > 3) {
printf("Invalid input. Try again.\n");
continue;
}
row--;
col--;
if (make_move(row, col)) {
if (check_win()) {
draw_board();
printf("Player %d wins!\n", player);
break;
} else if (check_tie()) {
draw_board();
printf("It's a tie!\n");
break;
} else {
switch_player();
}
} else {
printf("That space is already taken. Try again.\n");
}
}
printf("Play again? (y/n) ");
scanf(" %c", &play_again);
}
printf("Thanks for playing!\n");
return 0;
}
```
这个示例代码包含了三子棋游戏的主要功能,包括初始化棋盘,绘制棋盘,检查胜负和平局,切换玩家,下棋等操作。您可以通过运行代码来体验游戏,也可以根据自己的需要进行修改和扩展。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)