index.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <template>
  2. <div class="continuar">
  3. <div class="table">
  4. <div v-for="(item,index) in letter" :key="index" class="tr">
  5. <div v-for="i in [0,1,2,3,4,5,6]" :key='i' ref="td" @click="btn(item+i,digital[item]+i)"
  6. class="td">
  7. </div>
  8. </div>
  9. </div>
  10. <div class="enter">
  11. <input type="text" placeholder="A0" v-model="enter" maxlength="2"
  12. @keyup.enter="btn" id="coordinate" value="">
  13. <div class="btn" @click="btn">输入</div>
  14. <div class="btn reload" @click="reload">重置</div>
  15. </div>
  16. </div>
  17. </template>
  18. <script>
  19. export default {
  20. data() {
  21. return {
  22. letter: ['A', 'B', 'C', 'D', 'E', 'F', 'G'],
  23. digital: { A: 0, B: 7, C: 14, D: 21, E: 28, F: 35, G: 42 },
  24. enter: '',
  25. ships: {
  26. 0: Math.floor(Math.random() * 48),
  27. 1: Math.floor(Math.random() * 48),
  28. 2: Math.floor(Math.random() * 48),
  29. },
  30. };
  31. },
  32. methods: {
  33. /**
  34. * 获取点击元素在父元素中的下标
  35. * @param {String} enters 坐标~~如:A6
  36. * @param {Number} index 被点击目标的下标
  37. */
  38. btn(enters, index) {
  39. // 创建正则,匹配输入坐标是否符合格式要求如: A6
  40. let patt1 = new RegExp(/^[a-g][0-6]/i);
  41. let coordinateNumber, coordinateText;
  42. if (enters.length == 2) {
  43. // 同步更新输入框中的坐标,可关闭
  44. this.enter = enters;
  45. // 判断玩家输入的坐标上,是否有战舰存在,如果有的话,就击沉战舰
  46. this.setAttribute(index);
  47. return;
  48. } else if (patt1.test(this.enter)) {
  49. // 截取字符串中的首位,将其转化为大写字母
  50. coordinateText = this.enter[0].toUpperCase();
  51. // 玩家输入的战舰坐标
  52. coordinateNumber =
  53. Number.parseInt(this.digital[coordinateText]) +
  54. Number.parseInt(this.enter[1]);
  55. // 判断玩家输入的坐标上,是否有战舰存在,如果有的话,就击沉战舰
  56. this.setAttribute(coordinateNumber);
  57. } else {
  58. alert('请输入正确的坐标;\n如: A6!');
  59. }
  60. },
  61. /**
  62. * 为目标元素替换类名
  63. * @param {Number} coordinateNumber 被点击目的的下标: 例如 45
  64. */
  65. setAttribute(coordinateNumber) {
  66. let getClassName = this.$refs.td[coordinateNumber]
  67. .getAttribute('class')
  68. .split(' ');
  69. if (getClassName.length > 1) {
  70. alert(`该区域已经${getClassName[0]},请切换其他空白区域!`);
  71. return;
  72. }
  73. if (
  74. this.ships[0] === coordinateNumber ||
  75. this.ships[1] === coordinateNumber ||
  76. this.ships[2] === coordinateNumber
  77. ) {
  78. // 击沉战舰
  79. this.$refs.td[coordinateNumber].setAttribute('class', 'ship td');
  80. } else {
  81. // 没有击沉战舰
  82. this.$refs.td[coordinateNumber].setAttribute('class', 'miss td');
  83. }
  84. },
  85. /**
  86. * 重置this.data中部分kay的value
  87. */
  88. reload() {
  89. this.enter = '';
  90. for (let i in this.ships) {
  91. this.ships[i] = Math.floor(Math.random() * 48);
  92. }
  93. for (let i in this.$refs.td) {
  94. this.$refs.td[i].setAttribute('class', 'td');
  95. }
  96. },
  97. },
  98. };
  99. </script>
  100. <style scoped>
  101. .continuar {
  102. margin: 0;
  103. width: 650px;
  104. position: relative;
  105. height: 650px;
  106. background: url('/images/js/2018_11_02/board.jpg') no-repeat;
  107. background-size: contain;
  108. }
  109. .table {
  110. margin: 0 auto;
  111. padding-top: 62px;
  112. padding-left: 106px;
  113. }
  114. .tr {
  115. display: flex;
  116. }
  117. .tr .td {
  118. flex: 0 0 62px;
  119. height: 61px;
  120. line-height: 61px;
  121. text-align: center;
  122. color: white;
  123. box-sizing: border-box;
  124. }
  125. .td.ship {
  126. background: url('/images/js/2018_11_02/ship.png') no-repeat;
  127. background-size: 70%;
  128. background-position: center;
  129. }
  130. .td.miss {
  131. background: url('/images/js/2018_11_02/miss.png') no-repeat;
  132. background-size: 70%;
  133. background-position: center;
  134. }
  135. .enter {
  136. position: absolute;
  137. right: 5px;
  138. top: 5px;
  139. display: flex;
  140. padding: 5px;
  141. margin-left: 0;
  142. margin-right: auto;
  143. }
  144. .enter input {
  145. height: 35px;
  146. color: #000000;
  147. background-color: rgb(159, 255, 48);
  148. display: block;
  149. float: left;
  150. border: 0;
  151. }
  152. .enter .btn {
  153. height: 25px;
  154. float: left;
  155. padding: 0 5px;
  156. cursor: pointer;
  157. font-size: 12px;
  158. color: aliceblue;
  159. margin-left: 5px;
  160. line-height: 25px;
  161. user-select: none;
  162. text-align: center;
  163. background-color: #41510d;
  164. border: 5px solid #46e234;
  165. }
  166. </style>