// NeuralTic.java
// Copyright (c) 2007-2010 Ronald B. Cemer
// All rights reserved.
// This software is released under the BSD license.
// Please see the accompanying LICENSE.txt for details.

package com.roncemer.tic;

import java.io.*;

public class NeuralTic {
	public static void main(String[] args) {
		NeuralPlayer neuralPlayer = new NeuralPlayer();

		// Do training.
		System.out.println("Learning...");
		for (int pass = 1; ; pass++) {
			neuralPlayer.trainAllBoardsOnePass();
			int npass = neuralPlayer.getNumPassedMoves();
			int nfail = neuralPlayer.getNumFailedMoves();
			System.out.println("train pass="+pass+" npass="+npass+" nfail="+nfail);
			if (nfail == 0) break;
		}

		// Do competition.
		int numTestPasses = 100000;
		for (int whichOpponent = 1; whichOpponent <= 2; whichOpponent++) {
			Player opponent = (whichOpponent == 1) ? new IdealPlayer() : new RandomPlayer();
			System.out.println("Competing against "+opponent.getClass().getName()+"...");
			Tic tic = new Tic(neuralPlayer, opponent);
			int xWinCount = 0, oWinCount = 0, nWinCount = 0;
			for (int pass = 0; pass < numTestPasses; pass++) {
				tic.playGame(null, false, false);
				switch (tic.getWinner()) {
				case 'X':
					xWinCount++;
					break;
				case 'O':
					oWinCount++;
					break;
				default:
					nWinCount++;
					break;
				}
			}
			// Output test result counts.
			System.out.println("X won: "+xWinCount);
			System.out.println("O won: "+oWinCount);
			System.out.println("Nobody won: "+nWinCount);
		}

/*		System.out.println();
		System.out.println();
		System.out.println();
		System.out.println();
		System.out.println("Encoded network:");
		System.out.println(neuralPlayer.encodeNN());*/
	}
}

@@ -349,16 +349,56 @@
 layer();
+%0A%09%09HumanPlayer human= new HumanPlayer();
 %0A%0A%09%09// D
@@ -725,447 +725,71 @@
 ;%0A%09%09
-%7D%0A%0A%09%09// Do competition.%0A%09%09int numTestPasses = 100000;%0A%09%09for (int whichOpponent = 1; whichOpponent %3C= 2; whichOpponent++) %7B%0A%09%09%09Player opponent = (whichOpponent == 1) ? new IdealPlayer() : new RandomPlayer();%0A%09%09%09System.out.println(%22Competing against %22+opponent.getClass().getName()+%22...%22);%0A%09%09%09Tic tic = new Tic(neuralPlayer, opponent);%0A%09%09%09int xWinCount = 0, oWinCount = 0, nWinCount = 0;%0A%09%09%09for (int pass = 0; pass %3C numTestPasses; pass++) %7B
+%09if (pass%25200==0) %7B%0A%09%09%09%09Tic tic = new Tic(neuralPlayer, human);
 %0A%09%09%09
@@ -854,24 +854,25 @@
 er()) %7B%0A%09%09%09%09
+%09
 case 'X':%0A%09%09
@@ -878,33 +878,55 @@
 %09%09%09%09
-xWinCount++
+%09System.out.println(%22You won.%22)
 ;%0A%09%09%09%09%09
+%09
 break;%0A
+%09
 %09%09%09%09
@@ -944,21 +944,41 @@
 %09%09%09%09
-oWinCount++
+%09System.out.println(%22It won.%22)
 ;%0A
+%09
 %09%09%09%09
@@ -989,16 +989,17 @@
 ak;%0A%09%09%09%09
+%09
 default:
@@ -1008,21 +1008,45 @@
 %09%09%09%09
-nWinCount++
+%09System.out.println(%22Nobody won.%22)
 ;%0A
+%09
 %09%09%09%09
@@ -1070,40 +1070,11 @@
 %7D%0A%09%09
-%09// Output test result counts.%0A%09
+%7D%0A%0A
 %09%09Sy
@@ -1095,30 +1095,18 @@
 ln(%22
-X won: %22+xWinCount
+%5Cn%5Cn%5Cn%22
 );%0A
-%09
 %09%09Sy
@@ -1127,244 +1127,138 @@
 ln(%22
-O won: %22+oWinCount);%0A%09%09%09System.out.println(%22Nobody won: %22+nWinCount);%0A%09%09%7D%0A%0A/*%09%09System.out.println();%0A%09%09System.out.println();%0A%09%09System.out.println();%0A%09%09System.out.println();%0A%09%09System.out.println(%22Encoded network:%22);%0A%09%09System.out.println(
+Show encoded network? %22);%0A%09%09if (Character.toLowerCase(new Scanner(System.in).next().charAt(0))=='y')%0A%09%09%09System.out.println(%22%5Cn%5Cn%22+
 neur
@@ -1282,10 +1282,8 @@
 ());
-*/
 %0A%09%7D%0A

Make interactive interface to neural player