Advertisement
DontCallMeNuttoPleas

Reversi <Implementation>

Mar 24th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int board[8][8];
  5. int mode[8][2]={{1,0},{-1,0},{0,1},{0,-1},{1,1},{-1,-1},{-1,1},{1,-1}};
  6. int m,posx,posy;
  7. bool checknot(int x,int y,int place){
  8.     if(x<0&&x==8&&y<0&&y==8) return false;
  9.     if(board[x][y]==0) return false;
  10.     if(board[x][y]==place){
  11.         posx=x;
  12.         posy=y;
  13.         return true;
  14.     }
  15.     else return checknot(x+mode[m][0],y+mode[m][1],place);
  16. }
  17. void replace(int place){
  18.     posx-=mode[m][0];
  19.     posy-=mode[m][1];
  20.     if(board[posx][posy]!=place){
  21.         board[posx][posy]=place;
  22.     }else return;
  23.     replace(place);
  24. }
  25.  
  26. int main(){
  27.     int n;
  28.     char c;
  29.     int x,y;
  30.     //White = 2 Black =1;
  31.     board[3][3]=2;
  32.     board[4][4]=2;
  33.     board[3][4]=1;
  34.     board[4][3]=1;
  35.     scanf("%d",&n);
  36.     for(int i=1;i<=n;i++){
  37.         cin >> c >> x;
  38.         x--;
  39.         y=c-'a';
  40.         int u=i%2;
  41.         if(u==0) u=2;
  42.         board[x][y]=u;
  43.         for(int j=0;j<8;j++){
  44.             m=j;
  45.             if(checknot(x+mode[j][0],y+mode[j][1],u)){
  46.                 replace(u);
  47.             }
  48.         }
  49.     }
  50.     for(int i=0;i<8;i++){
  51.         for(int j=0;j<8;j++){
  52.             if(board[i][j]==0){
  53.                 printf(".");
  54.             }else if(board[i][j]==1){
  55.                 printf("X");
  56.             }else{
  57.                 printf("O");
  58.             }
  59.         }
  60.         printf("\n");
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement