2023/05/11

bing chatに読ませる用

 peribit.js


const STONES = ['./revelatio/bellum.png', './revelatio/natura.png', './revelatio/societas.png'];

const BOARD_SIZE = 5;

let boardCells;

let belowBoardCells;

let turnCounter =0;

let playerCollectedStones = [];

let cpuCollectedStones = [];

let win = 0;

let deck = [];

let pround2score;

let cround2score;


// ステータスバーの値

let metus = 0;

let round = 1;

let stellaMetus = 0;


// 選択中の手札の石を保存するための変数

let selectedHandStone = null;


let playerexchanged = false;

let cpuexchanged = false;


//石交換ボタン

let exchangeMode = false;

let selectedStonesForExchange = [];


document.getElementById('exchange-button').addEventListener('click', () => {

  toggleExchangeMode()

  console.log('Exchange mode:', exchangeMode);

});


let cpuHand = [];


function initializeCPUHand() {

  const cpuHand = document.getElementById('cpu-hand');


   for (let i = 0; i < 3; i++) {

        const cpuHandStone = document.createElement('div');

        cpuHandStone.className = 'cell hand';

        cpuHandStone.style.backgroundImage = drawStone();

        cpuHand.appendChild(cpuHandStone);

    }

}




function shuffledeck(array) {

  for (let i = 0; i < 3; i++) {

      for (let j = 0; j < 12; j++) {

        array.push(STONES[i]);

      }

  }

  for (let i = array.length - 1; i > 0; i--) {

    const j = Math.floor(Math.random() * (i + 1));

    [array[i], array[j]] = [array[j], array[i]];

  }

}

// 山札から石を1つ取得する関数

function drawStone() {

  if (deck.length === 0) {

    // 山札が空の場合、何も返さない

    return;

  }

  const stone = deck.pop();

  return `url(${stone})`;

}


const board = document.getElementById('board');

const hand = document.getElementById('hand');


function createBoard() {

  for (let i = 0; i < 7; i++) {

    const row = document.createElement('div');

    row.className = 'row';

    for (let j = 0; j < 11; j++) {

      const cell = document.createElement('div');

      if (j === 5 && i < 6 && i > 0) {

        cell.className = 'cell';

        cell.style.backgroundImage = drawStone();

      } else {

        cell.className = 'empty-cell';

      }

      row.appendChild(cell);

    }

    board.appendChild(row);

  }

  boardCells = document.querySelectorAll('.board .row .cell');

  console.log('board',boardCells)

}



function refillBoardStones() {

  const boardRows = document.querySelectorAll('.board .row');

  boardRows.forEach((row, rowIndex) => {

    const centerCell = row.children[5]; // 中央列(6列目)のセルを取得

    if (rowIndex === 0 || rowIndex === 6) { // 1行目と7行目の場合

      if (centerCell.classList.contains('empty-cell')) {

        centerCell.classList.add('empty-cell');

        centerCell.classList.remove('cell');

        centerCell.style.backgroundImage = '';

      }

  

    } else if (centerCell.classList.contains('empty-cell')) { // 空のセルかどうかチェック

      centerCell.classList.add('cell'); // 空のセルから石を持つセルにクラスを変更

      centerCell.classList.remove('empty-cell'); // 空のセルから石を持つセルにクラスを変更

      centerCell.style.backgroundImage = drawStone();

    }

  });


  // すべてのセルを更新

  boardCells = document.querySelectorAll('.board .row .cell');


}


function createHand() {

    for (let i = 0; i < 3; i++) {

        const cell = document.createElement('div');

        cell.className = 'cell hand';

        cell.style.backgroundImage = drawStone();

        cell.onclick = function (event) {

            handleHandStoneClick(event);

        };

        hand.appendChild(cell);

    }

}


