/**
* The MIT License (MIT)
*
* Copyright (c) 2017 D3v3l0p3d_0n3
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package ru.beykerykt.bullsandcows.base.players;

import ru.beykerykt.bullsandcows.base.gui.IUserInterface;

public abstract class BasePlayer {

	private String code;
	private String name;
	private IUserInterface gui;
	private boolean guessed = false;
	private int guesses = 0;

	public BasePlayer(String name, String code, IUserInterface gui) {
		this.name = name;
		this.code = code;
		this.gui = gui;
	}

	public String getSecretCode() {
		return code;
	}

	public String getPlayerName() {
		return name;
	}

	public IUserInterface getUserInterface() {
		return gui;
	}

	public boolean isGuessed() {
		return guessed;
	}

	public void setGuessed(boolean guessed) {
		this.guessed = guessed;
	}

	public int getGuesses() {
		return guesses;
	}

	public void setGuesses(int guesses) {
		this.guesses = guesses;
	}

	public void guessCodeTo(BasePlayer player, String guess) {
		guesses++;
		String hint = getHint(guess);
		if (hint.equals("WON")) {
			setGuessed(true);
			getUserInterface().showText("You won!");
		}
		getUserInterface().showText(hint);
	}

	public String getHint(String guess) {
		int bulls = 0;
		int cows = 0;

		for (int i = 0; i < getSecretCode().length(); i++) {
			if (guess.charAt(i) == getSecretCode().charAt(i)) {
				bulls++;
			} else if (getSecretCode().contains(guess.charAt(i) + "")) {
				cows++;
			}
		}
		// System.out.println(cows + " Cows and " + bulls + " Bulls.");
		if (bulls == getSecretCode().length()) {
			return "WON";
		}
		return bulls + ":" + cows;
	}

	public boolean next(BasePlayer opponent) {
		nextGuess(opponent);
		return true;
	}

	// Твой ход!
	protected abstract void nextGuess(BasePlayer player);
}
@@ -1224,16 +1224,71 @@
 terface;
+%0Aimport ru.beykerykt.bullsandcows.base.utils.GameUtils;
 %0A%0Apublic
@@ -2305,16 +2305,99 @@
 s = 0;%0A%0A
+%09%09if (guess.length() %3C GameUtils.CODE_LENGTH) %7B%0A%09%09%09return bulls + %22:%22 + cows;%0A%09%09%7D%0A%0A
 %09%09for (i
@@ -2859,16 +2859,16 @@
 %D0%BE%D0%B9 %D1%85%D0%BE%D0%B4!%0A
-
 %09protect
@@ -2913,10 +2913,721 @@
 player);
+%0A%0A%09@Override%0A%09public int hashCode() %7B%0A%09%09final int prime = 31;%0A%09%09int result = 1;%0A%09%09result = prime * result + ((code == null) ? 0 : code.hashCode());%0A%09%09result = prime * result + ((name == null) ? 0 : name.hashCode());%0A%09%09return result;%0A%09%7D%0A%0A%09@Override%0A%09public boolean equals(Object obj) %7B%0A%09%09if (this == obj)%0A%09%09%09return true;%0A%09%09if (obj == null)%0A%09%09%09return false;%0A%09%09if (getClass() != obj.getClass())%0A%09%09%09return false;%0A%09%09BasePlayer other = (BasePlayer) obj;%0A%09%09if (code == null) %7B%0A%09%09%09if (other.code != null)%0A%09%09%09%09return false;%0A%09%09%7D else if (!code.equals(other.code))%0A%09%09%09return false;%0A%09%09if (name == null) %7B%0A%09%09%09if (other.name != null)%0A%09%09%09%09return false;%0A%09%09%7D else if (!name.equals(other.name))%0A%09%09%09return false;%0A%09%09return true;%0A%09%7D
 %0A%7D

fix with guess code length exception