对于初学C语言的同学,五子棋不可谓不是一个好的练习。我们不但要考虑玩家及电脑的落子,还要考虑棋盘是否已满、是否有一方已经获得胜利。因此我们要考虑好各种情况,设定好函数及循环。
下面是两人五子棋的简单代码,注释部分是电脑产生随机数的函数,如若取消注释并注释掉关于玩家二的函数,运行起来就是人机对战。
程序如下:
game.h
#ifndef __GAME_H__
#define __GAME_H__
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define ROWS 5
#define COLS 5
void init_board(char board[ROWS][COLS], int row, int col);
void Display_board(char board[ROWS][COLS], int row, int col);
void player1_move(char board[ROWS][COLS], int row, int col);
void player2_move(char board[ROWS][COLS], int row, int col);
//void computer_move(char board[ROWS][COLS], int row, int col);
char Is_win(char board[ROWS][COLS], int row, int col);
#endif
game.c
#include "game.h"
void init_board(char board[ROWS][COLS], int row, int col)
{
memset(board, ' ', row*col*(sizeof(char))); //初始化棋盘
}
void Display_board(char board[ROWS][COLS], int row, int col)
{
int i=0;
for (i = 0; i < row; i++)
{
printf("%c |%c |%c |%c |%c\n", board[i][0], board[i][1], board[i][2], board[i][3], board[i][4]);
if (i != 4)
printf("--|--|--|--|--\n"); //打印棋盘规格
}
}
void player1_move(char board[ROWS][COLS], int row, int col)
{
int x = 0; int y = 0;
while (1)
{
printf("请玩家1输入一个坐标\n");
scanf_s("%d%d", &x, &y);
x--;
y--;
if (((x >= 0) && (x <= 4)) && ((y >= 0) && (y <= 4)))
{
if (board[x][y] == ' ')
{
board[x][y] = 'W'; //玩家1落子记为W
break;
}
else
printf("下标错误,请重新输入\n");
}
else
printf("下标错误,请重新输入\n");
}
}
void player2_move(char board[ROWS][COLS], int row, int col)
{
int x = 0; int y = 0;
while (1)
{
printf("请玩家2输入一个坐标\n");
scanf_s("%d%d", &x, &y);
x--;
y--;
if (((x >= 0) && (x <= 4)) && ((y >= 0) && (y <= 4)))
{
if (board[x][y] == ' ')
{
board[x][y] = 'X'; //玩家2落子记为X
break;
}
else
printf("下标错误,请重新输入\n");
}
else
printf("下标错误,请重新输入\n");
}
}
//void computer_move(char board[ROWS][COLS], int row, int col)
//{
// while (1)
// {
// int x = rand() % 5; //电脑产生随机值0,1,2,3,4
// int y = rand() % 5;
// if (board[x][y] == ' ')
// {
// board[x][y] = 'X ; //电脑落子记为X
// break;
// }
// }
//}
static int Is_full(char board[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 (board[i][j] == ' ')
return 0;
}
}
return 1;
}
char Is_win(char board[ROWS][COLS], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
if ((board[i][0] == board[i][1]) && (board[i][1] == board[i][2]) && (board[i][2] == board[i][3]) && (board[i][3] == board[i][4]) && board[i][1] != ' ')
return board[i][1];
}
for (i = 0; i < col; i++)
{
if ((board[0][i] == board[1][i]) && (board[1][i] == board[2][i]) && (board[2][i] == board[3][i]) && (board[3][i] == board[4][i])&&(board[2][i] != ' '))
return board[1][i];
}
if ((board[0][0] == board[1][1]) && (board[1][1] == board[2][2]) && (board[2][2] == board[3][3]) &&(board[3][3] == board[4][4]) && (board[2][2] != ' '))
return board[1][1];
if ((board[0][4] == board[1][3]) && (board[1][3] == board[2][2]) && (board[2][2] == board[3][1]) && (board[3][1] == board[4][0])&&(board[4][0] != ' '))
return board[1][1];
if (Is_full(board, row, col))
{
return 'Q';
}
return ' ';
}
wuziqi.c
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"game.h"
#include<time.h>
void game()
{
char board[ROWS][COLS];
char ret = 0;
init_board(board, ROWS, COLS);
srand((unsigned int)time(NULL));
while (1)
{
player1_move(board, ROWS, COLS); //玩家1落子
if ((ret=Is_win(board, ROWS, COLS)) != ' ')
break;
Display_board(board, ROWS, COLS); //打印棋盘
/*computer_move(board, ROWS, COLS);*/ //电脑落子
player2_move(board, ROWS, COLS); //玩家2落子
if((ret=Is_win(board, ROWS, COLS))!= ' ') //判断是否赢
break;
Display_board(board, ROWS, COLS);
}
if (ret == 'W')
printf("玩家1赢\n");
else if (ret == 'X')
printf("玩家二赢\n");
else if (ret == 'Q')
printf("平局\n");
/*Display_board(board, ROWS, COLS);*/
}
void menu()
{
printf("####################\n");
printf("##1、play##0、exit##\n"); //选择游戏还是退出
printf("####################\n");
}
int main()
{
int input = 0;
menu();
do
{
printf("请选择:>\n");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
break;
default:
printf("选择错误\n");
break;
}
}
while (input);
return 0;
}
接下来就可以玩了。为大家展示一下两人游戏时的场景。
再为大家展示一下人机游戏时的场景
标签:ROWS,&&,int,五子棋,COLS,C语言,board,人机,row From: https://blog.51cto.com/u_15861560/5822286