function highlightMatchingStones(playerStone) {

  if (exchangeMode){

    return

  }

  let hasMatchingStones = false;

  // ハイライトを全てリセット

  const highlightedCells = document.querySelectorAll(".highlight");

  highlightedCells.forEach((highlightedCell) => {

  highlightedCell.classList.remove("highlight");

  highlightedCell.onclick = null; // onclick イベントリスナーをリセット

});


  boardCells.forEach(cell => {

    let targetCell = cell.nextElementSibling;

    if (cell.style.backgroundImage === playerStone) {

      // 空のセルを見つけるまで右隣のセルをチェック

      while (

        targetCell !== null &&

        !targetCell.classList.contains("empty-cell")

      ) {

        targetCell = targetCell.nextElementSibling;

      }


      if (targetCell !== null) {

        targetCell.classList.add("highlight");

        hasMatchingStones = true;

        targetCell.onclick = function () {

          handleHighlightedCellClick(targetCell);

        };

      }

      

    } else {

      targetCell.classList.remove('highlight');

    }

    

  });

  //一致する石がない場合

    if(!hasMatchingStones) {

      const targetRow = document.querySelectorAll('.board .row')[6]; // 7行目を取得(0から数えるため、6を指定)

      const bottomCell = targetRow.children[5]; // 6列目のセルを取得(0から数えるため、5を指定)

      bottomCell.classList.add('highlight');

      bottomCell.onclick = function () {

      handleBottomCellClick(bottomCell);

      }     

    }

}



function playTurn(cell) {

    const playerStone = cell.style.backgroundImage;

    

    highlightMatchingStones(playerStone);


    

    // 選択中の石としてその石を保存

    selectedHandStone = cell;


}


//石捨て

async function handleBottomCellClick(cell) {

  if (selectedHandStone === null) {

    return;

  }

  // ハイライトを全てリセット

  const highlightedCells = document.querySelectorAll(".highlight");

  highlightedCells.forEach((highlightedCell) => {

  highlightedCell.classList.remove("highlight");

  highlightedCell.onclick = null; // onclick イベントリスナーをリセット

});

  const placeCell = cell;

  placeCell.classList.add('cell');

  placeCell.style.backgroundImage = selectedHandStone.style.backgroundImage;

  placeCell.classList.remove('empty-cell'); // 空のセルから石を持つセルにクラスを変更

    //CPU place stone

  const CPUplacedcell = CPUplaceStone();

    // 石を置いたタイミングで石を消す前に0.5秒待つ

  await new Promise(resolve => setTimeout(resolve, 500));

  CPUgetstone(CPUplacedcell); //CPU石を回収する

  //boardの開いた場所に石を移動

  const boardRows = document.querySelectorAll('.board .row');

  let stonePlaced = false;

  for (let rowIndex = 0; rowIndex < boardRows.length && !stonePlaced; rowIndex++) {

    if (rowIndex === 0 || rowIndex === 6) {

      continue;

    }

    const row = boardRows[rowIndex];

    const centerCell = row.children[5]; // 中央列(6列目)のセルを取得


    if (centerCell.classList.contains('empty-cell')) { // 空のセルかどうかチェック

      centerCell.classList.add('cell'); // 空のセルから石を持つセルにクラスを変更

      centerCell.classList.remove('empty-cell'); // 空のセルから石を持つセルにクラスを変更

      centerCell.style.backgroundImage = selectedHandStone.style.backgroundImage;

      stonePlaced = true;

    }

  }

  // すべてのセルを更新

  boardCells = document.querySelectorAll('.board .row .cell');

  placeCell.style.backgroundImage = ''; 

  placeCell.className = 'empty-cell'; // クラスを空のセルに戻す

  selectedHandStone.style.backgroundImage = '';

  selectedHandStone.remove();


  

  updateCollectedStones();

  endTurn();


  

}


// ハイライトされた場所をクリックしたときの処理を追加

