C語言版實現三子棋遊戲

如何用C語言來實現三子棋(井字棋),供大傢參考,具體內容如下

大致思路如下

1.先打印出菜單供用戶進行遊戲還是退出遊戲
2.編寫打印棋盤的函數
3.編寫用戶和電腦下棋的函數
4.最後對玩傢或用戶下棋後進行判斷輸贏

貼張勝利的圖片

實現打印菜單的函數

void menu()
{
 printf("******************************************\n");
 printf("*************     1. play    *************\n");
 printf("*************     2.exit     *************\n");
 printf("******************************************\n");
}

主函數的實現

int main()
{
 int input;
 srand((unsigned int)time(NULL));
 do
 {
  menu();
  printf("請選擇>");
  scanf("%d", &input);
  switch (input)
  {
  case 0:
   printf("退出\n");
   break;
  case 1:
   game();
   break;
  default:
   printf("輸入錯誤,");
   break;
  }
 } while (input);
 return 0;
}

根據用戶的選擇是玩遊戲還是退出,玩完一局後也可以在玩一局

game函數的實現

void game()
{
 char board[ROW][COL];
 //初始化棋盤
 InitBoard(board, ROW, COL);

 //打印棋盤
 PrinBoard(board, ROW, COL);

 char flag = 0;
 //下棋
 while (1)
 {
  PlayMove(board, ROW, COL);
  PrinBoard(board, ROW, COL);
  //判斷當前狀態
  flag = IsWin(board, ROW, COL);
  if (flag != 'c')
   break;

  //電腦下棋
  CompMove(board, ROW, COL);
  PrinBoard(board, ROW, COL);
  //判斷當前狀態
  flag = IsWin(board, ROW, COL);
  if (flag != 'c')
   break;
 }
 if (flag == '*')
  printf("恭喜玩傢獲勝\n");
 else if (flag == '#')
  printf("恭喜電腦獲勝\n");
 else
  printf("平局啦\n");
}

在每次玩傢或電腦下完棋後都應該進行判斷,判斷當前棋盤的狀態.

打印棋盤

首先應先打印棋盤,而且在每次下完棋後也應在打印一下棋盤

void PrinBoard(char board[ROW][COL], int row, int col)
{
 int i = 0;
 for (i = 0; i < row; i++)
 {
  int j = 0;
  for (int j = 0; j < col; j++)
  {
   printf(" %c ", board[i][j]);
   if (j < col - 1)
    printf("|");
  }
  printf("\n");

  if (i < row - 1)
  {
   for (j = 0; j < col; j++)
   {
    printf("---");
    if (j < col - 1)
     printf("|");
   }

  }
  printf("\n");
 }
}

判斷函數

