c++数字华容道

#include <bits/stdc++.h>
#include <windows.h>
#include <conio.h>
using namespace std;
int num[10005]={0};
int n,p[105][105],x,y,sum;
bool iswin(){
    int f=1;
    for(int i=1;i<=n-1;i++){
        for(int j=1;j<=n;j++){
            if(p[i][j]!=f)return false;
            f++;
        }
    }
    for(int j=1;j<=n-1;j++){
        if(p[n][j]!=f)return false;
        f++;
    }
    return true;
}
int main(){
    srand(time(NULL));
    cout<<"Welcome to play n-puzzle!Make sure that you're using Windows System..."<<endl;
    cout<<"Please input the length:";
    cin>>n;
    x=rand()%n+1;
    y=rand()%n+1;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(i==x && j==y)continue;
            int k=rand()%(n*n-1)+1;
            while(num[k])k=rand()%(n*n-1)+1;
            p[i][j]=k;
            num[k]=1;
        }
    }
    for(int i=1;i<=n*n;i++)num[i]=0;
    while(!iswin()){
        char a;
        system("cls");
        cout<<"---------------------------------"<<endl;
        cout<<"  You have been moved "<<sum<<" steps."<<endl;
        cout<<"  Move: W A S D     Restart: R "<<endl;
        cout<<"---------------------------------"<<endl;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                if(i==x && j==y)printf("%-5c",'*');
                else printf("%-5d",p[i][j]);
            }
            cout<<endl;
        }
        a=getch();
        if(a=='w' || a=='W'){
            if(x!=n){
                p[x][y]=p[x+1][y];
                x++;    
            }
        }
        if(a=='s' || a=='S'){
            if(x!=1){
                p[x][y]=p[x-1][y];
                x--;    
            }
        }
        if(a=='a' || a=='A'){
            if(y!=n){
                p[x][y]=p[x][y+1];
                y++;    
            }
        }
        if(a=='d' || a=='D'){
            if(y!=1){
                p[x][y]=p[x][y-1];
                y--;    
            }
        }
        if(a=='r' || a=='R'){
            x=rand()%n+1;
            y=rand()%n+1;
            for(int i=1;i<=n;i++){
                for(int j=1;j<=n;j++){
                    if(i==x && j==y)continue;
                    int k=rand()%(n*n-1)+1;
                    while(num[k])k=rand()%(n*n-1)+1;
                    p[i][j]=k;
                    num[k]=1;
                }
            }
            for(int i=1;i<=n*n;i++)num[i]=0;
            sum=0;
        }
        sum++;
    }
    system("cls");
    cout<<"*-----------------*"<<endl;
   printf("|    Total:%-5d  |\n",sum);
    cout<<"|     You Win!    |"<<endl;
    cout<<"*-----------------*"<<endl;
    return 0;
}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
数字华容道是一个经典的拼图游戏,也被称为“拼图游戏中的魔方”。在这个游戏中,有一个 $4\times 4$ 的方格,其中有 $15$ 个数字方块和一个空格,玩家需要通过移动数字方块,使得它们按照从 $1$ 到 $15$ 的顺序排列,空格则是用来移动数字方块的。 数字华容道的实现可以使用 C++ 的面向对象特性,将方格和方块抽象为对象,实现它们的移动和交换操作。以下是一个简单的实现示例。 ```c++ #include <iostream> using namespace std; // 数字方块类 class NumberBlock { private: int value; // 方块上的数字 int row; // 方块所在的行号 int col; // 方块所在的列号 public: NumberBlock(int value, int row, int col) { this->value = value; this->row = row; this->col = col; } // 获取方块上的数字 int getValue() { return value; } // 获取方块所在的行号 int getRow() { return row; } // 获取方块所在的列号 int getCol() { return col; } // 设置方块所在的行号和列号 void setPosition(int row, int col) { this->row = row; this->col = col; } }; // 数字华容道类 class DigitalHuRongDao { private: NumberBlock* blocks[4][4]; // 数字方块的二维数组 int emptyRow; // 空格所在的行号 int emptyCol; // 空格所在的列号 public: DigitalHuRongDao() { // 初始化数字方块 for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { int value = i * 4 + j + 1; if (value == 16) { blocks[i][j] = nullptr; emptyRow = i; emptyCol = j; } else { blocks[i][j] = new NumberBlock(value, i, j); } } } } // 输出数字华容道的当前状态 void print() { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (blocks[i][j] == nullptr) { cout << " "; } else { cout << blocks[i][j]->getValue() << " "; } } cout << endl; } cout << endl; } // 移动数字方块 bool move(int row, int col) { if (row < 0 || row > 3 || col < 0 || col > 3) { return false; } if (blocks[row][col] != nullptr) { return false; } if (row == emptyRow && col == emptyCol) { return false; } NumberBlock* block = blocks[emptyRow][emptyCol]; blocks[emptyRow][emptyCol] = blocks[row][col]; blocks[row][col] = block; if (blocks[emptyRow][emptyCol] != nullptr) { blocks[emptyRow][emptyCol]->setPosition(emptyRow, emptyCol); } emptyRow = row; emptyCol = col; return true; } // 判断数字华容道是否已经完成 bool isCompleted() { int value = 0; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (blocks[i][j] != nullptr) { if (blocks[i][j]->getValue() != value + 1) { return false; } value++; } } } return true; } }; int main() { DigitalHuRongDao dh; dh.print(); while (!dh.isCompleted()) { int row, col; cout << "请输入要移动的数字方块的行号和列号(用空格分隔):"; cin >> row >> col; if (dh.move(row, col)) { dh.print(); } else { cout << "无效的移动,请重新输入!" << endl; } } cout << "恭喜你,完成了数字华容道!" << endl; return 0; } ``` 在这个示例代码中,`NumberBlock` 类表示数字方块,它有一个数字和一个位置(行号和列号)属性,并且可以获取和设置这些属性。`DigitalHuRongDao` 类表示数字华容道,它有一个 $4\times 4$ 的数字方块二维数组和一个空格位置属性,可以初始化数字方块、输出当前状态、移动数字方块和判断是否已经完成。在 `main` 函数中,首先创建一个数字华容道对象,然后进入一个循环,不断接受用户输入的移动指令,直到数字华容道被完成为止。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值