2023年11月30日 星期四

Week 12 Lenovo滑鼠好爛

Week 12 10160783

  

----------------正式開始-------------------

第一節

分享台大老師對於不上課的看法(後續完成各自的期中作業)

這是week12-6 先放程式碼

程式碼:
char[][] board;
char currentPlayer = 'O';
boolean gameFinished = false;
boolean draw = false;
int playerOWins = 0,playerXWins = 0;;


void setup() {//設置場景大小
  size(600,600);
  initializeGame();
}

void draw() {//轉換場景
  if (!gameFinished) {
    drawGame();
  } else {
    if (draw) {
      drawDrawScene();
    } else {
      drawWinScene();
    }
  }
}

void mousePressed() {
  if (!gameFinished) {
    int row = mouseY / (height / 4);
    int col = mouseX / (width / 4);

    if (isValidMove(row, col)) {
      board[row][col] = currentPlayer;
      if (checkWin(row, col)) {
        gameFinished = true;
        switchPlayer();
        updateWins();
      } else if (checkDraw()) {
        gameFinished = true;
        draw = true;
      } else {
        switchPlayer();
      }
    }
  } else {
    initializeGame();
    gameFinished = false;
    draw = false;
  }
}

void drawGame() {
  background(#FFC6E3);
  drawBoard();
}

void drawBoard() {
  //設置線
  float cellWidth = width / 4;
  float cellHeight = height / 4;

  stroke(0);
  strokeWeight(4);

  // 畫橫線
  for (int i = 1; i < 4; i++) {
    line(0, i * cellHeight, width, i * cellHeight);
  }

  // 畫直線
  for (int i = 1; i < 4; i++) {
    line(i * cellWidth, 0, i * cellWidth, height);
  }

  // 畫"O"和"X"
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j++) {
      float centerX = j * cellWidth + cellWidth / 2;
      float centerY = i * cellHeight + cellHeight / 2;

      if (board[i][j] == 'O') { //O玩家
        drawO (centerX, centerY);
      } else if (board[i][j] == 'X') { //X玩家
        drawX (centerX, centerY);
      }
    }
  }
}

void drawO(float x, float y) {
  noFill();
  ellipse(x, y, 60, 60);
}

void drawX(float x, float y) {
  float offset = 20;
  line(x - offset, y - offset, x + offset, y + offset);
  line(x + offset, y - offset, x - offset, y + offset);
}

void initializeGame() {
  board = new char[4][4];
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j++) {
      board[i][j] = '-';
    }
  }
}

void switchPlayer() {
  currentPlayer = (currentPlayer == 'O') ? 'X' : 'O';
}

boolean isValidMove(int row, int col) {
  return board[row][col] == '-';
}

boolean checkWin(int row, int col) {
  // 檢查行
  if (board[row][0] == currentPlayer && board[row][1] == currentPlayer && board[row][2] == currentPlayer && board[row][3] == currentPlayer) {
    return true;
  }

  // 檢查列
  if (board[0][col] == currentPlayer && board[1][col] == currentPlayer && board[2][col] == currentPlayer && board[3][col] == currentPlayer) {
    return true;
  }

  // 檢查對角線
  if (row == col && board[0][0] == currentPlayer && board[1][1] == currentPlayer && board[2][2] == currentPlayer && board[3][3] == currentPlayer) {
    return true;22
  }

  if (row + col == 3 && board[0][3] == currentPlayer && board[1][2] == currentPlayer && board[2][1] == currentPlayer && board[3][0] == currentPlayer) {
    return true;
  }

  return false;
}

boolean checkDraw() {
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j++) {
      if (board[i][j] == '-') {
        return false; // 存在空格,遊戲未結束(未平局)
      }
    }
  }
  return true; // 所有空格都被填滿,遊戲平局
}

void drawWinScene() {// 有人贏了的畫面
  background(0, 255, 0);
  textSize(32);
  textAlign(CENTER, CENTER);
  fill(#2A3F50);
  text("Player " + currentPlayer + " Win Congratulations!", width / 2, height / 2);
  text("Click to play again", width / 2, height / 2 + 40);
  displayWins();
}

void drawDrawScene() {// 平手畫面
  background(#349AEA);
  textSize(32);
  textAlign(CENTER, CENTER);
  fill(#2A3F50); 
  text("That is Draw", width / 2, height / 2);
  text("Click to play again", width / 2, height / 2 + 50);
  displayWins();
}

void updateWins() {
  if (currentPlayer == 'O') {
    playerOWins ++;
  } else {
    playerXWins ++;
  }
}

void displayWins() {
  textSize(20);
  textAlign(CENTER, CENTER);
  fill(0);
  text("Player O Wins: " + playerOWins, width / 2, height - 30);
  text("Player X Wins: " + playerXWins, width / 2, height - 10);
}





沒有留言:

張貼留言