IsWin函數應該有四種狀態分別為:玩傢贏(*)、電腦贏(#)、平局(d)和繼續©。

char IsWin(char board[ROW][COL], int row, int col)
{
 int i = 0;
 //判斷行
 for (i = 0; i < row; i++)
 {
  int j = 0;
  for (j = 0; j < col - 1; j++)
  {
   if (board[i][j] == ' ' || board[i][j] != board[i][j + 1])
    break;
  }
  if (j == col - 1)
   return board[i][j - 1];
 }
 //判斷列
 for (i = 0; i < col; i++)
 {
  int j = 0;
  for (j = 0; j < row - 1; j++)
  {
   if (board[j][i] == ' ' || board[j][i] != board[j + 1][i])
    break;
  }
  if (j == row - 1)
   return board[j][i];
 }

 //右斜
 for (i = 0; i < row - 1; i++)
 {
  if (board[i][i] == ' ' || board[i][i] != board[i + 1][i + 1])
   break;
 }
 if (i == row - 1)
  return board[i][i];
 int x = 1, y = col - 2;
 while (x < row&&y >= 0)
 {
  if (board[x][y] == ' ' || board[x][y] != board[x - 1][y + 1])
   break;
  x++;
  y--;
 }
 if (x == row && y < 0)
  return board[x - 1][y + 1];
 //判斷是否平局
 for (i = 0; i < row; i++)
 {
  int j = 0;
  for (j = 0; j < col; j++)
  {
   if (board[i][j] == ' ')
    return 'c';
  }
 }
 //平局
 return 'd';
}

下棋函數

電腦下棋時通過時間戳來生成隨機數,並判斷當前位置是否合法。每次下完棋後應該對棋盤進行判斷,通過調用函數IsWin根據返回值,判斷是繼續還是跳出。

void PlayMove(char board[ROW][COL], int row, int col)
{
 printf("請玩傢下棋>");
 while (1)
 {
  int x, y;
  printf("輸入您要下的位置");
  scanf("%d %d", &x, &y);
  if (x >= 1 && x <= row && y >= 1 && y <= col)
  {
   //判斷當前位置是否有棋子
   if (board[x - 1][y - 1] == ' ')
   {
    board[x - 1][y - 1] = '*';
    break;
   }
   else
    printf("此位置重復\n");
  }
  else
   printf("此坐標不合法,超出棋盤大小\n");
 }
}

void CompMove(char board[ROW][COL], int row, int col)
{
 printf("電腦下棋>\n");
 printf("\n");
 while (1)
 {
  int x = rand() % row;
  int y = rand() % col;
  if (board[x][y] == ' ')
  {
   board[x][y] = '#';
   break;
  }
 }
}

到這各個函數的實現已完成。

完整代碼如下

text.c

#include"game.h"

void menu()
{
 printf("******************************************\n");
 printf("*************     1. play    *************\n");
 printf("*************     2.exit     *************\n");
 printf("******************************************\n");
}
void game()
{
 char board[ROW][COL];
 //初始化棋盤
 InitBoard(board, ROW, COL);

 //打印棋盤
 PrinBoard(board, ROW, COL);

 char flag = 0;
 //下棋
 while (1)
 {
  PlayMove(board, ROW, COL);
  PrinBoard(board, ROW, COL);
  //判斷當前狀態
  flag = IsWin(board, ROW, COL);
  if (flag != 'c')
   break;

  //電腦下棋
  CompMove(board, ROW, COL);
  PrinBoard(board, ROW, COL);
  //判斷當前狀態
  flag = IsWin(board, ROW, COL);
  if (flag != 'c')
   break;
 }
 if (flag == '*')
  printf("恭喜玩傢獲勝\n");
 else if (flag == '#')
  printf("恭喜電腦獲勝\n");
 else
  printf("平局啦\n");
}
int main()
{
 int input;
 srand((unsigned int)time(NULL));
 do
 {
  menu();
  printf("請選擇>");
  scanf("%d", &input);
  switch (input)
  {
  case 0:
   printf("退出\n");
   break;
  case 1:
   game();
   break;
  default:
   printf("輸入錯誤,");
   break;
  }
 } while (input);
 return 0;
}

game.c

#include"game.h"


void InitBoard(char board[ROW][COL], int row, int col)
{
 int i = 0;
 for (i = 0; i < row; i++)
 {
  int j = 0;
  for (j = 0; j < col; j++)
  {
   board[i][j] = ' ';
  }
 }
}

void PrinBoard(char board[ROW][COL], int row, int col)
{
 int i = 0;
 for (i = 0; i < row; i++)
 {
  int j = 0;
  for (int j = 0; j < col; j++)
  {
   printf(" %c ", board[i][j]);
   if (j < col - 1)
    printf("|");
  }
  printf("\n");

  if (i < row - 1)
  {
   for (j = 0; j < col; j++)
   {
    printf("---");
    if (j < col - 1)
     printf("|");
   }

  }
  printf("\n");
 }
}


void PlayMove(char board[ROW][COL], int row, int col)
{
 printf("請玩傢下棋>\n");
 while (1)
 {
  int x, y;
  printf("輸入您要下的位置");
  scanf("%d %d", &x, &y);
  if (x >= 1 && x <= row && y >= 1 && y <= col)
  {
   //判斷當前位置是否有棋子
   if (board[x - 1][y - 1] == ' ')
   {
    board[x - 1][y - 1] = '*';
    break;
   }
   else
    printf("此位置重復\n");
  }
  else
   printf("此坐標不合法,超出棋盤大小\n");
 }
}

void CompMove(char board[ROW][COL], int row, int col)
{
 printf("電腦下棋>\n");
 printf("\n");
 while (1)
 {
  int x = rand() % row;
  int y = rand() % col;
  if (board[x][y] == ' ')
  {
   board[x][y] = '#';
   break;
  }
 }
}

char IsWin(char board[ROW][COL], int row, int col)
{
 int i = 0;
 //判斷行
 for (i = 0; i < row; i++)
 {
  int j = 0;
  for (j = 0; j < col - 1; j++)
  {
   if (board[i][j] == ' ' || board[i][j] != board[i][j + 1])
    break;
  }
  if (j == col - 1)
   return board[i][j - 1];
 }
 //判斷列
 for (i = 0; i < col; i++)
 {
  int j = 0;
  for (j = 0; j < row - 1; j++)
  {
   if (board[j][i] == ' ' || board[j][i] != board[j + 1][i])
    break;
  }
  if (j == row - 1)
   return board[j][i];
 }

 //右斜
 for (i = 0; i < row - 1; i++)
 {
  if (board[i][i] == ' ' || board[i][i] != board[i + 1][i + 1])
   break;
 }
 if (i == row - 1)
  return board[i][i];
 int x = 1, y = col - 2;
 while (x < row&&y >= 0)
 {
  if (board[x][y] == ' ' || board[x][y] != board[x - 1][y + 1])
   break;
  x++;
  y--;
 }
 if (x == row && y < 0)
  return board[x - 1][y + 1];
 //判斷是否平局
 for (i = 0; i < row; i++)
 {
  int j = 0;
  for (j = 0; j < col; j++)
  {
   if (board[i][j] == ' ')
    return 'c';
  }
 }
 //平局
 return 'd';
}

game.h

#pragma once
#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>
#include<time.h>
#include<stdlib.h>
enum
{
 ROW = 3,
 COL = 3
};

void InitBoard(char board[ROW][COL], int row, int col);
void PrinBoard(char board[ROW][COL], int row, int col);

void PlayMove(char board[ROW][COL], int row, int col);
void CompMove(char board[ROW][COL], int row, int col);

char IsWin(char board[ROW][COL], int row, int col);

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: