Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cassert>
- #include <random>
- #include <cstdio>
- #include <iostream>
- #include <iomanip>
- #include <clocale>
- using namespace std;
- const string space = "."; /// It should be given in templates...
- typedef long long ll;
- #define read cin
- #define eol '\n'
- #define endln cout << eol
- #define all(x) begin(x), end(x)
- template <class A> inline void print (A a) { cout << a << ' '; }
- template <class A> inline void println (A a) { cout << a << eol; }
- template <class A, class... B> inline void println (A a, B... b) { print(a); println(b...); }
- template <class A> inline void err (A a) { cerr << a << ' '; }
- template <class A> inline void errln (A a) { cerr << a << eol; }
- template <class A, class... B> inline void errln (A a, B... b) { err(a); errln(b...); }
- ////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////
- template <
- ostream &out,
- size_t hSize = 10, size_t wSize = 10,
- size_t sparse = 2
- >
- class MatrixPrinter
- {
- private:
- string M[hSize][wSize];
- public:
- void print (size_t iS = 0, size_t iF = hSize,
- size_t jS = 0, size_t jF = wSize)
- {
- out << string(4*(jF - jS)+5, '-') << eol;
- out << "№\t";
- for (size_t j = jS; j < jF; ++ j)
- if (j % sparse == 0)
- out << '\t' << j;
- else
- out << " ";
- out << eol;
- out << " \t+---";
- for (size_t j = jS + 1; j < jF; ++ j)
- out << "+---";
- out << "+" << eol;
- for (size_t i = iS; i < iF; ++ i)
- {
- out << i << "\t|";
- for (size_t j = jS; j < jF; ++ j)
- out << '\t' << M[i][j];
- out << eol;
- }
- out << string(4*(jF - jS)+5, '-') << eol;
- }
- void setPoint (size_t i, size_t j, string c)
- { M[i][j] = c; }
- string getPoint (size_t i, size_t j)
- {
- if (i < hSize && j < wSize)
- return M[i][j];
- return "";
- }
- MatrixPrinter ()
- {
- for (size_t i = 0; i < hSize; ++ i)
- for (size_t j = 0; j < wSize; ++ j)
- M[i][j] = space;
- out << "MatrixPrinter запущен!" << eol << eol;
- }
- };
- ////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////
- const bool LOG_TO_FILE = false;
- const size_t I = 5, /// Field size
- J = 5,
- K = 3; /// Points per turn
- const string a = "м",
- b = "ж",
- A = "М",
- B = "Ж";
- MatrixPrinter<cout, 5, 5, 1> mp;
- // #define DEBUG_INFO
- #ifdef DEBUG_INFO
- #define addLog(...) errln("[ log ] ", __VA_ARGS__)
- #else
- #define addLog(...) (void(0))
- #endif
- bool FIRST_PLAYER = false;
- void initialize ()
- {
- #ifdef DEBUG_INFO
- if (LOG_TO_FILE)
- freopen("log.txt", "w", stderr);
- #endif
- println("Hello!");
- println("Will you be a first player? (1 = yes, 0 = no)");
- read >> FIRST_PLAYER;
- println("Your answer:", FIRST_PLAYER);
- mp.setPoint(0, 0, a);
- mp.setPoint(I - 1, J - 1, b);
- mp.print();
- }
- struct crypto_t
- {
- mt19937 getRand;
- const ll MUL = 10;
- crypto_t (ll s) : getRand(s) {}
- crypto_t () : crypto_t(19) {}
- inline size_t zip (size_t i, size_t j)
- {
- size_t result = MUL*i + j;
- return result;
- }
- void unzip (size_t n, size_t &i, size_t &j)
- {
- i = n / MUL;
- j = n % MUL;
- }
- }; crypto_t crypto;
- bool checkPoint (size_t i, size_t j, string c)
- {
- return mp.getPoint(i, j + 1) == c
- || mp.getPoint(i, j - 1) == c
- || mp.getPoint(i + 1, j) == c
- || mp.getPoint(i - 1, j) == c;
- }
- bool canGo (ll i, ll j)
- {
- if ( mp.getPoint(i, j) == a
- || mp.getPoint(i, j) == A )
- {
- println("[ X ] It already belongs to you.");
- return false;
- }
- if ( checkPoint(i, j, a) )
- {
- if ( mp.getPoint(i,j) == B )
- {
- println("[ X ] There is enemy's fortress. (You can't eat it) ");
- return false;
- }
- println("[ V ] I think you can go there...");
- return true;
- } else
- {
- if ( checkPoint(i, j, A) )
- {
- println("[ ? ] Make sure, that fortress are connected and go.");
- println("Is the fortress connected?");
- string ans; read >> ans;
- return (ans == "yes" || ans == "Yes" || ans == "YES" || ans == "1");
- }
- println("[ X ] This point isn't connected.");
- return false;
- }
- }
- bool usrGo (ll i, ll j)
- {
- if (canGo(i, j))
- {
- if (mp.getPoint(i,j) == space) mp.setPoint(i, j, a);
- if (mp.getPoint(i,j) == b) mp.setPoint(i, j, A);
- mp.print();
- return true;
- } else
- {
- println("Try again...");
- return false;
- }
- }
- ll usrTurn ()
- {
- ll i, j;
- do { read >> i >> j; }
- while (! usrGo(i, j));
- return crypto.zip(i, j);
- }
- void netTurn (size_t net)
- {
- size_t i, j;
- crypto.unzip(net, i, j);
- i = I-1 - i; /// reversing
- j = J-1 - j; /// reversing
- println("Oponent goes to", i, j);
- if (mp.getPoint(i, j) == a) mp.setPoint(i, j, B);
- if (mp.getPoint(i, j) == space) mp.setPoint(i, j, b);
- }
- int main ()
- {
- setlocale(0, "");
- initialize();
- for (ll turns = 1; turns <= 1024; )
- {
- if (FIRST_PLAYER) {
- println("\n\n\nTurn #", turns ++);
- for (ll i=K;i--;) println("\t\t\t\t\tYour point", usrTurn());
- mp.print();
- }
- println("\n\n\nTurn #", turns ++); ll n;
- println("Waiting for net turns...");
- for (ll i=K;i--;) read >> n, netTurn(n);
- mp.print();
- if ( ! FIRST_PLAYER) {
- println("\n\n\nTurn #", turns ++);
- for (ll i=K;i--;) println("\t\t\t\t\tYour point", usrTurn());
- mp.print();
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement