首页 > 编程语言 > C语言实现三子棋游戏附注释
2021
08-20

C语言实现三子棋游戏附注释

本文实例为大家分享了C语言实现三子棋游戏的具体代码,供大家参考,具体内容如下

概述

三子棋棋盘为九宫格形式,玩家和电脑分别轮流落子,若有一方有三个棋连在一起的情况则胜。

实现过程

1、玩家交互菜单创建
2、棋盘创建与初始化
3、玩家与电脑落子
4、判定胜负关系

多文件实现

头文件 game.h

#ifndef __GAME_H__
#define __GAME_H__
 
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <windows.h>
 
#define ROW 3
#define COL 3
 
#define INIT ' '         //宏定义 空
#define WHITE 'X'        //Player
#define BLACK 'O'        //Computer
 
 
#define DRAW 'D'
#define NEXT 'N'
 
#pragma warning(disable:4996)
 
extern void Game();
 
#endif

源文件 main.c

#include "game.h"
 
void Menu()
{
 printf("+-------------------------------+\n");
 printf("| 1. Play              0. Exit  |\n");
 printf("+-------------------------------+\n");
}
 
int main()
{
 int select = 0;
 int quit = 0;
 while (!quit)
 {
  Menu();
  printf("Please Select# ");
  scanf("%d", &select);
  switch (select){
  case 1:
   Game();
   break;
  case 0:
   quit = 1;
   break;
  default:
   printf("Enter Error, Try Again!\n");
   break;
  }
 }
 printf("ByeBye!\n");
 system("pause");
 return 0;
}

源文件game.c

#include "game.h"
 
static void InitBoard(char board[][COL], int row, int col)     //对九宫格棋盘进行初始化
{
 for (int i = 0; i < row; i++)
    {
  for (int j = 0; j < col; j++)  //双重循环 二维数组 矩阵
        {
   board[i][j] = INIT;             //INIT宏定义为空
  }
 }
}
 
static void ShowBoard(char board[][COL], int row, int col)  //函数 打印棋盘
{
 system("cls");     //调用dos命令窗口进行清屏操作,实现刷新棋盘
 printf(" ");
 for (int i = 0; i < col; i++)
    {
  printf("%4d", i+1);
 }
 printf("\n--------------\n");
 for (int i = 0; i < row; i++)
    {
  printf("%-2d", i+1); //2
  for (int j = 0; j < col; j++)
         {
   printf("| %c ", board[i][j]); //12
   }
  printf("\n--------------\n");
 }
}
 
static char IsEnd(char board[][COL], int row, int col)  
{
 for (int i = 0; i < row; i++)       //判断每列是否有三子连线
    {
  if (board[i][0] == board[i][1] && \
   board[i][1] == board[i][2] && \
   board[i][0] != INIT)
        {
   return board[i][0];
  }
 }
 
 for (int j = 0; j < COL; j++)      判断每行是否有三子连线
    {
  if (board[0][j] == board[1][j] && \
   board[1][j] == board[2][j] && \
   board[0][j] != INIT)
        {
   return board[0][j];
  }
 }
 if (board[0][0] == board[1][1] && \         //判断右对角线是否三子连线
  board[1][1] == board[2][2] && \
  board[1][1] != INIT)
    {
  return board[1][1];
 }
 if (board[0][2] == board[1][1] && \         //判断左对角线是否三子连线
  board[1][1] == board[2][0] && \
  board[1][1] != INIT)
    {
  return board[1][1];
 }
 for (int i = 0; i < row; i++)
   {
  for (int j = 0; j < col; j++)
       {
   if (board[i][j] == INIT)             //判空
            {       
    return NEXT;
   }
  }
 }
 return DRAW;           //平局
}
 
static void PlayerMove(char board[][COL], int row, int col)    //玩家落子
{
 int x = 0;
 int y = 0;      //定义<x,y>坐标方式实现玩家落子
 while (1)
   {
  printf("Please Enter Position <x,y># ");
  scanf("%d %d", &x, &y);
  if (x < 1 || y < 1 || x > 3 || y > 3)    //非法判断
          {
   printf("Enter Position Error!\n");
   continue;
    }
  if (board[x - 1][y - 1] == INIT)    //当前位置为空方可落子 
          {
   board[x - 1][y - 1] = WHITE;     
   break;
       }
  else
          {
   printf("Position Is Not Empty!\n");
    }
 }
}
static void ComputerMove(char board[][COL], int row, int col)   //电脑落子
{
 while (1){
  int x = rand() % row;     
  int y = rand() % col;      //利用随机数种子 实现电脑落子
  if (board[x][y] == INIT){
   board[x][y] = BLACK;
   break;
  }
 }
}
 
void Game()
{
 char board[ROW][COL];                //棋盘定义
 InitBoard(board, ROW, COL);          //棋盘初始化
 
 srand((unsigned long)time(NULL));     //生成随机数种子
 char result = 0;
 while (1){
  ShowBoard(board, ROW, COL);         //显示棋盘
  PlayerMove(board, ROW, COL);        //玩家落子
  result = IsEnd(board, ROW, COL);    //判断游戏状态
  if (result != NEXT){
   break;
  }
  ShowBoard(board, ROW, COL);         //显示棋盘
  ComputerMove(board, ROW, COL);      //电脑落子
  result = IsEnd(board, ROW, COL);    //判断游戏状态
  if (result != NEXT){ 
   break;
  }
 }
 ShowBoard(board, ROW, COL);
 switch (result){
 case WHITE:
  printf("You Win!\n");
  break;
 case BLACK:
  printf("You Lose!\n");
  break;
 case DRAW:
  printf("You == Computer!\n");
  break;
 default:
  printf("BUG!\n"); //Do Nothing!
  break;
 }
}

三种结果展示

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自学编程网。

编程技巧