Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Feature function.js (feature-function.js) #2

Merged
merged 21 commits into from Apr 1, 2024

Conversation

yko-git
Copy link
Owner

@yko-git yko-git commented Mar 20, 2024

function.jsの編集 (feature-function.js)

@yko-git yko-git self-assigned this Mar 20, 2024
@yko-git yko-git changed the title add Feature function.js add Feature function.js (feature-function.js) Mar 20, 2024
board.classList.add("set");
};

//リセットボタンでリロード
Copy link

@version-1 version-1 Mar 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const itemsItem = document.querySelectorAll(".items-item");
const board = document.querySelector(".l-board");
const jsCell = document.querySelectorAll(".js-cell");
const state = document.querySelector(".js-state");
const jsReset = document.querySelector(".btn-restart");
const winPatterns = [
  [0, 1, 2],
  [3, 4, 5],
  [6, 7, 8],
  [0, 3, 6],
  [1, 4, 7],
  [2, 5, 8],
  [0, 4, 8],
  [2, 4, 6],
];

const cells = ['', '', '', '', '', '', '', '', '']

const countFunc = (() => {
  let count = 0;

  return {
    inner: () => {
      return count++;
    },
  };
})();

// イベントの登録
jsCell.forEach(function (cell, index) {
  cell.addEventListener("click", () => {
    if (cells[index]) {
       return
    }
    
    const count = countFunc.inner();
    
    const circleTurn = count % 2 !== 0
    const inputValue = circleTurn ? '◯' : 'x'
    
    ele.innerHTML = inputValue
    cells[index] = inputValue
    
    toggleTurn(itemsItem);

    const judged = judge(cells);
    
    if (judged) {
       message(inputValue)
    }
    
     // 揃わずに全てのセルが埋まったら メッセージをdrawに
     if (countFunc.inner() === 9) {
       state.innerHTML = "draw";
     }
  });
});

// 判定
const judge = (judgeCell) => {
  for (let i = 0; i < winPatterns.length; i++) {
    if (
      judgeCell[winPatterns[i][0]] === "○" &&
      judgeCell[winPatterns[i][1]] === "○" &&
      judgeCell[winPatterns[i][2]] === "○"
    ) {
      return true;
    }
    
    if (
      judgeCell[winPatterns[i][0]] === "×" &&
      judgeCell[winPatterns[i][1]] === "×" &&
      judgeCell[winPatterns[i][2]] === "×"
    ) {
      return true;
    }
  }
  
  return false
};

// セルをクリックするごとにitemsのactiveを切り替える
const toggleTurn = (items) => {
  for (let item of items) {
    item.classList.toggle("is-active");
  }
};

// どちらかが揃ったら メッセージをwinに
const message = (mark) => {
  state.innerHTML = `${mark} win`;
};

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@version-1 相談させていただけますでしょうか。
ele.innerHTML = inputValue
はどういった意図で設定しているのかわからず教えていただけますでしょうか。
eleはjsCellのforEachの引数celをイメージしているのですが、
cell.innerHTML = inputValue;
だと動きませんでした。

Copy link

@version-1 version-1 Mar 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

すみません。 ご指摘のように

cell.innerHTML = inputValue

が正しいです。

ここを直しても全ては動かず他にも誤りがありますね。。。

ちょうど良いデバックの教材かと思いますので、どこが良くないのか見つけて修正できると良いと思います。ポイントとしては、「Jiro がここを触っていたからここかな?」という探し方よりはdebugger を使って、「この行ではこの値がこうなっているからここが間違っている」というように原因を特定できると良いです。

js/function.js Outdated
state.innerHTML = "draw";
}

// 揃わずに全てのセルが埋まったら メッセージをdrawに

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コメントの場所が違いますね。

js/function.js Outdated
};

const doneFunc = (target) => {
target.classList.toggle("set");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ゲームが終わっているかは dom に情報を残すのではなく、変数で定義してあげると良いかと思います。

js/function.js Outdated Show resolved Hide resolved
@yko-git yko-git merged commit ef662fd into develop Apr 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants