10 Replies - 281 Views - Last Post: 01 June 2012 - 08:21 PM Rate Topic: -----

#1 EscSequenceAlpha  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 20-March 12

How can I condense this code into more inteligent code? (simple a.i.)

Posted 31 May 2012 - 11:25 AM

for the last week or so I've been developing a naughts and crosses game (tic-tac-toe) I'm happy to say this has been a success and the game functions as I wanted it

theres nothing wrong with how the code works. I developed this game as learning experience, no classroom, so I would like to see how else it could have been coded.

my a.i. code is over a 1000 lines, it individually checks certain conditions. there must be a more inteligent solution than my own.

package naughtsAndCrosses;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

public class AI extends Player
{

	public void move(Board board)
	{
		Random randomNumbers = new Random();
		int legal;
		List<Integer> list = new ArrayList<Integer>(); //Used to hold possible priority 3 moves

		// First Priority (IMMEDIATE WIN)

		// EOO
		// ---
		// ---
		if( board.getBoard()[0][0] == Board.EMPTY() && board.getBoard()[0][1] == Board.O() 
				&& board.getBoard()[0][2] == Board.O() ) 
		{
			board.addValue(0,0, this);
			return;
		}

		// OEO
		// ---
		// ---
		else if( board.getBoard()[0][0] == Board.O() && board.getBoard()[0][1] == Board.EMPTY() 
				&& board.getBoard()[0][2] == Board.O() ) 
		{
			board.addValue(0,1, this);
			return;
		}

		// OOE
		// ---
		// ---
		else if( board.getBoard()[0][0] == Board.O() && board.getBoard()[0][1] == Board.O() 
				&& board.getBoard()[0][2] == Board.EMPTY() ) 
		{
			board.addValue(0,2, this);
			return;
		}

		// ---
		// EOO
		// ---
		else if( board.getBoard()[1][0] == Board.EMPTY() && board.getBoard()[1][1] == Board.O() 
				&& board.getBoard()[1][2] == Board.O() )
		{
			board.addValue(1, 0, this);
			return;
		}

		// ---
		// OEO
		// ---
		else if( board.getBoard()[1][0] == Board.O() && board.getBoard()[1][1] == Board.EMPTY() 
				&& board.getBoard()[1][2] == Board.O() )
		{
			board.addValue(1, 1, this);
			return;
		}

		// ---
		// OOE
		// ---
		else if( board.getBoard()[1][0] == Board.O() && board.getBoard()[1][1] == Board.O() 
				&& board.getBoard()[1][2] == Board.EMPTY() )
		{
			board.addValue(1, 2, this);
			return;
		}

		// ---
		// ---
		// EOO
		else if( board.getBoard()[2][0] == Board.EMPTY() && board.getBoard()[2][1] == Board.O() 
				&& board.getBoard()[2][2] == Board.O())
		{
			board.addValue(2, 0, this);
			return;
		}

		// ---
		// ---
		// OEO
		else if( board.getBoard()[2][0] == Board.O() && board.getBoard()[2][1] == Board.EMPTY() 
				&& board.getBoard()[2][2] == Board.O())
		{
			board.addValue(2, 1, this);
			return;
		}

		// ---
		// ---
		// OOE
		else if( board.getBoard()[2][0] == Board.O() && board.getBoard()[2][1] == Board.O() 
				&& board.getBoard()[2][2] == Board.EMPTY())
		{
			board.addValue(2, 2, this);
			return;
		}

		// E--
		// O--
		// O--
		else if( board.getBoard()[0][0] == Board.EMPTY() && board.getBoard()[1][0] == Board.O() 
				&& board.getBoard()[2][0] == Board.O() )
		{
			board.addValue(0, 0, this);
			return;
		}

		// O--
		// E--
		// O--
		else if( board.getBoard()[0][0] == Board.O() && board.getBoard()[1][0] == Board.EMPTY() 
				&& board.getBoard()[2][0] == Board.O() )
		{
			board.addValue(1, 0, this);
			return;
		}

		// O--
		// O--
		// E--
		else if( board.getBoard()[0][0] == Board.O() && board.getBoard()[1][0] == Board.O() 
				&& board.getBoard()[2][0] == Board.EMPTY() )
		{
			board.addValue(2, 0, this);
			return;
		}

		// -E-
		// -O-
		// -O-
		else if( board.getBoard()[0][1] == Board.EMPTY() && board.getBoard()[1][1] == Board.O() 
				&& board.getBoard()[2][1] == Board.O() )
		{
			board.addValue(0, 1, this);
			return;
		}

		// -O-
		// -E-
		// -O-
		else if( board.getBoard()[0][1] == Board.O() && board.getBoard()[1][1] == Board.EMPTY() 
				&& board.getBoard()[2][1] == Board.O() )
		{
			board.addValue(1, 1, this);
			return;
		}

		// -O-
		// -O-
		// -E-
		else if( board.getBoard()[0][1] == Board.O() && board.getBoard()[1][1] == Board.O() 
				&& board.getBoard()[2][1] == Board.EMPTY() )
		{
			board.addValue(2, 1, this);
			return;
		}

		// --E
		// --O
		// --O
		else if( board.getBoard()[0][2] == Board.EMPTY() && board.getBoard()[1][2] == Board.O() 
				&& board.getBoard()[2][2] == Board.O() )
		{
			board.addValue(0, 2, this);
			return;
		}

		// --O
		// --E
		// --O
		else if( board.getBoard()[0][2] == Board.O() && board.getBoard()[1][2] == Board.EMPTY() 
				&& board.getBoard()[2][2] == Board.O() )
		{
			board.addValue(1, 2, this);
			return;
		}

		// --O
		// --O
		// --E
		else if( board.getBoard()[0][2] == Board.O() && board.getBoard()[1][2] == Board.O() 
				&& board.getBoard()[2][2] == Board.EMPTY() )
		{
			board.addValue(2, 2, this);
			return;
		}

		// E--
		// -O-
		// --O
		else if( board.getBoard()[0][0] == Board.EMPTY() && board.getBoard()[1][1] == Board.O() 
				&& board.getBoard()[2][2] == Board.O() )
		{
			board.addValue(0, 0, this);
			return;
		}

		// O--
		// -E-
		// --O
		else if( board.getBoard()[0][0] == Board.O() && board.getBoard()[1][1] == Board.EMPTY() 
				&& board.getBoard()[2][2] == Board.O() )
		{
			board.addValue(1, 1, this);
			return;
		}

		// O--
		// -O-
		// --E
		else if( board.getBoard()[0][0] == Board.O() && board.getBoard()[1][1] == Board.O() 
				&& board.getBoard()[2][2] == Board.EMPTY() )
		{
			board.addValue(2, 2, this);
			return;
		}

		// --E
		// -O-
		// O--
		else if( board.getBoard()[0][2] == Board.EMPTY() && board.getBoard()[1][1] == Board.O() 
				&& board.getBoard()[2][0] == Board.O() )
		{
			board.addValue(0, 2, this);
			return;
		}

		// --O
		// -E-
		// O--
		else if( board.getBoard()[0][2] == Board.O() && board.getBoard()[1][1] == Board.EMPTY() 
				&& board.getBoard()[2][0] == Board.O() )
		{
			board.addValue(1, 1, this);
			return;
		}

		// --O
		// -O-
		// E--
		else if( board.getBoard()[0][2] == Board.O() && board.getBoard()[1][1] == Board.O() 
				&& board.getBoard()[2][0] == Board.EMPTY() )
		{
			board.addValue(2, 0, this);
			return;
		}

		// Second Priority (IMMEDIATE BLOCK)

		// EXX
		// ---
		// ---
		if( board.getBoard()[0][0] == Board.EMPTY() && board.getBoard()[0][1] == Board.X() 
				&& board.getBoard()[0][2] == Board.X() ) 
		{
			board.addValue(0,0, this);
			return;
		}

		// XEX
		// ---
		// ---
		else if( board.getBoard()[0][0] == Board.X() && board.getBoard()[0][1] == Board.EMPTY() 
				&& board.getBoard()[0][2] == Board.X() ) 
		{
			board.addValue(0,1, this);
			return;
		}

		// XXE
		// ---
		// ---
		else if( board.getBoard()[0][0] == Board.X() && board.getBoard()[0][1] == Board.X() 
				&& board.getBoard()[0][2] == Board.EMPTY() ) 
		{
			board.addValue(0,2, this);
			return;
		}

		// ---
		// EXX
		// ---
		else if( board.getBoard()[1][0] == Board.EMPTY() && board.getBoard()[1][1] == Board.X() 
				&& board.getBoard()[1][2] == Board.X() )
		{
			board.addValue(1, 0, this);
			return;
		}

		// ---
		// XEX
		// ---
		else if( board.getBoard()[1][0] == Board.X() && board.getBoard()[1][1] == Board.EMPTY() 
				&& board.getBoard()[1][2] == Board.X() )
		{
			board.addValue(1, 1, this);
			return;
		}

		// ---
		// XXE
		// ---
		else if( board.getBoard()[1][0] == Board.X() && board.getBoard()[1][1] == Board.X() 
				&& board.getBoard()[1][2] == Board.EMPTY() )
		{
			board.addValue(1, 2, this);
			return;
		}

		// ---
		// ---
		// EXX
		else if( board.getBoard()[2][0] == Board.EMPTY() && board.getBoard()[2][1] == Board.X() 
				&& board.getBoard()[2][2] == Board.X())
		{
			board.addValue(2, 0, this);
			return;
		}

		// ---
		// ---
		// XEX
		else if( board.getBoard()[2][0] == Board.X() && board.getBoard()[2][1] == Board.EMPTY() 
				&& board.getBoard()[2][2] == Board.X())
		{
			board.addValue(2, 1, this);
			return;
		}

		// ---
		// ---
		// XXE
		else if( board.getBoard()[2][0] == Board.X() && board.getBoard()[2][1] == Board.X() 
				&& board.getBoard()[2][2] == Board.EMPTY())
		{
			board.addValue(2, 2, this);
			return;
		}

		// E--
		// X--
		// X--
		else if( board.getBoard()[0][0] == Board.EMPTY() && board.getBoard()[1][0] == Board.X() 
				&& board.getBoard()[2][0] == Board.X() )
		{
			board.addValue(0, 0, this);
			return;
		}

		// X--
		// E--
		// X--
		else if( board.getBoard()[0][0] == Board.X() && board.getBoard()[1][0] == Board.EMPTY() 
				&& board.getBoard()[2][0] == Board.X() )
		{
			board.addValue(1, 0, this);
			return;
		}

		// X--
		// X--
		// E--
		else if( board.getBoard()[0][0] == Board.X() && board.getBoard()[1][0] == Board.X() 
				&& board.getBoard()[2][0] == Board.EMPTY() )
		{
			board.addValue(2, 0, this);
			return;
		}

		// -E-
		// -X-
		// -X-
		else if( board.getBoard()[0][1] == Board.EMPTY() && board.getBoard()[1][1] == Board.X() 
				&& board.getBoard()[2][1] == Board.X() )
		{
			board.addValue(0, 1, this);
			return;
		}

		// -X-
		// -E-
		// -X-
		else if( board.getBoard()[0][1] == Board.X() && board.getBoard()[1][1] == Board.EMPTY() 
				&& board.getBoard()[2][1] == Board.X() )
		{
			board.addValue(1, 1, this);
			return;
		}

		// -X-
		// -X-
		// -E-
		else if( board.getBoard()[0][1] == Board.X() && board.getBoard()[1][1] == Board.X() 
				&& board.getBoard()[2][1] == Board.EMPTY() )
		{
			board.addValue(2, 1, this);
			return;
		}

		// --E
		// --X
		// --X
		else if( board.getBoard()[0][2] == Board.EMPTY() && board.getBoard()[1][2] == Board.X() 
				&& board.getBoard()[2][2] == Board.X() )
		{
			board.addValue(0, 2, this);
			return;
		}

		// --X
		// --E
		// --X
		else if( board.getBoard()[0][2] == Board.X() && board.getBoard()[1][2] == Board.EMPTY() 
				&& board.getBoard()[2][2] == Board.X() )
		{
			board.addValue(1, 2, this);
			return;
		}

		// --X
		// --X
		// --E
		else if( board.getBoard()[0][2] == Board.X() && board.getBoard()[1][2] == Board.X() 
				&& board.getBoard()[2][2] == Board.EMPTY() )
		{
			board.addValue(2, 2, this);
			return;
		}

		// E--
		// -X-
		// --X
		else if( board.getBoard()[0][0] == Board.EMPTY() && board.getBoard()[1][1] == Board.X() 
				&& board.getBoard()[2][2] == Board.X() )
		{
			board.addValue(0, 0, this);
			return;
		}

		// X--
		// -E-
		// --X
		else if( board.getBoard()[0][0] == Board.X() && board.getBoard()[1][1] == Board.EMPTY() 
				&& board.getBoard()[2][2] == Board.X() )
		{
			board.addValue(1, 1, this);
			return;
		}

		// X--
		// -X-
		// --E
		else if( board.getBoard()[0][0] == Board.X() && board.getBoard()[1][1] == Board.X() 
				&& board.getBoard()[2][2] == Board.EMPTY() )
		{
			board.addValue(2, 2, this);
			return;
		}

		// --E
		// -X-
		// X--
		else if( board.getBoard()[0][2] == Board.EMPTY() && board.getBoard()[1][1] == Board.X() 
				&& board.getBoard()[2][0] == Board.X() )
		{
			board.addValue(0, 2, this);
			return;
		}

		// --X
		// -E-
		// X--
		else if( board.getBoard()[0][2] == Board.X() && board.getBoard()[1][1] == Board.EMPTY() 
				&& board.getBoard()[2][0] == Board.X() )
		{
			board.addValue(1, 1, this);
			return;
		}

		// --X
		// -X-
		// E--
		else if( board.getBoard()[0][2] == Board.X() && board.getBoard()[1][1] == Board.X() 
				&& board.getBoard()[2][0] == Board.EMPTY() )
		{
			board.addValue(2, 0, this);
			return;
		}

		// Third Priority (GOOD MOVE)

		// ORR
		// ---
		// ---
		if( board.getBoard()[0][0] == Board.O() && board.getBoard()[0][1] == Board.EMPTY() 
				&& board.getBoard()[0][2] == Board.EMPTY() ) 
		{

			list.add(1);
		}

		// ROR
		// ---
		// ---
		if( board.getBoard()[0][0] == Board.EMPTY() && board.getBoard()[0][1] == Board.O() 
				&& board.getBoard()[0][2] == Board.EMPTY() ) 
		{

			list.add(2);
		}

		// RRO
		// ---
		// ---
		if( board.getBoard()[0][0] == Board.EMPTY() && board.getBoard()[0][1] == Board.EMPTY() 
				&& board.getBoard()[0][2] == Board.O() ) 
		{

			list.add(3);
		}

		// ---
		// ORR
		// ---
		if( board.getBoard()[1][0] == Board.O() && board.getBoard()[1][1] == Board.EMPTY() 
				&& board.getBoard()[1][2] == Board.EMPTY() )
		{
			list.add(4);
		}

		// ---
		// ROR
		// ---
		if( board.getBoard()[1][0] == Board.EMPTY() && board.getBoard()[1][1] == Board.O() 
				&& board.getBoard()[1][2] == Board.EMPTY() )
		{
			list.add(5);
		}

		// ---
		// RRO
		// ---
		if( board.getBoard()[1][0] == Board.EMPTY() && board.getBoard()[1][1] == Board.EMPTY() 
				&& board.getBoard()[1][2] == Board.O() )
		{
			list.add(6);
		}

		// ---
		// ---
		// ORR
		if( board.getBoard()[2][0] == Board.O() && board.getBoard()[2][1] == Board.EMPTY() 
				&& board.getBoard()[2][2] == Board.EMPTY())
		{
			list.add(7);
		}

		// ---
		// ---
		// ROR
		if( board.getBoard()[2][0] == Board.EMPTY() && board.getBoard()[2][1] == Board.O() 
				&& board.getBoard()[2][2] == Board.EMPTY())
		{
			list.add(8);
		}

		// ---
		// ---
		// RRO
		if( board.getBoard()[2][0] == Board.EMPTY() && board.getBoard()[2][1] == Board.EMPTY() 
				&& board.getBoard()[2][2] == Board.O())
		{
			list.add(9);
		}

		// O--
		// R--
		// R--
		if( board.getBoard()[0][0] == Board.O() && board.getBoard()[1][0] == Board.EMPTY() 
				&& board.getBoard()[2][0] == Board.EMPTY() )
		{
			list.add(10);
		}

		// R--
		// O--
		// R--
		if( board.getBoard()[0][0] == Board.EMPTY() && board.getBoard()[1][0] == Board.O() 
				&& board.getBoard()[2][0] == Board.EMPTY() )
		{
			list.add(11);
		}

		// R--
		// R--
		// O--
		if( board.getBoard()[0][0] == Board.EMPTY() && board.getBoard()[1][0] == Board.EMPTY() 
				&& board.getBoard()[2][0] == Board.O() )
		{
			list.add(12);
		}

		// -O-
		// -R-
		// -R-
		if( board.getBoard()[0][1] == Board.O() && board.getBoard()[1][1] == Board.EMPTY() 
				&& board.getBoard()[2][1] == Board.EMPTY() )
		{
			list.add(13);
		}

		// -R-
		// -O-
		// -R-
		if( board.getBoard()[0][1] == Board.EMPTY() && board.getBoard()[1][1] == Board.O() 
				&& board.getBoard()[2][1] == Board.EMPTY() )
		{
			list.add(14);
		}

		// -R-
		// -R-
		// -O-
		if( board.getBoard()[0][1] == Board.EMPTY() && board.getBoard()[1][1] == Board.EMPTY() 
				&& board.getBoard()[2][1] == Board.O() )
		{
			list.add(15);
		}

		// --O
		// --R
		// --R
		if( board.getBoard()[0][2] == Board.O() && board.getBoard()[1][2] == Board.EMPTY() 
				&& board.getBoard()[2][2] == Board.EMPTY() )
		{
			list.add(16);
		}

		// --R
		// --O
		// --R
		if( board.getBoard()[0][2] == Board.EMPTY() && board.getBoard()[1][2] == Board.O() 
				&& board.getBoard()[2][2] == Board.EMPTY() )
		{
			list.add(17);
		}

		// --R
		// --R
		// --O
		if( board.getBoard()[0][2] == Board.EMPTY() && board.getBoard()[1][2] == Board.EMPTY() 
				&& board.getBoard()[2][2] == Board.O() )
		{
			list.add(18);
		}

		// O--
		// -R-
		// --R
		if( board.getBoard()[0][0] == Board.O() && board.getBoard()[1][1] == Board.EMPTY() 
				&& board.getBoard()[2][2] == Board.EMPTY() )
		{
			list.add(19);
		}

		// R--
		// -O-
		// --R
		if( board.getBoard()[0][0] == Board.EMPTY() && board.getBoard()[1][1] == Board.O() 
				&& board.getBoard()[2][2] == Board.EMPTY() )
		{
			list.add(20);
		}

		// R--
		// -R-
		// --O
		if( board.getBoard()[0][0] == Board.EMPTY() && board.getBoard()[1][1] == Board.EMPTY() 
				&& board.getBoard()[2][2] == Board.O() )
		{
			list.add(21);
		}

		// --O
		// -R-
		// R--
		if( board.getBoard()[0][2] == Board.O() && board.getBoard()[1][1] == Board.EMPTY() 
				&& board.getBoard()[2][0] == Board.EMPTY() )
		{
			list.add(22);
		}

		// --R
		// -O-
		// R--
		if( board.getBoard()[0][2] == Board.EMPTY() && board.getBoard()[1][1] == Board.O() 
				&& board.getBoard()[2][0] == Board.EMPTY() )
		{
			list.add(23);
		}

		// --R
		// -R-
		// O--
		if( board.getBoard()[0][2] == Board.EMPTY() && board.getBoard()[1][1] == Board.EMPTY() 
				&& board.getBoard()[2][0] == Board.O() )
		{
			list.add(24);
		}

		if( list.isEmpty() == false)
		{

			int randomIndex = 1 + randomNumbers.nextInt(list.size() -1);
			int move = list.get(randomIndex);

			switch (move)
			{
			case 1: 
				if( 1 + randomNumbers.nextInt(2) == 1 )
				{
					board.addValue(0, 1, this);
					return;
				}
				else // == 2
				{
					board.addValue(0, 2, this);
					return;
				}

			case 2:
				if( 1 + randomNumbers.nextInt(2) == 1 )
				{
					board.addValue(0, 0, this);
					return;
				}
				else // == 2
				{
					board.addValue(0, 2, this);
					return;
				}

			case 3:
				if( 1 + randomNumbers.nextInt(2) == 1 )
				{
					board.addValue(0, 0, this);
					return;
				}
				else // == 2
				{
					board.addValue(0, 1, this);
					return;
				}

			case 4:
				if( 1 + randomNumbers.nextInt(2) == 1 )
				{
					board.addValue(1, 1, this);
					return;
				}
				else // == 2
				{
					board.addValue(1, 2, this);
					return;
				}

			case 5:
				if( 1 + randomNumbers.nextInt(2) == 1 )
				{
					board.addValue(1, 0, this);
					return;
				}
				else // == 2
				{
					board.addValue(1, 2, this);
					return;
				}

			case 6:
				if( 1 + randomNumbers.nextInt(2) == 1 )
				{
					board.addValue(1, 0, this);
					return;
				}
				else // == 2
				{
					board.addValue(1, 1, this);
					return;
				}

			case 7:
				if( 1 + randomNumbers.nextInt(2) == 1 )
				{
					board.addValue(2, 1, this);
					return;
				}
				else // == 2
				{
					board.addValue(2, 2, this);
					return;
				}

			case 8:
				if( 1 + randomNumbers.nextInt(2) == 1 )
				{
					board.addValue(2, 0, this);
					return;
				}
				else // == 2
				{
					board.addValue(2, 2, this);
					return;
				}

			case 9:
				if( 1 + randomNumbers.nextInt(2) == 1 )
				{
					board.addValue(2, 0, this);
					return;
				}
				else // == 2
				{
					board.addValue(2, 1, this);
					return;
				}

			case 10:
				if( 1 + randomNumbers.nextInt(2) == 1 )
				{
					board.addValue(1, 0, this);
					return;
				}
				else // == 2
				{
					board.addValue(2, 0, this);
					return;
				}

			case 11:
				if( 1 + randomNumbers.nextInt(2) == 1 )
				{
					board.addValue(0, 0, this);
					return;
				}
				else // == 2
				{
					board.addValue(2, 0, this);
					return;
				}

			case 12:
				if( 1 + randomNumbers.nextInt(2) == 1 )
				{
					board.addValue(0, 0, this);
					return;
				}
				else // == 2
				{
					board.addValue(1, 0, this);
					return;
				}

			case 13:
				if( 1 + randomNumbers.nextInt(2) == 1 )
				{
					board.addValue(1, 1, this);
					return;
				}
				else // == 2
				{
					board.addValue(2, 1, this);
					return;
				}

			case 14:
				if( 1 + randomNumbers.nextInt(2) == 1 )
				{
					board.addValue(0, 1, this);
					return;
				}
				else // == 2
				{
					board.addValue(2, 1, this);
					return;
				}

			case 15:
				if( 1 + randomNumbers.nextInt(2) == 1 )
				{
					board.addValue(0, 1, this);
					return;
				}
				else // == 2
				{
					board.addValue(1, 1, this);
					return;
				}

			case 16:
				if( 1 + randomNumbers.nextInt(2) == 1 )
				{
					board.addValue(1, 2, this);
					return;
				}
				else // == 2
				{
					board.addValue(2, 2, this);
					return;
				}

			case 17:
				if( 1 + randomNumbers.nextInt(2) == 1 )
				{
					board.addValue(0, 2, this);
					return;
				}
				else // == 2
				{
					board.addValue(2, 2, this);
					return;
				}

			case 18:
				if( 1 + randomNumbers.nextInt(2) == 1 )
				{
					board.addValue(0, 2, this);
					return;
				}
				else // == 2
				{
					board.addValue(1, 2, this);
					return;
				}

			case 19:
				if( 1 + randomNumbers.nextInt(2) == 1 )
				{
					board.addValue(1, 1, this);
					return;
				}
				else // == 2
				{
					board.addValue(2, 2, this);
					return;
				}

			case 20:
				if( 1 + randomNumbers.nextInt(2) == 1 )
				{
					board.addValue(0, 0, this);
					return;
				}
				else // == 2
				{
					board.addValue(2, 2, this);
					return;
				}

			case 21:
				if( 1 + randomNumbers.nextInt(2) == 1 )
				{
					board.addValue(0, 0, this);
					return;
				}
				else // == 2
				{
					board.addValue(1, 1, this);
					return;
				}

			case 22:
				if( 1 + randomNumbers.nextInt(2) == 1 )
				{
					board.addValue(1, 1, this);
					return;
				}
				else // == 2
				{
					board.addValue(2, 0, this);
					return;
				}

			case 23:
				if( 1 + randomNumbers.nextInt(2) == 1 )
				{
					board.addValue(0, 2, this);
					return;
				}
				else // == 2
				{
					board.addValue(2, 0, this);
					return;
				}

			case 24:
				if( 1 + randomNumbers.nextInt(2) == 1 )
				{
					board.addValue(0, 2, this);
					return;
				}
				else // == 2
				{
					board.addValue(1, 1, this);
					return;
				}
			}
		}

		// No Priority

		else //list is empty
		{
			do
			{
				legal = 0;

				switch(1 + randomNumbers.nextInt(9))
				{
				case 1: legal = board.addValue(0,0, this);
				break;
				case 2: legal = board.addValue(0,1, this);
				break;
				case 3: legal = board.addValue(0,2, this);
				break;
				case 4: legal = board.addValue(1,0, this);
				break;
				case 5: legal = board.addValue(1,1, this);
				break;
				case 6: legal = board.addValue(1,2, this);
				break;
				case 7: legal = board.addValue(2,0, this);
				break;
				case 8: legal = board.addValue(2,1, this);
				break;
				case 9: legal = board.addValue(2,2, this);

				}
			}
			while (!(legal == 1));
		}
	}
}



