三子棋是一种民间传统游戏,又叫九宫棋、圈圈叉叉、一条龙等。将正方形对角线连起来,相对两边依次摆上三个双方棋子,只要将自己的三个棋子走成一条线,对方就算输了。
这次主要是用多文件方式编写程序,对于这个多文件也不是很懂,还在熟悉。
1、首先建立三个文件,头文件chess.h(主要包括函数的声明以及宏定义),
头文件的格式:
#ifndef _CHESS_H_
#define _CHESS_H_
......
#endif
2、两个.c文件:
(1)、chess.c(函数的定义),主要包含程序所需的所有函数定义和实现。
(2)、main.c(主函数,代码的实现,函数调用,显示菜单,大体框架)
其中有几个点要注意一下:
1、在电脑走时设置随机函数,之前记得要定义随机数种子(srand((unsigned int)time(NULL));)。
2、二维数组定义和初始化以及字符作为判断依据时函数的定义和返回值类型。
下面是源程序(这次的代码并没有实现优化和其它功能,之后会更新)
头文件chess.h
#ifndef _CHESS_H_
#define _CHESS_H_
#include<stdio.h>
#include<windows.h>
#include<time.h>
#define ROW 3
#define COL 3
#define PLAYER_COLOR 'X'
#define COMPUTER_COLOR 'O'
#pragma warning(disable:4996)
void Game();
void InitBoard(char board[][COL],int row,int col);
void ShowBoard(char board[][COL], int row, int col);
void PlayerMove(char board[][COL], int row, int col);
void ComputerMove(char board[][COL], int row, int col);
char Judge(char board[][COL], int row, int col);
#endif
chess.c
#include "chess.h"
void InitBoard(char board[][COL], int row, int col)
{
int i;
for (i = 0; i < row; i++){
int j;
for (j=0; j < col; j++){
board[i][j] = ' ';//初始化数组为空格
}
}
}
void ShowBoard(char board[][COL], int row, int col)
{
printf(" 1 | 2 | 3 |\n");
printf("----------------\n");
int i;
for (i = 1; i <= 3; i++){
printf(" %d | %c | %c | %c \n",i, board[i - 1][0], board[i - 1][1], board[i - 1][2]);
if (i != 3){
printf("----------------\n");
}
}
}
void PlayerMove(char board[][COL], int row, int col)
{
while (1){
int x = 0;
int y = 0;
printf("Please Enter Pos<x, y>: ");//输入你要填的位置
scanf("%d %d", &x, &y);
if (x >= 1 && x <= 3 && y >= 1 && y <= 3){
if (board[x - 1][y - 1] == ' '){
board[x - 1][y - 1] = PLAYER_COLOR;//如果选的位置为空,则将棋子填入棋盘
break;
}
else{
printf("你的落子位置已经被占用, 请重新输入!\n");
}
}
else{
printf("你输入的坐标不合法,请重新输入!\n");
}
}
}
//电脑落子位置随机,在种下随机数种子后,调用函数
void ComputerMove(char board[][COL], int row, int col)
{
while (1){
int x = rand()%row;
int y = rand()%col;//[0-2];
if (board[x - 1][y - 1] == ' '){
board[x - 1][y - 1] = COMPUTER_COLOR;
break;
}
}
}
char Judge(char board[][COL], int row, int col)
{
int i;
//判断行
for (i = 0; i < row; i++){
if (board[i][0] != ' ' && board[i][0] == board[i][1] &&board[i][1] == board[i][2]){
return board[i][0];
}
}
//判断列
for (i = 0; i < col; i++){
if (board[0][i] != ' ' && board[0][i] == board[1][i] &&board[1][i] == board[2][i]){
return board[0][i];
}
}
//判断对角线
if (board[0][0] != ' ' &&board[0][0] == board[1][1] &&board[1][1] == board[2][2]){
return board[2][2];
}
if (board[0][2] != ' ' &&board[0][2] == board[1][1] &&board[1][1] == board[2][0]){
return board[1][1];
}
//只要棋盘还有空,则棋盘没填满,游戏继续,否则就是平局
for (i = 0; i < row; i++){
int j;
for (j = 0; j < col; j++){
if (board[i][j] == ' '){
return 'N';
}
}
}
return 'E';
}
void Game()
{
int result = 'N';
char board[ROW][COL];//定义二维数组,即三子棋棋盘
InitBoard(board,ROW,COL);
ShowBoard(board, ROW, COL);
srand((unsigned int)time(NULL));
while (1){
PlayerMove(board, ROW, COL);
ShowBoard(board, ROW, COL);
Judge(board, ROW, COL);
result = Judge(board, ROW, COL);
if (result != 'N'){
break;
}
ComputerMove(board, ROW, COL);
ShowBoard(board, ROW, COL);
Judge(board, ROW, COL);
result = Judge(board, ROW, COL);
if (result != 'N'){
break;
}
switch (result){
case 'N':
break;
case 'X':
printf("You win!");
break;
case 'O':
printf("You lost");
break;
case 'E':
printf("It ends in a draw");
default:
printf("Bug?\n");
break;
}
}
}
main.c
#include "chess.h"//调用头文件
void ShowMenu()
{
printf("######################################\n");
printf("##1、play 2、exit##\n");
printf("######################################\n");
printf("Please enter your choice: ");
}
int main()
{
int choice = 0;
int quit = 0;
while (!quit){
ShowMenu();//显示菜单
scanf("%d",&choice);
switch (choice){
case 1:
Game();
break;
case 2:
printf("Byebye!\n");
break;
default:
printf("Your choice is error,please try again!\n");
break;
}
}
system("pause");
return 0;
}