async function handleHighlightedCellClick(cell) {

  if (selectedHandStone === null) {

    return;

  }



  // ハイライトを全てリセット

  const highlightedCells = document.querySelectorAll(".highlight");

  highlightedCells.forEach((highlightedCell) => {

  highlightedCell.classList.remove("highlight");

  highlightedCell.onclick = null; // onclick イベントリスナーをリセット

});


  const placeCell = cell;

  placeCell.classList.add('cell');

  placeCell.style.backgroundImage = selectedHandStone.style.backgroundImage;

  placeCell.classList.remove('empty-cell'); // 空のセルから石を持つセルにクラスを変更


  //CPU place stone

  const CPUplacedcell = CPUplaceStone();


   selectedHandStone.style.backgroundImage = '';

   selectedHandStone.remove();


  // 石を置いたタイミングで石を消す前に0.5秒待つ

  await new Promise(resolve => setTimeout(resolve, 500));


  //置いた列

  const row = cell.parentElement;

  const rowIndex = Array.from(row.parentElement.children).indexOf(row);

  console.log('playerrow',rowIndex)

  

  //衝突

  if (CPUplacedcell !== rowIndex){


  

    let collectedStonesCount = 0;

      // 石を置いた行の左側から、石を回収して空のセルに変更する

    const boardRows = document.querySelectorAll('.board .row');

    const targetRow = boardRows[rowIndex];


    for (let i = 0; i < 11; i++) {

      const currentCell = targetRow.children[i];

      if (currentCell.classList.contains("cell")) {

          playerCollectedStones.push(currentCell.style.backgroundImage);

          collectedStonesCount++;

        currentCell.style.backgroundImage = ""; // 石を回収した後、元の場所をクリア

        currentCell.className = "empty-cell";

      }

    }


    if (collectedStonesCount === 2) {

      metus ++;

    } else if (collectedStonesCount > 2) {

      metus += collectedStonesCount;

    }


    console.log('multa',playerCollectedStones)

    

    //CPUも石を回収する

    CPUgetstone(CPUplacedcell);

  }


  updateCollectedStones();

  endTurn();



  


}



//手札とボード補充

function hojyu() {

  const hand = document.querySelector('#hand');

  const currentHandSize = hand.childElementCount;

  console.log("myhandcount: ", currentHandSize);


  // 足りない石の数だけ追加する

  for (let i = currentHandSize; i < 3; i++) {

    const newStone = document.createElement("div");

    newStone.className = "cell hand";

    newStone.style.backgroundImage = drawStone();

    newStone.onclick = function (event) {

      handleHandStoneClick(event)

    };

    hand.appendChild(newStone);

  }

     // 場の石を補充

  refillBoardStones();


        // CPUの手札を補充

  const cpuHandElement = document.getElementById('cpu-hand');

  const currentCpuHandSize = cpuHandElement.childElementCount;

  console.log("CPUhand: ", currentCpuHandSize);

  for (let i = currentCpuHandSize; i < 3; i++) {

    const newCpuStone = document.createElement('div');

    newCpuStone.className = 'cell hand';

    newCpuStone.style.backgroundImage = drawStone();

    cpuHandElement.appendChild(newCpuStone);

    }

    playerexchanged=false;

    cpuexchanged = false; 


}



  //CPUが石を選んで置く

