实验(变位字谜 )

主程序:

#include"游戏函数.h"

int main() {
    play_puzzle();
    return 0;
}

头文件:标头.h

#pragma once
#include<iostream>
#include<string>
#include<cstdlib>

using namespace std;

typedef char elem_type;

const int LIST_SIZE = 1000; //假设表容量是1000

                            //双向循环链表类型定义
typedef struct Node {
    elem_type data;
    struct Node *prior, *next;  //前驱指针prior,后继指针next
    struct Node *head, *rear;   //链表头指针head,尾指针rear
    struct Node *cursor;    //用于标记数据项的指针
    int len;    //表长
}node, *dlinklist;

void dlinklist_init(dlinklist &L);      //创建空表
void destroy(dlinklist &L);     //销毁表,释放内存
void insert(dlinklist &L, elem_type e); //插入数据,操作完成后当前指针指向该数据项
void remove(dlinklist &L);      //删除数据,如果删除的数据项在列表末尾,移动指针指向列表开始
void replace(dlinklist &L, elem_type e);    //用e替换指针标记的数据项
void clear(dlinklist &L);       //删除表中的所有数据项
bool empty(dlinklist L);        //表空,返回true,否则返回false
bool full(dlinklist L);     //表满返回true,否则返回false
void go_to_beginning(dlinklist &L); //移动指针到表头
void go_to_end(dlinklist &L);       //移动指针到表尾
void go_to_next(dlinklist &L);  //移动指针到下一个数据项
void go_to_prior(dlinklist &L); //移动指针到前一个数据项
void show_cursor(dlinklist L);      //返回指针所标记的数据项的值
void show_structure(dlinklist L);   //输出表中的数据项,如果表空,输出“Empty list”

void error_message(string s);   //错误提示函数,发生错误会输出s,并强制结束程序
void help();    //提示输出
void go();      //执行程序

头文件:基本函数.h

#pragma once
#include "标头.h"



void error_message(string s) {
    cout << s << endl;
    exit(-1);
}

void dlinklist_init(dlinklist &L) {
    L = new node;
    if (L == nullptr) error_message("内存分配失败");
    L->head = L->rear = L->next = L->cursor = L->prior = L;
    L->len = 0;
}

void destroy(dlinklist &L) {
    node *p = L->head;
    node *q = nullptr;
    while (p != L->rear) {
        q = p->next;
        delete p;
        p = q;
    }
    delete L->rear;
}

void insert(dlinklist &L, elem_type e) {
    if (L->len >= LIST_SIZE) error_message("表已满,不能再插入");
    node *tmp = new node;
    if (!tmp) error_message("内存分配失败");
    tmp->data = e;
    tmp->next = L->cursor->next;
    tmp->prior = L->cursor;
    L->cursor->next->prior = tmp;
    L->cursor->next = tmp;
    L->len++;
    L->cursor = tmp;
    L->rear = L->prior;
}

void remove(dlinklist &L) {
    if (L->len == 0) error_message("表是空的,不能进行删除操作");
    node *tmp = L->cursor->next;
    L->cursor->next->prior = L->cursor->prior;
    L->cursor->prior->next = L->cursor->next;
    delete L->cursor;
    L->cursor = tmp;
    L->len--;
}

void replace(dlinklist &L, elem_type e) {
    L->cursor->data = e;
}

void clear(dlinklist &L) {
    node *p = L->next;
    node *q = nullptr;
    while (p != L->rear) {
        q = p->next;
        delete p;
        p = q;
    }
    delete L->rear;
    L->head = L->rear = L->next = L->prior = L->cursor = L;
    L->len = 0;
}

bool empty(dlinklist L) {
    return L->len == 0;
}

bool full(dlinklist L) {
    return L->len == LIST_SIZE;
}

void go_to_beginning(dlinklist &L) {
    if (L->len>0) {
        L->cursor = L->head->next;
        //cout<<"指向表头: ";
        //cout<<L->cursor->data<<endl;
    }
    else
        cout << "表是空的,无法指向表头" << endl;
}

void go_to_end(dlinklist &L) {
    if (L->len>0) {
        L->cursor = L->rear;
        //cout<<"指向表尾: "<<L->cursor->data<<endl;
    }
    else
        cout << "表是空的,无法指向表尾" << endl;
}

void go_to_next(dlinklist &L) {
    if (L->cursor == L->rear) L->cursor = L->head->next;
    else L->cursor = L->cursor->next;
}

void go_to_prior(dlinklist &L) {
    if (L->cursor == L->head->next) L->cursor = L->rear;
    else L->cursor = L->cursor->prior;
}

void show_cursor(dlinklist L) {
    if (L->len>0)
        cout << L->cursor->data << endl;
    else
        cout << "表是空的,指针不指向元素,无法获取" << endl;
}

