拖延症重度患者………… 现在才来写这篇五子棋的博客……….
其实只要自己在草稿纸上画出一个简易的五子棋棋盘,再一步一步地从如何下棋、怎么判断输赢等方面理清逻辑就可以写出代码了。
分为玩家走和电脑走,电脑走的位置是由随机数”rand()”产生;判断输赢部分对赢的情况有哪些要仔细,避免漏写;
头文件:
game.h
#ifndef __GAME_H__
#define __GAME_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ROW 5
#define COL 5
void init_board(char arr[ROW][COL], int row, int col);
void print_board(char arr[ROW][COL], int row, int col);
void player_move(char arr[ROW][COL], int row, int col);
void computer_move(char arr[ROW][COL], int row, int col);
static int is_full(char arr[ROW][COL], int row, int col);
char judge_win(char arr[ROW][COL], int row, int col);
#endif __GAME_H__
各函数实现:
game.c
#include "game.h"
void init_board(char arr[ROW][COL], int row, int col)
{
memset (arr,' ',(row*col)*sizeof(arr));
}//初始化
void print_board(char arr[ROW][COL],int row,int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
printf(" %c | %c | %c | %c | %c \n", arr[i][0], arr[i][1], arr[i][2], arr[i][3], arr[i][4]);
if (i!=4)
printf("---|---|---|---|--- \n");
}
}//打印
void player_move(char arr[ROW][COL], int row, int col)
{
int x = 0, y = 0;
while (1)
{
printf("请输入坐标:");
scanf("%d %d", &x, &y);
x--;
y--;
if (((x >= 0) && (x<= 4)) && ((y >= 0) && (y <= 4)))
{
if (arr[x][y] == ' ')
{
arr[x][y] = 'x';
break;
}
else
{
printf("坐标输入错误");
}
}
}
}//玩家走
void computer_move(char arr[ROW][COL], int row, int col)
{
while (1)
{
int x = rand() % 5;
int y = rand() % 5;
if (arr[x][y] == ' ')
{
arr[x][y] = '@';
break;
}
}
}//电脑走
static int is_full(char arr[ROW][COL], int row, int col)
{
int i = 0, j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (arr[i][j] == ' ')
return 0;//no full
}
}return 1;//full
}
char judge_win(char arr[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
if ((arr[i][0] == arr[i][1]) && (arr[i][1] == arr[i][2]) && (arr[i][2] == arr[i][3]) && (arr[i][3] == arr[i][4]) && (arr[i][0] != ' '))
{
return(arr[i][1]);
}
}
for (i = 0; i < col; i++)
{
if ((arr[0][i] == arr[1][i]) && (arr[1][i] == arr[2][i]) && (arr[2][i] == arr[3][i]) && (arr[3][i] == arr[4][i]) && (arr[0][i] != ' '))
{
return(arr[1][i]);
}
}
if ((arr[0][0] == arr[1][1]) && (arr[1][1] == arr[2][2]) && (arr[2][2] == arr[3][3]) && (arr[3][3] == arr[4][4]) && (arr[0][0] != ' '))
return(arr[0][0]);
if ((arr[0][4] == arr[1][3]) && (arr[1][3] == arr[2][2]) && (arr[2][2] == arr[3][1]) && (arr[3][1] == arr[4][0]) && (arr[0][4] != ' '))
return(arr[0][4]);
if (is_full(arr, row, col))
return('d');
else
return(' ');
}//判断输赢
实现部分:
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
#include "game.h"
void menu()
{
printf("*************************\n");
printf("**** 1.play 0.exit *****\n");
printf("*************************\n");
}
void game()
{
char arr[ROW][COL];
char ret = 0;
init_board( arr,ROW, COL);
print_board(arr, ROW, COL);
srand((unsigned int)time(NULL));
while (1)
{
player_move(arr, ROW, COL);
ret = judge_win(arr, ROW, COL);
if (ret != ' ')
{
break;
}
print_board(arr, ROW, COL);
printf("\n");
computer_move(arr, ROW, COL);
ret = judge_win(arr, ROW, COL);
if (ret != ' ')
{
break;
}
print_board(arr, ROW, COL);
printf("\n");
}
print_board(arr, ROW, COL);
if (ret == 'x')
printf("Congratulations!You Win!!!\n");
else if (ret == '@')
printf("Sorry,You Lose!\n");
else
printf("A Draw!\n");
}
int main()
{
int input = 0;
do
{
menu();
printf("请选择: ");
scanf("%d", &input);
switch (input)
{
case 1:
printf("开始游戏\n");
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误,请重新选择");
}
} while (input);
return 0;
}