function CPUplaceStone() {

  const cpuHandStones = document.querySelectorAll('#cpu-hand .cell');

  let CPUplacedStone = false;

  let CPUplacedcell;


  //①場に衝突が起こっている場合、衝突している石と同じ石が手札にあれば、衝突しているセルの左側のempty-cellに置く

  const collisionCells = Array.from(document.querySelectorAll('.board .row .cell:nth-child(5)')).filter(

  (cell) => cell.classList.contains('cell')

  );

  console.log('collisioncell',collisionCells)


  if (collisionCells.length > 0) {

    for (const collisionCell of collisionCells) {

      const collisionStone = collisionCell.style.backgroundImage;

      const matchingStoneInHand = Array.from(cpuHandStones).find(

        (stone) => stone.style.backgroundImage === collisionStone

      );


      if (matchingStoneInHand) {

        let leftCell = collisionCell.previousElementSibling;

        while (leftCell && leftCell.className !== 'empty-cell') {

          leftCell = leftCell.previousElementSibling;

        }


        if (leftCell) {

          leftCell.style.backgroundImage = matchingStoneInHand.style.backgroundImage;

          leftCell.className = 'cell';

          CPUplacedStone = true;

          // rowIndexを取得する

          const row = leftCell.parentElement;

          const rowIndex = Array.from(row.parentElement.children).indexOf(row);          

          CPUplacedcell = rowIndex;

          console.log('CPUplacedconcul',CPUplacedcell)

          matchingStoneInHand.remove();

          break;

        }

      }

    }

  }


  // ②場の石の中で一番少ない石のところを選んで、左のempty-cellに石を置く

  if (!CPUplacedStone) {

    const boardStoneCounts = countStones(

      Array.from(document.querySelectorAll('.board .row .cell')).map((cell) => cell.style.backgroundImage)

    );

    const minCountStone = Object.keys(boardStoneCounts).reduce((a, b) =>

      boardStoneCounts[a] < boardStoneCounts[b] ? a : b

    );


    for (let rowIndex = 1; rowIndex <= 5 && !CPUplacedStone; rowIndex++) {

      const targetRow = document.querySelectorAll('.board .row')[rowIndex];

      const targetCell = targetRow.children[4];

      const selectedCell = targetCell.nextElementSibling;


      if (selectedCell.style.backgroundImage === minCountStone) {

        const matchingStoneInHand = Array.from(cpuHandStones).find(

          (stone) => stone.style.backgroundImage === minCountStone

        );


        if (matchingStoneInHand) {

          targetCell.style.backgroundImage = matchingStoneInHand.style.backgroundImage;

          targetCell.className = 'cell';

          CPUplacedStone = true;

          CPUplacedcell = rowIndex;

          matchingStoneInHand.remove();

        }

      }

    }

  }


  // ③上側から探して置ける場所の左のempty-cellに石を置く

  if (!CPUplacedStone) {

    for (const handStone of cpuHandStones) {

      for (let rowIndex = 1; rowIndex <= 5 && !CPUplacedStone; rowIndex++) {

        const targetRow = document.querySelectorAll('.board .row')[rowIndex];

        const targetCell = targetRow.children[4];

        const selectedCell = targetCell.nextElementSibling;


        if (selectedCell.style.backgroundImage === handStone.style.backgroundImage) {

          targetCell.style.backgroundImage = handStone.style.backgroundImage;

          targetCell.className = 'cell';

          CPUplacedStone = true;

          CPUplacedcell = rowIndex;

          handStone.remove();

        }

      }

    }

  }


  // ④手札における石がない場合、exchangeCPUhand()をする。そして交換後の石をおけるかを①から③の順に再度検証

  if (!CPUplacedStone && !cpuexchanged) {

    exchangeCPUhand();

    return CPUplaceStone();

  }


  if(!CPUplacedStone) {

    const selectedStone = cpuHandStones[0];

    const targetRow = document.querySelectorAll('.board .row')[0]; 

    const topCell = targetRow.children[5]; // 6列目のセルを取得(0から数えるため、5を指定)

    topCell.style.backgroundImage = selectedStone.style.backgroundImage; // 石を置く

    selectedStone.remove();

    CPUplacedcell = 0;

    console.log('topcell',topCell);

  }

  console.log('CPUplaced?',CPUplacedcell);

  return CPUplacedcell

}


function exchangeCPUhand() {

  const cpuHandStones = document.querySelectorAll('#cpu-hand .cell');


  // 手札の前から2つの石を選ぶ

  const firstStone = cpuHandStones[0];

  const secondStone = cpuHandStones[1];


  const stoneImageUrl1 = firstStone.style.backgroundImage;

  deck.splice(0, 0, stoneImageUrl1.slice(5, -2));


  const stoneImageUrl2 = secondStone.style.backgroundImage;

  deck.splice(0, 0, stoneImageUrl2.slice(5, -2));


  // 選んだ石を新しいランダムな石と交換する

  firstStone.style.backgroundImage = drawStone();

  secondStone.remove();

  cpuexchanged = true; 

}


function CPUgetstone(rowIndex) {

  // 石を置いた行の左側から、石を回収して空のセルに変更する

    const boardRows = document.querySelectorAll('.board .row');

    const targetRow = boardRows[rowIndex];

    const targetStone = targetRow.children[5];


    if (rowIndex !==0) {

      let collectedStonesCount = 0;


      for (let i = 0; i < 11; i++) {

        const currentCell = targetRow.children[i];

        if (currentCell.classList.contains("cell")) {

            cpuCollectedStones.push(currentCell.style.backgroundImage);

          currentCell.style.backgroundImage = ""; // 石を回収した後、元の場所をクリア

          collectedStonesCount++;

          currentCell.className = "empty-cell";

        }

      }

      if (collectedStonesCount === 2) {

        metus ++;

      } else if (collectedStonesCount > 2) {

        metus += collectedStonesCount;

      }

    } else {

      

    //boardの開いた場所に石を移動

    let stonePlaced = false;

    for (let rowInd = 1; rowInd < boardRows.length -1 && !stonePlaced; rowInd++) {


      const row = boardRows[rowInd];

      const centerCell = row.children[5]; // 中央列(6列目)のセルを取得


      if (centerCell.classList.contains('empty-cell')) { // 空のセルかどうかチェック

        centerCell.classList.add('cell'); // 空のセルから石を持つセルにクラスを変更

        centerCell.classList.remove('empty-cell'); // 空のセルから石を持つセルにクラスを変更

        centerCell.style.backgroundImage = targetStone.style.backgroundImage;

        stonePlaced = true;

      }

    }

    // すべてのセルを更新

    boardCells = document.querySelectorAll('.board .row .cell');

    targetStone.style.backgroundImage = ''; 

    targetStone.className = 'empty-cell'; // クラスを空のセルに戻す


    }

    console.log('cpumulta',cpuCollectedStones)

}



