欢迎来到代码驿站!

C代码

当前位置:首页 > 软件编程 > C代码

C语言版实现三子棋游戏

时间:2022-01-01 11:29:22|栏目: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);

上一篇:浅谈char*类型返回值和字符串常量

栏    目:C代码

下一篇:基于C++全局变量的声明与定义的详解

本文标题:C语言版实现三子棋游戏

本文地址:http://www.codeinn.net/misctech/188745.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有