Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long LL;
- typedef pair<int, int> PII;
- typedef pair<LL, LL> PLL;
- class piece{
- public:
- void set_x(int x0){
- x = x0;
- }
- void set_y(int y0){
- y = y0;
- }
- void set_type(int t){
- type = t;
- }
- void set_show(){
- if (type == 1) show = "b";
- else if (type == 2) show = "w";
- else show = "+";
- }
- int x, y, type;
- string show;
- };
- class player{
- public:
- void set_name(string s){
- name = s;
- }
- string name;
- };
- class board{
- public:
- void init_pieces(){
- for (int i = 0; i < 15; i++){
- for (int j = 0; j < 15; j++){
- pieces[i][j].set_x(i);
- pieces[i][j].set_y(j);
- pieces[i][j].set_type(0);
- pieces[i][j].set_show();
- }
- }
- }
- void showboard(){
- for (int i = 0; i < 15; i++){
- for (int j = 0; j < 15; j++){
- cout << pieces[i][j].show << ' ';
- }
- cout << '\n';
- }
- }
- piece pieces[15][15];
- };
- class game{
- public:
- void init_players(player p1, player p2){
- players[0] = p1;
- players[1] = p2;
- }
- void set_B(board b){
- B = b;
- }
- void set_turn(bool t){
- turn = t;
- }
- void showRule(){
- cout << "Rule: " << endl;
- }
- void showB(){
- B.showboard();
- }
- void judge(int x, int y){
- int cnt = 1;
- for (int i = 1; i < 15; i++){
- if (B.pieces[i][y].type != 0 && B.pieces[i][y].type == B.pieces[i - 1][y].type){
- cnt ++;
- if (cnt == 5){
- showB();
- if (turn) cout << players[0].name + " won!" << endl;
- else cout << players[1].name + " won!" << endl;
- check = false;
- return;
- }
- } else cnt = 1;
- }
- cnt = 1;
- for (int j = 1; j < 15; j++){
- if (B.pieces[x][j].type != 0 && B.pieces[x][j].type == B.pieces[x][j - 1].type){
- cnt ++;
- if (cnt == 5){
- showB();
- if (turn) cout << players[0].name + " won!" << endl;
- else cout << players[1].name + " won!" << endl;
- check = false;
- return;
- }
- } else cnt = 1;
- }
- int x0 = x;
- int y0 = y;
- while (x0 > 0 && y0 > 0){
- x0 --;
- y0 --;
- }
- x0 ++;
- y0 ++;
- cnt = 1;
- while (x0 < 14 && y0 < 14){
- if (B.pieces[x0][y0].type != 0 && B.pieces[x0][y0].type == B.pieces[x0 - 1][y0 - 1].type){
- cnt ++;
- if (cnt == 5){
- showB();
- if (turn) cout << players[0].name + " won!" << endl;
- else cout << players[1].name + " won!" << endl;
- check = false;
- return;
- }
- } else cnt = 1;
- x0 ++;
- y0 ++;
- }
- x0 = x;
- y0 = y;
- while (x0 > 0 && y0 < 14){
- x0 --;
- y0 ++;
- }
- x0 ++;
- y0 --;
- cnt = 1;
- while (x0 < 14 && y0 > 0){
- if (B.pieces[x0][y0].type != 0 && B.pieces[x0][y0].type == B.pieces[x0 - 1][y0 + 1].type){
- cnt ++;
- if (cnt == 5){
- showB();
- if (turn) cout << players[0].name + " won!" << endl;
- else cout << players[1].name + " won!" << endl;
- check = false;
- return;
- }
- }
- else cnt = 1;
- x0 ++;
- y0 --;
- }
- }
- void begin(){
- showRule();
- cout << "The game is started" << endl;
- cout << "Please input player1's name: " << endl;
- string n1, n2;
- cin >> n1;
- cout << "Please input player2's name: " << endl;
- cin >> n2;
- player p1, p2;
- p1.set_name(n1);
- p2.set_name(n2);
- init_players(p1, p2);
- }
- void begingame(){
- int t, x, y;
- while (check){
- showB();
- if (turn){
- cout << "Now it's " + players[0].name + "'s turn" << endl;
- t = 1;
- } else{
- cout << "Now it's " + players[1].name + "'s turn" << endl;
- t = 2;
- }
- cout << "Please input position: " << endl;
- cin >> x >> y;
- piece p;
- p.set_x(x);
- p.set_y(y);
- p.set_type(t);
- p.set_show();
- B.pieces[x - 1][y - 1] = p;
- judge(x - 1, y - 1);
- set_turn(not (turn));
- }
- cout << "Game over!";
- }
- player players[2];
- board B;
- bool turn, check = true;
- };
- int main(){
- ios::sync_with_stdio(false);
- cin.tie(0);
- board b1;
- b1.init_pieces();
- game G;
- G.set_B(b1);
- G.set_turn(true);
- G.begin();
- G.begingame();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement