Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: A logic / forloop question

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Stressed
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default A logic / forloop question

    This is another portion of the same program referenced earlier. It's sort of like tic tac toe, but with a variable board size. I have already gotten my method for checking to see if anyone has a tic tac toe to work, but currently I have a different method for each board size, and each player, which gets really really long. I know there must be a way to only do it once. I was thinking nested forloops, but it doesn't seem to work with the logic I am using, as the x and y increment together. Some tips would be great. I need this to work for any board size, and check horizontal, vertical and diagonal win possibilities.
    	/** Checks the board to see if the player has scored any points by filling a row, column or diagonal with their pieces. If so,
    	 * player is awarded points for each space filled and pieces are removed from the board. @param gameBoard. @return gameBoard.
    	 * 
    	 */
    	public static int[][] checkBoard3Player1(int[][]gameBoard)
    	{
    		boolean[] rows = new boolean[gameBoard.length];
    		for(int x = 0; x < gameBoard.length; x ++){
    			if(gameBoard[x][0] == gameBoard[x][1] && gameBoard[x][0] == gameBoard[x][2] && gameBoard[x][0] != 0)
    				rows[x] = true;
     
    		}
    		boolean[] cols = new boolean[gameBoard.length];
    				for(int y = 0; y < gameBoard.length; y++)
    				{	if(gameBoard[0][y] == gameBoard[1][y] && gameBoard[0][y] == gameBoard[2][y] && gameBoard[0][y] != 0)
    						cols[y] = true;
    				}
    		boolean diagLtoR = false;
    				if(gameBoard[0][0] == gameBoard[1][1] && gameBoard[0][0] == gameBoard [2][2] && gameBoard[0][0] != 0)
    					diagLtoR = true;
    		boolean diagRtoL = false;
    				if(gameBoard[0][2] == gameBoard[1][1] && gameBoard[0][2] == gameBoard [2][0] && gameBoard[0][2] != 0)
    					diagRtoL = true;
     
    		for(int x = 0; x < rows.length; x ++)
    		{
    			if(rows[x])
    			{
    			for(int gbi = 0; gbi < gameBoard.length; gbi ++)
    				{
    				gameBoard[x][gbi] = 0;
    				player1Score++;
    				}
    			}
    		}	
    		for(int y = 0; y < cols.length; y ++)
    		{
    			if(cols[y])
    			{
    				for(int gbi = 0; gbi < gameBoard.length; gbi ++)
    				{
    				gameBoard[gbi][y] = 0;
    				player1Score++;
    				}
    			}
    		}
    			if(diagLtoR)
    			{
    			gameBoard[0][0] = 0;
    			gameBoard[1][1] = 0;
    			gameBoard[2][2] = 0;
    			player1Score++;
    			}
    			if(diagRtoL)
    			{
    			gameBoard[0][2] = 0;
    			gameBoard[1][1] = 0;
    			gameBoard[2][0] = 0;
    			player1Score++;
    			}	
     
    		return gameBoard;
    	}


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: A logic / forloop question

    As It's your design process, It's kind of hard to comment, but I'd recommend you go with a more object oriented approach, by splitting the logic into multiple classes, such as 1 that defines a player, his score, how to check his score and say another class that defines the board.

    Obviously that was a very vague answer, but where others may easily be able to interpret what you're doing, I'm not quite with you
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Stressed
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: A logic / forloop question

    Thanks Newbie, you're right about the object design. My original game was all in one class, which was a bit of a monster.

    I have seperated it into appropriate classes now, and want to move this method into the board class as well. I will modify it for that.

    All I am really asking is how to check each portion of the board without having to have a different method for each board size.

    for example,
    3x3 board
    5x5 board

    want logic to check both for having tic tac toe win, without having 2 differnt methods.

Similar Threads

  1. Problem with the Logic
    By sudh in forum Java Theory & Questions
    Replies: 4
    Last Post: March 31st, 2011, 07:21 AM
  2. [SOLVED] Quick logic question (Sine function)
    By Actinistia in forum Java Theory & Questions
    Replies: 6
    Last Post: March 24th, 2011, 01:08 PM
  3. whats logic behind this?
    By X0X0 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 26th, 2011, 02:48 PM
  4. Help with java logic
    By dever in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 7th, 2011, 08:41 AM
  5. Need Help with Operators/ logic
    By codekiller in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 3rd, 2010, 09:25 AM