void show_structure(dlinklist L) {
    if (L->len == 0) {
        cout << "表是空的" << endl;
        return;
    }
    //cout<<"list: ";
    node *tmp = L->head->next;
    while (tmp != L->rear) {
        cout << tmp->data/*<<" "*/;
        tmp = tmp->next;
    }
    cout << L->rear->data/*<<endl*/;
}

游戏函数.h

#pragma once
#include"基本函数.h"

//变位字谜数据类型构造
typedef struct {
    dlinklist solution;     //答案
    dlinklist puzzle;   //单词字母顺序打乱
}puz;

void init_puzzle(puz &p, string ans, string init);//创建一个字谜游戏
void left_shift(puz &p);    //所有字母左移一个单位
void right_shift(puz &p);//所有字母右移一个单位
void swap_ends(puz &p); //交换两端的字母
void display(puz p);    //显示字谜游戏
bool is_solved(puz p);//如果字谜解决,返回true,否则返回false

void puzzle_help(); //游戏操作提示
void play_puzzle(); //玩游戏


void init_puzzle(puz &p, string ans, string init) {
    dlinklist_init(p.solution);
    dlinklist_init(p.puzzle);
    for (string::iterator it1 = ans.begin(), it2 = init.begin(); it1 != ans.end(), it2 != init.end(); it1++, it2++) {
        insert(p.solution, *it1);
        insert(p.puzzle, *it2);
    }
}

void left_shift(puz &p) {
    char tmp;
    go_to_beginning(p.puzzle);
    tmp = p.puzzle->cursor->data;
    while (p.puzzle->cursor != p.puzzle->rear) {
        go_to_next(p.puzzle);
        p.puzzle->cursor->prior->data = p.puzzle->cursor->data;
    }
    p.puzzle->rear->data = tmp;
}

void right_shift(puz &p) {
    char tmp;
    go_to_end(p.puzzle);
    tmp = p.puzzle->cursor->data;
    while (p.puzzle->cursor != p.puzzle->head->next) {
        go_to_prior(p.puzzle);
        p.puzzle->cursor->next->data = p.puzzle->cursor->data;
    }
    p.puzzle->cursor->data = tmp;
}

void swap_ends(puz &p) {
    char tmp = p.puzzle->head->next->data;
    p.puzzle->head->next->data = p.puzzle->rear->data;
    p.puzzle->rear->data = tmp;
}

void display(puz p) {
    cout << "字谜:";
    show_structure(p.solution);
    cout << " ← ";
    show_structure(p.puzzle);
    cout << endl;
}

bool is_solved(puz p) {
    go_to_beginning(p.puzzle);
    go_to_beginning(p.solution);
    while (p.puzzle->cursor != p.puzzle->rear) {
        if (p.puzzle->cursor->data != p.solution->cursor->data)
            return false;
        go_to_next(p.puzzle);
        go_to_next(p.solution);
    }
    if (p.solution->rear->data != p.puzzle->rear->data) return false;
    return true;
}

void play_help() {
    cout << "      ****************操作提示****************" << endl;
    cout << "      |C →创建游戏                          |" << endl;
    cout << "      |--------------------------------------|" << endl;
    cout << "      |L →所有字母左移一位,首字母移到最右边|" << endl;
    cout << "      |--------------------------------------|" << endl;
    cout << "      |R →所有字母右移一位,首字母移到最左边|" << endl;
    cout << "      |--------------------------------------|" << endl;
    cout << "      |S →交换两端的字母                    |" << endl;
    cout << "      |--------------------------------------|" << endl;
    cout << "      |M →强行结束本局                      |" << endl;
    cout << "      |--------------------------------------|" << endl;
    cout << "      |Q →退出游戏                          |" << endl;
    cout << "      ****************操作提示****************" << endl;
}

void play_puzzle() {
    play_help();
    puz p;
    char c;
    string a, b;
    while (1) {
        cin >> c;
        switch (c) {
        case 'C': {
            cout << "请输入一个单词: "; cin >> a;
            cout << "请把刚才输入的单词字母顺序打乱,再输入: "; cin >> b;
            init_puzzle(p, a, b);
            cout << "游戏开始:"; display(p); cout << endl;
            break;
        }
        case 'L':left_shift(p); display(p); break;
        case 'R':right_shift(p); display(p); break;
        case 'S':swap_ends(p); display(p); break;
        case 'Q':cout << "欢迎再玩,游戏已退出" << endl; return;
        case 'M': {
            if (!is_solved(p)) {
                cout << "请再接再厉!" << endl; play_help();
            }
            else {
                cout << "恭喜您过关" << endl;
            }
            break;
        }
        default: cout << "输入非法,请重新输入" << endl; break;
        }
        if (is_solved(p)) {
            cout << "恭喜您过关" << endl;
            play_help();
        }
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值