function updateCollectedStones() {

  const playerCollectedStonesElement = document.getElementById('player-collected-stones');

  const cpuCollectedStonesElement = document.getElementById('cpu-collected-stones');

  

  playerCollectedStonesElement.innerHTML = '';

  cpuCollectedStonesElement.innerHTML = '';


  playerCollectedStones.forEach(stone => {

    const stoneElement = document.createElement('div');

    stoneElement.className = 'collected-stone';

    stoneElement.style.backgroundImage = stone;

    playerCollectedStonesElement.appendChild(stoneElement);

  });


  cpuCollectedStones.forEach(stone => {

    const stoneElement = document.createElement('div');

    stoneElement.className = 'collected-stone';

    stoneElement.style.backgroundImage = stone;

    cpuCollectedStonesElement.appendChild(stoneElement);

  });

}


//石交換

// 交換モードのステータスをトグルする関数

function toggleExchangeMode() {

    const hand = document.querySelector('#hand');

    const currentHandSize = hand.childElementCount;


    // ハイライトを全てリセット

  const highlightedCells = document.querySelectorAll(".highlight");

  highlightedCells.forEach((highlightedCell) => {

  highlightedCell.classList.remove("highlight");

  highlightedCell.onclick = null; // onclick イベントリスナーをリセット

});


  if (exchangeMode) {

    const selectedStones = document.querySelectorAll(".hand.exchange-selected");

    

    if (selectedStones.length==2) {

    selectedStones.forEach(stone => {

      const stoneImageUrl = stone.style.backgroundImage;

      deck.splice(0, 0, stoneImageUrl.slice(5, -2));

      hand.removeChild(stone);

      

    });

    console.log('Deck size:', deck);

    

    // 新しい石を一つ追加する

      const newCell = document.createElement("div");

      newCell.className = "cell hand";

      newCell.style.backgroundImage = drawStone();

      newCell.onclick = function (event) {

        handleHandStoneClick(event);

      };

      hand.appendChild(newCell);

    

    exchangeMode = false;

    document.getElementById("exchange-mode-text").style.display = "none";

    playerexchanged = true;


    } else {

      exchangeMode = false;

      document.getElementById("exchange-mode-text").style.display = "none";

    }

  } else if (currentHandSize >= 2 && !playerexchanged) {

    exchangeMode = true;

    document.getElementById("exchange-mode-text").style.display = "inline";

  }

}


let exchangeSelectedStones = []; // 交換用の選択された石を格納する配列


// ハンドストーンがクリックされたときの処理

function handleHandStoneClick(event) {

  const selectedStone = event.target;

  if (exchangeMode) {

    if (selectedStone.classList.contains("exchange-selected")) {

      // 既に選択されている石の選択を解除

      selectedStone.classList.remove("exchange-selected");

      exchangeSelectedStones = exchangeSelectedStones.filter((stone) => stone !== selectedStone);

    } else {

      // 2つの石が選択されている場合、最初に選択された石の選択を解除

      if (exchangeSelectedStones.length === 2) {

        const firstSelectedStone = exchangeSelectedStones.shift();

        firstSelectedStone.classList.remove("exchange-selected");

      }

      // 新たに選択された石を選択状態にする

      selectedStone.classList.add("exchange-selected");

      exchangeSelectedStones.push(selectedStone);

    }

  } else {

    playTurn(event.target);

  }

}



function handleCollision() {

    // 衝突が発生したときの処理をここに追加

}


function endTurn() {

  updateMetus(metus);

  if (metus<12) {


    // ターンカウンターをインクリメント

    turnCounter++;


       // ターンカウンターが2の倍数のとき、手札を補充

    if (turnCounter > 0 && turnCounter % 2 == 0) {

      hojyu()    

    }

    console.log('Turn ended. Turn counter:', turnCounter);

  } else {

    

    endGame()

  }

}



