Skip to content

Commit

Permalink
Introduced Player name concept which will help identifying players an…
Browse files Browse the repository at this point in the history
…d in future will help for example implementing #63.
  • Loading branch information
lrozenblyum committed Mar 7, 2017
1 parent 938fdf2 commit 305768d
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/main/java/com/leokom/chess/player/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,19 @@ public interface Player {
* @return position position which is not null
*/
Position getPosition();

/**
* Get name of the player.
* It will help identifying it.
* Also it will make it possible to specify human player name.
*
* A correct implementation of the name method
* must not return different results for the same
* player instance.
*
* @return name of the player.
*/
default String name() {
return this.getClass().getSimpleName();
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/leokom/chess/player/legal/LegalPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,9 @@ public Position getPosition() {
public boolean isRecordingMode() {
return recordingMode;
}

@Override
public String name() {
return Player.super.name() + " : " + this.decisionMaker.name();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@ to allow evolving interface while not forcing existing
default Move findBestMoveForOpponent( Position position ) {
return null;
}

default String name() {
return this.getClass().getSimpleName();
}
}
9 changes: 7 additions & 2 deletions src/test/java/com/leokom/chess/PlayerFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.leokom.chess.player.winboard.WinboardPlayer;
import org.junit.*;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class PlayerFactoryTest {
Expand Down Expand Up @@ -43,15 +44,19 @@ public void clearSystemProperties() {
@Test
public void noSystemPropertiesDefaultPlayer() {
final Player player = PlayerFactory.createPlayer( Side.BLACK );
assertTrue( player instanceof SimplePlayer );
assertIsSimple( player );
}

@Test
public void canSelectSimpleEngineForWhite() {
System.setProperty( "white", "Simple" );

final Player player = PlayerFactory.createPlayer( Side.WHITE );
assertTrue( player instanceof SimplePlayer );
assertIsSimple( player );
}

private void assertIsSimple(Player player) {
assertEquals( "LegalPlayer : SimpleBrains", player.name() );
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.leokom.chess.player.legal;

import org.junit.Test;

import static com.leokom.chess.PlayerFactory.PlayerSelection.LEGAL;
import static com.leokom.chess.PlayerFactory.PlayerSelection.SIMPLE;
import static org.junit.Assert.assertEquals;

public class LegalPlayerNameTest {
@Test
public void defaultName() {
assertEquals( "LegalPlayer : DenormalizedDecisionMaker", LEGAL.create().name() );
}

@Test
public void customNameWithCustomBrain() {
assertEquals( "LegalPlayer : SimpleBrains", SIMPLE.create().name() );
}
}

0 comments on commit 305768d

Please sign in to comment.