Is This A Good Question/Topic? 1
  • +

Replies To: How can I condense this code into more inteligent code? (simple a.i.)

#2 Sheph  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 428
  • View blog
  • Posts: 998
  • Joined: 12-October 11

Re: How can I condense this code into more inteligent code? (simple a.i.)

Posted 31 May 2012 - 01:01 PM

You could use something like heuristics. When the other player is about to win, give the square he needs a huge value. (Actually if you are about to win and its your turn that should have the highest value) If there is a row or diagonal that has one of his x's, give a decent value to his thing. If there is a row or diagonal with YOUR mark, give it a decent value as well. (let these values overlap, so that if you can simultaneously set yourself up and block him, that move will have a large value) You can balance the numbers later to deal with an aggressive or defense player. Then the computer just picks the square with the highest value. You can give squares that will not lead to victory for either player a 0 value and already taken squares a -1.

So an AI's turn simply recalculates the values based on the board and then picks the highest one.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8065
  • View blog
  • Posts: 31,308
  • Joined: 06-March 08

Re: How can I condense this code into more inteligent code? (simple a.i.)

Posted 31 May 2012 - 01:17 PM

You have been patient to code all these if() to see if you had a winner :)

Not a good idea to call getBoard() hundredth of times call it onlty one at the beginning of the method.