function updateMetus(value) {

  const metusElement = document.getElementById("metus");

  metusElement.textContent = `Metus: ${value} /12`;

}


function updateRound(value) {

  const roundElement = document.getElementById("round");

  roundElement.textContent = `Round: ${value}`;

}


function updateScore(value1,value2) {

  const scoreElement = document.getElementById("score");

  scoreElement.textContent = `Score: ${value1} - ${value2}`;

}


function updateStellaMetus(value) {

  const stellaMetusElement = document.getElementById("stella-metus");

  stellaMetusElement.textContent = `Stella Metus: ${value}`;

}


let playerscore = 0;

let CPUscore = 0;


async function endGame() {


  const pscore = calculateScore(playerCollectedStones);

  playerscore += pscore;

  const cscore = calculateScore(cpuCollectedStones);

  CPUscore += cscore;

  console.log('playerscore',playerscore); 

  console.log('cpuscore',CPUscore); 

  

  // 勝ち負けの判断とメッセージ表示

  const modal = document.getElementById("modal");

  const endGameMessage = document.getElementById("end-game-message");

  const cpuScoreElement = document.getElementById("cpu-score");

const playerScoreElement = document.getElementById("player-score");

cpuScoreElement.innerHTML = `CPU: ${cscore}`;

playerScoreElement.innerHTML = `You: ${pscore}`;

await new Promise(resolve => setTimeout(resolve, 1000));


  if (pscore > cscore) {

    endGameMessage.innerHTML = "You Win!";

    win += 1;


  } else if (pscore == cscore) {

    endGameMessage.innerHTML = "Draw";

  }else{

    endGameMessage.innerHTML = "You Lose...";

    win -=1;

  }

  

  modal.classList.remove("hidden");

  console.log(endGameMessage)


  // 1秒後にメッセージを非表示にする

  // setTimeout(() => {

  

  // }, 1500);

  await new Promise(resolve => setTimeout(resolve, 1500));

  modal.classList.add("hidden");


  if (round==3) {

    pround2score = playerscore - pscore;

    cround2score = CPUscore - cscore;


    finalstellaMetus();


  }


  const board = document.getElementById("board");


  // ボードの子要素をすべて削除

  while (board.firstChild) {

    board.removeChild(board.firstChild);

  }


  const hand = document.getElementById("hand");


  // 手札の子要素をすべて削除

  while (hand.firstChild) {

    hand.removeChild(hand.firstChild);

  }

  const cpuHand = document.getElementById('cpu-hand');

  // 手札の子要素をすべて削除

  while (cpuHand.firstChild) {

    cpuHand.removeChild(cpuHand.firstChild);

  }



  shuffledeck(deck);


  createBoard();

  createHand();

  initializeCPUHand();

  playerCollectedStones = [];

  cpuCollectedStones = [];

   updateCollectedStones();


   round++;

  updateRound(round);

  startround();

  metus = metus-12;

  stellaMetus += metus;

  updateMetus(metus);

  updateStellaMetus(stellaMetus);

  updateScore(CPUscore,playerscore)



}


async function finalstellaMetus() {

  if (stellaMetus > 0){

    const stellaModal = document.getElementById("modal-stella");

    const stellaTitle = document.getElementById("stella-title");

    // const StellaMessage = document.getElementById("stella");

    // stella.innerHTML = "Stella Metus";

    stellaModal.classList.remove("hidden");

    stellaTitle.classList.remove("hidden");

    for (let i = 0; i < stellaMetus; i++) {

        const cell = document.createElement('div');

        cell.className = 'cell';

        cell.style.backgroundImage = drawStone();

        

        stella.appendChild(cell);

        if (win > 0) {

          playerCollectedStones.push(cell.style.backgroundImage);

        } else if (win <0){

          cpuCollectedStones.push(cell.style.backgroundImage);

        }

    }

    await new Promise(resolve => setTimeout(resolve, 2000));

    stellaModal.classList.add("hidden");

    updateCollectedStones()

    const pscore = calculateScore(playerCollectedStones);

    playerscore = pround2score + pscore;

    const cscore = calculateScore(cpuCollectedStones);

    CPUscore = cround2score + cscore;

    console.log('playerscore',playerscore); 

    console.log('cpuscore',CPUscore); 

  }


  // 勝ち負けの判断とメッセージ表示

  const modal = document.getElementById("modal");

  const endGameMessage = document.getElementById("end-game-message");

  modal.classList.remove("hidden");

  if (playerscore > CPUscore) {

    endGameMessage.innerHTML = "You Win!";

    win += 1;


  } else if (playerscore == CPUscore) {

    endGameMessage.innerHTML = "Draw";

  }else{

    endGameMessage.innerHTML = "You Lose...";

    win -=1;

  }

}



