三字棋游戏:玩家与电脑对抗,棋盘是3*3的小方格,当任一行或任一列,或者正对角线,或者副对角线棋子个数为三或棋子类型完全一致,则该把棋局就赢了,但是如果棋盘满了有没有赢,则为平局。下面我们就开始我们的程序设计:
chess.h 头文件(用于函数的声明)
chess.c 源文件(用于函数的定义)
main.c 源文件(用于主函数的实现)
代码如下:
chess.h 头文件(用于函数的声明)
#ifndef __CHESS_H_
#define __CHESS_H_
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#pragma warning(disable:4996)
#define ROWS 3 //三字棋 //5 五子棋
#define COLS 3 //三字棋 //5 五子棋
void show(char showboard[][COLS], int rows, int cols);
char play(char showboard[][COLS], int rows, int cols);
void game();
#endif //__CHESS_H_
chess.c 源文件(用于函数的定义【实现】)
#include "chess.h"
void show(char showboard[][COLS], int rows, int cols)
{
int i = 0;
for (; i < ROWS; i++)
{
int j = 0;
for (; j < COLS; j++)
{
if (j == COLS - 1){
printf(" %c ", showboard[i][j]);
}
else{
printf(" %c |", showboard[i][j]);
}
}
printf("\n");
for (j = 0; j < COLS; j++){
if (i < ROWS - 1){
if (j < COLS - 1){
printf("---|");
}
else{
printf("---");
}
}
}
printf("\n");
}
}
static int IsWin(char showboard[][COLS], int rows, int cols)
{
int i, j;
for (i = 0; i < rows; i++)
{
if ((showboard[i][0] != ' ') && (showboard[i][0] == showboard[i][1]) && (showboard[i][1] == showboard[i][2]))
{
return 1;
}
}
for (j = 0; j < cols; j++)
{
if ((showboard[0][j] != ' ') && (showboard[0][j] == showboard[1][j]) && (showboard[1][j] == showboard[2][j]))
{
return 1;
}
}
//正对角线
int count = 0;
for (i = 0; i < rows; i++){
if ((showboard[i][i] == '*')){
count++;
}
}
if (count == rows){
return 1;
}
count = 0;
for (i = 0; i < rows; i++){
if ((showboard[i][i]) == '#'){
count++;
}
}
if (count == rows){
return 1;
}
//反对角线
count = 0;
j = 0;
for (i = rows - 1; i >= j; i--){ //只用检查到两个坐标相等即可
if ((showboard[i][j] == showboard[j][i]) && (showboard[i][j] == '*')){
count++;
}
j += 1;
}
if (count == j){
return 1;
}
count = 0;
j = 0;
for (i = rows - 1; i >= j; i--){
if ((showboard[i][j] == showboard[j][i]) && (showboard[i][j] == '#')){
count++;
}
j++;
}
if (count == j){
return 1;
}
return 0;
}
static int IsFull(char showboard[][COLS], int rows, int cols)
{
int i, j;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
if (showboard[i][j] == ' ')
{
return 0;
}
}
}
return 1;
}
char play(char showboard[][COLS], int rows, int cols)
{
//电脑先走
srand((unsigned int)time(NULL));
show(showboard, ROWS, COLS);
while (1){
printf("computer play> ");
Sleep(1000);
while (1){
int x = rand() % (rows + 1);//随机产生0-rows的数字
int y = rand() % (cols + 1);//随机产生0-cols的数字
if ((x > 0) && (y > 0) && (showboard[x - 1][y - 1] == ' ')){
printf("%d %d\n\n", x, y);
showboard[x - 1][y - 1] = '*';
Sleep(1000);
system("CLS");
break;
}
}
int ret = IsWin(showboard, rows, cols);
int tmp = IsFull(showboard, rows, cols);
if (ret == 1)
{
return '*';//电脑胜了
}
else if (tmp == 1){
return 'p';
}
//玩家走
else if (ret == 0){
show(showboard, ROWS, COLS);
while (1){
printf("Dear user play> ");
int x, y;
scanf("%d%d", &x, &y);
Sleep(1000);
system("CLS");
if ((x > 0 && x <= rows) && (y > 0 && y <= cols) && (showboard[x - 1][y - 1] == ' ')){
showboard[x - 1][y - 1] = '#';
show(showboard, ROWS, COLS);
break;
}
else{
printf("can't push, try again> \n");
show(showboard, ROWS, COLS);
}
}
int ret = IsWin(showboard, rows, cols);
int tmp = IsFull(showboard, rows, cols);
if (ret == 1)
{
return '#';//玩家胜了
}
else if (tmp == 1)
{
return 'p';
}
}
}
return 0;
}
void game()
{
//定义棋盘数组
char showboard[ROWS][COLS] = { 0 };
//初始化棋盘
memset(showboard, ' ', sizeof(showboard));
//打印棋盘
//show(showboard, ROWS, COLS);
//开始下棋
system("CLS");
char ret = play(showboard, ROWS, COLS);
if ('*' == ret)//返回*则电脑赢了
{
//打印棋盘
show(showboard, ROWS, COLS);
printf("computer win !!!\n");
}
else if ('p' == ret)//返回b平局
{
show(showboard, ROWS, COLS);
printf("balance !!!\n");
}
else
{
//show(showboard, ROWS, COLS);
printf("you win !!!\n");
}
}
main.c 源文件(用于主函数的实现)
#include "chess.h"
void menu()
{
printf("************************\n");
printf("*** 1. play 2.exit ***\n");
printf("************************\n");
printf("please select number<1,2> ");
}
int main()
{
int i = 0;
menu();
scanf("%d", &i);
switch (i)
{
case 1:
game();
break;
case 2:
exit(0);
break;
default:
break;
}
system("pause");
return 0;
}
此程序设计还是很冗余,性能不够优越,以后慢慢不断更新此版本,
注意:此程序既可实现三字棋,也可实现五子棋,只需要改变头文件中的宏,ROWS 和 COLS 的值就可以了。
源代码见GitHub连接:https://github.com/xiaobaiyuan-bit/chess_3/commit/5c39d152c2aba539d4e639d57a39113be1330c07
请大家多提建议,多多指教,谢谢!