Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. using namespace std;
  4.  
  5. char **board;
  6. int borderSize, columnCounts;
  7. string spaces = "   ";
  8.  
  9. struct Point{
  10.     int x, y;
  11. };
  12.  
  13. int intLength(int x){
  14.     int n=1;
  15.     while ((x/=10) > 0) n++;
  16.     return n;
  17. }
  18.  
  19. void drawHeader(){
  20.     cout << spaces;
  21.     for(int i=0;i<borderSize;i++){
  22.         cout << char('A'+i) << " ";
  23.     }
  24.     cout << endl;
  25. }
  26.  
  27. void draw(){
  28.     drawHeader();
  29.    
  30.     for(int i=0;i<borderSize;i++){
  31.         cout << (borderSize-i) << spaces.substr(intLength(borderSize-i));
  32.        
  33.         for(int j=0;j<borderSize;j++){
  34.             if(board[i][j]==0){
  35.                 if((i+j)%2==0)
  36.                     cout << "  ";
  37.                 else
  38.                     cout << "██";
  39.             }else if(board[i][j] == 1)
  40.                 cout << "<>";
  41.             else
  42.                 cout << "@@";
  43.         }
  44.         cout << spaces.substr(intLength(borderSize-i)) << (borderSize-i);
  45.         cout << endl;
  46.     }
  47.    
  48.     drawHeader();
  49. }
  50.  
  51. Point stringToPoint(string s){
  52.     Point p;
  53.     p.x = s[0]-'A';
  54.     p.y = borderSize-stoi(s.substr(1));
  55.     return p;
  56. }
  57.  
  58. bool move(int player, Point from, Point to){
  59.     if(board[from.y][from.x]==player && abs(from.x-to.x)==1 && abs(from.y-to.y)==1 && board[to.y][to.x]==0){
  60.         board[from.y][from.x] = 0;
  61.         board[to.y][to.x] = player;
  62.         return true;
  63.     }else if(board[from.y][from.x]==player && abs(from.x-to.x)==2 && abs(from.y-to.y)==2 && board[to.y][to.x]==0 && board[(from.y+to.y)/2][(from.x+to.x)/2]!=player){
  64.         board[from.y][from.x] = 0;
  65.         board[(from.y+to.y)/2][(from.x+to.x)/2] = 0;
  66.         board[to.y][to.x] = player;
  67.         return true;
  68.     }
  69.     return false;
  70. }
  71.  
  72. int main()
  73. {
  74.     cout << "Введите размер доски:" << endl;
  75.     cin >> borderSize;
  76.     borderSize = min(borderSize, 26);
  77.     cout << "Введите количество рядов шашек:" << endl;
  78.     cin >> columnCounts;
  79.     columnCounts = min(columnCounts, borderSize/2);
  80.     string log = "";
  81.    
  82.     board = new char*[borderSize];
  83.     for(int i=0;i<borderSize;i++){
  84.         board[i] = new char[borderSize];
  85.         for(int j=0;j<borderSize;j++){
  86.             board[i][j] = 0;
  87.             if((i+j)%2 == 1){
  88.                 if(i<columnCounts)
  89.                     board[i][j] = 1;
  90.                 else if((borderSize-i)<=columnCounts)
  91.                     board[i][j] = 2;
  92.             }
  93.         }
  94.     }
  95.     int player = 1;
  96.     while(true){
  97.         draw();
  98.         cout << "Ход игрока №" << (player) << endl;
  99.         string from, to;
  100.         cin >> from;
  101.        
  102.         if(from == "stop"){
  103.             cout << log << endl;
  104.             break;
  105.         }
  106.         cin >> to;
  107.        
  108.         if(move(player, stringToPoint(from), stringToPoint(to))){
  109.             log += "Игрок ";
  110.             log += (player+'0');
  111.             log += ": " + from + "->" + to + "\n";
  112.             player = (player%2) + 1;
  113.         }
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement