• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C语言 > 基于C语言实现五子棋游戏完整实例代码

基于C语言实现五子棋游戏完整实例代码

作者: 字体:[增加 减小] 来源:互联网 时间:2017-05-28

通过本文主要向大家介绍了c语言五子棋游戏代码,五子棋游戏c语言,五子棋游戏c,c语言小游戏五子棋,五子棋游戏在线玩等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了基于C语言实现五子棋游戏的方法,代码备有比较完整的注释,可以帮助读者更好的加以理解。

五子棋游戏代码如下:

/*
 * 使用键盘的上下左右键移动棋盘,空格键表示下棋,ESC键退出程序
 */
#include <stdio.h>
#include <stdlib.h>
#include <bios.h>
#include <graphics.h>
#include<malloc.h>
/*
 * 对应键盘键的十六进制数字
 */
#define ESC 0x11b
#define UP 0x4800
#define DOWN 0x5000
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define BLANK 0x3920

#define PLAYER1 1
#define PLAYER2 2
#define COMPUTER 2
#define LENGTH 15
#define SEARCH_DEEP 2
/*
 * 用来在函数can_expand()查找可以扩展的节点步长
 */
#define STEP 1

/************全局变量定义***************/
int       x1 = 240,
        y1 = 240,
        oldx = 240,
        oldy = 240;
int       key_mode;
int       key_net;
int       step_sum = 0;
int       chessman[LENGTH][LENGTH];
int       depth = 2; /* 搜索的深度 */
int       a = 0,
        b = 0;
int       flag_run;
int       win_flag = 0;


typedef struct five_chess *point;
struct five_chess {
  int       x;
  int       y;
  int       layer;
  double     value;
  double     score;
  int       chess[LENGTH][LENGTH];
  int       record[LENGTH][LENGTH];
} A;

int       stack_deep0 = 0;
point      stack_c[10];
point      close[600];

void
push(point s0)
{
  if (stack_deep0 < 10)
 stack_c[stack_deep0++] = s0;
}

point
top()
{
  if (stack_deep0 > 0)
 return stack_c[stack_deep0 - 1];
  /*else return 一个什么样的东西?*/
}

void
pop()
{
  if (stack_deep0 > 0)
 stack_deep0--;
}

int
is_empty()
{
  if (stack_deep0 != 0)
 return 1;
  else
 return 0;
}

 

/************函数的声明**************/
void      five();
void      show();
int       win_or_not(int x0, int y0, int who,
   int chessman[LENGTH][LENGTH], int a);
void      set_chessman();
void      print_result();
/************评价函数部分************/
double     score_row(int i, int j, int chessman[LENGTH][LENGTH]);
double     score_col(int i, int j, int chessman[LENGTH][LENGTH]);
double     score_diag_45(int i, int j, int chessman[LENGTH][LENGTH]);
double     score_diag_135(int i, int j, int chessman[LENGTH][LENGTH]);
double     total_score(int who_running, int chessman[LENGTH][LENGTH]);
double     score(int chessman[LENGTH][LENGTH]);
int       rowdt(int i, int j, int chessman[LENGTH][LENGTH]);
int       coldt(int i, int j, int chessman[LENGTH][LENGTH]);
int       diadt(int i, int j, int chessman[LENGTH][LENGTH]);
int       vdiadt(int i, int j, int chessman[LENGTH][LENGTH]);
int       can_expand(int i, int j, int chessman[LENGTH][LENGTH]);
void     copy(point s1, point s0);

int
POW(int s, int t)
{
  int       sum = s,
          i;
  if (t <= 0)
 return 1;
  for (i = 0; i < t; i++)
 sum *= sum;
  return sum;

}


/*
 * 定义computer先手
 */
point
expand(point s0)
{
  int       flag;
  int       i,
          j;
  point      new_chess = (point) malloc(sizeof(struct five_chess));
/*************************这里出错***********************************/
  for (i = 0; i < LENGTH; i++)
 for (j = 0; j < LENGTH; j++)
   new_chess->chess[i][j] = s0->chess[i][j];

  for (i = 0; i < LENGTH; i++)
 for (j = 0; j < LENGTH; j++)
   new_chess->record[i][j] = s0->chess[i][j];

/*************************这里出错***********************************/
  if (s0->layer % 2 == 0)
 flag = COMPUTER;
  else
 flag = PLAYER1;


  for (i = 0; i < LENGTH; i++)
 for (j = 0; j < LENGTH; j++) {

   if (s0->record[i][j])             /*如果有棋子*/
 continue;
   if (can_expand(i, j, s0->chess) == 0)  /*如果有棋子,而且沿水平,树直,左上—右下,右上—左下,四个方向可以扩展*/
 continue;
   s0->record[i][j] = flag;
   new_chess->chess[i][j] = flag;
   new_chess->layer = s0->layer + 1;
   new_chess->x = i;
   new_chess->y = j;
   new_chess->record[i][j] = flag;
   return new_chess;
 }
  /*
   * for(i=5;i<10;i++) for(j=5;j<10;j++){ if(s0->record[i][j]) continue;
   * if(can_expand(i,j,s0->chess)==0) continue; s0->record[i][j]=flag;
   * new_chess->x=i; new_chess->y=j; new_chess->record[i][j]=flag;
   * new_chess->layer=s0->layer+1; new_chess->chess[i][j]=flag ; return
   * new_chess; } for(i=2;i<12;i++) for(j=2;j<12;j++){
   * if(i>4&&i<10&&j>4&&j<10) continue; if(s0->record[i][j]) continue;
   * if(can_expand(i,j,s0->chess)==0) continue; s0->record[i][j]=flag;
   * new_chess->x=i; new_chess->y=j; new_chess->record[i][j]=flag;
   * new_chess->layer=s0->layer+1; new_chess->chess[i][j]=flag; return
   * new_chess;
   * 
   * } for(i=0;i<LENGTH;i++) for(j=0;j<LENGTH;j++){
   * if(i>1&&i<12&&j>1&&j<12) continue; if(s0->record[i][j]) continue;
   * if(can_expand(i,j,s0->chess)==0) continue; s0->record[i][j]=flag;
   * new_chess->chess[i][j]=flag; new_chess->layer=s0->layer+1;
   * new_chess->x=i; new_chess->y=j; new_chess->record[i][j]=flag; return 
   * new_chess; } 
   */
  new_chess->layer = -1;
  return new_chess;
}

 