function calculateScore(CollectedStones) {

  const stoneCounts = countStones(CollectedStones);

  console.log('stonecounts', stoneCounts); // 石の種類ごとのカウントが格納されたオブジェクトが表示されます

  let score = 0;

  


   // 3種類の石がそれぞれ4個ずつの場合

  if (

    Object.keys(stoneCounts).length === 3 &&

    Object.values(stoneCounts).every((count) => count >= 4)

  ) {

    score += 15;

    for (const stone in stoneCounts) {

      stoneCounts[stone] -= 4;

    }

  }

  // 同じ石10個の場合

  for (const stone in stoneCounts) {

    if (stoneCounts[stone] >= 10) {

      score += 10;

      stoneCounts[stone] -= 10;

    }

  }


  // 同じ石7個の場合

  for (const stone in stoneCounts) {

    if (stoneCounts[stone] >= 7) {

      score += 6;

      stoneCounts[stone] -= 7;

    }

  }


  // 3種類の石がそれぞれ3個ずつの場合

  if (

    Object.keys(stoneCounts).length === 3 &&

    Object.values(stoneCounts).every((count) => count >= 3)

  ) {

    score += 9;

  for (const stone in stoneCounts) {

      stoneCounts[stone] -= 3;

    }

  }


  // 3種類の石がそれぞれ2個ずつの場合

  if (

    Object.keys(stoneCounts).length === 3 &&

    Object.values(stoneCounts).every((count) => count >= 2)

  ) {

    score += 5;

  for (const stone in stoneCounts) {

      stoneCounts[stone] -= 2;

    }

  }


  // 3種類の石がそれぞれ1個ずつの場合

  if (

    Object.keys(stoneCounts).length === 3 &&

    Object.values(stoneCounts).every((count) => count >= 1)

  ) {

    score += 2;

  for (const stone in stoneCounts) {

      stoneCounts[stone] -= 1;

    }

  }


  // 同じ石3個の場合

  for (const stone in stoneCounts) {

    if (stoneCounts[stone] >= 3) {

      score += 1;

      stoneCounts[stone] -= 3;

    }

  }



  console.log('score',score)

  return score;

}


function countStones(stones) {

  const stoneCounts = {};


  stones.forEach(stone => {

    const stoneType = stone;

    if (stoneCounts[stoneType]) {

      stoneCounts[stoneType]++;

    } else {

      stoneCounts[stoneType] = 1;

    }

  });


  return stoneCounts;

}

function startround(){

  if (round<4){

    //ラウンドの開始時にモーダルを表示する

    const roundModal = document.getElementById("round-modal");

    const roundMessage = document.getElementById("round-message");

    roundMessage.textContent = `Round: ${round}`

    // roundMessage.innerHTML = "Round ${round}";

    roundModal.classList.remove("hidden");

    console.log(roundMessage);


    //1秒後にモーダルを非表示にする

    setTimeout(() => {

      roundModal.classList.add("hidden");

    }, 1500);

    }

}


// 初期化

shuffledeck(deck);

createBoard();

createHand();

startround();

initializeCPUHand();

2010/06/26

バックアップ失敗かと思った

ubuntu10.04 バックアップからの復帰に失敗。deja-dup使用。コマンドラインのデバッグ情報から直接duplicityを叩いたところ上手く行ってるっぽい。まだ途中なんで安心はできないけど。

2010/02/16

おひさ

よく消えないなこれ
google太っ腹だな

2009/07/07

楽しい夏休み

再試コンプまであと1つ・・・!
俺の戦いにご期待ください!

2009/06/02

おいィ?

携帯から投降テスト

test

あ~あ~なんか3年ぶりぐらいの投稿になるんだが
これでいけるんだろうか?
ていうか生きてたんだなこのアカウント。

2006/07/08

自分イメージ。