我又制作小游戏了!!!
<<<<<<<<后面有彩蛋!!!>>>>>>>>
代码如下:
#include<bits/stdc++.h>
#include<conio.h>
#include<windows.h>
using namespace std;
char a[4][4],ch;
void start();
void game();
void go(int x,int y){
COORD pos;
pos.X=x-1;
pos.Y=y-1;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void Return(){
Sleep(2000);
cout<<"按任意键返回主界面";
getch();
system("cls");
cout<<"我将为您返回主界面…";
Sleep(2000);
start();
return;
}
void win(char x){
go(1,6);
cout<<x<<"方胜利!!!!!\n";
Return();
}
void iswin(char x){
int i,j,f;
for(i=1;i<4;i++)if(a[2][i]==a[1][i]&&a[2][i]==a[3][i]&&a[1][i]!=' ')win(x);
for(i=1;i<4;i++)if(a[i][2]==a[i][1]&&a[i][2]==a[i][3]&&a[i][1]!=' ')win(x);
if(a[1][1]==a[2][2]&&a[2][2]==a[3][3]&&a[1][1]!=' ')win(x);
if(a[1][3]==a[2][2]&&a[2][2]==a[3][1]&&a[1][3]!=' ')win(x);
f=1;
for(i=1;i<4;i++)for(j=1;j<4;j++)if(a[i][j]==' ')f=0;
if(f){
go(1,6);
cout<<"双方平局!!!!!\n";
Return();
}
return;
}
void stop(){
system("cls");
cout<<"是否退出?\n1:是\n2:否\n";
ch=getch();
if(ch=='1'){
system("cls");
cout<<"正在退出…";
Sleep(2000);
exit(0);
}
else if(ch=='2'){
system("cls");
cout<<"我将为您返回主界面…";
Sleep(2000);
start();
return;
}
else{
stop();
return;
}
}
void start(){
system("cls");
cout<<"井字棋游戏V1.0\n1:开始\n2:规则\n按其他退出\n";
go(1,5);
ch=getch();
if(ch=='1')game();
else if(ch=='2'){
system("cls");
cout<<"在游戏中,按w、a、s、d、控制上下左右需要落子的位置,按空格确认落子\n按任意键退出";
getch();
start();
}
else{
stop();
return;
}
}
void game(){
system("cls");
cout<<"开始!";
Sleep(1000);
system("cls");
int i=1,j=1,x=1,y=1,ff=0;
for(;i<4;i++)for(j=1;j<4;j++)a[i][j]=' ';
cout<<" | |\n-|-|-\n | |\n-|-|-\n | |\n该O落子";
char z='O';
go(1,1);
for(;;){
ch=getch();
if(ch=='a'&&y>1)y--;
else if(ch=='w'&&x>1)x--;
else if(ch=='d'&&y<3)y++;
else if(ch=='s'&&x<3)x++;
go(y*2-1,x*2-1);
if(ch==' '){
ff=0;
if(a[y][x]=='O'||a[y][x]=='X'){
go(1,7);
ff=1;
cout<<"非法落子";
Sleep(1000);
go(1,7);
cout<<" ";
go(y*2-1,x*2-1);
}
else{
a[y][x]=z;
cout<<z;
go(y*2-1,x*2-1);
}
if(ff==0){
iswin(z);
if(z=='O')z='X';
else if(z=='X')z='O';
go(1,6);
cout<<"该"<<z<<"落子";
go(1,1);
x=y=1;
}
}
}
}
int main(){
start();
return 0;
}
做了1个小时。
彩蛋如下:
|
|
|
|
V
#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
void color(int x){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),x);
}
void print(const char *x){
while(*x){
cout<<(*x++);
color(rand()%9+7);
}
}
int main(){
srand(time(0));
for(int i=1;i<=100;i++)print("制作不易,求关注!!!");
color(7);
return 0;
}