Results 1 to 7 of 7
  1. #1
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default What kind of collection will help me do this?

    basically this is what I want to do... I have a list of a.i actions set up in a very long line of if else statements.

    however there may be more than one possible action, if this is the case I dont want my ai to just pick the first available one (as in the case of if else)

    so I have an ingenius solution... I change the if else statements to if statements and then if that condition is met I add a number to a list. each if statement has a different number.

    at the end I should have a list such as 4, 12, 25 meaning there are 3 moves the ai can make... now I want to use the length of this list as argument to a random number generator so that it picks one element at random in the list.

    then the number is entered in a case statement which will execute the appropriate move.

    the thing is I dont really know anything about collections, there seems to be quite a few different types with their own methods, I used the term list here as a general term. what kind of collection should I use for this and what methods does it have to help me perform this?
    Legend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: What kind of collection will help me do this?

    There are so many solutions you should determine what is most important with regards to design: what you foresee to change (encapsulate what changes) or what your foresee as making sense from a design standpoint. Here's one alternative: create an interface which defines two methods: one returns a boolean (each implementation of the interface defines the evaluation of the if statements) and the other defines the behavior to implement. Add these to a List...when you need to get the behavior, loop over this list and evaluate each boolean method of the interface - if true, add to a local List. When complete, randomly select an index of the local List and call its behavior method. A more complex alternative (or addition) would have to do with bit switching, in which a bit is set based upon the conditionals, and the behavior is retrieved from a Map keyed with a particular int that identifies the bits set, and a List of behaviors.

  3. #3
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default Re: What kind of collection will help me do this?

    I'm new to collections and interfaces so this is going to be difficult. this is what I've got from you, (just psuedo code)

    Move << interface >>
    boolean isTrue() //evaluate if statement
    void performAction(board[][])

    classes that implements Move (a, b, c etc)

    addList( a,b,c )

    loop list
    if (isTrue == true) addLocallist(this)

    randomSelect(locallist)
    call index.performAction()

    --- I'm confused here... I must have it wrong... a class implements an interface and has to define its own implementation... each object that implements move needs a different implementation therefore thay all have to be classes but then I'd end up with like 50 classes that cant be right
    Legend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: What kind of collection will help me do this?

    Doesn't mean you need that many, as you can use the same class or inheritance to implement common/reused behavior.

    I threw out this idea because - although this is a stab in the dark given there's not much info to go by - implementing an interface in this context allows you to a) easily plug and play with different implementations of the interface (thus easily changing the behavior of the algorithm) - going through several if's can get ugly very quickly, and doesn't encapsulate the behavior you wish. b) with interfaces and abstract method definitions as I suggest, the algorithm is written such that it doesn't care what it calls - all it cares about is picking a select few behaviors, then randomly choosing one to call...thus making the algorithm reusable and not locked into an implementation.

  5. #5
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default Re: What kind of collection will help me do this?

    I sort of see what you're saying (I think), but I'm not sure I understand it practically... the actionPerformed code might be shared but wont the isTrue conditions be different every time? thus dont you need different classes each time as a result of that. I'm sorry I'm not a programmer yet so its hard to visualise this stuff.

    I just finished my a.i!!! I want you to be the first to see it (and use it as an example for your implementation idea, I would be willing to make another version based on your idea to try and understand what you're saying and compare them) afterall this isnt class stuff, everything I do is of my own will (which is great it gives me the freedom to study and program whatever I want)

    AI class extends player
    Java Code:
    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));
    		}
    	}
    }
    Legend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.

  6. #6
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: What kind of collection will help me do this?

    Well, I'll be blunt and say those >1000 lines make my eyes hurt. As I mentioned above, there are many changes you can make to reduce all those statements, for instance boil them down using loops, reusable methods, or any one (or combination). I still don't know what the end goal here is (looks like tic-tac-toe or something similar), but knowing this info would help with advising an alternative.

  7. #7
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default Re: What kind of collection will help me do this?

    Oh I'm sorry, I had a tictactoe thread but that doesnt mean you saw it

    yes thats exactly what it is... the ORR means that there is an O (assume O to be the ai's symbol) and two empty's on that line (R because I use a random number 1-2 for one of those positions). the reason for the list is that this for example

    RRO and --O and --O can all be true, however without the list the ai would only respond to the first line making it predictable.
    --- --R -R-
    --- --R R--

    the list allows the program to pick one of these routes not just the first one, and then one of those positions in that route afterwards.

    I thought of this myself but I think it makes sense and seems to work fine. but like you say its way too long for a method. an alternative would be nice however I'm still not sure how to implement a more intelligent solution.
    Legend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.

Similar Threads

  1. What kind of loop?
    By gabrielpr12 in forum New To Java
    Replies: 15
    Last Post: 01-28-2012, 06:32 PM
  2. what kind of application is this?
    By mymark in forum SWT / JFace
    Replies: 0
    Last Post: 08-03-2011, 12:11 AM
  3. Kind of stuck
    By Nicky Swans in forum New To Java
    Replies: 8
    Last Post: 10-22-2010, 02:46 PM
  4. ';' expected, not of the common kind..
    By Addez in forum New To Java
    Replies: 6
    Last Post: 09-02-2009, 04:37 AM
  5. Total Newbie, Be Kind :)
    By dazza-s in forum New To Java
    Replies: 11
    Last Post: 04-26-2008, 10:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •