打飞机
#include<iostream>
#include<windows.h>
#include<conio.h>
#include<time.h>
#include<string>
using namespace std;
/*=============== all the structures ===============*/
typedef struct Frame
{
COORD position[2];
int flag;
}Frame;
/*=============== all the functions ===============*/
void SetPos(COORD a)// set cursor
{
HANDLE out=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(out, a);
}
void SetPos(int i, int j)// set cursor
{
COORD pos={i, j};
SetPos(pos);
}
void HideCursor()
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
//把第y行,[x1, x2) 之间的坐标填充为 ch
void drawRow(int y, int x1, int x2, char ch)
{
SetPos(x1,y);
for(int i = 0; i <= (x2-x1); i++)
cout<<ch;
}
//在a, b 纵坐标相同的前提下,把坐标 [a, b] 之间填充为 ch
void drawRow(COORD a, COORD b, char ch)
{
if(a.Y == b.Y)
drawRow(a.Y, a.X, b.X, ch);
else
{
SetPos(0, 25);
cout<<"error code 01:无法填充行,因为两个坐标的纵坐标(x)不相等";
system("pause");
}
}
//把第x列,[y1, y2] 之间的坐标填充为 ch
void drawCol(int x, int y1, int y2, char ch)
{
int y=y1;
while(y!=y2+1)
{
SetPos(x, y);
cout<<ch;
y++;
}
}
void drawCol(COORD a, COORD b, char ch)
{
if(a.X == b.X)
drawCol(a.X, a.Y, b.Y, ch);
else
{
SetPos(0, 25);
cout<<"error code 02:无法填充列,因为两个坐标的横坐标(y)不相等";
system("pause");
}
}
//左上角坐标、右下角坐标、用row填充行、用col填充列
void drawFrame(COORD a, COORD b, char row, char col)
{
drawRow(a.Y, a.X+1, b.X-1, row);
drawRow(b.Y, a.X+1, b.X-1, row);
drawCol(a.X, a.Y+1, b.Y-1, col);
drawCol(b.X, a.Y+1, b.Y-1, col);
}
void drawFrame(int x1, int y1, int x2, int y2, char row, char col)
{
COORD a={x1, y1};
COORD b={x2, y2};
drawFrame(a, b, row, col);
}
void drawFrame(Frame frame, char row, char col)
{
COORD a = frame.position[0];
COORD b = frame.position[1];
drawFrame(a, b, row, col);
}
void drawPlaying()
{
drawFrame(0, 0, 48, 24, '=', '|');// draw map frame;
drawFrame(49, 0, 79, 4, '-', '|');// draw output frame
drawFrame(49, 4, 79, 9, '-', '|');// draw score frame
drawFrame(49, 9, 79, 20, '-', '|');// draw operate frame
drawFrame(49, 20, 79, 24, '-', '|');// draw other message frame
SetPos(52, 6);
cout<<"得分:";
SetPos(52, 7);
cout<<"称号:";
SetPos(52,10);
cout<<"操作方式:";
SetPos(52,12);
cout<<" a,s,d,w 控制战机移动。";
SetPos(52,14);
cout<<" p 暂停游戏。";
SetPos(52,16);
cout<<" e 退出游戏。";
}
//在[a, b)之间产生一个随机整数
int random(int a, int b)
{
int c=(rand() % (a-b))+ a;
return c;
}
//在两个坐标包括的矩形框内随机产生一个坐标
COORD random(COORD a, COORD b)
{
int x=random(a.X, b.X);
int y=random(a.Y, b.Y);
COORD c={x, y};
return c;
}
bool judgeCoordInFrame(Frame frame, COORD spot)
{
if(spot.X>=frame.position[0].X)
if(spot.X<=frame.position[1].X)
if(spot.Y>=frame.position[0].Y)
if(spot.Y<=frame.position[0].Y)
return true;
return false;
}
void printCoord(COORD a)
{
cout <<"( "<<a.X<<" , "<<a.Y<<" )";
}
void printFrameCoord(Frame a)
{
printCoord(a.position[0]);
cout <<" - ";
printCoord(a.position[1]);
}
int drawMenu()
{
SetPos(30, 1);
cout<<"P l a n e W a r";
drawRow(3, 0, 79, '-');
drawRow(5, 0, 79, '-');
SetPos(28, 4);
cout<<"w 和 s 选择, k 确定";
SetPos(15, 11);
cout<<"1. 简单的敌人";
SetPos(15, 13);
cout<<"2. 冷酷的敌人";
drawRow(20, 0, 79, '-');
drawRow(22, 0, 79, '-');
SetPos(47, 11);
cout<<"简单的敌人:";
SetPos(51, 13);
cout<<"简单敌人有着较慢的移动速度。";
SetPos(24, 21);
cout<<"制作:<bits/stdc++.h>";
int j=11;
SetPos(12, j);
cout<<">>";
//drawFrame(45, 9, 79, 17, '=', '|');
while(1)
{ if( _kbhit() )
{
char x=_getch();
switch (x)
{
case 'w' :
{
if( j == 13)
{
SetPos(12, j);
cout<<" ";
j = 11;
SetPos(12, j);
cout<<">>";
SetPos(51, 13);
cout<<" ";
SetPos(47, 11);
cout<<"简单的敌人:";
SetPos(51, 13);
cout<<"简单敌人有较慢的移动速度,比较容易对付";
}
break;
}
case 's' :
{
if( j == 11 )
{
SetPos(12, j);
cout<<" ";
j = 13;
SetPos(12, j);
cout<<">>";
SetPos(51, 13);
cout<<" ";
SetPos(47, 11);
cout<<"冷酷的敌人:";
SetPos(51, 13);
cout<<"冷酷的敌人移动速度较快,不是很好对付哟。";
}
break;
}
case 'k' :
{
if (j == 8) return 1;
else return 2;
}
}
}
}
}
class Game
{
public:
COORD position[10];
COORD bullet[10];
Frame enemy[8];
int score;
int rank;
int rankf;
string title;
int flag_rank;
Game ();
//初始化所有
void initPlane();
void initBullet();
void initEnemy();
//初始化其中一个
//void initThisBullet( COORD );
//void initThisEnemy( Frame );
void planeMove(char);
void bulletMove();
void enemyMove();
//填充所有
void drawPlane();
void drawPlaneToNull();
void drawBullet();
void drawBulletToNull();
void drawEnemy();
void drawEnemyToNull();
//填充其中一个
void drawThisBulletToNull( COORD );
void drawThisEnemyToNull( Frame );
void Pause();
void Playing();
void judgePlane();
void judgeEnemy();
void Shoot();
void GameOver();
void printScore();
};
Game::Game()
{
initPlane();
initBullet();
initEnemy();
score = 0;
rank = 25;
rankf = 0;
flag_rank = 0;
}
void Game::initPlane()
{
COORD centren={39, 22};
position[0].X=position[5].X=position[7].X=position[9].X=centren.X;
position[1].X=centren.X-2;
position[2].X=position[6].X=centren.X-1;
position[3].X=position[8].X=centren.X+1;
position[4].X=centren.X+2;
for(int i=0; i<=4; i++)
position[i].Y=centren.Y;
for(int i=6; i<=8; i++)
position[i].Y=centren.Y+1;
position[5].Y=centren.Y-1;
position[9].Y=centren.Y-2;
}
void Game::drawPlane()
{
for(int i=0; i<9; i++)
{
SetPos(position[i]);
if(i!=5)
cout<<"O";
else if(i==5)
cout<<"|";
}
}
void Game::drawPlaneToNull()
{
for(int i=0; i<9; i++)
{
SetPos(position[i]);
cout<<" ";
}
}
void Game::initBullet()
{
for(int i=0; i<10; i++)
bullet[i].Y = 30;
}
void Game::drawBullet()
{
for(int i=0; i<10; i++)
{
if( bullet[i].Y != 30)
{
SetPos(bullet[i]);
cout<<"^";
}
}
}
void Game::drawBulletToNull()
{
for(int i=0; i<10; i++)
if( bullet[i].Y != 30 )
{
COORD pos={bullet[i].X, bullet[i].Y+1};
SetPos(pos);
cout<<" ";
}
}
void Game::initEnemy()
{
COORD a={1, 1};
COORD b={45, 15};
for(int i=0; i<8; i++)
{
enemy[i].position[0] = random(a, b);
enemy[i].position[1].X = enemy[i].position[0].X + 3;
enemy[i].position[1].Y = enemy[i].position[0].Y + 2;
}
}
void Game::drawEnemy()
{
for(int i=0; i<8; i++)
drawFrame(enemy[i].position[0], enemy[i].position[1], '-', '|');
}
void Game::drawEnemyToNull()
{
for(int i=0; i<8; i++)
{
drawFrame(enemy[i].position[0], enemy[i].position[1], ' ', ' ');
}
}
void Game::Pause()
{
SetPos(61,2);
cout<<" ";
SetPos(61,2);
cout<<"暂停中...";
char c=_getch();
while(c!='p')
c=_getch();
SetPos(61,2);
cout<<" ";
}
void Game::planeMove(char x)
{
if(x == 'a')
if(position[1].X != 1)
for(int i=0; i<=9; i++)
position[i].X -= 2;
if(x == 's')
if(position[7].Y != 23)
for(int i=0; i<=9; i++)
position[i].Y += 1;
if(x == 'd')
if(position[4].X != 47)
for(int i=0; i<=9; i++)
position[i].X += 2;
if(x == 'w')
if(position[5].Y != 3)
for(int i=0; i<=9; i++)
position[i].Y -= 1;
}
void Game::bulletMove()
{
for(int i=0; i<10; i++)
{
if( bullet[i].Y != 30)
{
bullet[i].Y -= 1;
if( bullet[i].Y == 1 )
{
COORD pos={bullet[i].X, bullet[i].Y+1};
drawThisBulletToNull( pos );
bullet[i].Y=30;
}
}
}
}
void Game::enemyMove()
{
for(int i=0; i<8; i++)
{
for(int j=0; j<2; j++)
enemy[i].position[j].Y++;
if(24 == enemy[i].position[1].Y)
{
COORD a={1, 1};
COORD b={45, 3};
enemy[i].position[0] = random(a, b);
enemy[i].position[1].X = enemy[i].position[0].X + 3;
enemy[i].position[1].Y = enemy[i].position[0].Y + 2;
}
}
}
void Game::judgePlane()
{
for(int i = 0; i < 8; i++)
for(int j=0; j<9; j++)
if(judgeCoordInFrame(enemy[i], position[j]))
{
SetPos(62, 1);
cout<<"坠毁";
drawFrame(enemy[i], '+', '+');
Sleep(1000);
GameOver();
break;
}
}
void Game::drawThisBulletToNull( COORD c)
{
SetPos(c);
cout<<" ";
}
void Game::drawThisEnemyToNull( Frame f )
{
drawFrame(f, ' ', ' ');
}
void Game::judgeEnemy()
{
for(int i = 0; i < 8; i++)
for(int j = 0; j < 10; j++)
if( judgeCoordInFrame(enemy[i], bullet[j]) )
{
score += 5;
drawThisEnemyToNull( enemy[i] );
COORD a={1, 1};
COORD b={45, 3};
enemy[i].position[0] = random(a, b);
enemy[i].position[1].X = enemy[i].position[0].X + 3;
enemy[i].position[1].Y = enemy[i].position[0].Y + 2;
drawThisBulletToNull( bullet[j] );
bullet[j].Y = 30;
}
}
void Game::Shoot()
{
for(int i=0; i<10; i++)
if(bullet[i].Y == 30)
{
bullet[i].X = position[5].X;
bullet[i].Y = position[5].Y-1;
break;
}
}
void Game::printScore()
{
if(score == 120 && flag_rank == 0)
{
rank -= 3;
flag_rank = 1;
}
else if( score == 360 && flag_rank == 1)
{
rank -= 5;
flag_rank = 2;
}
else if( score == 480 && flag_rank == 2)
{
rank -= 5;
flag_rank = 3;
}
int x=rank/5;
SetPos(60, 6);
cout<<score;
if( rank!=rankf )
{
SetPos(60, 7);
if( x == 5)
title="初级飞行员";
else if( x == 4)
title="中级飞行员";
else if( x == 3)
title="高级飞行员";
else if( x == 2 )
title="王牌飞行员";
cout<<title;
}
rankf = rank;
}
void Game::Playing()
{
//HANDLE MFUN;
//MFUN= CreateThread(NULL, 0, MusicFun, NULL, 0, NULL);
drawEnemy();
drawPlane();
int flag_bullet = 0;
int flag_enemy = 0;
while(1)
{
Sleep(8);
if(_kbhit())
{
char x = _getch();
if ('a' == x || 's' == x || 'd' == x || 'w' == x)
{
drawPlaneToNull();
planeMove(x);
drawPlane();
judgePlane();
}
else if ('p' == x)
Pause();
else if( 'k' == x)
Shoot();
else if( 'e' == x)
{
//CloseHandle(MFUN);
GameOver();
break;
}
}
/* 处理子弹 */
if( 0 == flag_bullet )
{
bulletMove();
drawBulletToNull();
drawBullet();
judgeEnemy();
}
flag_bullet++;
if( 5 == flag_bullet )
flag_bullet = 0;
/* 处理敌人 */
if( 0 == flag_enemy )
{
drawEnemyToNull();
enemyMove();
drawEnemy();
judgePlane();
}
flag_enemy++;
if( flag_enemy >= rank )
flag_enemy = 0;
/* 输出得分 */
printScore();
}
}
void Game::GameOver()
{
system("cls");
COORD p1={28,9};
COORD p2={53,15};
drawFrame(p1, p2, '=', '|');
SetPos(36,12);
string str="Game Over!";
for(int i=0; i<str.size(); i++)
{
Sleep(80);
cout<<str[i];
}
Sleep(1000);
system("cls");
drawFrame(p1, p2, '=', '|');
SetPos(31, 11);
cout<<"击落敌机:"<<score/5<<" 架";
SetPos(31, 12);
cout<<"得 分:"<<score;
SetPos(31, 13);
cout<<"获得称号:"<<title;
SetPos(30, 16);
Sleep(1000);
cout<<"继续? 是(y)| 否(n)制作:<bits/stdc++.h>";
as:
char x=_getch();
if (x == 'n')
exit(0);
else if (x == 'y')
{
system("cls");
Game game;
int a = drawMenu();
if(a == 2)
game.rank = 20;
system("cls");
drawPlaying();
game.Playing();
}
else goto as;
}
/*================== the main function ==================*/
int main()
{
//游戏准备
srand((int)time(0)); //随机种子
HideCursor(); //隐藏光标
Game game;
int a = drawMenu();
if(a == 2)
game.rank = 20;
system("cls");
drawPlaying();
game.Playing();
}
//from 昊天无极
彩票游戏
#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
#include <string>
#include <stack>
#include <ctime>
using namespace std;
int money=100;
int t=0;
int tt=-1;
int v;
int d;
void borrow()
{
if(tt!=-1)
{
cout<<"你现在不能借钱"<<endl;
return;
}
cout<<"你要借多少钱?"<<endl;
cin>>v;
if (d==1||d==2)
{
if (v>1000) cout<<"你不能借这么多钱!"<<endl,cout<<"你要借多少钱?"<<endl,cin>>v;
if (v<=1000) money+=v,cout<<"10天以后你要还"<<2*v<<"美元"<<endl;
return;
}
else
{
if (v>100) cout<<"你不能借这么多钱!"<<endl,cout<<"你要借多少钱?"<<endl,cin>>v;
if (v<=100) money+=v,cout<<"10天以后你要还"<<2*v<<"美元"<<endl;
return;
}
}
bool pay()
{
cout<<"你现在要还 "<<2*v<<endl;
money-=2*v;
tt=-1;
v=0;
if(money<=0) return false;
else return true;
}
int main(int argc, char *argv[])
{
cout<<"欢迎来到彩票游戏!"<<endl<<endl<<"温馨提示:在游戏内请勿使用小数、负数、英文字母、特殊字符(游戏中另说明除外),"<<endl<<"否则后果自负。"<<endl<<endl;
cout<<"请选择难度(输入序号即可):"<<endl<<"1:入门"<<" "<<"2:简单"<<" "<<"3:普通"<<" "<<"4:中等"<<" "<<"5:困难"<<endl;
cin>>d;
if (d==1) cout<<"已选择难度:入门"<<endl<<endl;
if (d==2) cout<<"已选择难度:简单"<<endl<<endl;
if (d==3) cout<<"已选择难度:普通"<<endl<<endl;
if (d==4) cout<<"已选择难度:中等"<<endl<<endl;
if (d==5) cout<<"已选择难度:困难"<<endl<<endl;
while(1)
{
if (d<1) cout<<"没有这个选项"<<endl<<"请选择难度(输入序号即可):"<<endl<<"1:入门"<<" "<<"2:简单"<<" "<<"3:普通"<<" "<<"4:中等"<<" "<<"5:困难"<<endl,cin>>d;
if (d>5) cout<<"没有这个选项"<<endl<<"请选择难度(输入序号即可):"<<endl<<"1:入门"<<" "<<"2:简单"<<" "<<"3:普通"<<" "<<"4:中等"<<" "<<"5:困难"<<endl,cin>>d;
if (d==1 || d==2 || d==3 || d==4 || d==5) break;
}
bool flag=true;
srand(time(0));
int a[10000];
int n,p;
char c[4];
if (d==1)
{
cout<<"每张彩票 购入价为1美元,出售价为5美元,有五个奖项"<<endl<<"小奖:10美元"<<endl<<"中奖:20美元"<<endl<<"大奖:100美元"<<endl<<"巨额奖金:1,000美元"<<endl<<"杰克壶:10,000美元"<<endl;
cout<<"你有100美元,当你赚了超过10,000美元,你就发财了。"<<endl<<"但如果你失去了所有的钱,你破产了。"<<endl;
cout<<"你可能想借钱(最多1,000美元),如果你想,按0。"<<endl<<"请记住,当你借了十天以后。你必须偿还两倍,"<<endl;
cout<<"你想买彩票还是卖彩票?(buy/sell)"<<endl;
}
if (d==2)
{
cout<<"每张彩票 购入价为2美元,出售价为3美元,有五个奖项"<<endl<<"小奖:5美元"<<endl<<"中奖:20美元"<<endl<<"大奖:100美元"<<endl<<"巨额奖金:1,000美元"<<endl<<"杰克壶:10,000美元"<<endl;
cout<<"你有100美元,当你赚了超过10,000美元,你就发财了。"<<endl<<"但如果你失去了所有的钱,你破产了。"<<endl;
cout<<"你可能想借钱(最多1,000美元),如果你想,请按0。"<<endl<<"请记住,当你借了十天以后。你必须偿还两倍,"<<endl;
cout<<"你想买彩票还是卖彩票?(buy/sell)"<<endl;
}
if (d==3)
{
cout<<"每张彩票 售价2美元,有五个奖项"<<endl<<"小奖:5美元"<<endl<<"中奖:20美元"<<endl<<"大奖:100美元"<<endl<<"巨额奖金:1,000美元"<<endl<<"杰克壶:10,000美元"<<endl;
cout<<"你有100美元,当你赚了超过10,000美元,你就发财了。"<<endl<<"但如果你失去了所有的钱,你破产了。"<<endl;
cout<<"你可能想借钱(最多100美元),如果你想,请按0。"<<endl<<"请记住,当你借了十天以后。你必须偿还两倍,"<<endl;
cout<<"你想买彩票还是卖彩票?(buy/sell)"<<endl;
}
if (d==4)
{
cout<<"每张彩票 售价3美元,有五个奖项"<<endl<<"小奖:5美元"<<endl<<"中奖:20美元"<<endl<<"大奖:100美元"<<endl<<"巨额奖金:1,000美元"<<endl<<"杰克壶:10,000美元"<<endl;
cout<<"你有100美元,当你赚了超过100,000美元,你就发财了。"<<endl<<"但如果你失去了所有的钱,你破产了。"<<endl;
cout<<"你可能想借钱(最多100美元),如果你想,请按0。"<<endl<<"请记住,当你借了七天以后。你必须偿还两倍,"<<endl;
cout<<"你想买彩票还是卖彩票?(buy/sell)"<<endl;
}
if (d==5)
{
cout<<"每张彩票 购入价为5美元,出售价为2美元,有五个奖项"<<endl<<"小奖:5美元"<<endl<<"中奖:20美元"<<endl<<"大奖:100美元"<<endl<<"巨额奖金:1,000美元"<<endl<<"杰克壶:5,000美元"<<endl;
cout<<"你有100美元,当你赚了超过100,000美元,你就发财了。"<<endl<<"但如果你失去了所有的钱,你就破产了。"<<endl;
cout<<"你可能想借钱(最多100美元),如果你想,请按0。"<<endl<<"请记住,当你借了五天以后。你必须偿还两倍,"<<endl;
cout<<"你想买彩票还是卖彩票?(buy/sell)"<<endl;
}
cin>>c;
if(c[0]=='b'||c[0]=='B')
{
while(money>0)
{
if (d==1||d==2||d==3) if(money>=10000)
{
cout<<"你发财了! "<<endl;
cout<<"你花了 "<<t<<"天"<<endl;
system("pause");
return 0;
}
if (d==4||d==5) if(money>=100000)
{
cout<<"你发财了! "<<endl;
cout<<"你花了 "<<t<<"天"<<endl;
system("pause");
return 0;
}
t++;
cout<<"你要买几张票?"<<" "<<"你有$"<<money<<endl;
cin>>n;
if(n==0)
{
if (d=4)
{
borrow();
tt=t+7;
}
if (d=5)
{
borrow();
tt=t+5;
}
else
{
borrow();
tt=t+10;
}
}
if(t==tt)
{
flag=pay();
}
if(t==tt-1) cout<<"[警告]你必须在明天还钱!"<<endl;
if(flag==false)
{
cout<<"你不能偿还你借的钱!"<<endl;
cout<<"你破产了。"<<endl;
cout<<"你生存了"<<t<<"天。"<<endl;
system("pause");
return 0;
}
flag=true;
if(n<0)
{
cout<<"因为你违反规则,所以系统强制停止了你的游戏"<<endl;
system("pause");
}
if (d==1) money=money-n;
if (d==2||d==3) money=money-n*2;
if (d==4) money=money-n*3;
if (d==5) money=money-n*5;
if(money<0)
{
cout<<"你破产了。"<<endl;
cout<<"你生存了"<<t<<"天"<<endl;
system("pause");
return 0;
}
for(int i=0; i<n; i++)
{
p=rand()%12000;
if(p==0)
{
int q=0;
q=rand()%4;
if(q==1)
{
cout<<"你获得了杰克壶!"<<endl;
if (d==5) money+=5000;
else money+=10000;
}
}
else if(p>=1&&p<=8)
{
int g=0;
g=rand()%2;
if(g==0)
{
cout<<"你获得了巨额奖金! "<<endl;
money+=1000;
}
}
else if(p>=9&&p<=99)
{
cout<<"你获得了大奖!"<<endl;
money+=100;
}
else if((p>=100&&p<=399)||(p>1500&&p<=1600))
{
cout<<"你获得了中间奖!"<<endl;
money+=20;
}
else if(p>=400&&p<=1500)
{
cout<<"你获得了小奖!"<<endl;
if (d==1) money+=10;
else money+=5;
}
}
if(money<=0)
{
cout<<"你破产了。"<<endl;
cout<<"你生存了"<<t<<"天。"<<endl;
system("pause");
return 0;
}
}
}
if(c[0]=='s'||c[0]=='S')
{
if (d==1)
{
cout<<"你有$"<<money<<endl;
cout<<"你必须付25美元买一个商店。 "<<endl;
cout<<"你只能卖同样数量的票作为你的钱。 "<<endl;
money-=25;
}
if (d==2)
{
cout<<"你有$"<<money<<endl;
cout<<"你必须付35美元买一个商店。 "<<endl;
cout<<"你只能卖同样数量的票作为你的钱。 "<<endl;
money-=35;
}
if (d==3||d==4)
{
cout<<"你有$"<<money<<endl;
cout<<"你必须付50美元买一个商店。 "<<endl;
cout<<"你只能卖同样数量的票作为你的钱。 "<<endl;
money-=50;
}
if (d==5)
{
cout<<"你有$"<<money<<endl;
cout<<"你必须付60美元买一个商店。 "<<endl;
cout<<"你只能卖同样数量的票作为你的钱。 "<<endl;
money-=60;
}
cout<<"你有$"<<money<<endl;
while(money>0)
{
if (d==1||d==2||d==3) if(money>=10000)
if (d==4||d==5) if(money>=100000)
{
cout<<"你发财了!"<<endl;
cout<<"你花了"<<t<<"天。"<<endl;
system("pause");
return 0;
}
t++;
cout<<"你要卖几张票? "<<" "<<"你有$"<<money<<endl;
cin>>n;
if(n==0)
{
borrow();
tt=t+10;
}
if(t==tt) bool flag=pay();
if(flag==false)
{
cout<<"你不能偿还你借的钱。"<<endl;
cout<<"你破产了!"<<endl;
cout<<"你生存了"<<t<<" 天。"<<endl;
system("pause");
return 0;
}
if(n<0 || n>money)
{
cout<<"注意"<<endl;
cout<<"你破产了!"<<endl;
cout<<"你生存了"<<t<<" 天"<<endl;
system("pause");
return 0;
}
if (d=1) money=money+n*5;
if (d=2) money=money+n*3;
else money=money+n*2;
if(money<0)
{
cout<<"你破产了!"<<endl;
cout<<"你生存了"<<t<<" 天"<<endl;
system("pause");
return 0;
}
for(int i=0; i<n; i++)
{
p=rand()%12000;
if(p==0)
{
int y;
y=rand()%4;
if(y==1)
{
cout<<"你失去了杰克壶!"<<endl;
if (d==5) money-=5000;
else money-=10000;
}
}
else if(p>=1&&p<=8)
{
cout<<"你失去了巨额奖金!"<<endl;
money-=1000;
}
else if(p>=15&&p<=50)
{
cout<<"你失去了大奖!"<<endl;
money-=100;
}
else if(p>=61&&p<=360)
{
cout<<"你失去了中奖!"<<endl;
money-=20;
}
else if(p>=401&&p<=1500)
{
cout<<"你失去了小奖!"<<endl;
money-=5;
}
}
}
}
if(money<=0)
{
cout<<"你破产了。"<<endl;
cout<<"你生存了"<<t<<"天。"<<endl;
system("pause");
return 0;
}
}
俄罗斯方块
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <windows.h>
#ifdef _MSC_VER
#if _MSC_VER <= 1200
#error 你是不是还在用VC6?!
#else
#if _MSC_VER >= 1600
#include <stdint.h>
#else
typedef signed char int8_t;
typedef unsigned short uint16_t;
#endif
#ifndef __cplusplus
typedef int bool;
#define true 1
#define false 0
#endif
#endif
#else
#include <stdint.h>
#ifndef __cplusplus
#include <stdbool.h>
#endif
#endif
static const uint16_t gs_uTetrisTable[7][4] = {
{ 0x00F0U, 0x2222U, 0x00F0U, 0x2222U },
{ 0x0072U, 0x0262U, 0x0270U, 0x0232U },
{ 0x0223U, 0x0074U, 0x0622U, 0x0170U },
{ 0x0226U, 0x0470U, 0x0322U, 0x0071U },
{ 0x0063U, 0x0264U, 0x0063U, 0x0264U },
{ 0x006CU, 0x0462U, 0x006CU, 0x0462U },
{ 0x0660U, 0x0660U, 0x0660U, 0x0660U }
};
static const uint16_t gs_uInitialTetrisPool[28] = {
0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U,
0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U,
0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U,
0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xFFFFU, 0xFFFFU
};
#define COL_BEGIN 2
#define COL_END 14
#define ROW_BEGIN 4
#define ROW_END 26
typedef struct TetrisManager {
uint16_t pool[28];
int8_t x;
int8_t y;
int8_t type[3];
int8_t orientation[3];
unsigned score;
unsigned erasedCount[4];
unsigned erasedTotal;
unsigned tetrisCount[7];
unsigned tetrisTotal;
bool dead;
}
TetrisManager;
typedef struct TetrisControl {
bool pause;
bool clockwise;
int8_t direction;
int8_t color[28][16];
}
TetrisControl;
HANDLE g_hConsoleOutput;
void initGame(TetrisManager *manager, TetrisControl *control);
void restartGame(TetrisManager *manager, TetrisControl *control);
void giveTetris(TetrisManager *manager);
bool checkCollision(const TetrisManager *manager);
void insertTetris(TetrisManager *manager);
void removeTetris(TetrisManager *manager);
void horzMoveTetris(TetrisManager *manager, TetrisControl *control);
void moveDownTetris(TetrisManager *manager, TetrisControl *control);
void rotateTetris(TetrisManager *manager, TetrisControl *control);
void dropDownTetris(TetrisManager *manager, TetrisControl *control);
bool checkErasing(TetrisManager *manager, TetrisControl *control);
void keydownControl(TetrisManager *manager, TetrisControl *control, int key);
void setPoolColor(const TetrisManager *manager, TetrisControl *control);
void gotoxyWithFullwidth(short x, short y);
void printPoolBorder();
void printTetrisPool(const TetrisManager *manager, const TetrisControl *control);
void printCurrentTetris(const TetrisManager *manager, const TetrisControl *control);
void printNextTetris(const TetrisManager *manager);
void printScore(const TetrisManager *manager);
void runGame(TetrisManager *manager, TetrisControl *control);
void printPrompting();
bool ifPlayAgain();
int main() {
TetrisManager tetrisManager;
TetrisControl tetrisControl;
initGame(&tetrisManager, &tetrisControl);
do {
printPrompting();
printPoolBorder();
runGame(&tetrisManager, &tetrisControl);
if (ifPlayAgain()) {
SetConsoleTextAttribute(g_hConsoleOutput, 0x7);
system("cls");
restartGame(&tetrisManager, &tetrisControl);
} else {
break;
}
} while (1);
gotoxyWithFullwidth(0, 0);
CloseHandle(g_hConsoleOutput);
return 0;
}
void initGame(TetrisManager *manager, TetrisControl *control) {
CONSOLE_CURSOR_INFO cursorInfo = { 1, FALSE };
g_hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorInfo(g_hConsoleOutput, &cursorInfo);
SetConsoleTitleA("俄罗斯方块控制台版——By: NEWPLAN");
restartGame(manager, control);
}
void restartGame(TetrisManager *manager, TetrisControl *control) {
memset(manager, 0, sizeof(TetrisManager));
memcpy(manager->pool, gs_uInitialTetrisPool, sizeof(uint16_t [28]));
srand((unsigned)time(NULL));
manager->type[1] = rand() % 7;
manager->orientation[1] = rand() & 3;
manager->type[2] = rand() % 7;
manager->orientation[2] = rand() & 3;
memset(control, 0, sizeof(TetrisControl));
giveTetris(manager);
setPoolColor(manager, control);
}
void giveTetris(TetrisManager *manager) {
uint16_t tetris;
manager->type[0] = manager->type[1];
manager->orientation[0] = manager->orientation[1];
manager->type[1] = manager->type[2];
manager->orientation[1] = manager->orientation[2];
manager->type[2] = rand() % 7;
manager->orientation[2] = rand() & 3;
tetris = gs_uTetrisTable[manager->type[0]][manager->orientation[0]];
if (tetris & 0xF000) {
manager->y = 0;
} else {
manager->y = (tetris & 0xFF00) ? 1 : 2;
}
manager->x = 6;
if (checkCollision(manager)) {
manager->dead = true;
} else {
insertTetris(manager);
}
++manager->tetrisTotal;
++manager->tetrisCount[manager->type[0]];
printNextTetris(manager);
printScore(manager);
}
bool checkCollision(const TetrisManager *manager) {
uint16_t tetris = gs_uTetrisTable[manager->type[0]][manager->orientation[0]];
uint16_t dest = 0;
dest |= (((manager->pool[manager->y + 0] >> manager->x) << 0x0) & 0x000F);
dest |= (((manager->pool[manager->y + 1] >> manager->x) << 0x4) & 0x00F0);
dest |= (((manager->pool[manager->y + 2] >> manager->x) << 0x8) & 0x0F00);
dest |= (((manager->pool[manager->y + 3] >> manager->x) << 0xC) & 0xF000);
return ((dest & tetris) != 0);
}
void insertTetris(TetrisManager *manager) {
uint16_t tetris = gs_uTetrisTable[manager->type[0]][manager->orientation[0]];
manager->pool[manager->y + 0] |= (((tetris >> 0x0) & 0x000F) << manager->x);
manager->pool[manager->y + 1] |= (((tetris >> 0x4) & 0x000F) << manager->x);
manager->pool[manager->y + 2] |= (((tetris >> 0x8) & 0x000F) << manager->x);
manager->pool[manager->y + 3] |= (((tetris >> 0xC) & 0x000F) << manager->x);
}
void removeTetris(TetrisManager *manager) {
uint16_t tetris = gs_uTetrisTable[manager->type[0]][manager->orientation[0]];
manager->pool[manager->y + 0] &= ~(((tetris >> 0x0) & 0x000F) << manager->x);
manager->pool[manager->y + 1] &= ~(((tetris >> 0x4) & 0x000F) << manager->x);
manager->pool[manager->y + 2] &= ~(((tetris >> 0x8) & 0x000F) << manager->x);
manager->pool[manager->y + 3] &= ~(((tetris >> 0xC) & 0x000F) << manager->x);
}
void setPoolColor(const TetrisManager *manager, TetrisControl *control) {
int8_t i, x, y;
uint16_t tetris = gs_uTetrisTable[manager->type[0]][manager->orientation[0]];
for (i = 0; i < 16; ++i) {
y = (i >> 2) + manager->y;
if (y > ROW_END) {
break;
}
x = (i & 3) + manager->x;
if ((tetris >> i) & 1) {
control->color[y][x] = (manager->type[0] | 8);
}
}
}
void rotateTetris(TetrisManager *manager, TetrisControl *control) {
int8_t ori = manager->orientation[0];
removeTetris(manager);
manager->orientation[0] = (control->clockwise) ? ((ori + 1) & 3) : ((ori + 3) & 3);
if (checkCollision(manager)) {
manager->orientation[0] = ori;
insertTetris(manager);
} else {
insertTetris(manager);
setPoolColor(manager, control);
printCurrentTetris(manager, control);
}
}
void horzMoveTetris(TetrisManager *manager, TetrisControl *control) {
int x = manager->x;
removeTetris(manager);
control->direction == 0 ? (--manager->x) : (++manager->x);
if (checkCollision(manager)) {
manager->x = x;
insertTetris(manager);
} else {
insertTetris(manager);
setPoolColor(manager, control);
printCurrentTetris(manager, control);
}
}
void moveDownTetris(TetrisManager *manager, TetrisControl *control) {
int8_t y = manager->y;
removeTetris(manager);
++manager->y;
if (checkCollision(manager)) {
manager->y = y;
insertTetris(manager);
if (checkErasing(manager, control)) {
printTetrisPool(manager, control);
}
} else {
insertTetris(manager);
setPoolColor(manager, control);
printCurrentTetris(manager, control);
}
}
void dropDownTetris(TetrisManager *manager, TetrisControl *control) {
removeTetris(manager);
for (; manager->y < ROW_END; ++manager->y) {
if (checkCollision(manager)) {
break;
}
}
--manager->y;
insertTetris(manager);
setPoolColor(manager, control);
checkErasing(manager, control);
printTetrisPool(manager, control);
}
bool checkErasing(TetrisManager *manager, TetrisControl *control) {
static const unsigned scores[5] = { 0, 10, 30, 90, 150 };
int8_t count = 0;
int8_t k = 0, y = manager->y + 3;
do {
if (y < ROW_END && manager->pool[y] == 0xFFFFU) {
++count;
memmove(manager->pool + 1, manager->pool, sizeof(uint16_t) * y);
memmove(control->color[1], control->color[0], sizeof(int8_t [16]) * y);
} else {
--y;
++k;
}
} while (y >= manager->y && k < 4);
manager->erasedTotal += count;
manager->score += scores[count];
if (count > 0) {
++manager->erasedCount[count - 1];
}
giveTetris(manager);
setPoolColor(manager, control);
return (count > 0);
}
void keydownControl(TetrisManager *manager, TetrisControl *control, int key) {
if (key == 13) {
control->pause = !control->pause;
}
if (control->pause) {
return;
}
switch (key) {
case 'w':
case 'W':
case '8':
case 72:
control->clockwise = true;
rotateTetris(manager, control);
break;
case 'a':
case 'A':
case '4':
case 75:
control->direction = 0;
horzMoveTetris(manager, control);
break;
case 'd':
case 'D':
case '6':
case 77:
control->direction = 1;
horzMoveTetris(manager, control);
break;
case 's':
case 'S':
case '2':
case 80:
moveDownTetris(manager, control);
break;
case ' ':
dropDownTetris(manager, control);
break;
case '0':
control->clockwise = false;
rotateTetris(manager, control);
break;
default:
break;
}
}
void gotoxyWithFullwidth(short x, short y) {
static COORD cd;
cd.X = (short)(x << 1);
cd.Y = y;
SetConsoleCursorPosition(g_hConsoleOutput, cd);
}
void printPoolBorder() {
int8_t y;
SetConsoleTextAttribute(g_hConsoleOutput, 0xF0);
for (y = ROW_BEGIN; y < ROW_END; ++y) {
gotoxyWithFullwidth(10, y - 3);
printf("%2s", "");
gotoxyWithFullwidth(23, y - 3);
printf("%2s", "");
}
gotoxyWithFullwidth(10, y - 3);
printf("%28s", "");
}
#define gotoxyInPool(x, y) gotoxyWithFullwidth(x + 9, y - 3)
void printTetrisPool(const TetrisManager *manager, const TetrisControl *control) {
int8_t x, y;
for (y = ROW_BEGIN; y < ROW_END; ++y) {
gotoxyInPool(2, y);
for (x = COL_BEGIN; x < COL_END; ++x) {
if ((manager->pool[y] >> x) & 1) {
SetConsoleTextAttribute(g_hConsoleOutput, control->color[y][x]);
printf("■");
} else {
SetConsoleTextAttribute(g_hConsoleOutput, 0);
printf("%2s", "");
}
}
}
}
void printCurrentTetris(const TetrisManager *manager, const TetrisControl *control) {
int8_t x, y;
y = (manager->y > ROW_BEGIN) ? (manager->y - 1) : ROW_BEGIN;
for (; y < ROW_END && y < manager->y + 4; ++y) {
x = (manager->x > COL_BEGIN) ? (manager->x - 1) : COL_BEGIN;
for (; x < COL_END && x < manager->x + 5; ++x) {
gotoxyInPool(x, y);
if ((manager->pool[y] >> x) & 1) {
SetConsoleTextAttribute(g_hConsoleOutput, control->color[y][x]);
printf("■");
} else {
SetConsoleTextAttribute(g_hConsoleOutput, 0);
printf("%2s", "");
}
}
}
}
void printNextTetris(const TetrisManager *manager) {
int8_t i;
uint16_t tetris;
SetConsoleTextAttribute(g_hConsoleOutput, 0xF);
gotoxyWithFullwidth(26, 1);
printf("┏━━━━┳━━━━┓");
gotoxyWithFullwidth(26, 2);
printf("┃%8s┃%8s┃", "", "");
gotoxyWithFullwidth(26, 3);
printf("┃%8s┃%8s┃", "", "");
gotoxyWithFullwidth(26, 4);
printf("┃%8s┃%8s┃", "", "");
gotoxyWithFullwidth(26, 5);
printf("┃%8s┃%8s┃", "", "");
gotoxyWithFullwidth(26, 6);
printf("┗━━━━┻━━━━┛");
tetris = gs_uTetrisTable[manager->type[1]][manager->orientation[1]];
SetConsoleTextAttribute(g_hConsoleOutput, manager->type[1] | 8);
for (i = 0; i < 16; ++i) {
gotoxyWithFullwidth((i & 3) + 27, (i >> 2) + 2);
((tetris >> i) & 1) ? printf("■") : printf("%2s", "");
}
tetris = gs_uTetrisTable[manager->type[2]][manager->orientation[2]];
SetConsoleTextAttribute(g_hConsoleOutput, 8);
for (i = 0; i < 16; ++i) {
gotoxyWithFullwidth((i & 3) + 32, (i >> 2) + 2);
((tetris >> i) & 1) ? printf("■") : printf("%2s", "");
}
}
void printScore(const TetrisManager *manager) {
static const char *tetrisName = "ITLJZSO";
int8_t i;
SetConsoleTextAttribute(g_hConsoleOutput, 0xE);
gotoxyWithFullwidth(2, 2);
printf("■得分:%u", manager->score);
gotoxyWithFullwidth(1, 6);
printf("■消行总数:%u", manager->erasedTotal);
for (i = 0; i < 4; ++i) {
gotoxyWithFullwidth(2, 8 + i);
printf("□消%d:%u", i + 1, manager->erasedCount[i]);
}
gotoxyWithFullwidth(1, 15);
printf("■方块总数:%u", manager->tetrisTotal);
for (i = 0; i < 7; ++i) {
gotoxyWithFullwidth(2, 17 + i);
printf("□%c形:%u", tetrisName[i], manager->tetrisCount[i]);
}
}
void printPrompting() {
SetConsoleTextAttribute(g_hConsoleOutput, 0xB);
gotoxyWithFullwidth(26, 10);
printf("■控制:");
gotoxyWithFullwidth(27, 12);
printf("□向左移动:← A 4");
gotoxyWithFullwidth(27, 13);
printf("□向右移动:→ D 6");
gotoxyWithFullwidth(27, 14);
printf("□向下移动:↓ S 2");
gotoxyWithFullwidth(27, 15);
printf("□顺时针转:↑ W 8");
gotoxyWithFullwidth(27, 16);
printf("□逆时针转:0");
gotoxyWithFullwidth(27, 17);
printf("□直接落地:空格");
gotoxyWithFullwidth(27, 18);
printf("□暂停游戏:回车");
gotoxyWithFullwidth(25, 23);
printf("■By: NEWPLAN @ UESTC");
}
void runGame(TetrisManager *manager, TetrisControl *control) {
clock_t clockLast, clockNow;
clockLast = clock();
printTetrisPool(manager, control);
while (!manager->dead) {
while (_kbhit()) {
keydownControl(manager, control, _getch());
}
if (!control->pause) {
clockNow = clock();
if (clockNow - clockLast > 0.45F * CLOCKS_PER_SEC) {
clockLast = clockNow;
keydownControl(manager, control, 80);
}
}
}
}
bool ifPlayAgain() {
int ch;
SetConsoleTextAttribute(g_hConsoleOutput, 0xF0);
gotoxyWithFullwidth(15, 10);
printf("游戏结束");
gotoxyWithFullwidth(13, 11);
printf("按Y重玩,按N退出");
do {
ch = _getch();
if (ch == 'Y' || ch == 'y') {
return true;
} else if (ch == 'N' || ch == 'n') {
return false;
}
} while (1);
}
扫雷游戏
#include <iostream>
#include <cstdlib>
#include <stdio.h>
#include <ctime>
#include <windows.h>
#include <conio.h>
#define KEY_UP 0xE048
#define KEY_DOWN 0xE050
#define KEY_LEFT 0xE04B
#define KEY_RIGHT 0xE04D
#define KEY_ESC 0x001B
#define KEY_1 '1'
#define KEY_2 '2'
#define KEY_3 '3'
#define GAME_MAX_WIDTH 100
#define GAME_MAX_HEIGHT 100
#define STR_GAMETITLE "按方向键移动光标 1:翻开 2:插旗 3:翻开周围"
#define STR_GAMEWIN "祝贺!!! 你赢了!!!!\n谢谢光临!\n\n"
#define STR_GAMEOVER "你输了!!!\n"
#define STR_GAMEEND "按“ESC”继续\n"
class CConsoleWnd {
public:
static int TextOut(const char *);
static int GotoXY(int, int);
static int CharOut(int, int, const int);
static int TextOut(int, int, const char *);
static int GetKey();
public:
};
int CConsoleWnd::GetKey() {
int nkey = getch(), nk = 0;
if (nkey >= 128 || nkey == 0)
nk = getch();
return nk > 0 ? nkey * 256 + nk : nkey;
}
int CConsoleWnd::GotoXY(int x, int y) {
COORD cd;
cd.X = x;
cd.Y = y;
return SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cd);
}
int CConsoleWnd::TextOut(const char *pstr) {
for (; *pstr; ++pstr)
putchar(*pstr);
return 0;
}
int CConsoleWnd::CharOut(int x, int y, const int pstr) {
GotoXY(x, y);
return putchar(pstr);
}
int CConsoleWnd::TextOut(int x, int y, const char *pstr) {
GotoXY(x, y);
return TextOut(pstr);
}
class CSLGame: public CConsoleWnd {
private:
private:
int curX, curY;
int poolWidth, poolHeight;
int bm_gamepool[GAME_MAX_HEIGHT + 2][GAME_MAX_WIDTH + 2];
public:
CSLGame(): curX(0), curY(0) {
poolWidth = poolHeight = 0;
}
int InitPool(int, int, int);
int MoveCursor() {
return CConsoleWnd::GotoXY(curX, curY);
}
int DrawPool(int);
int WaitMessage();
int GetShowNum(int, int);
int TryOpen(int, int);
private:
int DFSShowNum(int, int);
private:
const static int GMARK_BOOM;
const static int GMARK_EMPTY;
const static int GMARK_MARK;
};
const int CSLGame::GMARK_BOOM = 0x10;
const int CSLGame::GMARK_EMPTY = 0x100;
const int CSLGame::GMARK_MARK = 0x200;
int CSLGame::InitPool(int Width, int Height, int nBoom) {
poolWidth = Width;
poolHeight = Height;
if (nBoom <= 0 || nBoom >= Width * Height
|| Width <= 0 || Width > GAME_MAX_WIDTH
|| Height <= 0 || Height > GAME_MAX_HEIGHT
) {
return 1;
}
for (int y = 0; y <= Height + 1; ++y) {
for (int x = 0; x <= Width + 1; ++x) {
bm_gamepool[y][x] = 0;
}
}
srand(time(NULL));
while (nBoom) {
int x = rand() % Width + 1, y = rand() % Height + 1;
if (bm_gamepool[y][x] == 0) {
bm_gamepool[y][x] = GMARK_BOOM;
--nBoom;
}
}
curX = curY = 1;
MoveCursor();
return 0;
}
int CSLGame::DrawPool(int bDrawBoom = 0) {
for (int y = 1; y <= poolHeight; ++y) {
CConsoleWnd::GotoXY(1, y);
for (int x = 1; x <= poolWidth; ++x) {
if (bm_gamepool[y][x] == 0) {
putchar('.');
} else if (bm_gamepool[y][x] == GMARK_EMPTY) {
putchar(' ');
} else if (bm_gamepool[y][x] > 0 && bm_gamepool[y][x] <= 8) {
putchar('0' + bm_gamepool[y][x]);
} else if (bDrawBoom == 0 && (bm_gamepool[y][x] & GMARK_MARK)) {
putchar('@');
} else if (bm_gamepool[y][x] & GMARK_BOOM) {
if (bDrawBoom)
putchar('*');
else
putchar('.');
}
}
}
return 0;
}
int CSLGame::GetShowNum(int x, int y) {
int nCount = 0;
for (int Y = -1; Y <= 1; ++Y)
for (int X = -1; X <= 1; ++X) {
if (bm_gamepool[y + Y][x + X] & GMARK_BOOM)
++nCount;
}
return nCount;
}
int CSLGame::TryOpen(int x, int y) {
int nRT = 0;
if (bm_gamepool[y][x] & GMARK_BOOM) {
nRT = EOF;
} else {
int nCount = GetShowNum(x, y);
if (nCount == 0) {
DFSShowNum(x, y);
} else
bm_gamepool[y][x] = nCount;
}
return nRT;
}
int CSLGame::DFSShowNum(int x, int y) {
if ((0 < x && x <= poolWidth) &&
(0 < y && y <= poolHeight) &&
(bm_gamepool[y][x] == 0)) {
int nCount = GetShowNum(x, y);
if (nCount == 0) {
bm_gamepool[y][x] = GMARK_EMPTY;
for (int Y = -1; Y <= 1; ++Y)
for (int X = -1; X <= 1; ++X) {
DFSShowNum(x + X, y + Y);
}
} else
bm_gamepool[y][x] = nCount;
}
return 0;
}
int CSLGame::WaitMessage() {
int nKey = CConsoleWnd::GetKey();
int nRT = 0, nArrow = 0;
switch (nKey) {
case KEY_UP: {
if (curY > 1)
--curY;
nArrow = 1;
}
break;
case KEY_DOWN: {
if (curY < poolHeight)
++curY;
nArrow = 1;
}
break;
case KEY_LEFT: {
if (curX > 1)
--curX;
nArrow = 1;
}
break;
case KEY_RIGHT: {
if (curX < poolWidth)
++curX;
nArrow = 1;
}
break;
case KEY_1: {
nRT = TryOpen(curX, curY);
}
break;
case KEY_2: {
if ((bm_gamepool[curY][curX]
& ~(GMARK_MARK | GMARK_BOOM)) == 0) {
bm_gamepool[curY][curX] ^= GMARK_MARK;
}
}
break;
case KEY_3: {
if (bm_gamepool[curY][curX] & 0xF) {
int nb = bm_gamepool[curY][curX] & 0xF;
for (int y = -1; y <= 1; ++y)
for (int x = -1; x <= 1; ++x) {
if (bm_gamepool[curY + y][curX + x] & GMARK_MARK)
--nb;
}
if (nb == 0) {
for (int y = -1; y <= 1; ++y)
for (int x = -1; x <= 1; ++x) {
if ((bm_gamepool[curY + y][curX + x]
& (0xF | GMARK_MARK)) == 0) {
nRT |= TryOpen(curX + x, curY + y);
}
}
}
}
}
break;
case KEY_ESC: {
nRT = EOF;
}
break;
}
if (nKey == KEY_1 || nKey == KEY_3) {
int y = 1;
for (; y <= poolHeight; ++y) {
int x = 1;
for (; x <= poolWidth; ++x) {
if (bm_gamepool[y][x] == 0)
break;
}
if (x <= poolWidth)
break;
}
if (! (y <= poolHeight)) {
nRT = 1;
}
}
if (nArrow == 0) {
DrawPool();
}
MoveCursor();
return nRT;
}
int main(void) {
int x = 50, y = 20, b = 100, n;
CSLGame slGame;
{
CConsoleWnd::GotoXY(0, 0);
CConsoleWnd::TextOut(STR_GAMETITLE);
slGame.InitPool(x, y, b);
slGame.DrawPool();
slGame.MoveCursor();
}
while ((n = slGame.WaitMessage()) == 0);
{
slGame.DrawPool(1);
CConsoleWnd::TextOut("\n");
if (n == 1) {
CConsoleWnd::TextOut(STR_GAMEWIN);
} else {
CConsoleWnd::TextOut(STR_GAMEOVER);
}
CConsoleWnd::TextOut(STR_GAMEEND);
}
while (CConsoleWnd::GetKey() != KEY_ESC);
return 0;
}
面向对象 %%%