1.主函数
先显示菜单告知玩家操作;1进入游戏;0退出游戏
2 menu()
提醒玩家操作;
3 game()
先初始化键盘并打印出健盘再进入循环玩家先下棋之后是电脑下棋;每执行完下棋的步骤后进行判断是否赢如果没赢打印出棋盘现状;循环结束后判断玩家还是电脑赢还是平局。
4 Init_board(char board[row][col], int Row, int Col)
对棋盘进行初始化,将数组里的元素初始化成' ',因为在game()中数组初始化为0,0与'\0'一样不占空间结果如图所示:
代码为
5.Display_board(char board[row][col], int Row, int Col)
对棋盘进行打印;利用循环对棋盘进行打印使代码实用性更强。
6.player_move(char board[row][col], int Row, int Col)
让玩家输入棋要下的坐标,判断是否为' ' ,若是' ' 给数组赋值;若不是输出请重新输入,若坐标超出范围输出"输入错误";
7 computer_move(char board[row][col], int Row, int Col)
用rand()函数给出随机值作为坐标,为了让坐标在数组范围之内要分别对行数和列数进行取余;
同时为了让数目随机要在main函数中加入srand函数同时加上头文件
8.is_win(char board[row][col], int Row, int Col)
判断游戏状态;若返回 # 则电脑赢,若为 * 则为玩家赢;若为Q则是棋盘已满,平局;C为继续;
选择#和*是因为返回值可以直接返回board[i][0]或board[0][i];
9 is_full(char board[row][col], int Row, int Col)
利用循环判断数组中元素是否为' ';若是返回0;否则返回1。
10 其他知识点
分文件写代码时,要用#include"头文件.h"包含头文件;
要有 #define _CRT_SECURE_NO_WARNINGS 1 却保scanf正常使用;
11 完整代码
game.h:
# pragma once
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define row 3
#define col 3
//初始化棋盘
void Init_board(char board[row][col], int Row, int Col);
//打印棋盘
void Display_board(char board[row][col], int Row, int Col);
//玩家下棋
void player_move(char board[row][col], int Row, int Col);
//电脑下棋
void computer_move(char board[row][col], int Row, int Col);
//判断是否满
int is_full(char board[row][col], int Row, int Col);
//判断游戏状态
char is_win(char board[row][col], int Row, int Col);
game1a.c:
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void Init_board(char board[row][col],int Row,int Col)
{
int i = 0, j = 0;
for(i=0;i<Row;i++)
for (j = 0; j < Col; j++)
{
board[i][j] = ' ';
}
}
void Display_board(char board[row][col], int Row, int Col)
{
int i = 0, j = 0;
for (i = 0; i < Row; i++)
{
for (j = 0; j < Col; j++)
{
printf(" %c ", board[i][j]);
if (j < Col - 1)
printf("|");
}
printf("\n");
if (i < Row - 1)
{
for (j = 0; j < Col; j++)
{
printf("---");
if (j < Col - 1)
printf("|");
}
printf("\n");
}
}
}
void player_move(char board[row][col], int Row, int Col)
{
int x = 0, y = 0;
printf("请玩家下棋>\n");
while (1)
{
printf("请输入下棋的坐标:\n");
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] = '*';
break;
}
else
{
printf("请重新输入.\n");
}
}
else
{
printf("输入错误\n");
}
}
}
void computer_move(char board[row][col], int Row, int Col)
{
printf("电脑下棋>\n");
while (1)
{
int i = rand() % row;
int j = rand() % col;
if (board[i][j] != '*')
{
board[i][j] = '#';
break;
}
}
}
int is_full(char board[row][col], int Row, int Col)
{
int i = 0, 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[row][col], 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][0] != ' ')
return board[i][0];
}
for (i = 0; i < Col; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
return board[0][i];
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ')
return board[0][0];
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ')
return board[0][2];
//判断平局;
if (is_full(board, row, col) == 1)
//Q为平局;
return 'Q';
else
//c为继续;
return 'C';
}
game1b.c:
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
//游戏函数
void game()
{
char ret=0;
char board[row][col] = { 0 };
//初始化棋盘;
Init_board(board, row, col);
//打印初始棋盘;
Display_board(board, row, col);
while (1)
{
//玩家下棋;
player_move(board, row, col);
ret = is_win(board, row, col);
//继续游戏C;
if (ret != 'C')
break;
Display_board(board, row, col);
//电脑下棋
computer_move(board, row, col);
ret = is_win(board,row,col);
if (ret != 'C')
break;
Display_board(board, row, col);
}
//电脑赢#
if (ret == '#')
{
printf("电脑赢");
}
else
{
//玩家赢*
if (ret == '*')
{
printf("玩家赢");
}
else
printf("平局");
}
}
//菜单
void menu()
{
printf("************************\n");
printf("***********1.play*******\n");
printf("***********0.end********\n");
printf("************************\n");
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
scanf("%d",&input);
printf("请选择:\n");
switch (input)
{
case 1:game();
break;
case 0:printf("游戏结束");
break;
default:printf("输入错误");
break;
}
} while (input);
return 0;
}