欢迎访问宙启技术站
智能推送

利用C语言实现三子棋(井字棋)小游戏

发布时间:2023-05-14 23:50:14

1.介绍

三子棋是一种简单、经典的棋类游戏,也称为井字棋或输赢棋。两位玩家轮流在 3 x 3 的棋盘上放置 X(先手)或 O(后手),谁先在横、竖、斜方向上连成三个相同的棋子,谁就赢了。

在本文中,将介绍如何使用 C 语言实现一个简单的三子棋游戏。

2.程序实现

2.1 初始化棋盘

首先,需要定义一个字符串数组作为棋盘。可以使用循环将每个格子赋值为空格。

char board[3][3];
// 初始化棋盘
for (int i = 0; i < 3; i++) {
  for (int j = 0; j < 3; j++) {
    board[i][j] = ' ';
  }
}

2.2 显示棋盘

接下来需要实现一个函数,用于在控制台输出当前的棋盘。可以使用循环遍历每个格子,然后输出对应的字符。

void display_board() {
  printf("  1 2 3
");
  for (int i = 0; i < 3; i++) {
    printf("%d ", i+1);
    for (int j = 0; j < 3; j++) {
      printf("%c", board[i][j]);
      if (j < 2) {
        printf("|");
      }
    }
    printf("
");
    if (i < 2) {
      printf("  -----
");
    }
  }
}

2.3 玩家交替下棋

玩家可以使用 scanf() 函数从控制台输入两个整数,分别表示行和列。程序需要检查这个位置是否已经被占据,如果没有则将当前玩家的标志写入对应的格子中。

void player_move(char player) {
  int row, col;
  printf("Player %c's turn. Please enter row and column: ", player);
  scanf("%d %d", &row, &col);
  if (row > 0 && row < 4 && col > 0 && col < 4 && board[row-1][col-1] == ' ') {
    board[row-1][col-1] = player;
  } else {
    printf("Invalid move. Please try again.
");
    player_move(player);
  }
}

2.4 判断胜负

在每次玩家下棋之后,需要检查是否有一方已经连成三个棋子,如果有,则游戏结束并宣布胜者。

可以使用一个嵌套循环来检查每种可能的获胜棋型:3 个横连、3 个竖连、2 个斜连。如果找到满足条件的棋型,就宣布对应的玩家胜利并返回 true;否则,如果棋盘已满,则宣布平局。

bool check_win(char player) {
  // Check horizontal lines.
  for (int i = 0; i < 3; i++) {
    if (board[i][0] == player && board[i][1] == player && board[i][2] == player) {
      printf("Player %c wins!
", player);
      return true;
    }
  }
  // Check vertical lines.
  for (int j = 0; j < 3; j++) {
    if (board[0][j] == player && board[1][j] == player && board[2][j] == player) {
      printf("Player %c wins!
", player);
      return true;
    }
  }
  // Check diagonal lines.
  if (board[0][0] == player && board[1][1] == player && board[2][2] == player) {
    printf("Player %c wins!
", player);
    return true;
  }
  if (board[0][2] == player && board[1][1] == player && board[2][0] == player) {
    printf("Player %c wins!
", player);
    return true;
  }
  // Check draw.
  int empty_count = 0;
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      if (board[i][j] == ' ') {
        empty_count++;
      }
    }
  }
  if (empty_count == 0) {
    printf("Game draw!
");
    return true;
  }
  // No winner yet.
  return false;
}

3.完整代码

#include <stdio.h>
#include <stdbool.h>

char board[3][3];

void display_board() {
  printf("  1 2 3
");
  for (int i = 0; i < 3; i++) {
    printf("%d ", i+1);
    for (int j = 0; j < 3; j++) {
      printf("%c", board[i][j]);
      if (j < 2) {
        printf("|");
      }
    }
    printf("
");
    if (i < 2) {
      printf("  -----
");
    }
  }
}

void player_move(char player) {
  int row, col;
  printf("Player %c's turn. Please enter row and column: ", player);
  scanf("%d %d", &row, &col);
  if (row > 0 && row < 4 && col > 0 && col < 4 && board[row-1][col-1] == ' ') {
    board[row-1][col-1] = player;
  } else {
    printf("Invalid move. Please try again.
");
    player_move(player);
  }
}

bool check_win(char player) {
  // Check horizontal lines.
  for (int i = 0; i < 3; i++) {
    if (board[i][0] == player && board[i][1] == player && board[i][2] == player) {
      printf("Player %c wins!
", player);
      return true;
    }
  }
  // Check vertical lines.
  for (int j = 0; j < 3; j++) {
    if (board[0][j] == player && board[1][j] == player && board[2][j] == player) {
      printf("Player %c wins!
", player);
      return true;
    }
  }
  // Check diagonal lines.
  if (board[0][0] == player && board[1][1] == player && board[2][2] == player) {
    printf("Player %c wins!
", player);
    return true;
  }
  if (board[0][2] == player && board[1][1] == player && board[2][0] == player) {
    printf("Player %c wins!
", player);
    return true;
  }
  // Check draw.
  int empty_count = 0;
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      if (board[i][j] == ' ') {
        empty_count++;
      }
    }
  }
  if (empty_count == 0) {
    printf("Game draw!
");
    return true;
  }
  // No winner yet.
  return false;
}

int main() {
  char player = 'X';
  bool game_over = false;

  // Initialize board.
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      board[i][j] = ' ';
    }
  }

  // Game loop.
  while (!game_over) {
    display_board();
    player_move(player);
    game_over = check_win(player);
    if (!game_over) {
      player = (player == 'X') ? 'O' : 'X';
    }
  }

  return 0;
}

4.小结

本文介绍了如何使用 C 语言实现一个简单的三子棋游戏。通过掌握本文中的知识,读者可以深入理解数组、函数、循环、条件语句等 C 语言的基础概念和语法,从而为以后学习更加高级、复杂的计算机编程奠定坚实的基础。