翻硬币游戏

· · 个人记录

规则:

  1. 游戏会给出两个字符串,* 表示硬币正面, o 表示硬币背面

  2. 每一轮可以输入一个正整数 x,表示将第 x 和第 x + 1 的硬币翻过来。如果输入的数字过大或过小则无效

  3. 游戏目的是通过翻转第一行相邻的两个硬币来使第一行与第二行的排列方式相同

例如:

将 **oo* 变成 ***** 只需要输入一个 3 将第 3 和第 4 个硬币反转,使两个字符串相同。

代码:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
#define ll long long
string s,e;
ll x;
void f(ll m){
    if(e[m] == 'o'){
        e[m] = '*';
    }
    else{
        e[m] = 'o';
    }
    if(e[m + 1] == 'o'){
        e[m + 1] = '*';
    }
    else{
        e[m + 1] = 'o';
    }
} 
void func(ll m){
    if(s[m] == 'o'){
        s[m] = '*';
    }
    else{
        s[m] = 'o';
    }
    if(s[m + 1] == 'o'){
        s[m + 1] = '*';
    }
    else{
        s[m + 1] = 'o';
    }
}
int main(){
    srand(time(0));
    ll tmp = (rand() % 11 + 10);
    for(ll i = 0;i <= tmp;i ++){
        ll a = rand();
        if(a & 1){
            s += 'o';
        }
        else{
            s += '*';
        }
    }
    e = s;
    ll tmp2 = (rand() % 67 + 66);
    for(ll i = 1;i <= tmp2;i ++){
        ll a = rand();
        f(a % (s.size() - 1));
    }
    cout << s << endl << e << endl;
    while(s != e){
        cin >> x;
        if(x >= s.size() || x < 1){
            system("cls");
            cout << s << endl << e << endl;
            continue;
        }
        x --;
        func(x);
        system("cls");
        cout << s << endl;
        cout << e << endl;
    }
    cout << "Great!" << endl;
    system("pause");
    return 0;
}
注意:用 exe 文件玩效果更好呦 ~