前言
经过这么久的C语言学习,自己想试着通过所学知识想设计一个三子棋游戏,并且在自己不懈努力下完成了这个设计,本篇文章将分析一下三子棋游戏的设计过程。
一.设计需求
1.实现一个菜单页面
2.实现一个棋盘
3.实现玩家和电脑的交互下棋
4.实现输赢判断
二.各功能代码
1.菜单
void menu()
{
printf("********1. play********\n");
printf("********0. exit********\n");
}
2.初始化棋盘
void init_board(char board[ROW][COL], int a, int b)
{
int i = 0;
for (i = 0; i < a; i++)
{
int j = 0;
for (j = 0; j < b; j++)
{
board[i][j] = ' ';//初始化即棋盘每个格子为空格
}
}
}
3.打印棋盘
void print_board(char board[ROW][COL], int a, int b)
{
int i = 0;
for (i = 0; i < a; i++)
{
int j = 0;
for (j = 0; j < b; j++)
{
printf("%c", board[i][j]);
if (j < b - 1)
{
printf(" |");
}
}
printf("\n");
if (i < a - 1)
{
for (j = 0; j < b; j++)
{
printf("----");
if (j < b - 1)
{
printf("|");
}
}
printf("\n");
}
}
}
棋盘效果如图所示:
4.玩家下棋
void player_move(char board[ROW][COL], int a, int b)
{
int x = 0;
int y = 0;
printf("请输入下棋位置:\n");
while (1)
{
scanf("%d %d", &x, &y);
if (x <= a && x >= 1 && y <= b && y >= 1)\\判断玩家输入的坐标是否在棋盘范围
{
if (board[x-1][y-1] == '*'&&board[x-1][y-1] == '#')\\判断该位置是否有棋子
{
printf("请重新输入位置:\n");
}
else
{
board[x-1][y-1] = '*';
break;
}
}
else
{
printf("坐标非法,请输入位置:\n");
}
}
}
5.电脑下棋
void computer_move(char board[ROW][COL], int a, int b)
{
printf("电脑下棋:\n");
while (1)
{
int x = rand() % a;
int y = rand() % b;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
下棋效果图
5,判断棋盘下满
char is_full(char board[ROW][COL], int a, int b)
{
int i = 0;
int j = 0;
for (i = 0; i < a; i++)
{
for (j = 0; j < b; j++)
{
if (board[i][j] == ' ')\\如果有一个格子为空格则棋盘未满
{
return 'y';
}
}
}
return 'f';
}
6.判断输赢
char is_win(char board[ROW][COL], int a, int b)
{
int i = 0;
for (i = 0; i < a; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')\\某一行三个相同的情况
{
return board[i][1];
}
}
for (i = 0; i < b; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][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];
}
else if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')\\副对角线三个相同的情况
{
return board[1][1];
}
else if (is_full(board,a,b) != 'y')\\棋盘下满的情况
{
return 'x';
}
else
{
return 'C';
}
}
三.主要函数
代码如下:
void game()
{
char board[ROW][COL];
init_board(board,ROW,COL);
print_board(board,ROW,COL);
char ret = 0;
while (1)
{
player_move(board,ROW,COL);
print_board(board, ROW, COL);
ret = is_win(board, ROW, COL);
if (ret != 'C')
{
break;
}
computer_move(board, ROW, COL);
print_board(board, ROW, COL);
ret = is_win(board, ROW, COL);
if (ret != 'C')
{
break;
}
}
if (ret == '*')
{
printf("玩家胜利\n");
}
else if (ret == '#')
{
printf("电脑胜利\n");
}
else
{
printf("平局\n");
}
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
printf("请选择>\n");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误\n");
break;
}
} while (input);
return 0;
}
四.代码文件
1.game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include"game.h"
#include<time.h>
#include<stdlib.h>
void init_board(char board[ROW][COL], int a, int b)
{
int i = 0;
for (i = 0; i < a; i++)
{
int j = 0;
for (j = 0; j < b; j++)
{
board[i][j] = ' ';
}
}
}
void print_board(char board[ROW][COL], int a, int b)
{
int i = 0;
for (i = 0; i < a; i++)
{
int j = 0;
for (j = 0; j < b; j++)
{
printf("%c", board[i][j]);
if (j < b - 1)
{
printf(" |");
}
}
printf("\n");
if (i < a - 1)
{
for (j = 0; j < b; j++)
{
printf("----");
if (j < b - 1)
{
printf("|");
}
}
printf("\n");
}
}
}
void player_move(char board[ROW][COL], int a, int b)
{
int x = 0;
int y = 0;
printf("请输入下棋位置:\n");
while (1)
{
scanf("%d %d", &x, &y);
if (x <= a && x >= 1 && y <= b && y >= 1)
{
if (board[x-1][y-1] == '*')
{
printf("请重新输入位置:\n");
}
else
{
board[x-1][y-1] = '*';
break;
}
}
else
{
printf("坐标非法,请输入位置:\n");
}
}
}
void computer_move(char board[ROW][COL], int a, int b)
{
printf("电脑下棋:\n");
while (1)
{
int x = rand() % a;
int y = rand() % b;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
char is_full(char board[ROW][COL], int a, int b)
{
int i = 0;
int j = 0;
for (i = 0; i < a; i++)
{
for (j = 0; j < b; j++)
{
if (board[i][j] == ' ')
{
return 'y';
}
}
}
return 'f';
}
char is_win(char board[ROW][COL], int a, int b)
{
int i = 0;
for (i = 0; i < a; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
{
return board[i][1];
}
}
for (i = 0; i < b; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][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];
}
else if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
{
return board[1][1];
}
else if (is_full(board,a,b) != 'y')
{
return 'x';
}
else
{
return 'C';
}
}
2.text.c
#define _CRT_SECURE_NO_WARNINGS 1
#include<windows.h>
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
#include"game.h"
void menu()
{
printf("********1. play********\n");
printf("********0. exit********\n");
}
void game()
{
char board[ROW][COL];
init_board(board,ROW,COL);
print_board(board,ROW,COL);
char ret = 0;
while (1)
{
player_move(board,ROW,COL);
print_board(board, ROW, COL);
ret = is_win(board, ROW, COL);
if (ret != 'C')
{
break;
}
computer_move(board, ROW, COL);
print_board(board, ROW, COL);
ret = is_win(board, ROW, COL);
if (ret != 'C')
{
break;
}
}
if (ret == '*')
{
printf("玩家胜利\n");
}
else if (ret == '#')
{
printf("电脑胜利\n");
}
else
{
printf("平局\n");
}
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
printf("请选择>\n");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误\n");
break;
}
} while (input);
return 0;
}
3.game.h
#pragma once
#include<stdio.h>
#define ROW 3
#define COL 3
void init_board(char board[ROW][COL], int a, int b);
void print_board(char board[ROW][COL], int a, int b);
void player_move(char board[ROW][COL], int a, int b);
void computer_move(char board[ROW][COL], int a, int b);
char is_win(char board[ROW][COL],int a,int b);
五. 总结
不足:在实现完代码后,发现电脑下棋不够智能,下得比较‘呆’,希望自己在后期学习了更多的c语言知识后能够完善一下这个三子棋游戏。
其次自己也对于c语言更多问题的解决充满了好奇与期待,c语言对于自己来说就像一个巨大的宝藏,等着挖掘!!!