欢迎来到代码驿站!

C代码

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

C语言实现三子棋(井字棋)算法

时间:2022-01-28 09:23:03|栏目:C代码|点击:

本文实例为大家分享了C语言实现三子棋算法,供大家参考,具体内容如下

游戏文件主干(test.c):

#include"game.h"
 
void menu()//游戏菜单
{
 printf("************************************************\n");
 printf("**********          1.play             *********\n");
 printf("**********          0.exit             *********\n");
 printf("************************************************\n");
}
void game()//游戏玩法实现主体(粗略)
{
 printf("三子棋:>\n");
 char board[LINE][ROW];
 Init_checkerboard(board, LINE, ROW);//初始化棋盘
 Print_checkerboard(board, LINE, ROW);//打印棋盘
 char ret = 0;
 while (1)//条件为1可以一直循环
 {
  Player_game(board, LINE, ROW);//玩家行动
  Print_checkerboard(board, LINE, ROW);//玩家行动后的棋盘
  ret = Iswin(board, LINE, ROW);//判断是否出现胜负
  if (ret != 'c')//出现胜负就跳出循环
  {
   break;
  }
  Commputer_game(board, LINE, ROW);//电脑回合
  Print_checkerboard(board, LINE, ROW);//电脑行动后的棋盘
  ret = Iswin(board, LINE, ROW);//判断是否出现胜负
  if (ret != 'c')
  {
   break;
  }
  
 }
  if (ret == '*')
  {
   printf("玩家胜利!\n");
  }
  else if (ret == '#')
  {
   printf("电脑胜利!\n");
  }
  else
  {
   printf("平局\n");
  }
 
 
}
 
int main()//整个源文件的入口
{
 int input = 0;
 srand((unsigned int)time(NULL));//电脑下棋的随机数
 do
 {
  menu();
  printf("请选择:>");
  scanf("%d", &input);
  switch (input)
  {
  case 1:
   game();//游戏函数主题(粗略)
   break;
  case 0:
   printf("您已退出游戏!\n");
   break;
  default:
   printf("输入数字无效,请重新输入");
   break;
  }
 } while (input);
 return 0;
}

游戏主干(game.c):

#include"game.h"//游戏主干(详细)
void Init_checkerboard(char board[LINE] [ROW], int line, int row)
{
 int i = 0;
 int j = 0;
 for ( i = 0; i <line; i++)
 {
  for ( j = 0; j <row; j++)
  {
   board[i][j]=' ';
  }
 }
}//棋盘的初始化
 
void Print_checkerboard(char board[LINE][ROW], int line, int row)
{
 int i = 0;
 for (i = 0; i < line; i++)
 {
  int j = 0;
  for (j = 0; j < line; j++)
  {
   printf(" %c ",board[i][j]);
   if (j < row - 1)
   {
    printf("|");
   }
  }
  printf("\n");
  if (i < row - 1)
  {
   for (j = 0; j < row; j++)
   
   {
    printf("---");
    if (j < row - 1)
    {
     printf("|");
    }
   }
  }
  printf("\n");
 }
}//生成3-3的棋盘
 
void Player_game(char board[LINE][ROW], int line, int row)
{
 printf("玩家下棋:>");
 int x = 0;
 int y = 0;
 while (1)
 {
  printf("请输入想要下棋的位置坐标:>");
  scanf("%d%d",&x,&y);
  if (x >= 1 && x <= line && y >= 1 && y <= row)
  {
   if (board[x - 1][y - 1] == ' ')
   {
    board[x - 1][y - 1] = '*';
    break;
   }
   else
   {
    printf("坐标被占用,请重新输入\n");
   }
  }
  else
  {
   printf("非法坐标,超出范围\n");
  }
 }
}//玩家行动的限制与实现
 
 
void Commputer_game(char board[LINE][ROW], int line, int row)
{
 int x = 0;
 int y = 0;
 while (1)
 {
  x = rand() % line;
  y = rand() % row;
  if (board[x][y]==' ')
  {
   board[x][y] = '#';
   break;
  }
 }
}//电脑行动的限制与实现
 
 
char con(char board[LINE][ROW], int line, int row)
{
 int i = 0;
 int j = 0;
 for (i = 0; i < line; i++)
 {
  for (j = 0; j < row; j++)
  {
   if (board[i][j] == ' ')
   {
    return 'a';
   }
  }
 }
}//判断游戏是否继续的实现
 
char rank(char board[LINE][ROW], int line, int row)
{
 int i = 0;
 int j = 0;
 for (i = 0; i < row; i++)
 {
  for (j = 0; j < line; j++)
  {
   if (board[j][i] == '*' && board[j][i] == board[j+1][i] && board[j+2][i] == board[j][i])
   {
    return 'q';
   }
   else if (board[j][i] == '#' && board[j][i] == board[j + 1][i] && board[j + 2][i] == board[j][i])
   {
    return 'w';
   }
  }
 }
}//判断一列的胜负条件
 
char Iswin(char board[LINE][ROW], int line, int row)//判断胜负的条件
{
 int i = 0;
 int j = 0;
 for (i = 0; i < line; i++)
 {
  for (j = 0; j < row; j++)
  {
 
   //判断行的胜负条件
   if (board[i][j] == board[i][j + 1] && board[i][j] == '*' && board[i][j] == board[i][j + 2])
   {
     return '*';
   }
   else if (board[i][j] == '#' && board[i][j] == board[i][j + 1] && board[i][j] == board[i][j + 2])
   {
    return '#';
   }
   //判断列
   else if (rank(board,LINE,ROW)=='q')
   {
     return '*';
   }
   else if (rank(board, LINE, ROW) == 'w')
   {
     return '#';
   }
   //对角线判断
   else if ((board[i][j] == board[i + 1][j + 1] && board[i][j] == board[i + 2][j + 2] && board[i][j] == '*') || (board[i][j + 2] == board[i + 1][j + 1] && board[i + 2][j] == board[i][j + 2] && board[i][j + 2] == '*'))
   {
     return '*';
   }
   else if ((board[i][j] == board[i + 1][j + 1] && board[i][j] == board[i + 2][j + 2] && board[i][j] == '#') || (board[i][j + 2] == board[i + 1][j + 1] && board[i + 2][j] == board[i][j + 2] && board[i][j + 2] == '#'))
   {
     return '#';
   }
   else if (con(board, LINE, ROW) == 'a')
   {
    return 'c';
   }
   else
   {
    return 'p';
   }
  }
 }
 return 0;
}

游戏的头文件(game.h):

#pragma once//保证被重复调用后依然只执行一次
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
 
#define LINE 3
#define ROW  3
 
//初始化棋盘:
extern void Init_checkerboard(char board[LINE][ROW], int line, int row);
//打印棋盘:
extern void Print_checkerboard(char board[LINE][ROW], int line, int row);
//玩家走:
extern void Player_game(char board[LINE][ROW], int line, int row);
//电脑走:
extern void Commputer_game(char board[LINE][ROW], int line, int row);
//判断胜负:
extern char Iswin(char board[LINE][ROW], int line, int row);

代码运行图:

上一篇:c++查询最短路径示例

栏    目:C代码

下一篇:10个步骤Opencv轻松检测出图片中条形码

本文标题:C语言实现三子棋(井字棋)算法

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

推荐教程

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

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

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

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

Copyright © 2020 代码驿站 版权所有