您的位置:首页 > 编程语言 > C语言/C++

用C语言实现三子棋

2017-11-03 21:12 169 查看
#ifndef __GAME_H__

#define __GAME_H__

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<time.h>

#define COLS 3

#define ROWS  3

void init_board(char boar[ROWS][COLS], int row, int col);   初始化棋盘

void display_board(char boar[ROWS][COLS], int row, int col);显示棋盘

void player_mov(char boar[ROWS][COLS], int row, int col);玩家走

void computer_mov(char boar[ROWS][COLS], int row, int col);电脑走

char check_win(char boar[ROWS][COLS], int row, int col);判输赢
#endif

//game.h中要用函数的定义

#include "game.h"

void init_board(char boar[ROWS][COLS], int row, int col)

{
{

int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
boar[i][j] = ' ';
//printf("%c", boar[i][j]);
}
}

}

void display_board(char boar[ROWS][COLS], int row, int col)

{
int i = 0;
for (i = 0; i < row; i++)
{
printf("%c  | %c | %c\n", boar[i][0], boar[i][1], boar[i][2]);
if (i != 2)
printf("--- --- ---\n");
}
printf("\n");

}

void player_mov(char boar[ROWS][COLS], int row, int col)

{
int x = 0, y = 0;
while (1)
{

printf("请输入坐标:\n");
scanf_s("%d%d", &x, &y);
x--;
y--;
if (((x >= 0) && (x <= 2)) && ((y >= 0) && (y <= 2)))
{

if (boar[x][y] == ' ')
{
boar[x][y] = '!';
break;
}
else
{
printf("下标有误,请重新输入!");
}
}
else
{
printf("下标有误,请重新输入!");
}
}

}

void computer_mov(char boar[ROWS][COLS], int row, int col)

{
while (1)
{
int x = rand() % 3;
int y= rand() % 3;
if (boar[x][y] == ' ')
{
boar[x][y] = '$';
break;
}
}

}

static int is_full(char boar[ROWS][COLS], int row, int col)

{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (boar[i][j] == ' ')
return 0;
}

}
return 1;

}

char check_win(char boar[ROWS][COLS], int row, int col)

{
int i = 0;
for (i = 0; i < row; i++)
{
if (boar[i][0] == boar[i][1] && boar[i][1] == boar[i][2] && boar[i][0] != ' ')
return boar[i][0];
}
for (i = 0; i < col; i++)
{
if (boar[0][i] == boar[1][i] && boar[1][i] == boar[2][i] && boar[0][i] != ' ')
return boar[0][i];
}
if (boar[0][0] == boar[1][1] && boar[1][1] == boar[2][2] && boar[0][0] != ' ')
return boar[0][0];
if (boar[0][2] == boar[1][1] && boar[1][1] == boar[2][0] && boar[1][1] != ' ')
return boar[1][1];

if (is_full(boar, row, col))

{
return 'q';

}

return ' ';

}

game.c中各个函数的实现

#include"game.h"

void game()

{
char boar[ROWS][COLS], ret = 0;
init_board(boar, ROWS, COLS);
display_board(boar, ROWS, COLS);
srand((unsigned int)time(NULL));
while (1)
{
player_mov(boar, ROWS, COLS);
if ((ret = check_win(boar, ROWS, COLS)) != ' ')
break;
display_board(boar, ROWS, COLS);
computer_mov(boar, ROWS, COLS);
if ((ret = check_win(boar, ROWS, COLS)) != ' ')
break;
display_board(boar, ROWS, COLS);
}
if (ret == '!')
{
printf("player win\n");
}
else if (ret == '$')
{
printf("player win\n");
}
else if (ret == 'q')
{
printf("deucd \n");
}
display_board(boar, ROWS, COLS);

}

void menu()

{
printf("****************************************************************\n");
printf("**********************1.play        0.exit***********************\n");
printf("*****************************************************************\n");

}

int main()

{
int input = 0;
do{
menu();
printf("请选择:");
scanf_s("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
break;
default:
printf("选择错误\n");
break;
}
} while (input);
return 0;

}

test.c中函数的调用
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: