猜数字
cpp
#include<iostream>
using namespace std;
#include<ctime>
int main() //猜数字
{
unsigned int count_max = 8;
srand((unsigned int)time(NULL));
unsigned int num = rand() % 100 + 1; //随机生成1~100
unsigned int a = 0, count = 0, score = 0;
while (1)
{
cout << "请输入数字:" << endl;
cin >> a;
if (a > num)
{
cout << "数字过大。" << endl;
count++;
if (count > count_max) break;
}
else if (a < num)
{
cout << "数字过小。" << endl;
count++;
if (count > count_max) break;
}
else
{
cout << "猜测正确!" << endl;
score += 10;
cout << "你已得:" << score << "分" << endl;
count = 0;
count_max = count_max / 2;
cout << "游戏继续..." << endl;
srand((unsigned int)time(NULL));
num = rand() % 100 + 1;
}
}
cout << "GAME OVER!" << endl;
cout << "数字为:" << num << endl;
cout << "获得积分:" << score << endl;
}
数字炸弹
cpp
#include<iostream>
//使用< iostream>时,该头文件没有定义全局命名空间,必须使用namespace std;这样才能正确使用cout。命名空间std封装的是标准程序库的名称。
using namespace std;
#include<ctime>
int main() //数字炸弹
{
srand((unsigned int)time(NULL));
while (1)
{
unsigned int num = rand() % 100 + 1; //随机生成1~100
unsigned int a = 0, max = 100, min = 1;
while (1)
{
cout << "炸弹在" << min << "到" << max << "之间(边界可选)。" << endl;
cout << "请选择数字:" << endl;
cin >> a;
if (a >= min && a <= max)
{
if (a > num)
{
max = a - 1;
cout << "请继续..." << endl;
}
else if (a < num)
{
min = a + 1;
cout << "请继续..." << endl;
}
else
{
break;
}
}
else
{
cout << "警告!输入错误!" << endl;
}
if (max == min)
{
cout << "艾玛,接下来的小兄弟中奖了~~~" << endl;
break;
}
}
cout << "Bomb!炸弹是:" << num << endl;
cout << "你输了!" << endl;
cout << "按空格键重新开始游戏" << endl;
system("pause");//按任意键继续
system("cls");//清屏
}
}
三子棋
首先导入game.h:
cpp
#define ROW 3
#define COL 3
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
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);
int IsFull(char board[ROW][COL], int row, int col);
初始化棋面:
cpp
void InitBoard(char board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
}
棋面显示:
cpp
void DisplayBoard(char board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
printf(" %c ",board[i][j]);
if (j < col - 1)printf("|");
}
printf("\n");
if (i < row - 1)
{
for (int j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)printf("|");
}
printf("\n");
}
}
}
玩家输入x、y坐标控制棋子移动:
cpp
void PlayerMove(char board[ROW][COL], int row, int col)
{
int x = 0,y = 0;
printf("玩家走>\n");
while (1)
{
//scanf("%d %d", &x,&y);
printf("请输入要走的x坐标:");
scanf("%d", &x);
printf("请输入要走的y坐标:");
scanf("%d", &y);
if (x > 0 && x <= row && y > 0 && y <= col && board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("坐标非法,请重新输入!\n");
}
}
}
电脑随机产生棋子位置控制棋子移动:
cpp
void ComputerMove(char board[ROW][COL], int row, int col)
{
int x = 0, y = 0;
printf("电脑走>\n");
while (1)
{
x = rand() % row;
y= rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
判断棋面是否满:
cpp
int IsFull(char board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (board[i][j] == ' ')
return 0;
}
}
return 1;
}
游戏输赢的机制:
cpp
char IsWin(char board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
{
return board[i][1];
}
}
for (int j = 0; j < col; j++)
{
if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] != ' ')
{
return board[1][j];
}
}
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 'Q';
}
return 'C';
}
游戏显示菜单:
cpp
void menu()
{
printf("*************************************\n");
printf("**********1.play 0.exit**********\n");
printf("*************************************\n");
}
游戏执行机制:
cpp
void game()
{
char board[ROW][COL] = { 0 };
char ret;
InitBoard(board, ROW, COL);
DisplayBoard(board, ROW, COL);
while (1)
{
PlayerMove(board, ROW, COL);
system("cls");
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
{
printf("平局!\n");
}
}
执行主函数:
cpp
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
printf("请选择>");
scanf("%d", &input);
switch (input)
{
case 1:
{
system("cls");
game();
break;
}
case 0:
{
system("cls");
printf(" ˳ Ϸ\n");
break;
}
default:
{
printf("请重新输入!\n");
system("pause");
system("cls");
break;
}
}
} while (input);
return 0;
}
结果: