漢諾塔遊戲,可以玩

用了easyx寫了一個漢諾塔的實現過程,還挺好玩的

可是它,只能看不能自己玩!!!
有時間寫一些可以自己玩的。

在這裏插入圖片描述

#include<bits/stdc++.h>
#include<graphics.h>
#include<Windows.h>
#include<conio.h>

using namespace std;

vector<int>p[3];




void draw_rec() {
	for (int i = 0; i < p[0].size(); i++) {
		rectangle(40 + 15 * (8 - p[0][i]), 400 - 50 * i, 340 - 15 * (8 - p[0][i]), 450 - 50 * i);
	}
	for (int i = 0; i < p[1].size(); i++) {
		rectangle(380 + 15 * (8 - p[1][i]), 400 - 50 * i, 680 - 15 * (8 - p[1][i]), 450 - 50 * i);
	}
	for (int i = 0; i < p[2].size(); i++) {
		rectangle(720 + 15 * (8 - p[2][i]), 400 - 50 * i, 1020 - 15 * (8 - p[2][i]), 450 - 50 * i);
	}
}

void draw_line() {
	int dx1 = p[0].size() * 50;
	int dx2 = p[1].size() * 50;
	int dx3 = p[2].size() * 50;
	rectangle(190, 20, 190, 450 - dx1);
	rectangle(530, 20, 530, 450 - dx2);
	rectangle(870, 20, 870, 450 - dx3);
}

void move(int from, int to) {
	p[to - 1].push_back(*p[from - 1].rbegin());
	p[from - 1].erase(p[from-1].begin() + p[from-1].size() - 1);


	cleardevice();
	draw_rec();
	draw_line();

	_getch();
}

void hanoi(int n, int from, int tmp, int to) {
	if (n == 1) {
		move(from, to);
		return;
	}
	hanoi(n - 1, from, to, tmp);
	move(from, to);	
	hanoi(n - 1, tmp, from, to);

}

int main() {
	int n;
	cout << "輸入 n : " << endl;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		p[0].push_back(8-i+1);
	}

	initgraph(1060, 550);
	setlinestyle(PS_SOLID, 5);
	setlinecolor(WHITE);

	draw_rec();
	draw_line();

	_getch();
	hanoi(n, 1, 2, 3);

	_getch();
}

不務正業,不想刷題。

它可以自己玩了,就是有點醜。

#include<bits/stdc++.h>
#include<graphics.h>
#include<Windows.h>
#include<conio.h>

const int line1 = 190;
const int dx = 400;
const int h = 50;
const int dx_rec = 30;
const int init_rec = 180;
const int botton_size = 140;
const int down = 450;
using namespace std;

vector<int>p[3];

int cnt = 0;

void draw_button() {
	setfillcolor(BLUE);
	fillrectangle(line1 - botton_size, down + 30, line1 + botton_size, down + 30 + 100);
	fillrectangle(line1 + dx - botton_size, down + 30, line1 + dx + botton_size, down + 30 + 100);
	fillrectangle(line1 + dx * 2 - botton_size, down + 30, line1 + dx * 2 + botton_size, down + 30 + 100);
}

void draw_rec() {
	for (int i = 0; i < p[0].size(); i++) {
		rectangle(line1 - init_rec + dx_rec * (8 - p[0][i]), down - h - h * i, line1 + init_rec - dx_rec * (8 - p[0][i]), down - h * i);
	}
	for (int i = 0; i < p[1].size(); i++) {
		rectangle(line1 + dx - init_rec + dx_rec * (8 - p[1][i]), down - h - h * i, line1 + dx + init_rec - dx_rec * (8 - p[1][i]), down - h * i);
	}
	for (int i = 0; i < p[2].size(); i++) {
		rectangle(line1 + 2 * dx - init_rec + dx_rec * (8 - p[2][i]), down - h - h * i, line1 + 2 * dx + init_rec - dx_rec * (8 - p[2][i]), down - h * i);
	}
	
	draw_button();
}

void draw_line() {
	int dx1 = p[0].size() * h;
	int dx2 = p[1].size() * h;
	int dx3 = p[2].size() * h;
	rectangle(line1, 20, line1 , 450 - dx1);
	rectangle(line1 + dx, 20, line1 + dx, 450 - dx2);
	rectangle(line1 + 2*dx, 20, line1 + 2 * dx, 450 - dx3);
}

void move(int from, int to) {
	if (from == to) return;
	cnt++;
	if (p[from - 1].size() == 0) return;
	int a = p[from - 1][p[from - 1].size() - 1];
	int b = 100;
	if (p[to - 1].size() != 0) {
		b = p[to - 1][p[to - 1].size() - 1];
	}
	
	if (a > b) return;

	p[to - 1].push_back(*p[from - 1].rbegin());
	p[from - 1].erase(p[from-1].begin() + p[from-1].size() - 1);


	cleardevice();
	draw_rec();
	draw_line();

	//_getch();
}

void hanoi(int n, int from, int tmp, int to) {
	if (n == 1) {
		move(from, to);
		return;
	}
	hanoi(n - 1, from, to, tmp);
	move(from, to);	
	hanoi(n - 1, tmp, from, to);

}

bool check() {
	return(p[0].size() == 0 && p[1].size() == 0);
}
void play() {
	int A[2] = { 0 };
	int cnt = 0; 
	MOUSEMSG m;

	while (1) {
		if (check()) break;
		m = GetMouseMsg();

		
		if (m.y >= down + 30 && m.y <= down + 30 + 100 ) {
			if (m.x >= line1 - botton_size && m.x <= line1 + botton_size) {
				if(m.uMsg == WM_LBUTTONDOWN)
					A[cnt++] = 1;
			}
			if (m.x >= line1 + dx - botton_size && m.x <= line1 + dx + botton_size) {
				if (m.uMsg == WM_LBUTTONDOWN)
					A[cnt++] = 2;
			}
			if (m.x >= line1 + 2 * dx - botton_size && m.x <= line1 + 2 * dx + botton_size) {
				if (m.uMsg == WM_LBUTTONDOWN)
					A[cnt++] = 3;
			}
		}
	
		if (cnt == 2) {
			move(A[0], A[1]);
			cnt = 0;
			A[0] = A[1] = 0;
		}
	}

}

int main() {
	int n;
	cout << "輸入 n (最大爲6 ,畫不下了...) : " << endl;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		p[0].push_back(8-i+1);
	}
	int x;
	cout << "你想自己玩嗎(輸入1) ? 或者觀看程序運行(其他) " << endl;
	cin >> x;
	x = 1;

	initgraph(1200, 640);
	setlinestyle(PS_SOLID, 5);
	setlinecolor(WHITE);

	draw_rec();
	draw_line();

	//_getch();


	if (x == 1) {
		play();
	}
	else {
		hanoi(n, 1, 2, 3);
	}
	cout << "your steps : " << cnt << endl;
	_getch();
}

在這裏插入圖片描述

哦我今天一天啥都沒幹

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章