Add a int value for all your slot and initialize them to 0
If the X played at a position put a 10 in that value
If the O played at a position put a 100 in that value

Then with simple loop you can add all the value of a single row, column, diagonal
If the total for a row, column, diag=onal is 30 the X won, if it is 300 the O won
Was This Post Helpful? 2
  • +
  • -

#4 EscSequenceAlpha  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 20-March 12

Re: How can I condense this code into more inteligent code? (simple a.i.)

Posted 31 May 2012 - 05:51 PM

View Postpbl, on 31 May 2012 - 01:17 PM, said:

You have been patient to code all these if() to see if you had a winner :)

Not a good idea to call getBoard() hundredth of times call it onlty one at the beginning of the method.

Add a int value for all your slot and initialize them to 0
If the X played at a position put a 10 in that value
If the O played at a position put a 100 in that value

Then with simple loop you can add all the value of a single row, column, diagonal
If the total for a row, column, diag=onal is 30 the X won, if it is 300 the O won

I'm not sure I understand you guys... if there are two x's the row will add up to 20, this would mean a row with one O (100) will take priority and the ai wont block XX- when it needs to.

I'm not sure a single numbers calculation will be a solution. even changing the numbers wont a OX- always beat a XX-? and you need to block that when it comes. I might need more clarity here

