Simple website with some JavaScript games.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
4.0 KiB

  1. var btns = document.querySelectorAll('button');
  2. var score=document.getElementsByClassName("player");
  3. var XO = 'x';
  4. var win1=0;
  5. var win2=0;
  6. var NoOneWon=0;
  7. var player=[];
  8. var AI=[];
  9. for(i = 0; i<btns.length; i++){
  10. btns[i].onclick = function(){
  11. if( ! this.classList.contains('unclickable')){
  12. player.push(this.attributes.id);
  13. this.textContent = XO;
  14. this.style.fontSize = '20px';
  15. this.classList.add('unclickable');
  16. NoOneWon++;
  17. if(XO==='x'){
  18. XO = 'o';
  19. }else{XO = 'x';}
  20. if(checkWinner(player)){alert('palyer 1 won'); init();
  21. win1++;
  22. score[0].firstElementChild.textContent = "player 1: "+win1;
  23. }
  24. againstAi(); //error at the last click of the 1st playerX but doesn't matter :/
  25. NoOneWon++;
  26. if(NoOneWon==9){
  27. alert('no One Won :|');init();
  28. }
  29. }//onclick end
  30. }
  31. }
  32. function againstAi(){
  33. var emptyList= new Array();
  34. for(i = 0; i<btns.length ; i++){
  35. if( ! btns[i].classList.contains('unclickable')){
  36. emptyList.push(btns[i]);
  37. }
  38. }
  39. /*for(i = 0; i<emptyList.length ; i++){
  40. console.log(emptyList[i]);
  41. }*/
  42. //random btn
  43. var random=Math.floor(Math.random() *emptyList.length);
  44. AI.push(emptyList[random].attributes.id);
  45. emptyList[random].textContent=XO
  46. emptyList[random].style.fontSize = '20px';
  47. emptyList[random].classList.add('unclickable');
  48. // console.log(random);
  49. if(XO==='x'){
  50. XO = 'o';
  51. }else{XO = 'x';}
  52. if(checkWinner(AI)){alert('player 2 won ');init();
  53. win2++;
  54. score[0].firstElementChild.textContent = "player 1: "+win2;
  55. }
  56. }//againstAi end
  57. function checkWinner(playerX){
  58. if((playerX.indexOf( btnId( 0 ) ) !=-1 && playerX.indexOf( btnId( 1 ) ) !=-1 && playerX.indexOf( btnId( 2 ) ) !=-1) ){
  59. //console.log(playerX.indexOf( btnId( 5 ) ) !=-1)
  60. return true;
  61. }
  62. else if((playerX.indexOf( btnId( 3 ) ) !=-1 && playerX.indexOf( btnId( 4 ) ) !=-1 && playerX.indexOf( btnId( 5 ) ) !=-1)){
  63. return true;
  64. }
  65. else if((playerX.indexOf( btnId( 6 ) ) !=-1 && playerX.indexOf( btnId( 7 ) ) !=-1 && playerX.indexOf( btnId( 8 ) ) !=-1)){
  66. return true;
  67. }
  68. else if((playerX.indexOf( btnId( 0 ) ) !=-1 && playerX.indexOf( btnId( 3 ) ) !=-1 && playerX.indexOf( btnId( 6 ) ) !=-1)){
  69. return true;
  70. }
  71. else if((playerX.indexOf( btnId( 1 ) ) !=-1 && playerX.indexOf( btnId( 4 ) ) !=-1 && playerX.indexOf( btnId( 7 ) ) !=-1)){
  72. return true;
  73. }
  74. else if((playerX.indexOf( btnId( 2 ) ) !=-1 && playerX.indexOf( btnId( 5 ) ) !=-1 && playerX.indexOf( btnId( 8 ) ) !=-1)){
  75. return true;
  76. }
  77. else if((playerX.indexOf( btnId( 0 ) ) !=-1 && playerX.indexOf( btnId( 4 ) ) !=-1 && playerX.indexOf( btnId( 8 ) ) !=-1)){
  78. return true;
  79. }
  80. else if((playerX.indexOf( btnId( 2 ) ) !=-1 && playerX.indexOf( btnId( 4 ) ) !=-1 && playerX.indexOf( btnId( 6 ) ) !=-1)){
  81. return true;
  82. }
  83. else{return false;}
  84. }
  85. function btnId(a){
  86. return btns[a].attributes.id;
  87. }
  88. function init(){
  89. for(i = 0 ; i<9 ; i++){
  90. btns[i].style.fontSize='1px';
  91. btns[i].textContent="x" // just an initialisation :)
  92. if(btns[i].classList.contains('unclickable')){btns[i].classList.remove('unclickable');}
  93. player=[];
  94. AI=[];
  95. NoOneWon=0;
  96. }
  97. }