> show canvas only <


/* built with Studio Sketchpad: 
 *   https://sketchpad.cc
 * 
 * observe the evolution of this sketch: 
 *   https://studio.sketchpad.cc/sp/pad/view/ro.HMVw0etDvjh/rev.1581
 * 
 * authors: 
 *   sarojaba
 *   
 *   Hyun
 *   eee
 *   

 * license (unless otherwise specified): 
 *   creative commons attribution-share alike 3.0 license.
 *   https://creativecommons.org/licenses/by-sa/3.0/ 
 */ 



int SIZE = 40;
int MAX_WALL = 10;

char[][] board = new char[SIZE][SIZE];
int cellWidth;
int cellHeight;

int RUNNING = 0;
int P1WIN = 1;
int P2WIN = 2;
int TIE = 4;

int state;

Player p1, p2;

void setup() {

  size(500, 500);
  frameRate(3);

  cellWidth = width / SIZE;
  cellHeight = height / SIZE;

  for (int r = 0; r < SIZE; r++) {
    for (int c = 0; c < SIZE; c++) {
      board[r][c] = '0';
    }
  }

  for (int i = 0; i < MAX_WALL; i++) {
    int r = (int) random(SIZE);
    int c = (int) random(SIZE);
    
    board[r][c] = '1';
  }

  p1 = new Player(SIZE / 2 - 1, 0, color(255, 0, 0));
  p2 = new Player(SIZE / 2 - 1, SIZE - 1, color(0, 0, 255));

  state = RUNNING;
}

void draw() {

  if (state == RUNNING) {
    drawRunning();
  } else if (state == TIE) {
    drawGameOver("TIE!");
  } else if (state == P1WIN) {
    drawGameOver("Player 1 Win!");
  } else if (state == P2WIN) {
    drawGameOver("Player 2 Win!");
  }
}

void drawRect(int r, int c, color c2) {
  fill(c2);
  rect(c * cellWidth, r * cellHeight, cellWidth, cellHeight);
}

void drawEllipse(int r, int c, color c2) {
  ellipseMode(CORNER);
  fill(c2);  
  ellipse(c * cellWidth, r * cellHeight, cellWidth, cellHeight);
}

void drawRunning() {

  background(255);
  
  for (int r = 0; r < SIZE; r++) {
    for (int c = 0; c < SIZE; c++) {
      if (board[r][c] == '0') {
        strokeWeight(1);
        drawRect(r, c, 255);
      } else if (board[r][c] == '1') {
        strokeWeight(1);
        drawRect(r, c, 0);
      } else if (board[r][c] == 'R') {
        strokeWeight(2);
        drawEllipse(r, c, p1.c2);
      } else if (board[r][c] == 'B') {
        strokeWeight(2);
        drawEllipse(r, c, p2.c2);
      } else if (board[r][c] == 'r') {
        strokeWeight(2);
        drawRect(r, c, p1.c2);
      } else if (board[r][c] == 'b') {
        strokeWeight(2);
        drawRect(r, c, p2.c2);
      }
    }
  }

  board[p1.r][p1.c] = 'r';
  board[p2.r][p2.c] = 'b';

  char dir1 = move1(board, p1, p2);
  char dir2 = move2(board, p2, p1);

  p1.update(dir1);
  p2.update(dir2);

  boolean p1Lose = lose(board, p1);
  boolean p2Lose = lose(board, p2);

  state = getState(p1Lose, p2Lose);

  if (state != RUNNING) {
    return;
  }

  board[p1.r][p1.c] = 'R';
  board[p2.r][p2.c] = 'B';
}

void drawGameOver(String t) {

  fill(0);
  textSize(96);
  textAlign(CENTER, CENTER);
  text(t, width / 2, height / 2);
}

int getState(boolean p1Lose, boolean p2Lose) {

  if (p1Lose && p2Lose) {
    return TIE;
  } else if (p1Lose) {
    return P2WIN;
  } else if (p2Lose) {
    return P1WIN;
  } else {
    return RUNNING;
  }
}

boolean lose(char[][] board, Player p) {

  if (p.r < 0 || p.r >= SIZE
    || p.c < 0 || p.c >= SIZE) {
    return true;
  }

  if (board[p.r][p.c] != '0') {
    return true;
  }

  return false;
}

class Player {
  int r;
  int c;
  color c2;

  Player(int r, int c, color c2) {
    this.r = r;
    this.c = c;
    this.c2 = c2;
  }

  void update(char dir) {
    switch(dir) {
    case 'N':
      r--;
      break;
    case 'S':
      r++;
      break;
    case 'W':
      c--;
      break;
    case 'E':
      c++;
      break;
    }
  }
}

/*
 * [Board]
 * 0: blank
 * 1: wall
 * R/r: Player1
 * B/b: Player2
 *
 * [Player]
 * r: row index (0 ~ SIZE-1)
 * c: col index (0 ~ SIZE-1)
 *
 * [Direction]
 * E: east
 * W: west
 * S: south
 * N: north
 */

char move1(char[][] board, Player bot, Player opp) {
int row = bot.r;
int col = bot.c;

if(board[row][col+1]!='1' && col<opp.c-2){
        return 'E';
        }
else{

        if(row==39){
            return 'W';
        }
     
        
    return 'S';
}



}

char move2(char[][] board, Player bot, Player opp) {
//상
if(bot.r - 1 >= 0 && board[bot.r - 1][bot.c] == '0'){
return 'N';
}

//좌
if(bot.c - 1 >= 0 && board[bot.r][bot.c - 1] == '0'){
return 'W';
}
//우
if(bot.c + 1 < SIZE && board[bot.r][bot.c + 1] == '0'){
return 'E';
}
//하
if(bot.r + 1 < SIZE && board[bot.r + 1][bot.c] == '0'){
return 'S';
}
}