also did you mean assigning the board variable to another 2d array when the method is called and then to refer to that and therefore not need to call getboard multiple times? thats something I can fix now.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8065
  • View blog
  • Posts: 31,308
  • Joined: 06-March 08

Re: How can I condense this code into more inteligent code? (simple a.i.)

Posted 31 May 2012 - 06:01 PM

A row/column/diagonal with a total of 20 or 200 needs an immediate intervention by the other player
Was This Post Helpful? 0
  • +
  • -

#6 Sheph  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 428
  • View blog
  • Posts: 998
  • Joined: 12-October 11

Re: How can I condense this code into more inteligent code? (simple a.i.)

Posted 31 May 2012 - 06:10 PM

Don't confuse my advice with pbl's advice. Take one or the other. They are separate concepts.
Was This Post Helpful? 0
  • +
  • -

#7 EscSequenceAlpha  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 20-March 12

Re: How can I condense this code into more inteligent code? (simple a.i.)

Posted 31 May 2012 - 07:12 PM

View PostSheph, on 31 May 2012 - 06:10 PM, said:

Don't confuse my advice with pbl's advice. Take one or the other. They are separate concepts.


terrible sorry.

Ok I see, with the value for rows/coloumns concept I need an exception where if the added values equals two x's it would overide the value of a o and a x. we can call that a scoring exception.

