一、游戏思路
三子棋游戏是三个连着的棋为胜的一款游戏,c语言实现步骤如下:
1.设计游戏菜单
2.打印棋盘
3.进行游戏
4.判断游戏输赢
二、游戏框架
1.游戏菜单
通过一个menu函数设计一个游戏菜单
void menu()
{
printf("*****************\n");
printf("******1.play*****\n");
printf("******0.exit*****\n");
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;
}
2.游戏主体
1.初始化棋盘
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] = ' ';
}
}
}
2.打印棋盘
第一行中每三个空格就打印一个|
第二行每打印---一次,就打印一次|,注意在循环过程中要减1否则会导致左边会多打印一次
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
4.游戏进行
1.玩家操作
注意要进行判断所输入的数组是否越界
void Playermove(char board[ROW][COL], int row, int col)
{
printf("玩家下棋");
int x = 0;
int y = 0;
while (1)
{
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");
}
}
}
2.电脑操作
void computermove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("电脑下\n");
while (1)
{
x = rand() % ROW;
y = rand() % COL;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
5.判断输赢和平局
int is_full(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 is_win(char board[ROW][COL], int row, int col)
{
int i = 0;
for ( i = 0; i < row; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i]&&board[1][i] != ' ')
{
return board[1][i];
}
}
for ( i = 0; i < col; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
{
return board[i][1];
}
}
if (board[0][0]==board[1][1]&&board[1][1]==board[2][2] && board[1][1] != ' ')
{
return board[1][1];
}
if (board[2][0] == board[1][1] && board[1][1] == board[0][2]&&board[1][1] != ' ')
{
return board[1][1];
}
//平手
if (1 == is_full(board,row,col))
{
return 'Q';
}
return'c';
}
3.游戏运行函数
#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 is_win(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};
InitBoard(board, ROW, COL);
//打印棋盘
displayboard(board, ROW, COL);
char ret = 0;
//下棋
while (1)
{
Playermove(board, ROW, COL);
displayboard(board, ROW, COL);
ret = is_win(board, ROW, COL);
if (ret != 'c')
break;
computermove(board, ROW, COL);
displayboard(board, ROW, COL);
ret = is_win(board, ROW, COL);
if (ret != 'c')
break;
}
if (ret == '*')
{
printf("win\n");
}
else if (ret == '#')
{
printf("loser\n");
}
else
{
printf("bargin\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.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 is_win(char board[ROW][COL], int row, int col);
game.c
int is_full(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 is_win(char board[ROW][COL], int row, int col)
{
int i = 0;
for ( i = 0; i < row; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i]&&board[1][i] != ' ')
{
return board[1][i];
}
}
for ( i = 0; i < col; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
{
return board[i][1];
}
}
if (board[0][0]==board[1][1]&&board[1][1]==board[2][2] && board[1][1] != ' ')
{
return board[1][1];
}
if (board[2][0] == board[1][1] && board[1][1] == board[0][2]&&board[1][1] != ' ')
{
return board[1][1];
}
//平手
if (1 == is_full(board,row,col))
{
return 'Q';
}
return'c';
}