首先选手 分为玩家和电脑,各自轮流在棋盘上 下棋,玩家先其次电脑。
- 如果有一方最先达到一行或者一列或对角线上棋子一样 ,则赢了
- 如果双方都没有达到一行棋子一样且棋盘满了,则游戏平局;
写代码前理清代码逻辑很重要的。
代码文件分布:
总体是采用 TDD(Test Drive Development )测试驱动开发,进行的游戏设计
#include "game.h"
void menu()
{
printf("*****************************\n");
printf("***** 1 . play ***********\n");
printf("***** 0 . exit ***********\n");
printf("*****************************\n");
}
void game()
{
printf("玩游戏\n");
}
int main()
{
int input = 0;
do
{
//菜单
menu();
printf("请选择:>");
scanf("%d",&input);
switch (input) //进行选择
{
case 1:
game();
break;
case 0:
printf("退出游戏 \n");
break;
default:
printf("输入错误,请重新输入\n");
break;
}
} while (input);
return 0;
}
函数功能实现主要在game.c中实现
//在test.c中添加函数调用
void game()
{
//设置棋盘
char board[ROW][COL] = {0};
InitBoard(board,ROW,COL);
//展示棋盘
DisplayBoard(board,ROW,COL);
}
//game.c
#include "game.h"
//实现初始换棋盘
void InitBoard(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row;i++)
{
for (j = 0; j < col;j++)
{
board[i][j] = ' ';
}
}
}
//实现展示棋盘
void DisplayBoard(char board[ROW][COL],int row,int col)
{
int i = 0;
int 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");
}
}
//game.h 记得声明函数声明,引头文件
#include
#define ROW 3
#define COL 3
//初始化棋盘
void InitBoard(char board[ROW][COL],int row,int col);
//展示棋盘
void DisplayBoard(char board[ROW][COL],int row,int col);
//test.c中的函数调用
void game()
{
//设置棋盘
char board[ROW][COL] = {0};
InitBoard(board,ROW,COL);
//展示棋盘
DisplayBoard(board,ROW,COL);
//玩家下棋
while (1)
{
PlayerMove(board,ROW,COL);
DisplayBoard(board,ROW,COL);
}
}
//game.c
//实现玩家下棋
void PlayerMove(char board[ROW][COL],int row,int col)
{
//输入坐标
int x = 0;
int y = 0;
printf("玩家下棋\n");
while (1)
{
printf("请输入坐标:");
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");
}
}
}
//game.h
//玩家下棋
void PlayerMove(char board[ROW][COL],int row,int col);
// game .h 要打上
#include
#include
//电脑下棋
void ComputerMove(char board[ROW][COL],int row,int col);
//game.c
//实现电脑下棋
void ComputerMove(char board[ROW][COL],int row,int col)
{
printf("电脑下棋\n");
int x = 0;
int y = 0;
while (1)
{
//生成随机坐标
x = rand() % row;//注意这里生成坐标的范围是0-2
y = rand() % col;
if(board[x][y] == ' ') //坐标为空才放棋子
{
board[x][y] = '#'; //电脑输入的是#
break;
}
}
}
//在main函数中记得加一个生成随机数的种子
//srand((unsigned int )time(NULL));
void game()
{
//设置棋盘
char board[ROW][COL] = {0};
InitBoard(board,ROW,COL);
//展示棋盘
DisplayBoard(board,ROW,COL);
while (1)
{
//玩家下棋
PlayerMove(board,ROW,COL);
DisplayBoard(board,ROW,COL);
//电脑下棋
ComputerMove(board,ROW,COL);
DisplayBoard(board,ROW,COL);
}
}
//test.c
void game()
{
//设置棋盘
char board[ROW][COL] = {0};
char ret = 0;
InitBoard(board,ROW,COL);
//展示棋盘
DisplayBoard(board,ROW,COL);
//进行下棋
while (1)
{
//玩家下棋
PlayerMove(board,ROW,COL);
DisplayBoard(board,ROW,COL);
ret = IsWin(board,ROW,COL);
if (ret != 'C')
{
break;
}
//电脑下棋
ComputerMove(board,ROW,COL);
DisplayBoard(board,ROW,COL);
ret = IsWin(board, ROW, COL);
if (ret != 'C')
{
break;
}
//输赢的判断
//玩家赢 - '*'
//电脑赢 - '#'
//平局 --- 'Q'
//游戏继续-'C'
}
if (ret == '*')
{
printf("玩家赢了\n");
}
else if (ret == '#')
{
printf("电脑赢了\n");
}
else
{
printf("平局\n");
}
}
//game.h
//判断输赢
char IsWin(char board[ROW][COL],int row,int col);
//game.c
//实现输赢判断
static int Isfull(char board[ROW][COL],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 IsWin(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][2] != ' ')
return board[i][0];//一行相同
}
for (i = 0; i < col;i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[2][i] != ' ')
return board[1][i];//一列相同
}
//对角线
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
return board[1][1];
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
return board[1][1];
//判断棋盘是否满了
if (Isfull(board, row, col))
{
return 'Q';
}
return 'C';
}
game.h
#pragma once
#include
#include
#include
#define ROW 3
#define COL 3
//初始化棋盘
void InitBoard(char board[ROW][COL],int row,int col);
//展示棋盘
void DisplayBoard(char board[ROW][COL],int row,int col);
//玩家下棋
void PlayerMove(char board[ROW][COL],int row,int col);
//电脑下棋
void ComputerMove(char board[ROW][COL],int row,int col);
//判断输赢
char IsWin(char board[ROW][COL],int row,int col);
test.c
#define _CRT_SECURE_NO_WARNINGS
#include "game.h"
void menu()
{
printf("*****************************\n");
printf("***** 1 . play ***********\n");
printf("***** 0 . exit ***********\n");
printf("*****************************\n");
}
void game()
{
//设置棋盘
char board[ROW][COL] = {0};
char ret = 0;
InitBoard(board,ROW,COL);
//展示棋盘
DisplayBoard(board,ROW,COL);
//进行下棋
while (1)
{
//玩家下棋
PlayerMove(board,ROW,COL);
DisplayBoard(board,ROW,COL);
ret = IsWin(board,ROW,COL);
if (ret != 'C')
{
break;
}
//电脑下棋
ComputerMove(board,ROW,COL);
DisplayBoard(board,ROW,COL);
ret = IsWin(board, ROW, COL);
if (ret != 'C')
{
break;
}
//输赢的判断
//玩家赢 - '*'
//电脑赢 - '#'
//平局 --- 'Q'
//游戏继续-'C'
}
if (ret == '*')
{
printf("玩家赢了\n");
}
else if (ret == '#')
{
printf("电脑赢了\n");
}
else
{
printf("平局\n");
}
}
int main()
{
int input = 0;
srand((unsigned int )time(NULL));
do
{
//菜单
menu();
printf("请选择:>");
scanf("%d",&input);
switch (input) //进行选择
{
case 1:
game();
break;
case 0:
printf("退出游戏 \n");
break;
default:
printf("输入错误,请重新输入\n");
break;
}
} while (input);
return 0;
}
game.c
#define _CRT_SECURE_NO_WARNINGS
#include "game.h"
//实现初始换棋盘
void InitBoard(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row;i++)
{
for (j = 0; j < col;j++)
{
board[i][j] = ' ';
}
}
}
//实现展示棋盘
void DisplayBoard(char board[ROW][COL],int row,int col)
{
int i = 0;
int 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 PlayerMove(char board[ROW][COL],int row,int col)
{
//输入坐标
int x = 0;
int y = 0;
printf("玩家下棋\n");
while (1)
{
printf("请输入坐标:");
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 ComputerMove(char board[ROW][COL],int row,int col)
{
printf("电脑下棋\n");
int x = 0;
int y = 0;
while (1)
{
//生成随机坐标
x = rand() % row;//注意这里生成坐标的范围是0-2
y = rand() % col;
if(board[x][y] == ' ') //坐标为空才放棋子
{
board[x][y] = '#'; //电脑输入的是#
break;
}
}
}
//实现输赢判断
static int Isfull(char board[ROW][COL],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 IsWin(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][2] != ' ')
return board[i][0];
}
for (i = 0; i < col;i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[2][i] != ' ')
return board[1][i];
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
return board[1][1];
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
return board[1][1];
if (Isfull(board, row, col))
{
return 'Q';
}
return 'C';
}