with heiristicsamathing I evaluate each position on the board with respect to the current values on the board itself.
so... go through each element of the array if a o or x is in line with current position then increase score accordingly for that position.

this sounds simple but is actually hard to visualise practically. I'm not sure how I would find relative relations in a 2d array. arrays, loops and variables make my head hurt
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon

Reputation: 5578
  • View blog
  • Posts: 9,010
  • Joined: 19-March 11

Re: How can I condense this code into more inteligent code? (simple a.i.)

Posted 31 May 2012 - 08:07 PM

There are two problems that you're dealing with here, and they are both problems f representation.
The first problem is, how do you best represent the board o the computer can work with it and so you can tell the computer how to work with it efficiently. The second problem is, how do you represent a game so the computer can decide what moves are available and of those, which are the best. These are different problems, although they obviously are related.

To represent the board, you can use several strategies. One would be an object-oriented strategy, in which you have eight sets of three Cells. Each Cell can have one of three values - X, O, or unset. (E, for empty, if you like, though " " might be easier for display purposes). Alternatively, you could have a three-by-three array of chars, with the same values. Or, interestingly, you could have a single array of 9, which is formally identical if you do the math. (number the cells from top left, left to right then top to bottom. Now if you take a cell's number mod 3, you have its column number, and if you take its number div 3, you have its row number, and the diagonals are a special case)

However you do it, each of these strategies allow you to bundle up the important information for easy access. They'll all work, and you can come up with others.


Now, how do you come up with strategies, and how do you represent them?

That's the interesting question. Suppose that we think of a "position" as a particular configuration of pieces, with one particular player to move. (since X moves first, you never have an empty board and O to move - the identity of the player to move is critical to understanding a position - it doesn't matter if O has a winning move now if it's X's move, unless O can cheat) We want to be able to ask, first of all, how many winning moves does the player have in this position. We might also want to ask, for a given move, if we turn the board around, how many winning moves does the other player then have? And so forth - that's depth of search. Not hard in tic-tac-toe, since there's only 9 squares. Impossible in chess, since there's about a hexoogle possible games, and you can't run them all.
It might be interesting also to count the number of possible avenues towards a win a player has. If I'm X, and I play a corner piece, I have three possible wins. If O is equally stupid and plays one of the remaining corners, we each have two possible wins and one avenue is blocked. Maximizing your degrees of freedom might be a winning strategy. (this should really be a strategy that extends beyond simple tic tac to to pente or three-D tic tac toe - just winning tic tac toe is boring!) You might also think about weighting positions based on how far in the future they come. If I have a sure win in five moves, and a different sure win in three moves, I want the one in three.

