首页 > C语言-三子棋游戏

C语言-三子棋游戏

C语言中用写头文件的方式写了一个三子棋游戏

1.测试函数text.c

#define _CRT_SECURE_NO_WARNINGS 1#include
#include
#include
#include
#include"chess.h"void menu()
{printf("***********************
");printf("******  1.play  *******
");printf("******  0.exit  *******
");printf("***********************
");printf("请选择相应的数字>:");
}int main()
{char board[LINE][LIST];int input = 1;int a = 0;srand((unsigned int)time(NULL));while (input){memset(board, ' ', sizeof(char)*LINE*LIST);menu();scanf("%d", &input);printf("
");switch (input){case 1:a = game(board);break;case 0:break;default:printf("你的输入不符合要求,请重新输入>:"); break;}if (0 == input){break;}	}printf("游戏结束,欢迎再次使用!!!
");return 0;
}




2.头文件chess.h

#ifndef __CHESS_H__
#define __CHESS_H__#define LINE 3
#define LIST 3int game(char board[LINE][LIST]);
void Player(char board[LINE][LIST]);
void Computer(char board[LINE][LIST]);
void ChessBoard(char board[LINE][LIST]);
int Check(char board[LINE][LIST]);
//void ChessBoard(int LINE,int LIST );       此语句错误,不可以直接用宏作为参数,头文件中不可以这样写,LIST和LINE可以作为实参,不可以作为形参
#endif  //__CHESS_H__




3.函数文件chess.c



#define _CRT_SECURE_NO_WARNINGS 1#include
#include
#include
#include"chess.h"void ChessBoard(char board[LINE][LIST])
{printf("%c|%c|%c
", board[0][0], board[0][1], board[0][2]);printf("-----
");printf("%c|%c|%c
", board[1][0], board[1][1], board[1][2]);printf("-----
");printf("%c|%c|%c
", board[2][0], board[2][1], board[2][2]);printf("

");
}int game(char board[LINE][LIST])
{ChessBoard(board);//为了使电脑赢了之后可以显示出棋盘来while (1){Player(board);ChessBoard(board);if (Check(board)){return 0;}Computer(board);ChessBoard(board);if (Check(board)){return 0;}}
}void Player(char board[LINE][LIST])
{int x = 0, y = 0;int m = 1;//设置m是为了使输入数据不合法printf("玩家玩
请输入坐标>:");while (m){scanf("%d%d", &x, &y);printf("
");if ((x >= 1 && x <= 3) && (board[x - 1][y - 1] == ' ')){board[x - 1][y - 1] = 'X';m = 0;}else{printf("数据不符合要求
请重新输入>:");}}
}void Computer(char board[LINE][LIST])  
{int x = 0, y = 0;int m = 1;printf("电脑玩
");while (m){x = rand()% 3;y = rand()% 3;if (board[x][y] == ' '){board[x][y] = '#';m = 0;}}
}int Check(char board[LINE][LIST])   //When you pass an array, you must add the brackets in the back of the array.
{int i = 0,j = 0,flag = 0;for (i = 0; i <= 2; i++){if (((board[i][0] == board[i][1]) && (board[i][1] == board[i][2]) && (board[i][0] == 'X')) || ((board[0][i] == board[1][i]) && (board[1][i] == board[2][i]) && (board[0][i] == 'X'))){printf("The player wins the game!!!

");return 1;}if (((board[i][0] == board[i][1]) && (board[i][1] == board[i][2]) && (board[i][0] == '#')) || ((board[0][i] == board[1][i]) && (board[1][i] == board[2][i]) && (board[0][i] == '#'))){printf("The computer wins the game!!!

");return 1;}if (((board[0][0] == board[1][1]) && (board[0][0] == board[2][2]) && (board[0][0] == 'X')) || ((board[0][2] == board[1][1]) && (board[0][2] == board[2][0]) && (board[0][2] == 'X'))){printf("The player wins the game!!!

");return 1;}if (((board[0][0] == board[1][1]) && (board[0][0] == board[2][2]) && (board[0][0] == '#')) || ((board[0][2] == board[1][1]) && (board[0][2] == board[2][0]) && (board[0][2] == '#'))){printf("The computer wins the game!!!

");return 1;}for (j = 0; j <= 2; j++)  //平局{if (' ' != board[i][j]){flag++;}}if (9 == flag){printf("平局
");return 1;}}return 0;
}








更多相关:

  • 题目:矩阵中的路径 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一格开始,每一步可以在矩阵中向左、右、上、下移动一格。如果一条路径经过了矩阵的某一格,那么该路径不能再次进入该格子。例如,在下面的3×4的矩阵中包含一条字符串“bfce”的路径(路径中的字母用加粗标出)。 [["a...

  • #include int main(int args,char ** argv) {int map[3][3]={{1,2,3},{4,5,6},{7,8,9}};int **pMap=(int **)map;printf("%d ",map);//数组的首地址printf("%d ",*(map+1));//数...

  • awk格式化使用printf函数,类似于C语言中的printf函数 比如 awk '{printf "%s ", $1}' test1 上面的方式是awk每次处理一行,然后进行替换的,如果我们想要传入多个参数,此时就需要多个格式化...

  • 【目的】   定义一个结构体类,其中的成员变量数组长度不定,根据实例化的对象指定长度,所以想到用指针实现 【现状】   指针可以指向任意长度数组,但结构体类只分配指针本身4字节长度,所以无法扩展     1 /** 2 *****************************************************...

  • 1.有一个四位正整数,组成这个四位数的四个数字各不相同,如果把它们的首尾互换,第二位与第三位互换,组成一个新的四位数。原四位数为新四位数的4倍,请找出一个这样的四位数。 #include int main() {int a,b,c,d,e,f;for(a=1000;a<10000;a++){b=a%10;c=a/1...

  • if条件语句  非零即真   0即假 if(表达式){     //成立之后要处理的事情 }   以atm小程序为例 //判断用户选择的操作     if (operation == 1){            //输入密码         printf("输入密码 ");     }          if (operation...

  • list_for_each_safeBidirect-list list_for_each_safe().https://biscuitos.github.io/blog/LIST_list_for_each_safe/...

  • /*hanzhiguang coded at 2009.07.30 1:20*/ // nesting_map.cpp : Defines the entry point for the console application. // /*-----------------------------------------------...

  • 已知k个已排序链表头节点指针,将这k个链表合并,合并后仍为有序的 ,返回合并后的头节点 如下三个链表: 合并后的结果如下: 方法一(STL sort算法进行排序): 先将输入的排序链表插入一个迭代器中,vector数组中国呢直接对数组中的链表节点进行按值排序即可 实现算法如下,最终实现源码见文末: bool cmp(Dat...

  • 本题要求实现一个函数,找到并返回链式表的第K个元素。 函数接口定义: ElementType FindKth( List L, int K ); 其中List结构定义如下: typedef struct LNode *PtrToLNode; struct LNode {ElementType Data;PtrToLNode Ne...

  • 一、前述 企业中linux搭建ftp服务器还是很实用的,所以本文针对centoos7和centoos6搭建服务器教程做个总结。 二、具体 1、显示如下图则表示已安装 vsftp软件。如果未显示则需要安装vsftpd软件。 如果没有则通过yarm源进行安装 yum install -y vsftpd 2、安装完成之后 进入到ftp的根...