学习一门语言,不仅要活学活用,有时候还可以拿来做做游戏。
今天就来写一个三子棋的游戏代码。
写之前要有一个清楚的思路,不然你会像我一样,花了好久写的代码都没有用。废话不多说,开始程序。
这就是我的头文件啦
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
#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);
//"*"--玩家赢
//"#"--电脑赢
//"P"--平局
//"C"--继续
char IsWin(char board[ROW][COL], int row, int col);
想必你一看就觉得简单,不就是个程序流程嘛。哎对了,程序调用才刚刚开始。
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
while (input != 2)
{
menu();
printf("请输入对应的数字:>");
scanf("%d", &input);
switch (input)
{
case 1:game();
break;
case 2:printf("退出游戏\n");
break;
default:printf("输入错误,请重新输入\n");
break;
}
}
return 0;
}
当你输入1的时候,游戏就开始了。
void menu()
{
printf("************************\n");
printf("**** 1.play ****\n");
printf("**** 2.exit ****\n");
printf("************************\n");
}
void game()
{
char ret = 0;
char board[ROW][COL] = { 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;
}
}
if (ret == '*')
{
printf("玩家赢\n");
}
else if (ret == '#')
{
printf("电脑赢\n");
}
else if (ret == 'P')
{
printf("平局\n");
}
}
看见这个乱糟糟的代码,是不是不想看了,别着急慢慢看,看完我就告诉你会有惊喜哦。
#define _CRT_SECURE_NO_WARNINGS 1
#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 i = 0;
int j = 0;
printf("玩家走\n");
while (1)
{
printf("请输入坐标:>");
scanf("%d %d", &i, &j);
if (i >= 1 && i <= row && j >= 1 && j <= col)
{
if (board[i-1][j-1] == ' ')
{
board[i - 1][j - 1] = '*';
break;
}
else
{
printf("坐标被占用,请重新输入:>\n");
}
}
else
{
printf("坐标错误\n");
}
}
}
void ComputerMove(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
printf("电脑走\n");
while (1)
{
i = rand() % row;
j = rand() % col;
if (board[i][j] == ' ')
{
board[i][j] = '#';
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;
int j = 0;
char goal = 0;
//判断行是否相同
for (i = 0; i < row; i++)
{
int count = 0;
for (j = 0; j < col; j++)
{
goal = board[i][0];
if (goal == board[i][j] && board[i][j] !=' ')
{
count++;
}
if (col == count)
{
return goal;
}
}
}
//判断列是否相同
for (i = 0; i < col; i++)
{
int count = 0;
for (j = 0; j < row; j++)
{
goal = board[i][0];
if (goal == board[i][j] && board[i][j] != ' ')
{
count++;
}
if (row == count)
{
return goal;
}
}
}
//判断对角是否相同
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) == 1)
{
return 'P';
}
else
{
return 'C';
}
}
惊喜!!!
看,上面就是惊喜。今天花了好几个小时才敲出来的,其实最难的感觉就是判断。而且在对角是否相等那一部分没有找到好的算法,只能用个“死”方法。如果你有什么好的算法,我还是很想了解的哦。