void
computer()
{
  int       i,
          j,
          k,
          num = 0;
  int       break_now = 0;
  int       break_then = 0;
  int       go_on = 0;
  point     s0 = NULL,
          s1 = NULL,
          s2 = NULL,
          max_chess = NULL;
  point     temps = NULL,
          s01;
  /*
   * 堆栈的初始化
   */
  stack_deep0 = 0;
  s0 = malloc(sizeof(struct five_chess));
  for (i = 0; i < 600; i++)              /*为什么是600*/
 close[i] = NULL;                 /*close是一个point 数组*/
  close[num++] = s0;
  for (i = 0; i < LENGTH; i++)
 for (j = 0; j < LENGTH; j++) {
   s0->chess[i][j] = chessman[i][j];
   s0->record[i][j] = chessman[i][j];
 }
  s0->layer = 0;
  s0->value = -3000000;
  s0->score = -3000000;
  push(s0);
  while (is_empty() != 0) {        /*看是栈否为空*/
 s01 = top();                  /*如果不是空*/
 s1 = expand(s01);             /*从栈顶开始展开*/
 close[num++] = s1;
 if (s1->layer == -1) {
   pop();
   continue;
 }
 go_on =
   win_or_not((s1->x + 1) * 30, (s1->y + 1) * 30, 2, s1->chess,
     1);
 if (go_on == 2) {
   a = (s1->x + 1) * 30;
   b = (s1->y + 1) * 30;
   break_then = 1;
   break;
 }
 go_on =
   win_or_not((s1->x + 1) * 30, (s1->y + 1) * 30, 1, s1->chess,
     1);
 if (go_on == 1) {
   a = (s1->x + 1) * 30;
   b = (s1->y + 1) * 30;
   break_then = 1;
   break;
 }
 s1->value = 30000;
 push(s1);
 while (1) {
   s1 = top();
   s2 = expand(s1);
   if (s2->layer == -1) {
 pop();
 if (s1->value > top()->value) {
   top()->value = s1->value;
   max_chess = s1;
 }
 free(s2);
 break;
   }/*end of if*/
   s2->score = score(s2->chess);
   temps = top();
   if (s2->score < temps->value)
 temps->value = s2->score;
   free(s2);
 }/*end of whiile(1) */
  }
  if (break_then == 0) {
 for (i = 0; i < LENGTH; i++) {
   for (j = 0; j < LENGTH; j++)
 if (max_chess->chess[i][j] != chessman[i][j]) {
   a = i * 30 + 30;
   b = j * 30 + 30;
   break_now = 1;
   break;
 }
   if (break_now == 1)
 break;
 }
  }
  for (i = 0; i < 600; i++) {
 if (close[i] == NULL)
   continue;
 free(close[i]);
  }

}

/**********************************************************/
void
main()
{
  int       key;
  int       play_with_who = 1;

  printf("1.Play with human\n2.Play with computer\nPlease choice: ");
  scanf("%d", &play_with_who);
 
  five();              /*显示棋盘*/
  show();

  if (play_with_who == 1) {   /*人与人玩*/
 while (1) {  /*设置人与人玩的界面*/
   settextstyle(0, 0, 2);
   if ((step_sum + 1) % 2) {
 setcolor(1);
 outtext



 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • 基于C语言实现五子棋游戏完整实例代码

相关文章

  • 2017-05-28C语言 选择排序算法详解及实现代码
  • 2017-05-28C++实现读取特定路径下文件夹及文件名的方法
  • 2017-05-286个变态的C语言Hello World程序
  • 2017-05-28C++回溯法实例分析
  • 2017-05-28由static_cast和dynamic_cast到C++对象占用内存的全面分析
  • 2018-08-06C语言JSON查询
  • 2017-05-28C++算法之海量数据处理方法的总结分析
  • 2017-05-28详谈C++何时需要定义赋值/复制构造函数
  • 2017-05-28浅谈C++中对象的复制与对象之间的相互赋值
  • 2017-05-28C语言中交换int型变量的值及转换为字符数组的方法

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • operator new在C++中的各种写法总结
    • C 语言指针概念的详解
    • C和MFC巧妙获取外网IP的两种实现方法
    • c++拷贝构造函数防篡改示例
    • C语言中的abs()函数和exp()函数的用法
    • C++之普通成员函数、虚函数以及纯虚函数的区别与用法要点
    • c++中const的使用详解
    • 标准C++类string的Copy-On-Write技术
    • 尾递归详细总结分析
    • 顺序线性表的代码实现方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有