C++井字棋V1.0

我又制作小游戏了!!!

<<<<<<<<>>>>>>>>

代码如下:

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值