c语言 三子棋
时间: 2023-06-22 11:29:50 浏览: 102
C语言版三子棋
以下是一个简单的 C 语言三子棋游戏示例:
```c
#include <stdio.h>
// 定义棋盘大小
#define BOARD_SIZE 3
// 定义棋子类型
#define EMPTY 0
#define PLAYER1 1
#define PLAYER2 2
// 定义玩家类型
#define HUMAN 1
#define COMPUTER 2
// 定义函数声明
void initialize_board(int board[][BOARD_SIZE]);
void print_board(int board[][BOARD_SIZE]);
int make_move(int board[][BOARD_SIZE], int row, int col, int player);
int get_winner(int board[][BOARD_SIZE]);
int get_move(int board[][BOARD_SIZE], int player);
int check_move(int board[][BOARD_SIZE], int row, int col);
// 主函数
int main() {
int board[BOARD_SIZE][BOARD_SIZE];
int winner = 0;
int player = PLAYER1;
int move_row = 0;
int move_col = 0;
// 初始化棋盘
initialize_board(board);
// 游戏循环
while (winner == 0) {
// 打印棋盘
print_board(board);
// 获取玩家移动
if (player == HUMAN) {
printf("Player %d's turn\n", player);
printf("Enter row (1-%d) and column (1-%d): ", BOARD_SIZE, BOARD_SIZE);
scanf("%d %d", &move_row, &move_col);
}
else {
printf("Computer's turn\n");
get_move(board, player);
}
// 检查合法性并进行移动
if (check_move(board, move_row - 1, move_col - 1)) {
make_move(board, move_row - 1, move_col - 1, player);
winner = get_winner(board);
player = (player == PLAYER1) ? PLAYER2 : PLAYER1;
}
else {
printf("Invalid move. Please try again.\n");
}
}
// 游戏结束
print_board(board);
if (winner == PLAYER1) {
printf("Player 1 wins!\n");
}
else if (winner == PLAYER2) {
printf("Player 2 wins!\n");
}
else {
printf("Tie game.\n");
}
return 0;
}
// 初始化棋盘
void initialize_board(int board[][BOARD_SIZE]) {
int i, j;
for (i = 0; i < BOARD_SIZE; i++) {
for (j = 0; j < BOARD_SIZE; j++) {
board[i][j] = EMPTY;
}
}
}
// 打印棋盘
void print_board(int board[][BOARD_SIZE]) {
int i, j;
printf("\n");
for (i = 0; i < BOARD_SIZE; i++) {
for (j = 0; j < BOARD_SIZE; j++) {
switch (board[i][j]) {
case EMPTY:
printf(" ");
break;
case PLAYER1:
printf("X");
break;
case PLAYER2:
printf("O");
break;
}
if (j < BOARD_SIZE - 1) {
printf("|");
}
}
printf("\n");
if (i < BOARD_SIZE - 1) {
printf("-+-+-\n");
}
}
printf("\n");
}
// 进行移动
int make_move(int board[][BOARD_SIZE], int row, int col, int player) {
if (board[row][col] == EMPTY) {
board[row][col] = player;
return 1;
}
else {
return 0;
}
}
// 检查是否获胜
int get_winner(int board[][BOARD_SIZE]) {
int i, j;
int row_sum = 0;
int col_sum = 0;
int diag1_sum = 0;
int diag2_sum = 0;
// 检查每行和每列
for (i = 0; i < BOARD_SIZE; i++) {
row_sum = 0;
col_sum = 0;
for (j = 0; j < BOARD_SIZE; j++) {
row_sum += board[i][j];
col_sum += board[j][i];
}
if (row_sum == PLAYER1 * BOARD_SIZE || col_sum == PLAYER1 * BOARD_SIZE) {
return PLAYER1;
}
else if (row_sum == PLAYER2 * BOARD_SIZE || col_sum == PLAYER2 * BOARD_SIZE) {
return PLAYER2;
}
}
// 检查对角线
for (i = 0; i < BOARD_SIZE; i++) {
diag1_sum += board[i][i];
diag2_sum += board[i][BOARD_SIZE - i - 1];
}
if (diag1_sum == PLAYER1 * BOARD_SIZE || diag2_sum == PLAYER1 * BOARD_SIZE) {
return PLAYER1;
}
else if (diag1_sum == PLAYER2 * BOARD_SIZE || diag2_sum == PLAYER2 * BOARD_SIZE) {
return PLAYER2;
}
// 检查是否平局
for (i = 0; i < BOARD_SIZE; i++) {
for (j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == EMPTY) {
return 0;
}
}
}
return -1;
}
// 获取玩家移动
int get_move(int board[][BOARD_SIZE], int player) {
// todo: 实现计算机 AI
}
// 检查移动是否合法
int check_move(int board[][BOARD_SIZE], int row, int col) {
if (row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE && board[row][col] == EMPTY) {
return 1;
}
else {
return 0;
}
}
```
这只是一个简单的示例,计算机 AI 部分需要进行更复杂的实现。
阅读全文