c++对对碰

#include<iostream>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include <windows.h>
using namespace std;
int main()
{//地图资源
	int GameMap[2][4][4] = {
		{ { 1, 2, 3, 4 },
		{ 4, 3, 2, 1 },
		{ 4, 3, 2, 1 },
		{ 1, 2, 3, 4 } },
		{{ 1, 2, 3, 4 },
		{ 4, 3, 2, 1 },
		{ 4, 3, 2, 1 },
		{ 1, 2, 3, 4 } }
};
	for (int i = 0; i < 2; i++)
	{
		while (true)
		{
			//刷新地图资源
			for (int x = 0; x < 4; x++)
			{
				for (int y = 0; y < 4; y++)
				{
					switch (GameMap[i][x][y])
					{
					case 0:
						cout << "  ";
						break;
					case 1:
					case 2:
					case 3:
					case 4:
						cout << "▄" ;
					}
				}
				cout << endl;
			}
			
			int PosX1, PosX2, PosY1, PosY2;//定义4个变量用来存储输入坐标
			//对坐标进行赋值
			cout << "请输入你想要验证的2个坐标,每一个坐标用空格隔开" << endl;
			cout << "第一个:";
			cin >> PosX1 >> PosY1;
			cout << endl;
			cout << "第二个:";
			cin >> PosX2 >> PosY2;
			cout << endl;
			for (int i = 0; i < 100; i++)
			{
				if ((PosX1 == PosX2&&PosY1 == PosY2) || PosX1 < 0 || PosX2 < 0 || PosY1 < 0 || PosY2 < 0 || PosX1 >= 4 || PosX2 >= 4 || PosY1 >= 4 || PosY2 >= 4)//判断输入是否正确
				{
					cout << "输入错误,请重新输入!" << endl;
					cout << "第一个:";
					cin >> PosX1 >> PosY1;
					cout << "第二个:";
					cin >> PosX2 >> PosY2;
				}
				else
					break;
			}
			system("CLS");//清屏
			//如果它们相等
			if (GameMap[i][PosX1][PosY1] == GameMap[i][PosX2][PosY2])
			{
				GameMap[i][PosX1][PosY1] = 0;
				GameMap[i][PosX2][PosY2] = 0;
			}
			//如果它们不相等,告诉玩家这两个是什么
			else
			{
				for (int x = 0; x < 4; x++)
				{
					for (int y = 0; y < 4; y++)
					{
						if ((PosX1 == x&&PosY1 == y) || (PosX2 == x&&PosY2 == y))
						{
							switch (GameMap[i][x][y])
							{
							case 0:
								cout << "  ";
								break;
							case 1:
								cout << "◢" ;
								break;
							case 2:
								cout << "◣" ;
								break;
							case 3:
								cout << "◥" ;
								break;
							case 4:
								cout << "◤" ;
								break;
							}
						}
						else
						{
							switch (GameMap[i][x][y])
							{
							case 0:
								cout << "  ";
								break;
							case 1:
							case 2:
							case 3:
							case 4:
								cout << "▄";
							}
						}
					}
					cout << endl;
				}
				Sleep(800);
				system("CLS");//清屏
			}
			int num = 0;
			//判断是否通过次关
			for (int x = 0; x < 4; x++)
			{
				for (int y = 0; y < 4; y++)
				{
					if (0 != GameMap[i][x][y])
					{
						num++;
					}
				}
			}
			if (0 == num)
			{
				system("CLS");//清屏
				cout << "恭喜闯过本关"<<endl;
				i++;
			}
			//判断是否已经完全通关
			if (2 == i)
			{
				goto lab1;
			}
			
		}
	}
lab1:system("CLS");//清屏
	cout << "闯关成功" << endl;
	return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对对碰游戏是一种简单的益智类游戏,可以使用 Java 编程语言实现。 以下是一个简单的对对碰游戏 Java 实现的示例代码: ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MatchGame extends JFrame implements ActionListener { private JButton[] buttons; private int[] values; private int firstButtonIndex, secondButtonIndex; private int count; private Timer timer; public MatchGame() { setTitle("Match Game"); setSize(400, 400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel mainPanel = new JPanel(); mainPanel.setLayout(new GridLayout(4, 4)); getContentPane().add(mainPanel); buttons = new JButton[16]; values = new int[16]; for (int i = 0; i < 8; i++) { int value = i + 1; for (int j = 0; j < 2; j++) { int k; do { k = (int) (Math.random() * 16); } while (values[k] != 0); values[k] = value; } } for (int i = 0; i < 16; i++) { buttons[i] = new JButton(); buttons[i].setBackground(Color.white); buttons[i].addActionListener(this); mainPanel.add(buttons[i]); } timer = new Timer(1000, this); } public void actionPerformed(ActionEvent e) { if (e.getSource() instanceof JButton) { JButton button = (JButton) e.getSource(); int index = -1; for (int i = 0; i < 16; i++) { if (buttons[i] == button) { index = i; break; } } if (index >= 0 && values[index] != 0) { button.setText("" + values[index]); button.setEnabled(false); if (count == 0) { firstButtonIndex = index; count = 1; } else { secondButtonIndex = index; count = 0; timer.start(); } } } else if (e.getSource() == timer) { timer.stop(); if (values[firstButtonIndex] == values[secondButtonIndex]) { values[firstButtonIndex] = 0; values[secondButtonIndex] = 0; } else { buttons[firstButtonIndex].setText(""); buttons[firstButtonIndex].setEnabled(true); buttons[secondButtonIndex].setText(""); buttons[secondButtonIndex].setEnabled(true); } } } public static void main(String[] args) { MatchGame game = new MatchGame(); game.setVisible(true); } } ``` 该代码使用 `JFrame` 和 `JButton` 等 Swing 组件实现了对对碰游戏的界面。在构造函数中生成随机的 16 个数字,并将它们随机分配到 16 个按钮上。当用户点击一个按钮时,该按钮上的数字将被显示出来。如果是第一次点击,则记录下该按钮的位置;如果是第二次点击,则比较两个按钮上的数字是否相同。如果相同,则将这两个按钮禁用,否则将这两个按钮的数字隐藏起来。 还有一个 `Timer` 对象用于在比较两个按钮上的数字时等待一段时间,以便用户可以看到这两个数字。如果两个数字不相同,则在等待一段时间后将这两个按钮的数字隐藏起来。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值