Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 771 | blopes | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
||
| 3 | * contributor license agreements. See the NOTICE file distributed with |
||
| 4 | * this work for additional information regarding copyright ownership. |
||
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
||
| 6 | * (the "License"); you may not use this file except in compliance with |
||
| 7 | * the License. You may obtain a copy of the License at |
||
| 8 | * |
||
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
||
| 10 | * |
||
| 11 | * Unless required by applicable law or agreed to in writing, software |
||
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
||
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
| 14 | * See the License for the specific language governing permissions and |
||
| 15 | * limitations under the License. |
||
| 16 | */ |
||
| 17 | package websocket.snake; |
||
| 18 | |||
| 19 | import java.io.IOException; |
||
| 20 | import java.util.ArrayDeque; |
||
| 21 | import java.util.Collection; |
||
| 22 | import java.util.Deque; |
||
| 23 | |||
| 24 | import javax.websocket.CloseReason; |
||
| 25 | import javax.websocket.CloseReason.CloseCodes; |
||
| 26 | import javax.websocket.Session; |
||
| 27 | |||
| 28 | public class Snake { |
||
| 29 | |||
| 30 | private static final int DEFAULT_LENGTH = 5; |
||
| 31 | |||
| 32 | private final int id; |
||
| 33 | private final Session session; |
||
| 34 | |||
| 35 | private Direction direction; |
||
| 36 | private int length = DEFAULT_LENGTH; |
||
| 37 | private Location head; |
||
| 38 | private final Deque<Location> tail = new ArrayDeque<>(); |
||
| 39 | private final String hexColor; |
||
| 40 | |||
| 41 | public Snake(int id, Session session) { |
||
| 42 | this.id = id; |
||
| 43 | this.session = session; |
||
| 44 | this.hexColor = SnakeAnnotation.getRandomHexColor(); |
||
| 45 | resetState(); |
||
| 46 | } |
||
| 47 | |||
| 48 | private void resetState() { |
||
| 49 | this.direction = Direction.NONE; |
||
| 50 | this.head = SnakeAnnotation.getRandomLocation(); |
||
| 51 | this.tail.clear(); |
||
| 52 | this.length = DEFAULT_LENGTH; |
||
| 53 | } |
||
| 54 | |||
| 55 | private synchronized void kill() { |
||
| 56 | resetState(); |
||
| 57 | sendMessage("{\"type\": \"dead\"}"); |
||
| 58 | } |
||
| 59 | |||
| 60 | private synchronized void reward() { |
||
| 61 | length++; |
||
| 62 | sendMessage("{\"type\": \"kill\"}"); |
||
| 63 | } |
||
| 64 | |||
| 65 | |||
| 66 | protected void sendMessage(String msg) { |
||
| 67 | try { |
||
| 68 | session.getBasicRemote().sendText(msg); |
||
| 69 | } catch (IOException ioe) { |
||
| 70 | CloseReason cr = new CloseReason(CloseCodes.CLOSED_ABNORMALLY, ioe.getMessage()); |
||
| 71 | try { |
||
| 72 | session.close(cr); |
||
| 73 | } catch (IOException ioe2) { |
||
| 74 | // Ignore |
||
| 75 | } |
||
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 79 | public synchronized void update(Collection<Snake> snakes) { |
||
| 80 | Location nextLocation = head.getAdjacentLocation(direction); |
||
| 81 | if (nextLocation.x >= SnakeAnnotation.PLAYFIELD_WIDTH) { |
||
| 82 | nextLocation.x = 0; |
||
| 83 | } |
||
| 84 | if (nextLocation.y >= SnakeAnnotation.PLAYFIELD_HEIGHT) { |
||
| 85 | nextLocation.y = 0; |
||
| 86 | } |
||
| 87 | if (nextLocation.x < 0) { |
||
| 88 | nextLocation.x = SnakeAnnotation.PLAYFIELD_WIDTH; |
||
| 89 | } |
||
| 90 | if (nextLocation.y < 0) { |
||
| 91 | nextLocation.y = SnakeAnnotation.PLAYFIELD_HEIGHT; |
||
| 92 | } |
||
| 93 | if (direction != Direction.NONE) { |
||
| 94 | tail.addFirst(head); |
||
| 95 | if (tail.size() > length) { |
||
| 96 | tail.removeLast(); |
||
| 97 | } |
||
| 98 | head = nextLocation; |
||
| 99 | } |
||
| 100 | |||
| 101 | handleCollisions(snakes); |
||
| 102 | } |
||
| 103 | |||
| 104 | private void handleCollisions(Collection<Snake> snakes) { |
||
| 105 | for (Snake snake : snakes) { |
||
| 106 | boolean headCollision = id != snake.id && snake.getHead().equals(head); |
||
| 107 | boolean tailCollision = snake.getTail().contains(head); |
||
| 108 | if (headCollision || tailCollision) { |
||
| 109 | kill(); |
||
| 110 | if (id != snake.id) { |
||
| 111 | snake.reward(); |
||
| 112 | } |
||
| 113 | } |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | public synchronized Location getHead() { |
||
| 118 | return head; |
||
| 119 | } |
||
| 120 | |||
| 121 | public synchronized Collection<Location> getTail() { |
||
| 122 | return tail; |
||
| 123 | } |
||
| 124 | |||
| 125 | public synchronized void setDirection(Direction direction) { |
||
| 126 | this.direction = direction; |
||
| 127 | } |
||
| 128 | |||
| 129 | public synchronized String getLocationsJson() { |
||
| 130 | StringBuilder sb = new StringBuilder(); |
||
| 131 | sb.append(String.format("{\"x\": %d, \"y\": %d}", |
||
| 132 | Integer.valueOf(head.x), Integer.valueOf(head.y))); |
||
| 133 | for (Location location : tail) { |
||
| 134 | sb.append(','); |
||
| 135 | sb.append(String.format("{\"x\": %d, \"y\": %d}", |
||
| 136 | Integer.valueOf(location.x), Integer.valueOf(location.y))); |
||
| 137 | } |
||
| 138 | return String.format("{\"id\":%d,\"body\":[%s]}", |
||
| 139 | Integer.valueOf(id), sb.toString()); |
||
| 140 | } |
||
| 141 | |||
| 142 | public int getId() { |
||
| 143 | return id; |
||
| 144 | } |
||
| 145 | |||
| 146 | public String getHexColor() { |
||
| 147 | return hexColor; |
||
| 148 | } |
||
| 149 | } |