Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // WARCY.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <stdlib.h>
- using namespace std;
- int flaga;
- int flaga_bot;
- class plansza {
- public:
- int zaslonx, zaslony;
- int poglad[9][9];
- int gra[9][9];
- void resetuj() {
- int c = 1;
- for (int i = 0;i < 8;i++) {
- for (int j = 0; j < 8;j++) {
- if((i+j)%2==0){
- poglad[j][i] = c;
- c++;
- if (i < 3) {
- gra[j][i] = 1;
- }
- if (i > 4) {
- gra[j][i] = 2;
- }
- if (i == 3 || i == 4) {
- gra[j][i] = 0;
- }
- }
- else {
- poglad[j][i] = 0;
- gra[j][i] = 0;
- }
- }
- }
- }
- void wyswietl(){
- cout << "plansza" << endl;
- for (int i = 0;i < 8;i++) {
- for (int j = 0; j < 8; j++) {
- cout << gra[j][i] << " ";
- }cout << endl;
- }
- cout << "poglad, liczby" << endl;
- for (int i = 0;i < 8;i++) {
- for (int j = 0; j < 8; j++) {
- if (poglad[j][i] < 10) {
- cout << poglad[j][i] << " ";
- }
- else{
- cout << poglad[j][i] << " ";
- }
- }cout << endl;
- }
- }
- void przestaw(int i, int j) {
- int s;
- s = j - i;
- int x1, x2, y1, y2;
- for (int x = 0;x < 8;x++) {
- for (int y = 0;y < 8;y++) {
- if (poglad[y][x] == i) {
- x1 = x;
- y1 = y;
- }
- if (poglad[y][x] == j) {
- x2 = x;
- y2 = y;
- }
- }
- }
- if (((gra[y1][x1] == 1 || gra[y1][x1]==3 )&& s < 0) || gra[y1][x1] == 0 || gra[y2][x2]!=0 || abs(x1-x2)>1 || abs(y2-y1)>1) {
- cout << endl << "niedozwolony ruch" << endl;
- }
- else {
- swap(gra[y1][x1], gra[y2][x2]);
- flaga = 1;
- }
- }
- bool czybic() {
- for (int x = 0;x < 8;x++) {
- for (int y = 0;y < 8;y++) {
- if ((gra[y][x] == 1 || gra[y][x]==3) && (((gra[y - 1][x - 1] == 2 || gra[y - 1][x - 1] == 4) && gra[y - 2][x - 2] == 0) || ((gra[y - 1][x + 1] == 2 || gra[y - 1][x + 1] == 4) && gra[y - 2][x + 2] == 0) || ((gra[y + 1][x - 1] == 2 || gra[y + 1][x - 1] == 4)&& gra[y + 2][x - 2]==0) || ((gra[y + 1][x + 1] == 2 || gra[y + 1][x + 1] == 4) && gra[y + 2][x + 2]==0))){
- zaslonx = x;
- zaslonx = y;
- return true;
- }
- }
- }return false;
- }
- void bicie(int i, int j) {
- int s;
- s = j - i;
- int x1, x2,x3, y1, y2, y3;
- for (int x = 0;x < 8;x++) {
- for (int y = 0;y < 8;y++) {
- if (poglad[y][x] == i) {
- x1 = x;
- y1 = y;
- }
- if (poglad[y][x] == j) {
- x2 = x;
- y2 = y;
- }
- }
- }
- x3 = (x2 + x1) / 2;
- y3 = (y2 + y1) / 2;
- if ((gra[y1][x1] != 1 && gra[y1][x1] !=3) || gra[y2][x2]!=0 || (gra[y3][x3]!=2 && gra[y3][x3]!=4)) {
- cout << endl << "niedozwolony ruch bicia" << endl;
- }
- else {
- gra[y3][x3] = 0;
- swap(gra[y1][x1], gra[y2][x2]);
- flaga = 1;
- }
- }
- void zamiana() {
- for (int y = 0;y < 8;y++) {
- if (gra[y][0] == 2) {
- gra[y][0] = 4;
- }
- if (gra[y][7] == 1) {
- gra[y][7] = 3;
- }
- }
- }
- };
- class przeciwnik {
- public:
- bool zaslon(plansza *p) {
- if (p->czybic()) {
- for (int x = 0;x < 8;x++) {
- for (int y = 0;y < 8;y++) {
- if (( p->gra[y][x] == 2 || p->gra[y][x] == 4 ) && p->gra[y + 1][x - 1] == 0)
- {
- swap(p->gra[y][x], p->gra[y + 1][x - 1]);
- if (!p->czybic()) {
- return true;
- }
- else {
- swap(p->gra[y][x], p->gra[y + 1][x - 1]);
- }
- }
- if (( p->gra[y][x] == 2 || p->gra[y][x] == 4 ) && p->gra[y - 1][x - 1] == 0)
- {
- swap(p->gra[y][x], p->gra[y - 1][x - 1]);
- if (!p->czybic()) {
- return true;
- }
- else {
- swap(p->gra[y][x], p->gra[y - 1][x - 1]);
- }
- }
- }
- }return false;
- }return false;
- }
- bool ruszaj(plansza *p) {
- for (int x = 0;x < 8;x++) {
- for (int y = 0;y < 8;y++) {
- if (( p->gra[y][x] == 2 || p->gra[y][x] == 4 ) && p->gra[y + 1][x - 1] == 0 )
- {
- swap(p->gra[y][x], p->gra[y + 1][x - 1]);
- if (!p->czybic()) {
- return true;
- }
- else {
- swap(p->gra[y][x], p->gra[y + 1][x - 1]);
- }
- }
- if (( p->gra[y][x] == 2 || p->gra[y][x] == 4 ) && p->gra[y - 1][x - 1] == 0)
- {
- swap(p->gra[y][x], p->gra[y - 1][x - 1]);
- if (!p->czybic()) {
- return true;
- }
- else {
- swap(p->gra[y][x], p->gra[y - 1][x - 1]);
- }
- }
- }
- }return false;
- }
- bool ruszaj2(plansza *p) {
- for (int x = 0;x < 8;x++) {
- for (int y = 0;y < 8;y++) {
- if (( p->gra[y][x] == 2 || p->gra[y][x] == 4 ) && p->gra[y + 1][x - 1] == 0 )
- {
- swap(p->gra[y][x], p->gra[y + 1][x - 1]);
- return true;
- }
- if (( p->gra[y][x] == 2 || p->gra[y][x] == 4 ) && p->gra[y - 1][x - 1] == 0 )
- {
- swap(p->gra[y][x], p->gra[y - 1][x - 1]);
- return true;
- }
- }
- }return false;
- }
- bool czybic(plansza *p) {
- for (int x = 0;x < 8;x++) {
- for (int y = 0;y < 8;y++) {
- if (( p->gra[y][x] == 2 || p->gra[y][x] == 4 ) && (((p->gra[y - 1][x - 1] == 1 || p->gra[y - 1][x - 1] == 3) && p->gra[y - 2][x - 2] == 0) || ((p->gra[y - 1][x + 1] == 1 || p->gra[y - 1][x + 1] == 3) && p->gra[y - 2][x + 2] == 0) || ((p->gra[y + 1][x - 1] == 1 || p->gra[y + 1][x - 1] ==3) && p->gra[y + 2][x - 2] == 0) || ((p->gra[y + 1][x + 1] == 1 || p->gra[y + 1][x + 1] == 3 ) && p->gra[y + 2][x + 2] == 0))) {
- return true;
- }
- }
- }return false;
- }
- bool podwojne(plansza *p,int y, int x)
- {
- p->zamiana();
- if ((p->gra[y][x] == 2 || p->gra[y][x] == 4) && (p->gra[y - 1][x - 1] == 1 || p->gra[y - 1][x - 1] == 3) && p->gra[y - 2][x - 2] == 0) {
- swap(p->gra[y - 2][x - 2], p->gra[y][x]);
- p->gra[y - 1][x - 1] = 0;
- podwojne(&*p, y - 2, x - 2);
- return true;
- }
- if ((p->gra[y][x] == 2 || p->gra[y][x] == 4) && (p->gra[y + 1][x - 1] == 1 || p->gra[y + 1][x - 1] == 3) && p->gra[y + 2][x - 2] == 0) {
- swap(p->gra[y + 2][x - 2], p->gra[y][x]);
- p->gra[y + 1][x - 1] = 0;
- podwojne(&*p, y + 2, x - 2);
- return true;
- }
- if ((p->gra[y][x] == 2 || p->gra[y][x] == 4) && (p->gra[y - 1][x + 1] == 1 || p->gra[y - 1][x + 1] == 3) && p->gra[y - 2][x + 2] == 0) {
- swap(p->gra[y - 2][x + 2], p->gra[y][x]);
- p->gra[y - 1][x + 1] = 0;
- podwojne(&*p, y - 2, x + 2);
- return true;
- }
- if ((p->gra[y][x] == 2 || p->gra[y][x] == 4) && (p->gra[y + 1][x + 1] == 1 || p->gra[y + 1][x + 1] == 3) && p->gra[y + 2][x + 2] == 0) {
- swap(p->gra[y + 2][x + 2], p->gra[y][x]);
- p->gra[y + 1][x + 1] = 0;
- podwojne(&*p, y + 2, x + 2);
- return true;
- } return false;
- }
- bool bicie(plansza *p) {
- for (int x = 0;x < 8;x++) {
- for (int y = 0;y < 8;y++) {
- if (( p->gra[y][x] == 2 || p->gra[y][x] == 4 ) && (p->gra[y - 1][x - 1] == 1 || p->gra[y - 1][x - 1] == 3) && p->gra[y - 2][x - 2] == 0) {
- swap(p->gra[y - 2][x - 2], p->gra[y][x]);
- p->gra[y - 1][x - 1] = 0;
- if (podwojne(&*p,y-2,x-2)) {
- cout << endl << "Podwojne bicie" << endl;
- return true;
- }
- }if (( p->gra[y][x] == 2 || p->gra[y][x] == 4 ) &&( p->gra[y + 1][x - 1] == 1 || p->gra[y + 1][x - 1] ==3) && p->gra[y + 2][x - 2] == 0) {
- swap(p->gra[y + 2][x - 2], p->gra[y][x]);
- p->gra[y + 1][x - 1] = 0;
- if (podwojne(&*p, y + 2, x - 2)) {
- cout << endl << "Podwojne bicie" << endl;
- return true;
- }
- }if (( p->gra[y][x] == 2 || p->gra[y][x] == 4 ) && (p->gra[y - 1][x + 1] == 1 || p->gra[y - 1][x + 1] ==3) && p->gra[y - 2][x + 2] == 0) {
- swap(p->gra[y - 2][x + 2], p->gra[y][x]);
- p->gra[y - 1][x + 1] = 0;
- if (podwojne(&*p, y - 2, x + 2)) {
- cout << endl << "Podwojne bicie" << endl;
- return true;
- }
- }if (( p->gra[y][x] == 2 || p->gra[y][x] == 4 ) && (p->gra[y + 1][x + 1] == 1 || p->gra[y + 1][x + 1] ==3) && p->gra[y + 2][x + 2] == 0) {
- swap(p->gra[y + 2][x + 2], p->gra[y][x]);
- p->gra[y + 1][x + 1] = 0;
- if (podwojne(&*p, y + 2, x + 2)) {
- cout << endl << "Podwojne bicie" << endl;
- return true;
- }
- }
- }
- }return false;
- }
- };
- int main()
- {
- plansza p;
- przeciwnik prz;
- int koniec = 0;
- p.resetuj();
- do {
- int k, b;
- do {
- p.zamiana();
- if (p.czybic()) {
- cout << "trzeba bic" << endl;
- p.zamiana();
- p.wyswietl();
- flaga = 0;
- cin >> k;
- cin >> b;
- p.bicie(k, b);
- if (p.czybic()) {
- cout << "trzeba bic" << endl;
- p.zamiana();
- p.wyswietl();
- flaga = 0;
- cin >> k;
- cin >> b;
- p.bicie(k, b);
- if (p.czybic()) {
- cout << "trzeba bic" << endl;
- p.zamiana();
- p.wyswietl();
- flaga = 0;
- cin >> k;
- cin >> b;
- p.bicie(k, b);
- }
- }
- }
- else {
- p.zamiana();
- p.wyswietl();
- flaga = 0;
- cin >> k;
- cin >> b;
- p.przestaw(k, b);
- }
- } while (flaga == 0);
- system("cls");
- if(prz.czybic(&p)){
- cout << "bot bije" << endl;
- if (!prz.bicie(&p)) {
- cout << "problem z biciem" << endl;
- }
- }
- else {
- if (!prz.zaslon(&p)) {
- if (prz.ruszaj(&p)) {
- cout << "bot sie ruszyl" << endl;
- }
- else if (!prz.ruszaj2(&p)) {
- cout << "KONIEC" << endl;
- koniec = 1;
- }
- }
- }
- } while (koniec == 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement