File indexing completed on 2024-05-05 04:04:03
0001 /* 0002 * Copyright (c) 2018 Sune Vuorela <sune@vuorela.dk> 0003 * 0004 * Permission is hereby granted, free of charge, to any person 0005 * obtaining a copy of this software and associated documentation 0006 * files (the "Software"), to deal in the Software without 0007 * restriction, including without limitation the rights to use, 0008 * copy, modify, merge, publish, distribute, sublicense, and/or sell 0009 * copies of the Software, and to permit persons to whom the 0010 * Software is furnished to do so, subject to the following 0011 * conditions: 0012 * 0013 * The above copyright notice and this permission notice shall be 0014 * included in all copies or substantial portions of the Software. 0015 * 0016 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 0017 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 0018 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 0019 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 0020 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 0021 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 0022 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 0023 * OTHER DEALINGS IN THE SOFTWARE. 0024 */ 0025 0026 0027 // checks if a board is all colored the same 0028 function checkWin(board) { 0029 var firstColor = board[0][0]; 0030 var count = board.length; 0031 for(var i = 0 ; i < count ; i++) { 0032 for(var j = 0 ; j < count ; j++) { 0033 if (board[i][j] !== firstColor) { 0034 return false 0035 } 0036 } 0037 } 0038 return true 0039 } 0040 0041 // gather the neighbor points from a given point, if they exist within the max. 0042 // helper function for 'fill' 0043 function gatherNeighbors(point, max) { 0044 var n = [] 0045 if (point.x > 0) { 0046 n.push(Qt.point(point.x - 1, point.y)) 0047 } 0048 if (point.x < max) { 0049 n.push(Qt.point(point.x + 1, point.y)) 0050 } 0051 if (point.y > 0) { 0052 n.push(Qt.point(point.x, point.y - 1)) 0053 } 0054 if (point.y < max) { 0055 n.push(Qt.point(point.x, point.y + 1)) 0056 } 0057 return n 0058 } 0059 0060 // flood fills the board with the given color starting from 0,0 0061 function fill(color,board) { 0062 var frontier = [Qt.point(0,0)] 0063 var originalColor = board[0][0] 0064 if (originalColor == color) { 0065 return false 0066 } 0067 var seen = [] 0068 while (frontier.length > 0 ) { 0069 var current = frontier.pop() 0070 seen.push(current) 0071 if (board[current.x][current.y] !== originalColor) { 0072 continue 0073 } 0074 board[current.x][current.y] = color 0075 var neighbors = gatherNeighbors(current, board.length -1) 0076 while (neighbors.length > 0) { 0077 var neighbor = neighbors.pop() 0078 var found = false 0079 for(var i = 0 ; i < seen.length; i++) { 0080 if (seen[i].x === neighbor.x && seen[i].y === neighbor.y) { 0081 found = true 0082 break 0083 } 0084 } 0085 if (!found) { 0086 for(var i = 0 ; i < frontier.length; i++) { 0087 if (frontier[i].x === neighbor.x && frontier[i].y === neighbor.y) { 0088 found = true 0089 break 0090 } 0091 } 0092 } 0093 if (!found) { 0094 frontier.push(neighbor) 0095 } 0096 } 0097 } 0098 return true; 0099 } 0100 0101 // initializes a board with random colors from color list 0102 function createBoard(count, colorList) { 0103 var board = [] 0104 for(var i = 0 ; i < count ; i++) { 0105 var lane = [] 0106 for(var j = 0 ; j < count ; j++) { 0107 lane.push(colorList[Math.floor(Math.random() * colorList.length)]) 0108 } 0109 board.push(lane) 0110 } 0111 return board 0112 }