Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the win strategy in such a game?

Someday I was given such a question, two player(A,B) and 4 slots, each player put "N" or "O" to these slots, who first spell 'NON' win this game. Is there a strategy player A or player B will be surely success ? I am not very familiar with this, so he give some hints for below case, B will success not matter what A puts.

[N(A puts) |_ | _ | N(B puts)]

First A put N at the first index of this array, then B put N at the last position. Then no matter what and where A puts, B will win.

So the question is if the slots are added to 7 slots, is there a same strategy?

[ _ |_ | _ | _ | _ | _ | _ ]

I thought a way similar like cases of four solts, however it needs such preconditions. I am not sure whether there's some theory behind that.

[ N |_ | _ | N | _ | _ | N ]

like image 864
Ivan Avatar asked Oct 09 '22 16:10

Ivan


1 Answers

First player will always win this game. Winning move is _ _ _ N _ _ _

As only 7 slots, so there are only 3 ^ 7 states of this game. So each states can be easily calculated by dynamic programming. Here is my solution in c++

#include <cstdio>
#include <string>
#include <map>
#include <iostream>
using namespace std;

map<string, string> mp;

string go(string s) {
    if (mp.find(s) != mp.end()) {
        return mp[s];
    }

    if (s.find("_") == -1) {
        cout<<s<<" "<<"DRAW"<<endl;
        return mp[s] = "DRAW";
    }

    string s1 = s;
    bool draw_found = false;
    for (int i = 0; i < s.size(); ++i) {
        if (s[i] == '_') {
            string t = "NO";
            for (int j = 0; j < t.size(); ++j) {
                s[i] = t[j];
                if (s.find("NON") != -1) {
                    cout<<s1<<" WIN by move: "<<s<<endl;
                    return mp[s1] = "WIN";
                }
                string r = go(s);
                if (r == "LOSE") {
                    cout<<s1<<" "<<" WIN by move: "<<s<<endl;
                    return mp[s1] = "WIN";
                }
                else if (r == "DRAW") {
                    draw_found = true;
                }
                s[i] = 'O';
            }
            s[i] = '_';
        }
    }

    if (draw_found) {
        cout<<s<<" "<<"DRAW"<<endl;
        return mp[s] = "DRAW";
    }

    cout<<s<<" "<<"LOSE"<<endl;
    return mp[s] = "LOSE";
}

int main (void) {
    string s;
    for (int i = 0; i < 7; ++i) {
        s += "_";
    }
    string g = go(s);
    cout<<g<<endl;
    return 0;
}
like image 118
Arif Avatar answered Oct 11 '22 07:10

Arif