• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15import PictureItem from '../model/PictureItem'
16
17enum gameStatus {
18  running = 1,
19  over = 0
20}
21
22const EMPTY_PICTURE: PictureItem = new PictureItem(9, undefined)
23
24export default class GameRules {
25  public numArray: PictureItem[]
26  public gameStatus: number = gameStatus.over
27
28  itemChange(index: number, pictures: PictureItem[]) {
29    let emptyIndex = this.findEmptyIndex(pictures)
30    let temp: PictureItem = pictures[index]
31    pictures[index] = EMPTY_PICTURE
32    pictures[emptyIndex] = new PictureItem(temp.index, temp.pixelMap)
33    return pictures
34  }
35
36  findEmptyIndex(pictures: PictureItem[]) {
37    for (let i = 0;i < pictures.length; i++) {
38      if (pictures[i].index === EMPTY_PICTURE.index) {
39        return i
40      }
41    }
42    return -1
43  }
44
45  gameInit(i: number, pictures: PictureItem[]) {
46    let emptyIndex = this.findEmptyIndex(pictures)
47    if (this.gameStatus == gameStatus.running) {
48      switch (emptyIndex) {
49        case 0:
50          if (i === 1 || i === 3) {
51            pictures = this.itemChange(i, pictures)
52          }
53          break;
54        case 2:
55          if (i === 1 || i === 5) {
56            pictures = this.itemChange(i, pictures)
57          }
58          break;
59        case 6:
60          if (i === 3 || i === 7) {
61            pictures = this.itemChange(i, pictures)
62          }
63          break;
64        case 8:
65          if (i === 5 || i === 7) {
66            pictures = this.itemChange(i, pictures)
67          }
68          break;
69        case 3:
70          switch (i) {
71            case emptyIndex + 1:
72            case emptyIndex - 3:
73            case emptyIndex + 3:
74              pictures = this.itemChange(i, pictures)
75          }
76          break;
77        case 1:
78          switch (i) {
79            case emptyIndex + 1:
80            case emptyIndex - 1:
81            case emptyIndex + 3:
82              pictures = this.itemChange(i, pictures)
83          }
84          break;
85        case 5:
86          switch (i) {
87            case emptyIndex + 3:
88            case emptyIndex - 3:
89            case emptyIndex - 1:
90              pictures = this.itemChange(i, pictures)
91          }
92          break;
93        case 7:
94          switch (i) {
95            case emptyIndex + 1:
96            case emptyIndex - 3:
97            case emptyIndex - 1:
98              pictures = this.itemChange(i, pictures)
99          }
100          break;
101        case 4:
102          switch (i) {
103            case emptyIndex + 1:
104            case emptyIndex - 3:
105            case emptyIndex - 1:
106            case emptyIndex + 3:
107              pictures = this.itemChange(i, pictures)
108          }
109          break;
110      }
111    }
112    return pictures
113  }
114
115  gameBegin(pictures: PictureItem[]) {
116    this.gameStatus = gameStatus.running
117    let len = pictures.length
118    let index, temp
119    while (len > 0) {
120      index = Math.floor(Math.random() * len)
121      temp = pictures[len-1]
122      pictures[len-1] = pictures[index]
123      pictures[index] = temp
124      len--
125    }
126    return pictures
127  }
128}