1 /* 2 * Copyright (c) 2016, Oracle and/or its affiliates. 3 * All rights reserved. Use is subject to license terms. 4 * 5 * This file is available and licensed under the following license: 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * - Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * - Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the distribution. 16 * - Neither the name of Oracle Corporation nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 package minesweeperfx; 33 34 35 import javafx.scene.canvas.GraphicsContext; 36 import javafx.scene.image.Image; 37 import javafx.scene.image.ImageView; 38 39 40 public class Tile { 41 42 public enum TileState { Hidden, Showing, Flagged } 43 public enum FlagState { None, Flag, Unflag } 44 45 private TileState state = TileState.Hidden; 46 private Rectangle rectangle; 47 private Resources.ImageType imageType; 48 private Location position; 49 50 public Tile(double x, double y, double width, double height, Resources.ImageType imageType, Location position) { 51 rectangle = new Rectangle(x, y, width, height); 52 this.imageType = imageType; 53 this.position = position; 54 } 55 56 public Location getPosition() { 57 return position; 58 } 59 60 public void draw(GraphicsContext graphics, Point mouseLocation) { 61 Image image = null; 62 63 switch (state) { 64 case Hidden: { 65 if (rectangle.contains(mouseLocation) == true) { 66 image = Resources.getInstance().getImage(Resources.ImageType.Over); 67 } 68 else { 69 image = Resources.getInstance().getImage(Resources.ImageType.Blank); 70 } 71 break; 72 } 73 case Flagged: { 74 if (rectangle.contains(mouseLocation) == true) { 75 image = Resources.getInstance().getImage(Resources.ImageType.FlagOver); 76 } 77 else { 78 image = Resources.getInstance().getImage(Resources.ImageType.Flag); 79 } 80 break; 81 } 82 case Showing: { 83 image = Resources.getInstance().getImage(imageType); 84 break; 85 } 86 } 87 88 if (image != null) { 89 graphics.drawImage(image, rectangle.x, rectangle.y); 90 } 91 } 92 93 public void draw(GraphicsContext graphics) { 94 Image image = null; 95 96 if ((imageType == Resources.ImageType.Mine) && 97 (state == TileState.Showing)) { 98 99 image = Resources.getInstance().getImage(Resources.ImageType.HitMine); 100 } 101 else { 102 image = Resources.getInstance().getImage(imageType); 103 } 104 105 if (image != null) { 106 graphics.drawImage(image, rectangle.x, rectangle.y); 107 } 108 } 109 110 public boolean hitTest(Point mouseLocation) { 111 boolean result = false; 112 113 if (rectangle.contains(mouseLocation) == true) { 114 result = true; 115 } 116 117 return result; 118 } 119 120 public boolean selected(Point mouseLocation) { 121 boolean result = false; 122 123 if (rectangle.contains(mouseLocation) == true) { 124 state = TileState.Showing; 125 126 if (imageType == Resources.ImageType.Mine) { 127 result = true; 128 } 129 } 130 131 return result; 132 } 133 134 public FlagState flag(Point mouseLocation) { 135 FlagState result = FlagState.None; 136 137 if (rectangle.contains(mouseLocation) == true) { 138 if (state == TileState.Flagged) { 139 state = TileState.Hidden; 140 result = FlagState.Unflag; 141 } 142 else { 143 state = TileState.Flagged; 144 result = FlagState.Flag; 145 } 146 } 147 148 return result; 149 } 150 151 // True is success, False is failure. 152 public boolean isFlaggedAndMine() { 153 boolean result = false; 154 155 if ((state == TileState.Flagged && imageType == Resources.ImageType.Mine) || 156 (state != TileState.Flagged && imageType != Resources.ImageType.Mine)) { 157 result = true; 158 } 159 160 return result; 161 } 162 163 public enum TileUncover {Stop, Continue, Done} 164 165 public TileUncover uncover() { 166 TileUncover result = TileUncover.Stop; 167 168 if (state == TileState.Hidden && imageType != Resources.ImageType.Mine) { 169 if (imageType == Resources.ImageType.ExposedTile) { 170 state = TileState.Showing; 171 result = TileUncover.Continue; 172 } 173 else if (imageType != Resources.ImageType.ExposedTile) { 174 state = TileState.Showing; 175 result = TileUncover.Done; 176 } 177 } 178 179 return result; 180 } 181 }