[Help] Othello legalMoves algorithm for Java

tubui

Limp Gawd
Joined
May 15, 2008
Messages
346
Before you read further, I am not asking for code. I am asking for help to get started on an algorithm for the game of Othello's legal moves...

I am coding this in Java with GridWorld Grid and Location methods. GridWorld Case Study Part 3:

Any help is appreciated.
 
have you figured out the rules for legal moves in othello? if so, what are they? post them here.
 
This is my legalMoves() method in OthelloGame.java

Code:
pubic ArrayList<Location> legalMoves()
	{
		String chipColor = "";
		String oppChipColor = "";
		boolean checker = false;
		ArrayList<Location> occupiedLoc = new ArrayList();
		ArrayList<Location>	playerOccupiedLoc = new ArrayList();
		ArrayList<Location> adjacentOccupiedLoc = new ArrayList();
		ArrayList<Location> legalLoc = new ArrayList();
		Location loc = new Location(0, 0);
		
		if(turn == 0)
		{
			chipColor = player1.getColor();	
			oppChipColor = player2.getColor();
		}
		else
		{
			chipColor = player2.getColor();
			oppChipColor = player1.getColor();	
		}
			
		allOccupiedLoc = board.getOccupiedLocations();
		
		for(int i = 0; i < allOccupiedLoc.size(); i++)
		{
			if(board.get(occupiedLoc.get(i)).equals(chipColor))
				playerOccupiedLoc.add(occupiedLoc.get(i));
		}
		
		for(int i = 0; i < playerOccupiedLoc.size(); i++)
		{
			adjacentOccupiedLoc = board.getOccupiedAdjacentLocations(playerOccupiedLoc.get(i));
			
			for(int j = 0; j < adjacentOccupiedLoc.size(); j++)
			{
				loc = adjacentOccupiedLoc.get(j);
				int direction = playerOccupiedLoc.get(i).getDirectionToward(loc);
				
				while(board.get(loc) == oppChipColor && board.isValid(loc.getAdjacentLocation(direction)) == true)
				{
					loc = loc.getAdjacentLocation(direction);
					
					for(int k = 0; k < legalLoc.size(); k++)
					{
						if(loc.equals(legalLoc.get(k)) == true)
							checker == true;
					}
					
					if(board.isValid(loc) == true && board.get(loc) == null && checker == false)
					{
						legalLoc.add(loc);
					}
				}
			}
		}
		return(legalLoc);
	}

Can you look over it and see if I am missing anything or if anything looks wrong?
 
does that code work? any errors in behavior when you try to run it?
 
I didn't test it yet, I have to write the playGame() method now..

I'm not even positive it works. I was in the coding zone and wrote that in an hour... I might have to tweak it once I compile my game.. Ahh dinner's served.. I'm gonna lose my focus now =X
 
looking it over, it seems like you're on the right track... run it and report back :)
 
If anyone is reading this and would like to look over my code please PM me...

I finished it but I think I am missing something as it keeps going in an infinite loop!

I am not even sure if I'm checking who's winning or not...
 
Back
Top