五子棋(C语言)
程序员文章站
2024-03-18 14:54:40
...
五子棋
问题描述:在一个10*10的棋盘中进行人机对战
在这之前写了一篇三子棋的代码,五子棋与其原理相似,主要区别在于判断输赢上,10*10的棋盘,赢的条件是每行,每列,或正反对角线上有五个连续相同的棋子。
头文件 FiveChess.h
#ifndef _FIVECHESS_H_
#define _FIVECHESS_H_
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
#define ROW 10
#define COL 10
void Menu();//打印菜单
void Game();
void Computer(char board[][COL], int row, int col);//电脑下棋
void Show(char board[][COL], int row, int col);//棋盘展示
char Judge(char board[][COL], int row, int col);//判断输赢
void Player(char board[][COL], int row, int col);//玩家下棋
#endif
函数的实现 FiveChess.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "FiveChess.h"
void Menu()//打印菜单
{
printf("----------------------\n");
printf("----- 1.play -----\n");
printf("----- 2.exit -----\n");
printf("----------------------\n");
printf("Please enter your select:");
}
void Computer(char board[][COL], int row, int col)
{
int i = 0;
int j = 0;
while (1){
i = rand() % row;
j = rand() % row;
if (' ' == board[i][j]){
board[i][j] = 'X';
break;
}
}
}
void Show(char board[][COL], int row, int col)
{
printf(" | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10|\n");
printf("---------------------------------------------\n");
for (int i = 0; i < row; i++){
printf("%2d |", i + 1);
for (int j = 0; j < col; j++){
printf(" %c |", board[i][j]);
}
printf("\n---------------------------------------------\n");
//printf("\n");
}
printf("\n");
}
char Judge(char board[][COL], int row, int col)
{
//判断行
for (int i = 0; i < row; i++){
for (int j = 0; j <= col - 4; j++){
char ret = board[i][j];
if (ret != ' ' && ret == board[i][j + 1] && ret == board[i][j + 2] && \
ret == board[i][j + 3] && ret == board[i][j + 4] ){
return ret;
}
}
}
//判断列
for (int j = 0; j <= col; j++){
for (int i = 0; i < row-4; i++){
char ret = board[i][j];
if (ret != ' ' && ret == board[i+1][j] && ret == board[i+2][j] && \
ret == board[i+3][j] && ret == board[i+4][j]){
return ret;
}
}
}
//判断正斜线
for (int i = 0; i < row; i++){
for (int j = 0; j < col; j++){
char ret = board[i][j];
if ((ret == board[i + 1][j + 1]) && (ret != ' ') && (ret == board[i + 2][j + 2]) && \
(ret == board[i + 3][j + 3]) && (ret == board[i + 4][j + 4]) ){
return ret;
}
}
}
//判断反斜线
for (int i = 0; i <row; i++){
for (int j = col-1; j >=0; j--){
char ret = board[i][j];
if ((ret == board[i + 1][j - 1]) && (ret != ' ') && (ret == board[i + 2][j - 2]) && \
(ret == board[i + 3][j - 3]) && (ret == board[i + 4][j - 4])){
return ret;
}
}
}
for (int i = 0; i < row; i++){
for (int j = 0; j < col; j++){
if (' ' == board[i][j]){
return 'N';
}
}
}
return 'E';
}
void Player(char board[][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1){
printf("Please input your pos<x,y>:");
scanf("%d%d", &x, &y);
if ((x >= 1) && (x <= row) && (y >= 1) && (y <= col)){
if (' ' == board[x - 1][y - 1]){
board[x-1][y-1] = 'O';
break;
}
else{
printf("Your pos error!\n");
}
}
else{
printf("Input error,input again!\n");
}
}
}
void Game()
{
char board[ROW][COL];
memset(board, ' ', sizeof(board));
char result = 'N';
srand((unsigned int)time(NULL));
while (1){
system("cls");
Computer(board, ROW, COL);
Show(board, ROW, COL);
result = Judge(board, ROW, COL);
if ('N' != result){
break;
}
Player(board, ROW, COL);
Show(board, ROW, COL);
result = Judge(board, ROW, COL);
if ('N' != result){
break;
}
}
switch (result){
case 'X':
printf("Computer Win!\n");
break;
case 'O':
printf("YOU WIN!\n");
break;
case 'E':
printf("平局!\n");
break;
default:
break;
}
}
主函数 main.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "FiveChess.h"
int main()
{
int input = 0;
int quit = 0;
while (!quit){
Menu();
scanf("%d", &input);
switch (input){
case 1:
Game();
break;
case 2:
printf("Quit the game!\n");
quit = 1;
break;
default:
printf("Input error!\n");
break;
}
}
system("pause");
return 0;
}
由于我比较懒,所以展示部分游戏截屏,大家可以自己多玩几次,有不对的地方可以加以修改调整,当然了,电脑比较“笨”,你想输一次也不容易呢
上一篇: 简单的利用Python发送邮件
下一篇: 第一次写博客。。。表示很激动。。。