Probably for your purposes, maximizing degrees of freedom and minimizing your opponent's possibilities is the easiest to program, though it might not be the most intelligent player when you get to more complicated games.
Was This Post Helpful? 3
  • +
  • -

#9 Sheph  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 428
  • View blog
  • Posts: 998
  • Joined: 12-October 11

Re: How can I condense this code into more inteligent code? (simple a.i.)

Posted 31 May 2012 - 10:39 PM

Who wants to make a challenge to design the smartest TicTacToe AI? We would need to collaboratively write up an API for the game and an interface for the player, which we can all use to write our own AI implementations. Then we can have TicTacToe duels with our creations! (No humans necessary)

Would this be something we could possibly get going in the Challenges forum?
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8065
  • View blog
  • Posts: 31,308
  • Joined: 06-March 08

Re: How can I condense this code into more inteligent code? (simple a.i.)

Posted 01 June 2012 - 06:58 PM

For a simple game like TicTacToe, that have just a depth of 9, I would simply go through all possible next moves and select, as the actual next move, the one that guides me toward the more victories
Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon

Reputation: 5578
  • View blog
  • Posts: 9,010
  • Joined: 19-March 11

Re: How can I condense this code into more inteligent code? (simple a.i.)

Posted 01 June 2012 - 08:21 PM

View Postpbl, on 01 June 2012 - 08:58 PM, said:

For a simple game like TicTacToe, that have just a depth of 9, I would simply go through all possible next moves and select, as the actual next move, the one that guides me toward the more victories



Okay, now modify your solution to work for 3D tic-tac-toe.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1